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 ( |
| 18 | "encoding/json" |
| 19 | "fmt" |
| 20 | "net/http" |
| 21 | "bytes" |
| 22 | "regexp" |
| 23 | "strconv" |
| 24 | "time" |
| 25 | "os" |
| 26 | ) |
| 27 | |
| 28 | const REDFISH_PATH = "/redfish/v1/EventService/Subscriptions/" |
| 29 | const CONTENT_TYPE = "application/json" |
| 30 | |
| 31 | func add_subscription(ip string, event string) (rtn bool, id uint) { |
| 32 | rtn = false |
| 33 | id = 0 |
| 34 | |
| 35 | destip := os.Getenv("DEVICE_MANAGEMENT_DESTIP") + ":" + os.Getenv("DEVICE_MANAGEMENT_DESTPORT") |
| 36 | subscrpt_info := map[string]interface{}{"Context":"TBD","Protocol":"Redfish"} |
| 37 | subscrpt_info["Name"] = event + " event subscription" |
| 38 | subscrpt_info["Destination"] = "https://" + destip |
| 39 | subscrpt_info["EventTypes"] = []string{event} |
| 40 | sRequestJson, err := json.Marshal(subscrpt_info) |
| 41 | uri := ip + REDFISH_PATH |
| 42 | client := http.Client{Timeout: 10 * time.Second} |
| 43 | resp, err := client.Post(uri, CONTENT_TYPE, bytes.NewBuffer(sRequestJson)) |
| 44 | if err != nil { |
| 45 | fmt.Println(err) |
| 46 | return |
| 47 | } |
| 48 | defer resp.Body.Close() |
| 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"]) |
| 55 | fmt.Println("Add ", event, " subscription failed. HTTP response status: ", resp.Status) |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | rtn = true |
| 60 | loc := resp.Header["Location"] |
| 61 | re := regexp.MustCompile(`/(\w+)$`) |
| 62 | match := re.FindStringSubmatch(loc[0]) |
| 63 | idint, _ := strconv.Atoi(match[1]) |
| 64 | id = uint(idint) |
| 65 | fmt.Println("Subscription", event, "id", id, "was successfully added") |
| 66 | return |
| 67 | } |
| 68 | |
| 69 | func remove_subscription(ip string, id uint) bool { |
| 70 | uri := ip + REDFISH_PATH + strconv.Itoa(int(id)) |
| 71 | req, _ := http.NewRequest("DELETE", uri, nil) |
| 72 | resp, err := http.DefaultClient.Do(req) |
| 73 | if err != nil { |
| 74 | fmt.Println(err) |
| 75 | return false |
| 76 | } |
| 77 | defer resp.Body.Close() |
| 78 | |
| 79 | if code := resp.StatusCode; code < 200 && code > 299 { |
| 80 | result := make(map[string]interface{}) |
| 81 | json.NewDecoder(resp.Body).Decode(&result) |
| 82 | fmt.Println(result) |
| 83 | fmt.Println(result["data"]) |
| 84 | fmt.Println("Remove subscription failed. HTTP response status:", resp.Status) |
| 85 | return false |
| 86 | } |
| 87 | fmt.Println("Subscription id", id, "was successfully removed") |
| 88 | return true |
| 89 | } |
| 90 | |