85dc02a7b415018afdeefb3d612b973332b459ab
[src/xds/xds-agent.git] / lib / xaapiv1 / events.go
1 /*
2  * Copyright (C) 2017 "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 )
49
50 // EVTAllList List of all supported events
51 var EVTAllList = []string{
52         EVTServerConfig,
53         EVTProjectAdd,
54         EVTProjectDelete,
55         EVTProjectChange,
56 }
57
58 // EventMsg Event message send over Websocket, data format depend to Type (see DecodeXXX function)
59 type EventMsg struct {
60         Time          string      `json:"time"`      // Timestamp
61         FromSessionID string      `json:"sessionID"` // Session ID of client that emits this event
62         Type          string      `json:"type"`      // Data type
63         Data          interface{} `json:"data"`      // Data
64 }
65
66 // DecodeServerCfg Helper to decode Data field type ServerCfg
67 func (e *EventMsg) DecodeServerCfg() (ServerCfg, error) {
68         p := ServerCfg{}
69         if e.Type != EVTServerConfig {
70                 return p, fmt.Errorf("Invalid type")
71         }
72         d, err := json.Marshal(e.Data)
73         if err == nil {
74                 err = json.Unmarshal(d, &p)
75         }
76         return p, err
77 }
78
79 // DecodeProjectConfig Helper to decode Data field type ProjectConfig
80 func (e *EventMsg) DecodeProjectConfig() (ProjectConfig, error) {
81         var err error
82         p := ProjectConfig{}
83         switch e.Type {
84         case EVTProjectAdd, EVTProjectChange, EVTProjectDelete:
85                 d := []byte{}
86                 d, err = json.Marshal(e.Data)
87                 if err == nil {
88                         err = json.Unmarshal(d, &p)
89                 }
90         default:
91                 err = fmt.Errorf("Invalid type")
92         }
93         return p, err
94 }