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