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" |
| 21 | "net/http" |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 22 | "os" |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame^] | 23 | "regexp" |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 24 | ) |
| 25 | |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 26 | const RF_SUBSCRIPTION = "/EventService/Subscriptions/" |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 27 | |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 28 | func (s *Server) add_subscription(ip string, event string, f *os.File) (rtn bool) { |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 29 | rtn = false |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 30 | |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 31 | destip := os.Getenv("EVENT_NOTIFICATION_DESTIP") + ":" + os.Getenv("DEVICE_MANAGEMENT_DESTPORT") |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame^] | 32 | subscrpt_info := map[string]interface{}{"Context": "TBD-" + destip, "Protocol": "Redfish"} |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 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) |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 37 | uri := s.devicemap[ip].Protocol + "://" + ip + REDFISH_ROOT + RF_SUBSCRIPTION |
| 38 | client := s.httpclient |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 39 | 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"]) |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame^] | 51 | fmt.Println("Add ", event, " subscription failed. HTTP response status: ", resp.Status) |
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 | rtn = true |
| 55 | loc := resp.Header["Location"] |
| 56 | re := regexp.MustCompile(`/(\w+)$`) |
| 57 | match := re.FindStringSubmatch(loc[0]) |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 58 | s.devicemap[ip].Subscriptions[event] = match[1] |
| 59 | |
| 60 | if f != nil { |
| 61 | b, err := json.Marshal(s.devicemap[ip]) |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 62 | fmt.Println(string(b)) |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 63 | 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") |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 79 | return |
| 80 | } |
| 81 | |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 82 | func (s *Server) remove_subscription(ip string, event string, f *os.File) bool { |
| 83 | id := s.devicemap[ip].Subscriptions[event] |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 84 | uri := s.devicemap[ip].Protocol + "://" + ip + REDFISH_ROOT + RF_SUBSCRIPTION + id |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 85 | 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 | } |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 101 | 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 | } |
mc | 6a9f01a | 2019-06-26 21:31:23 +0000 | [diff] [blame] | 119 | fmt.Println("Subscription id", id, "was successfully removed") |
| 120 | return true |
| 121 | } |