Dinesh Belwalkar | 6c0bc75 | 2020-04-24 23:47:53 +0000 | [diff] [blame^] | 1 | // Copyright 2018-present Open Networking Foundation |
| 2 | // Copyright 2018-present Edgecore Networks Corporation |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 3 | // |
| 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 | |
| 16 | package main |
| 17 | |
| 18 | import ( |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 19 | "bytes" |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 20 | "encoding/json" |
| 21 | "fmt" |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 22 | logrus "github.com/sirupsen/logrus" |
Scott Baker | bdb962b | 2020-04-03 10:53:36 -0700 | [diff] [blame] | 23 | "io" |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 24 | "io/ioutil" |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 25 | "net/http" |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 26 | "os" |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 27 | "regexp" |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 28 | ) |
| 29 | |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 30 | const RF_EVENTSERVICE = "/redfish/v1/EventService/" |
| 31 | const RF_SUBSCRIPTION = RF_EVENTSERVICE + "Subscriptions/" |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 32 | |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 33 | func (s *Server) add_subscription(ip string, event string) (rtn bool) { |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 34 | rtn = false |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 35 | |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 36 | destip := os.Getenv("EVENT_NOTIFICATION_DESTIP") + ":" + os.Getenv("DEVICE_MANAGEMENT_DESTPORT") |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 37 | subscrpt_info := map[string]interface{}{"Context": "TBD-" + destip, "Protocol": "Redfish"} |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 38 | subscrpt_info["Name"] = event + " event subscription" |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 39 | subscrpt_info["Destination"] = RF_DEFAULT_PROTOCOL + destip |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 40 | subscrpt_info["EventTypes"] = []string{event} |
| 41 | sRequestJson, err := json.Marshal(subscrpt_info) |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 42 | if err != nil { |
| 43 | logrus.Errorf("Error JasonMarshal %s", err) |
| 44 | return |
| 45 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 46 | uri := RF_DEFAULT_PROTOCOL + ip + RF_SUBSCRIPTION |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 47 | client := s.httpclient |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 48 | resp, err := client.Post(uri, CONTENT_TYPE, bytes.NewBuffer(sRequestJson)) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 49 | if resp != nil { |
| 50 | defer resp.Body.Close() |
| 51 | } |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 52 | if err != nil { |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 53 | logrus.Errorf("client post error %s", err) |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 54 | return |
| 55 | } |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 56 | |
Scott Baker | bdb962b | 2020-04-03 10:53:36 -0700 | [diff] [blame] | 57 | if resp.StatusCode != 201 && resp.StatusCode != 204 { |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 58 | result := make(map[string]interface{}) |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 59 | dec := json.NewDecoder(resp.Body) |
Scott Baker | bdb962b | 2020-04-03 10:53:36 -0700 | [diff] [blame] | 60 | if err := dec.Decode(&result); err != nil && err != io.EOF { |
| 61 | logrus.Errorf("ERROR while adding event subscription:%s ", err.Error()) |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 62 | return |
| 63 | } |
| 64 | logrus.Infof("Result Decode %s", result) |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 65 | fmt.Println(result["data"]) |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 66 | logrus.Errorf("Add %s subscription failed. HTTP response status:%s ", event, resp.Status) |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 67 | return |
| 68 | } |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 69 | rtn = true |
| 70 | loc := resp.Header["Location"] |
| 71 | re := regexp.MustCompile(`/(\w+)$`) |
| 72 | match := re.FindStringSubmatch(loc[0]) |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 73 | s.devicemap[ip].Subscriptions[event] = match[1] |
| 74 | |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 75 | logrus.Infof("Subscription %s id %s was successfully added", event, match[1]) |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 76 | return |
| 77 | } |
| 78 | |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 79 | func (s *Server) remove_subscription(ip string, event string) bool { |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 80 | id := s.devicemap[ip].Subscriptions[event] |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 81 | uri := RF_DEFAULT_PROTOCOL + ip + RF_SUBSCRIPTION + id |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 82 | req, _ := http.NewRequest("DELETE", uri, nil) |
| 83 | resp, err := http.DefaultClient.Do(req) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 84 | if resp != nil { |
| 85 | defer resp.Body.Close() |
| 86 | } |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 87 | if err != nil { |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 88 | logrus.Errorf("Error DefaultClient.Do %s", err) |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 89 | return false |
| 90 | } |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 91 | |
| 92 | if code := resp.StatusCode; code < 200 && code > 299 { |
| 93 | result := make(map[string]interface{}) |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 94 | 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) |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 100 | fmt.Println(result["data"]) |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 101 | logrus.Errorf("Remove subscription failed. HTTP response status:%s", resp.Status) |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 102 | return false |
| 103 | } |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 104 | delete(s.devicemap[ip].Subscriptions, event) |
| 105 | |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 106 | logrus.Infof("Subscription id %s was successfully removed", id) |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 107 | return true |
| 108 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 109 | |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 110 | func (s *Server) get_event_types(ip string) (eventtypes []string) { |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 111 | resp, err := http.Get(RF_DEFAULT_PROTOCOL + ip + RF_EVENTSERVICE) |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 112 | logrus.Info("get_event_types") |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 113 | if resp != nil { |
| 114 | defer resp.Body.Close() |
| 115 | } |
| 116 | if err != nil { |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 117 | logrus.Errorf("http get Error %s", err) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 118 | return |
| 119 | } |
| 120 | body, err := ioutil.ReadAll(resp.Body) |
| 121 | if err != nil { |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 122 | logrus.Errorf("Read error %s", err) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 123 | return |
| 124 | } |
| 125 | |
| 126 | m := map[string]interface{}{} |
| 127 | err = json.Unmarshal([]byte(body), &m) |
| 128 | if err != nil { |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 129 | logrus.Errorf("ErrorUnmarshal %s", err) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 130 | return |
| 131 | } |
| 132 | e := m["EventTypesForSubscription"].([]interface{}) |
Dinesh Belwalkar | a6ba07d | 2020-01-10 23:22:34 +0000 | [diff] [blame] | 133 | logrus.Infof("supported event types %v\n", e) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 134 | for _, val := range e { |
| 135 | eventtypes = append(eventtypes, val.(string)) |
| 136 | } |
| 137 | return |
| 138 | } |