Migration to AGL gerrit (update go import)
[src/xds/xds-agent.git] / lib / xaapiv1 / events.go
1 /*
2  * Copyright (C) 2017-2018 "IoT.bzh"
3  * Author Sebastien Douheret <sebastien@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package xaapiv1
19
20 import (
21         "encoding/json"
22         "fmt"
23 )
24
25 // EventRegisterArgs is the parameters (json format) of /events/register command
26 type EventRegisterArgs struct {
27         Name      string `json:"name"`
28         ProjectID string `json:"filterProjectID"`
29 }
30
31 // EventUnRegisterArgs is the parameters (json format) of /events/unregister command
32 type EventUnRegisterArgs struct {
33         Name string `json:"name"`
34         ID   int    `json:"id"`
35 }
36
37 // Events Type definitions
38 const (
39         // EventTypePrefix Used as event prefix
40         EventTypePrefix = "event:" // following by event type
41
42         // Supported Events type
43         EVTAll           = EventTypePrefix + "all"
44         EVTServerConfig  = EventTypePrefix + "server-config"        // type EventMsg with Data type xaapiv1.ServerCfg
45         EVTProjectAdd    = EventTypePrefix + "project-add"          // type EventMsg with Data type xaapiv1.ProjectConfig
46         EVTProjectDelete = EventTypePrefix + "project-delete"       // type EventMsg with Data type xaapiv1.ProjectConfig
47         EVTProjectChange = EventTypePrefix + "project-state-change" // type EventMsg with Data type xaapiv1.ProjectConfig
48         EVTSDKInstall    = EventTypePrefix + "sdk-install"          // type EventMsg with Data type xaapiv1.SDKManagementMsg
49         EVTSDKRemove     = EventTypePrefix + "sdk-remove"           // type EventMsg with Data type xaapiv1.SDKManagementMsg
50 )
51
52 // EVTAllList List of all supported events
53 var EVTAllList = []string{
54         EVTServerConfig,
55         EVTProjectAdd,
56         EVTProjectDelete,
57         EVTProjectChange,
58         EVTSDKInstall,
59         EVTSDKRemove,
60 }
61
62 // EventMsg Event message send over Websocket, data format depend to Type (see DecodeXXX function)
63 type EventMsg struct {
64         Time          string      `json:"time"`      // Timestamp
65         FromSessionID string      `json:"sessionID"` // Session ID of client who produce this event
66         Type          string      `json:"type"`      // Data type
67         Data          interface{} `json:"data"`      // Data
68 }
69
70 // DecodeServerCfg Helper to decode Data field type ServerCfg
71 func (e *EventMsg) DecodeServerCfg() (ServerCfg, error) {
72         p := ServerCfg{}
73         if e.Type != EVTServerConfig {
74                 return p, fmt.Errorf("Invalid type")
75         }
76         d, err := json.Marshal(e.Data)
77         if err == nil {
78                 err = json.Unmarshal(d, &p)
79         }
80         return p, err
81 }
82
83 // DecodeProjectConfig Helper to decode Data field type ProjectConfig
84 func (e *EventMsg) DecodeProjectConfig() (ProjectConfig, error) {
85         var err error
86         p := ProjectConfig{}
87         switch e.Type {
88         case EVTProjectAdd, EVTProjectChange, EVTProjectDelete:
89                 d := []byte{}
90                 d, err = json.Marshal(e.Data)
91                 if err == nil {
92                         err = json.Unmarshal(d, &p)
93                 }
94         default:
95                 err = fmt.Errorf("Invalid type")
96         }
97         return p, err
98 }
99
100 // DecodeSDKMsg Helper to decode Data field type SDKManagementMsg
101 func (e *EventMsg) DecodeSDKMsg() (SDKManagementMsg, error) {
102         var err error
103         s := SDKManagementMsg{}
104         switch e.Type {
105         case EVTSDKInstall, EVTSDKRemove:
106                 d := []byte{}
107                 d, err = json.Marshal(e.Data)
108                 if err == nil {
109                         err = json.Unmarshal(d, &s)
110                 }
111         default:
112                 err = fmt.Errorf("Invalid type")
113         }
114         return s, err
115 }