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 | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 21 | "io/ioutil" |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 22 | "net/http" |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 23 | "os" |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 24 | "regexp" |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 25 | ) |
| 26 | |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 27 | const RF_EVENTSERVICE = "/redfish/v1/EventService/" |
| 28 | const RF_SUBSCRIPTION = RF_EVENTSERVICE + "Subscriptions/" |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 29 | |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 30 | func (s *Server) add_subscription(ip string, event string) (rtn bool) { |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 31 | rtn = false |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 32 | |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 33 | destip := os.Getenv("EVENT_NOTIFICATION_DESTIP") + ":" + os.Getenv("DEVICE_MANAGEMENT_DESTPORT") |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 34 | subscrpt_info := map[string]interface{}{"Context": "TBD-" + destip, "Protocol": "Redfish"} |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 35 | subscrpt_info["Name"] = event + " event subscription" |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 36 | subscrpt_info["Destination"] = RF_DEFAULT_PROTOCOL + destip |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 37 | subscrpt_info["EventTypes"] = []string{event} |
| 38 | sRequestJson, err := json.Marshal(subscrpt_info) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 39 | uri := RF_DEFAULT_PROTOCOL + ip + RF_SUBSCRIPTION |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 40 | client := s.httpclient |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 41 | resp, err := client.Post(uri, CONTENT_TYPE, bytes.NewBuffer(sRequestJson)) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 42 | if resp != nil { |
| 43 | defer resp.Body.Close() |
| 44 | } |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 45 | if err != nil { |
| 46 | fmt.Println(err) |
| 47 | return |
| 48 | } |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 49 | |
| 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 Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 55 | fmt.Println("Add ", event, " subscription failed. HTTP response status: ", resp.Status) |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 56 | return |
| 57 | } |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 58 | rtn = true |
| 59 | loc := resp.Header["Location"] |
| 60 | re := regexp.MustCompile(`/(\w+)$`) |
| 61 | match := re.FindStringSubmatch(loc[0]) |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 62 | s.devicemap[ip].Subscriptions[event] = match[1] |
| 63 | |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 64 | fmt.Println("Subscription", event, "id", match[1], "was successfully added") |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 65 | return |
| 66 | } |
| 67 | |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 68 | func (s *Server) remove_subscription(ip string, event string) bool { |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 69 | id := s.devicemap[ip].Subscriptions[event] |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 70 | uri := RF_DEFAULT_PROTOCOL + ip + RF_SUBSCRIPTION + id |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 71 | req, _ := http.NewRequest("DELETE", uri, nil) |
| 72 | resp, err := http.DefaultClient.Do(req) |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 73 | if resp != nil { |
| 74 | defer resp.Body.Close() |
| 75 | } |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 76 | if err != nil { |
| 77 | fmt.Println(err) |
| 78 | return false |
| 79 | } |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 80 | |
| 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 | } |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 89 | delete(s.devicemap[ip].Subscriptions, event) |
| 90 | |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 91 | fmt.Println("Subscription id", id, "was successfully removed") |
| 92 | return true |
| 93 | } |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 94 | |
Dinesh Belwalkar | 72ecafb | 2019-12-12 00:08:56 +0000 | [diff] [blame] | 95 | func (s *Server) get_event_types(ip string) (eventtypes []string) { |
mc | 20a4b5f | 2019-10-16 20:28:24 +0000 | [diff] [blame] | 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 | } |