blob: 160b599ef5f5473cece5ffac515680aa3b5dc5b3 [file] [log] [blame]
mc6a9f01a2019-06-26 21:31:23 +00001// Copyright 2018 Open Networking Foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000018 "bytes"
mc6a9f01a2019-06-26 21:31:23 +000019 "encoding/json"
20 "fmt"
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000021 logrus "github.com/sirupsen/logrus"
Scott Bakerbdb962b2020-04-03 10:53:36 -070022 "io"
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +000023 "io/ioutil"
mc6a9f01a2019-06-26 21:31:23 +000024 "net/http"
mc6a9f01a2019-06-26 21:31:23 +000025 "os"
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000026 "regexp"
mc6a9f01a2019-06-26 21:31:23 +000027)
28
mc20a4b5f2019-10-16 20:28:24 +000029const RF_EVENTSERVICE = "/redfish/v1/EventService/"
30const RF_SUBSCRIPTION = RF_EVENTSERVICE + "Subscriptions/"
mc6a9f01a2019-06-26 21:31:23 +000031
mc20a4b5f2019-10-16 20:28:24 +000032func (s *Server) add_subscription(ip string, event string) (rtn bool) {
mc6a9f01a2019-06-26 21:31:23 +000033 rtn = false
mc6a9f01a2019-06-26 21:31:23 +000034
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000035 destip := os.Getenv("EVENT_NOTIFICATION_DESTIP") + ":" + os.Getenv("DEVICE_MANAGEMENT_DESTPORT")
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000036 subscrpt_info := map[string]interface{}{"Context": "TBD-" + destip, "Protocol": "Redfish"}
mc6a9f01a2019-06-26 21:31:23 +000037 subscrpt_info["Name"] = event + " event subscription"
mc20a4b5f2019-10-16 20:28:24 +000038 subscrpt_info["Destination"] = RF_DEFAULT_PROTOCOL + destip
mc6a9f01a2019-06-26 21:31:23 +000039 subscrpt_info["EventTypes"] = []string{event}
40 sRequestJson, err := json.Marshal(subscrpt_info)
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000041 if err != nil {
42 logrus.Errorf("Error JasonMarshal %s", err)
43 return
44 }
mc20a4b5f2019-10-16 20:28:24 +000045 uri := RF_DEFAULT_PROTOCOL + ip + RF_SUBSCRIPTION
mc862dad02019-08-06 20:52:51 +000046 client := s.httpclient
mc6a9f01a2019-06-26 21:31:23 +000047 resp, err := client.Post(uri, CONTENT_TYPE, bytes.NewBuffer(sRequestJson))
mc20a4b5f2019-10-16 20:28:24 +000048 if resp != nil {
49 defer resp.Body.Close()
50 }
mc6a9f01a2019-06-26 21:31:23 +000051 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000052 logrus.Errorf("client post error %s", err)
mc6a9f01a2019-06-26 21:31:23 +000053 return
54 }
mc6a9f01a2019-06-26 21:31:23 +000055
Scott Bakerbdb962b2020-04-03 10:53:36 -070056 if resp.StatusCode != 201 && resp.StatusCode != 204 {
mc6a9f01a2019-06-26 21:31:23 +000057 result := make(map[string]interface{})
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000058 dec := json.NewDecoder(resp.Body)
Scott Bakerbdb962b2020-04-03 10:53:36 -070059 if err := dec.Decode(&result); err != nil && err != io.EOF {
60 logrus.Errorf("ERROR while adding event subscription:%s ", err.Error())
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000061 return
62 }
63 logrus.Infof("Result Decode %s", result)
mc6a9f01a2019-06-26 21:31:23 +000064 fmt.Println(result["data"])
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000065 logrus.Errorf("Add %s subscription failed. HTTP response status:%s ", event, resp.Status)
mc6a9f01a2019-06-26 21:31:23 +000066 return
67 }
mc6a9f01a2019-06-26 21:31:23 +000068 rtn = true
69 loc := resp.Header["Location"]
70 re := regexp.MustCompile(`/(\w+)$`)
71 match := re.FindStringSubmatch(loc[0])
mc862dad02019-08-06 20:52:51 +000072 s.devicemap[ip].Subscriptions[event] = match[1]
73
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000074 logrus.Infof("Subscription %s id %s was successfully added", event, match[1])
mc6a9f01a2019-06-26 21:31:23 +000075 return
76}
77
mc20a4b5f2019-10-16 20:28:24 +000078func (s *Server) remove_subscription(ip string, event string) bool {
mc862dad02019-08-06 20:52:51 +000079 id := s.devicemap[ip].Subscriptions[event]
mc20a4b5f2019-10-16 20:28:24 +000080 uri := RF_DEFAULT_PROTOCOL + ip + RF_SUBSCRIPTION + id
mc6a9f01a2019-06-26 21:31:23 +000081 req, _ := http.NewRequest("DELETE", uri, nil)
82 resp, err := http.DefaultClient.Do(req)
mc20a4b5f2019-10-16 20:28:24 +000083 if resp != nil {
84 defer resp.Body.Close()
85 }
mc6a9f01a2019-06-26 21:31:23 +000086 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000087 logrus.Errorf("Error DefaultClient.Do %s", err)
mc6a9f01a2019-06-26 21:31:23 +000088 return false
89 }
mc6a9f01a2019-06-26 21:31:23 +000090
91 if code := resp.StatusCode; code < 200 && code > 299 {
92 result := make(map[string]interface{})
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000093 dec := json.NewDecoder(resp.Body)
94 if err := dec.Decode(&result); err != nil {
95 logrus.Errorf("ERROR while removing event subscription: %s ", err.Error())
96 return false
97 }
98 logrus.Infof("Result %s", result)
mc6a9f01a2019-06-26 21:31:23 +000099 fmt.Println(result["data"])
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000100 logrus.Errorf("Remove subscription failed. HTTP response status:%s", resp.Status)
mc6a9f01a2019-06-26 21:31:23 +0000101 return false
102 }
mc862dad02019-08-06 20:52:51 +0000103 delete(s.devicemap[ip].Subscriptions, event)
104
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000105 logrus.Infof("Subscription id %s was successfully removed", id)
mc6a9f01a2019-06-26 21:31:23 +0000106 return true
107}
mc20a4b5f2019-10-16 20:28:24 +0000108
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000109func (s *Server) get_event_types(ip string) (eventtypes []string) {
mc20a4b5f2019-10-16 20:28:24 +0000110 resp, err := http.Get(RF_DEFAULT_PROTOCOL + ip + RF_EVENTSERVICE)
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000111 logrus.Info("get_event_types")
mc20a4b5f2019-10-16 20:28:24 +0000112 if resp != nil {
113 defer resp.Body.Close()
114 }
115 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000116 logrus.Errorf("http get Error %s", err)
mc20a4b5f2019-10-16 20:28:24 +0000117 return
118 }
119 body, err := ioutil.ReadAll(resp.Body)
120 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000121 logrus.Errorf("Read error %s", err)
mc20a4b5f2019-10-16 20:28:24 +0000122 return
123 }
124
125 m := map[string]interface{}{}
126 err = json.Unmarshal([]byte(body), &m)
127 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000128 logrus.Errorf("ErrorUnmarshal %s", err)
mc20a4b5f2019-10-16 20:28:24 +0000129 return
130 }
131 e := m["EventTypesForSubscription"].([]interface{})
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000132 logrus.Infof("supported event types %v\n", e)
mc20a4b5f2019-10-16 20:28:24 +0000133 for _, val := range e {
134 eventtypes = append(eventtypes, val.(string))
135 }
136 return
137}