blob: d4b75a3cc6ac5ce7c064a3950eb54d3bed1b8d6f [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"
21 "net/http"
mc6a9f01a2019-06-26 21:31:23 +000022 "os"
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000023 "regexp"
mc20a4b5f2019-10-16 20:28:24 +000024 "io/ioutil"
mc6a9f01a2019-06-26 21:31:23 +000025)
26
mc20a4b5f2019-10-16 20:28:24 +000027const RF_EVENTSERVICE = "/redfish/v1/EventService/"
28const RF_SUBSCRIPTION = RF_EVENTSERVICE + "Subscriptions/"
mc6a9f01a2019-06-26 21:31:23 +000029
mc20a4b5f2019-10-16 20:28:24 +000030func (s *Server) add_subscription(ip string, event string) (rtn bool) {
mc6a9f01a2019-06-26 21:31:23 +000031 rtn = false
mc6a9f01a2019-06-26 21:31:23 +000032
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000033 destip := os.Getenv("EVENT_NOTIFICATION_DESTIP") + ":" + os.Getenv("DEVICE_MANAGEMENT_DESTPORT")
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000034 subscrpt_info := map[string]interface{}{"Context": "TBD-" + destip, "Protocol": "Redfish"}
mc6a9f01a2019-06-26 21:31:23 +000035 subscrpt_info["Name"] = event + " event subscription"
mc20a4b5f2019-10-16 20:28:24 +000036 subscrpt_info["Destination"] = RF_DEFAULT_PROTOCOL + destip
mc6a9f01a2019-06-26 21:31:23 +000037 subscrpt_info["EventTypes"] = []string{event}
38 sRequestJson, err := json.Marshal(subscrpt_info)
mc20a4b5f2019-10-16 20:28:24 +000039 uri := RF_DEFAULT_PROTOCOL + ip + RF_SUBSCRIPTION
mc862dad02019-08-06 20:52:51 +000040 client := s.httpclient
mc6a9f01a2019-06-26 21:31:23 +000041 resp, err := client.Post(uri, CONTENT_TYPE, bytes.NewBuffer(sRequestJson))
mc20a4b5f2019-10-16 20:28:24 +000042 if resp != nil {
43 defer resp.Body.Close()
44 }
mc6a9f01a2019-06-26 21:31:23 +000045 if err != nil {
46 fmt.Println(err)
47 return
48 }
mc6a9f01a2019-06-26 21:31:23 +000049
50 if resp.StatusCode != 201 {
51 result := make(map[string]interface{})
52 json.NewDecoder(resp.Body).Decode(&result)
53 fmt.Println(result)
54 fmt.Println(result["data"])
Dinesh Belwalkarb5db83f2019-10-24 17:27:58 +000055 fmt.Println("Add ", event, " subscription failed. HTTP response status: ", resp.Status)
mc6a9f01a2019-06-26 21:31:23 +000056 return
57 }
mc6a9f01a2019-06-26 21:31:23 +000058 rtn = true
59 loc := resp.Header["Location"]
60 re := regexp.MustCompile(`/(\w+)$`)
61 match := re.FindStringSubmatch(loc[0])
mc862dad02019-08-06 20:52:51 +000062 s.devicemap[ip].Subscriptions[event] = match[1]
63
mc862dad02019-08-06 20:52:51 +000064 fmt.Println("Subscription", event, "id", match[1], "was successfully added")
mc6a9f01a2019-06-26 21:31:23 +000065 return
66}
67
mc20a4b5f2019-10-16 20:28:24 +000068func (s *Server) remove_subscription(ip string, event string) bool {
mc862dad02019-08-06 20:52:51 +000069 id := s.devicemap[ip].Subscriptions[event]
mc20a4b5f2019-10-16 20:28:24 +000070 uri := RF_DEFAULT_PROTOCOL + ip + RF_SUBSCRIPTION + id
mc6a9f01a2019-06-26 21:31:23 +000071 req, _ := http.NewRequest("DELETE", uri, nil)
72 resp, err := http.DefaultClient.Do(req)
mc20a4b5f2019-10-16 20:28:24 +000073 if resp != nil {
74 defer resp.Body.Close()
75 }
mc6a9f01a2019-06-26 21:31:23 +000076 if err != nil {
77 fmt.Println(err)
78 return false
79 }
mc6a9f01a2019-06-26 21:31:23 +000080
81 if code := resp.StatusCode; code < 200 && code > 299 {
82 result := make(map[string]interface{})
83 json.NewDecoder(resp.Body).Decode(&result)
84 fmt.Println(result)
85 fmt.Println(result["data"])
86 fmt.Println("Remove subscription failed. HTTP response status:", resp.Status)
87 return false
88 }
mc862dad02019-08-06 20:52:51 +000089 delete(s.devicemap[ip].Subscriptions, event)
90
mc6a9f01a2019-06-26 21:31:23 +000091 fmt.Println("Subscription id", id, "was successfully removed")
92 return true
93}
mc20a4b5f2019-10-16 20:28:24 +000094
95func (s *Server) get_event_types(ip string) (eventtypes []string ) {
96 resp, err := http.Get(RF_DEFAULT_PROTOCOL + ip + RF_EVENTSERVICE)
97 fmt.Println("get_event_types")
98 if resp != nil {
99 defer resp.Body.Close()
100 }
101 if err != nil {
102 fmt.Println(err)
103 return
104 }
105 body, err := ioutil.ReadAll(resp.Body)
106 if err != nil {
107 fmt.Println(err)
108 return
109 }
110
111 m := map[string]interface{}{}
112 err = json.Unmarshal([]byte(body), &m)
113 if err != nil {
114 fmt.Println(err)
115 return
116 }
117 e := m["EventTypesForSubscription"].([]interface{})
118 fmt.Printf("supported event types %v\n", e)
119 for _, val := range e {
120 eventtypes = append(eventtypes, val.(string))
121 }
122 return
123}