blob: 9fe0308bf891432211c6966950549ed0fb9a5a83 [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 (
18 "encoding/json"
19 "fmt"
20 "net/http"
21 "bytes"
22 "regexp"
mc6a9f01a2019-06-26 21:31:23 +000023 "os"
24)
25
nickhuang6b31f8f2019-09-26 02:02:14 +000026const RF_SUBSCRIPTION = "/EventService/Subscriptions/"
mc6a9f01a2019-06-26 21:31:23 +000027
mc862dad02019-08-06 20:52:51 +000028func (s *Server) add_subscription(ip string, event string, f *os.File) (rtn bool) {
mc6a9f01a2019-06-26 21:31:23 +000029 rtn = false
mc6a9f01a2019-06-26 21:31:23 +000030
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000031 destip := os.Getenv("EVENT_NOTIFICATION_DESTIP") + ":" + os.Getenv("DEVICE_MANAGEMENT_DESTPORT")
mc6a9f01a2019-06-26 21:31:23 +000032 subscrpt_info := map[string]interface{}{"Context":"TBD","Protocol":"Redfish"}
33 subscrpt_info["Name"] = event + " event subscription"
34 subscrpt_info["Destination"] = "https://" + destip
35 subscrpt_info["EventTypes"] = []string{event}
36 sRequestJson, err := json.Marshal(subscrpt_info)
mc862dad02019-08-06 20:52:51 +000037 uri := s.devicemap[ip].Protocol + "://" + ip + REDFISH_ROOT + RF_SUBSCRIPTION
38 client := s.httpclient
mc6a9f01a2019-06-26 21:31:23 +000039 resp, err := client.Post(uri, CONTENT_TYPE, bytes.NewBuffer(sRequestJson))
40 if err != nil {
41 fmt.Println(err)
42 return
43 }
44 defer resp.Body.Close()
45
46 if resp.StatusCode != 201 {
47 result := make(map[string]interface{})
48 json.NewDecoder(resp.Body).Decode(&result)
49 fmt.Println(result)
50 fmt.Println(result["data"])
51 fmt.Println("Add ", event, " subscription failed. HTTP response status: ", resp.Status)
52 return
53 }
mc6a9f01a2019-06-26 21:31:23 +000054 rtn = true
55 loc := resp.Header["Location"]
56 re := regexp.MustCompile(`/(\w+)$`)
57 match := re.FindStringSubmatch(loc[0])
mc862dad02019-08-06 20:52:51 +000058 s.devicemap[ip].Subscriptions[event] = match[1]
59
60 if f != nil {
61 b, err := json.Marshal(s.devicemap[ip])
nickhuang6b31f8f2019-09-26 02:02:14 +000062 fmt.Println(string(b))
mc862dad02019-08-06 20:52:51 +000063 if err != nil {
64 fmt.Println(err)
65 } else {
66 f.Truncate(0)
67 f.Seek(0, 0)
68 n, err := f.Write(b)
69 if err != nil {
70 fmt.Println("err wrote", n, "bytes")
71 fmt.Println(err)
72 }
73 }
74 } else {
75 fmt.Println("file handle is nil")
76 }
77
78 fmt.Println("Subscription", event, "id", match[1], "was successfully added")
mc6a9f01a2019-06-26 21:31:23 +000079 return
80}
81
mc862dad02019-08-06 20:52:51 +000082func (s *Server) remove_subscription(ip string, event string, f *os.File) bool {
83 id := s.devicemap[ip].Subscriptions[event]
nickhuang6b31f8f2019-09-26 02:02:14 +000084 uri := s.devicemap[ip].Protocol + "://" + ip + REDFISH_ROOT + RF_SUBSCRIPTION + id
mc6a9f01a2019-06-26 21:31:23 +000085 req, _ := http.NewRequest("DELETE", uri, nil)
86 resp, err := http.DefaultClient.Do(req)
87 if err != nil {
88 fmt.Println(err)
89 return false
90 }
91 defer resp.Body.Close()
92
93 if code := resp.StatusCode; code < 200 && code > 299 {
94 result := make(map[string]interface{})
95 json.NewDecoder(resp.Body).Decode(&result)
96 fmt.Println(result)
97 fmt.Println(result["data"])
98 fmt.Println("Remove subscription failed. HTTP response status:", resp.Status)
99 return false
100 }
mc862dad02019-08-06 20:52:51 +0000101 delete(s.devicemap[ip].Subscriptions, event)
102
103 if f != nil {
104 b, err := json.Marshal(s.devicemap[ip])
105 if err != nil {
106 fmt.Println(err)
107 } else {
108 f.Truncate(0)
109 f.Seek(0, 0)
110 n, err := f.Write(b)
111 if err != nil {
112 fmt.Println("!!!!! err wrote", n, "bytes")
113 fmt.Println(err)
114 } else {
115 fmt.Println("wrote", n, "bytes")
116 }
117 }
118 }
mc6a9f01a2019-06-26 21:31:23 +0000119 fmt.Println("Subscription id", id, "was successfully removed")
120 return true
121}
122