blob: a6740b49e889406628a42baab008919527188b16 [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"
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +000022 "io/ioutil"
mc6a9f01a2019-06-26 21:31:23 +000023 "net/http"
mc6a9f01a2019-06-26 21:31:23 +000024 "os"
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000025 "regexp"
mc6a9f01a2019-06-26 21:31:23 +000026)
27
mc20a4b5f2019-10-16 20:28:24 +000028const RF_EVENTSERVICE = "/redfish/v1/EventService/"
29const RF_SUBSCRIPTION = RF_EVENTSERVICE + "Subscriptions/"
mc6a9f01a2019-06-26 21:31:23 +000030
mc20a4b5f2019-10-16 20:28:24 +000031func (s *Server) add_subscription(ip string, event string) (rtn bool) {
mc6a9f01a2019-06-26 21:31:23 +000032 rtn = false
mc6a9f01a2019-06-26 21:31:23 +000033
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000034 destip := os.Getenv("EVENT_NOTIFICATION_DESTIP") + ":" + os.Getenv("DEVICE_MANAGEMENT_DESTPORT")
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000035 subscrpt_info := map[string]interface{}{"Context": "TBD-" + destip, "Protocol": "Redfish"}
mc6a9f01a2019-06-26 21:31:23 +000036 subscrpt_info["Name"] = event + " event subscription"
mc20a4b5f2019-10-16 20:28:24 +000037 subscrpt_info["Destination"] = RF_DEFAULT_PROTOCOL + destip
mc6a9f01a2019-06-26 21:31:23 +000038 subscrpt_info["EventTypes"] = []string{event}
39 sRequestJson, err := json.Marshal(subscrpt_info)
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000040 if err != nil {
41 logrus.Errorf("Error JasonMarshal %s", err)
42 return
43 }
mc20a4b5f2019-10-16 20:28:24 +000044 uri := RF_DEFAULT_PROTOCOL + ip + RF_SUBSCRIPTION
mc862dad02019-08-06 20:52:51 +000045 client := s.httpclient
mc6a9f01a2019-06-26 21:31:23 +000046 resp, err := client.Post(uri, CONTENT_TYPE, bytes.NewBuffer(sRequestJson))
mc20a4b5f2019-10-16 20:28:24 +000047 if resp != nil {
48 defer resp.Body.Close()
49 }
mc6a9f01a2019-06-26 21:31:23 +000050 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000051 logrus.Errorf("client post error %s", err)
mc6a9f01a2019-06-26 21:31:23 +000052 return
53 }
mc6a9f01a2019-06-26 21:31:23 +000054
55 if resp.StatusCode != 201 {
56 result := make(map[string]interface{})
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000057 dec := json.NewDecoder(resp.Body)
58 if err := dec.Decode(&result); err != nil {
59 logrus.Errorf("ERROR while adding event subscription:%s " + err.Error())
60 return
61 }
62 logrus.Infof("Result Decode %s", result)
mc6a9f01a2019-06-26 21:31:23 +000063 fmt.Println(result["data"])
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000064 logrus.Errorf("Add %s subscription failed. HTTP response status:%s ", event, resp.Status)
mc6a9f01a2019-06-26 21:31:23 +000065 return
66 }
mc6a9f01a2019-06-26 21:31:23 +000067 rtn = true
68 loc := resp.Header["Location"]
69 re := regexp.MustCompile(`/(\w+)$`)
70 match := re.FindStringSubmatch(loc[0])
mc862dad02019-08-06 20:52:51 +000071 s.devicemap[ip].Subscriptions[event] = match[1]
72
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000073 logrus.Infof("Subscription %s id %s was successfully added", event, match[1])
mc6a9f01a2019-06-26 21:31:23 +000074 return
75}
76
mc20a4b5f2019-10-16 20:28:24 +000077func (s *Server) remove_subscription(ip string, event string) bool {
mc862dad02019-08-06 20:52:51 +000078 id := s.devicemap[ip].Subscriptions[event]
mc20a4b5f2019-10-16 20:28:24 +000079 uri := RF_DEFAULT_PROTOCOL + ip + RF_SUBSCRIPTION + id
mc6a9f01a2019-06-26 21:31:23 +000080 req, _ := http.NewRequest("DELETE", uri, nil)
81 resp, err := http.DefaultClient.Do(req)
mc20a4b5f2019-10-16 20:28:24 +000082 if resp != nil {
83 defer resp.Body.Close()
84 }
mc6a9f01a2019-06-26 21:31:23 +000085 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000086 logrus.Errorf("Error DefaultClient.Do %s", err)
mc6a9f01a2019-06-26 21:31:23 +000087 return false
88 }
mc6a9f01a2019-06-26 21:31:23 +000089
90 if code := resp.StatusCode; code < 200 && code > 299 {
91 result := make(map[string]interface{})
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000092 dec := json.NewDecoder(resp.Body)
93 if err := dec.Decode(&result); err != nil {
94 logrus.Errorf("ERROR while removing event subscription: %s ", err.Error())
95 return false
96 }
97 logrus.Infof("Result %s", result)
mc6a9f01a2019-06-26 21:31:23 +000098 fmt.Println(result["data"])
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +000099 logrus.Errorf("Remove subscription failed. HTTP response status:%s", resp.Status)
mc6a9f01a2019-06-26 21:31:23 +0000100 return false
101 }
mc862dad02019-08-06 20:52:51 +0000102 delete(s.devicemap[ip].Subscriptions, event)
103
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000104 logrus.Infof("Subscription id %s was successfully removed", id)
mc6a9f01a2019-06-26 21:31:23 +0000105 return true
106}
mc20a4b5f2019-10-16 20:28:24 +0000107
Dinesh Belwalkar72ecafb2019-12-12 00:08:56 +0000108func (s *Server) get_event_types(ip string) (eventtypes []string) {
mc20a4b5f2019-10-16 20:28:24 +0000109 resp, err := http.Get(RF_DEFAULT_PROTOCOL + ip + RF_EVENTSERVICE)
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000110 logrus.Info("get_event_types")
mc20a4b5f2019-10-16 20:28:24 +0000111 if resp != nil {
112 defer resp.Body.Close()
113 }
114 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000115 logrus.Errorf("http get Error %s", err)
mc20a4b5f2019-10-16 20:28:24 +0000116 return
117 }
118 body, err := ioutil.ReadAll(resp.Body)
119 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000120 logrus.Errorf("Read error %s", err)
mc20a4b5f2019-10-16 20:28:24 +0000121 return
122 }
123
124 m := map[string]interface{}{}
125 err = json.Unmarshal([]byte(body), &m)
126 if err != nil {
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000127 logrus.Errorf("ErrorUnmarshal %s", err)
mc20a4b5f2019-10-16 20:28:24 +0000128 return
129 }
130 e := m["EventTypesForSubscription"].([]interface{})
Dinesh Belwalkara6ba07d2020-01-10 23:22:34 +0000131 logrus.Infof("supported event types %v\n", e)
mc20a4b5f2019-10-16 20:28:24 +0000132 for _, val := range e {
133 eventtypes = append(eventtypes, val.(string))
134 }
135 return
136}