blob: 3285f3d6af1c4b8b9773c4821b04b7242f3f4c9c [file] [log] [blame]
Dinesh Belwalkar6c0bc752020-04-24 23:47:53 +00001// Copyright 2018-present Open Networking Foundation
2// Copyright 2018-present Edgecore Networks Corporation
mc6a9f01a2019-06-26 21:31:23 +00003//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16package main
17
18import (
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000019 "bytes"
mc6a9f01a2019-06-26 21:31:23 +000020 "encoding/json"
21 "fmt"
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000022 logrus "github.com/sirupsen/logrus"
Scott Bakerbdb962b2020-04-03 10:53:36 -070023 "io"
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +000024 "io/ioutil"
mc6a9f01a2019-06-26 21:31:23 +000025 "net/http"
mc6a9f01a2019-06-26 21:31:23 +000026 "os"
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000027 "regexp"
mc6a9f01a2019-06-26 21:31:23 +000028)
29
mc20a4b5f2019-10-16 20:28:24 +000030const RF_EVENTSERVICE = "/redfish/v1/EventService/"
31const RF_SUBSCRIPTION = RF_EVENTSERVICE + "Subscriptions/"
mc6a9f01a2019-06-26 21:31:23 +000032
mc20a4b5f2019-10-16 20:28:24 +000033func (s *Server) add_subscription(ip string, event string) (rtn bool) {
mc6a9f01a2019-06-26 21:31:23 +000034 rtn = false
mc6a9f01a2019-06-26 21:31:23 +000035
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000036 destip := os.Getenv("EVENT_NOTIFICATION_DESTIP") + ":" + os.Getenv("DEVICE_MANAGEMENT_DESTPORT")
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000037 subscrpt_info := map[string]interface{}{"Context": "TBD-" + destip, "Protocol": "Redfish"}
mc6a9f01a2019-06-26 21:31:23 +000038 subscrpt_info["Name"] = event + " event subscription"
mc20a4b5f2019-10-16 20:28:24 +000039 subscrpt_info["Destination"] = RF_DEFAULT_PROTOCOL + destip
mc6a9f01a2019-06-26 21:31:23 +000040 subscrpt_info["EventTypes"] = []string{event}
41 sRequestJson, err := json.Marshal(subscrpt_info)
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000042 if err != nil {
43 logrus.Errorf("Error JasonMarshal %s", err)
44 return
45 }
mc20a4b5f2019-10-16 20:28:24 +000046 uri := RF_DEFAULT_PROTOCOL + ip + RF_SUBSCRIPTION
mc862dad02019-08-06 20:52:51 +000047 client := s.httpclient
mc6a9f01a2019-06-26 21:31:23 +000048 resp, err := client.Post(uri, CONTENT_TYPE, bytes.NewBuffer(sRequestJson))
mc20a4b5f2019-10-16 20:28:24 +000049 if resp != nil {
50 defer resp.Body.Close()
51 }
mc6a9f01a2019-06-26 21:31:23 +000052 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000053 logrus.Errorf("client post error %s", err)
mc6a9f01a2019-06-26 21:31:23 +000054 return
55 }
mc6a9f01a2019-06-26 21:31:23 +000056
Scott Bakerbdb962b2020-04-03 10:53:36 -070057 if resp.StatusCode != 201 && resp.StatusCode != 204 {
mc6a9f01a2019-06-26 21:31:23 +000058 result := make(map[string]interface{})
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000059 dec := json.NewDecoder(resp.Body)
Scott Bakerbdb962b2020-04-03 10:53:36 -070060 if err := dec.Decode(&result); err != nil && err != io.EOF {
61 logrus.Errorf("ERROR while adding event subscription:%s ", err.Error())
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000062 return
63 }
64 logrus.Infof("Result Decode %s", result)
mc6a9f01a2019-06-26 21:31:23 +000065 fmt.Println(result["data"])
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000066 logrus.Errorf("Add %s subscription failed. HTTP response status:%s ", event, resp.Status)
mc6a9f01a2019-06-26 21:31:23 +000067 return
68 }
mc6a9f01a2019-06-26 21:31:23 +000069 rtn = true
70 loc := resp.Header["Location"]
71 re := regexp.MustCompile(`/(\w+)$`)
72 match := re.FindStringSubmatch(loc[0])
mc862dad02019-08-06 20:52:51 +000073 s.devicemap[ip].Subscriptions[event] = match[1]
74
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000075 logrus.Infof("Subscription %s id %s was successfully added", event, match[1])
mc6a9f01a2019-06-26 21:31:23 +000076 return
77}
78
mc20a4b5f2019-10-16 20:28:24 +000079func (s *Server) remove_subscription(ip string, event string) bool {
mc862dad02019-08-06 20:52:51 +000080 id := s.devicemap[ip].Subscriptions[event]
mc20a4b5f2019-10-16 20:28:24 +000081 uri := RF_DEFAULT_PROTOCOL + ip + RF_SUBSCRIPTION + id
mc6a9f01a2019-06-26 21:31:23 +000082 req, _ := http.NewRequest("DELETE", uri, nil)
83 resp, err := http.DefaultClient.Do(req)
mc20a4b5f2019-10-16 20:28:24 +000084 if resp != nil {
85 defer resp.Body.Close()
86 }
mc6a9f01a2019-06-26 21:31:23 +000087 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000088 logrus.Errorf("Error DefaultClient.Do %s", err)
mc6a9f01a2019-06-26 21:31:23 +000089 return false
90 }
mc6a9f01a2019-06-26 21:31:23 +000091
92 if code := resp.StatusCode; code < 200 && code > 299 {
93 result := make(map[string]interface{})
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000094 dec := json.NewDecoder(resp.Body)
95 if err := dec.Decode(&result); err != nil {
96 logrus.Errorf("ERROR while removing event subscription: %s ", err.Error())
97 return false
98 }
99 logrus.Infof("Result %s", result)
mc6a9f01a2019-06-26 21:31:23 +0000100 fmt.Println(result["data"])
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000101 logrus.Errorf("Remove subscription failed. HTTP response status:%s", resp.Status)
mc6a9f01a2019-06-26 21:31:23 +0000102 return false
103 }
mc862dad02019-08-06 20:52:51 +0000104 delete(s.devicemap[ip].Subscriptions, event)
105
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000106 logrus.Infof("Subscription id %s was successfully removed", id)
mc6a9f01a2019-06-26 21:31:23 +0000107 return true
108}
mc20a4b5f2019-10-16 20:28:24 +0000109
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000110func (s *Server) get_event_types(ip string) (eventtypes []string) {
mc20a4b5f2019-10-16 20:28:24 +0000111 resp, err := http.Get(RF_DEFAULT_PROTOCOL + ip + RF_EVENTSERVICE)
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000112 logrus.Info("get_event_types")
mc20a4b5f2019-10-16 20:28:24 +0000113 if resp != nil {
114 defer resp.Body.Close()
115 }
116 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000117 logrus.Errorf("http get Error %s", err)
mc20a4b5f2019-10-16 20:28:24 +0000118 return
119 }
120 body, err := ioutil.ReadAll(resp.Body)
121 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000122 logrus.Errorf("Read error %s", err)
mc20a4b5f2019-10-16 20:28:24 +0000123 return
124 }
125
126 m := map[string]interface{}{}
127 err = json.Unmarshal([]byte(body), &m)
128 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000129 logrus.Errorf("ErrorUnmarshal %s", err)
mc20a4b5f2019-10-16 20:28:24 +0000130 return
131 }
132 e := m["EventTypesForSubscription"].([]interface{})
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000133 logrus.Infof("supported event types %v\n", e)
mc20a4b5f2019-10-16 20:28:24 +0000134 for _, val := range e {
135 eventtypes = append(eventtypes, val.(string))
136 }
137 return
138}