blob: b2f0112f550096f48a69808dc61008ed9b280a70 [file] [log] [blame]
mc6a9f01a2019-06-26 21:31:23 +00001// 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
15package main
16
17import (
18 "encoding/json"
19 "fmt"
20 "net/http"
21 "bytes"
22 "regexp"
23 "strconv"
24 "time"
25 "os"
26)
27
mce7028402019-07-18 04:10:01 +000028const RF_SUBSCRIPTION = "/EventService/Subscriptions"
mc6a9f01a2019-06-26 21:31:23 +000029
30func add_subscription(ip string, event string) (rtn bool, id uint) {
31 rtn = false
32 id = 0
33
Dinesh Belwalkare1e85ad2019-07-31 23:06:47 +000034 destip := os.Getenv("EVENT_NOTIFICATION_DESTIP") + ":" + os.Getenv("DEVICE_MANAGEMENT_DESTPORT")
mc6a9f01a2019-06-26 21:31:23 +000035 subscrpt_info := map[string]interface{}{"Context":"TBD","Protocol":"Redfish"}
36 subscrpt_info["Name"] = event + " event subscription"
37 subscrpt_info["Destination"] = "https://" + destip
38 subscrpt_info["EventTypes"] = []string{event}
39 sRequestJson, err := json.Marshal(subscrpt_info)
mce7028402019-07-18 04:10:01 +000040 uri := ip + REDFISH_ROOT + RF_SUBSCRIPTION
mc6a9f01a2019-06-26 21:31:23 +000041 client := http.Client{Timeout: 10 * time.Second}
42 resp, err := client.Post(uri, CONTENT_TYPE, bytes.NewBuffer(sRequestJson))
43 if err != nil {
44 fmt.Println(err)
45 return
46 }
47 defer resp.Body.Close()
48
49 if resp.StatusCode != 201 {
50 result := make(map[string]interface{})
51 json.NewDecoder(resp.Body).Decode(&result)
52 fmt.Println(result)
53 fmt.Println(result["data"])
54 fmt.Println("Add ", event, " subscription failed. HTTP response status: ", resp.Status)
55 return
56 }
57
58 rtn = true
59 loc := resp.Header["Location"]
60 re := regexp.MustCompile(`/(\w+)$`)
61 match := re.FindStringSubmatch(loc[0])
62 idint, _ := strconv.Atoi(match[1])
63 id = uint(idint)
64 fmt.Println("Subscription", event, "id", id, "was successfully added")
65 return
66}
67
68func remove_subscription(ip string, id uint) bool {
mce7028402019-07-18 04:10:01 +000069 uri := ip + REDFISH_ROOT + RF_SUBSCRIPTION + strconv.Itoa(int(id))
mc6a9f01a2019-06-26 21:31:23 +000070 req, _ := http.NewRequest("DELETE", uri, nil)
71 resp, err := http.DefaultClient.Do(req)
72 if err != nil {
73 fmt.Println(err)
74 return false
75 }
76 defer resp.Body.Close()
77
78 if code := resp.StatusCode; code < 200 && code > 299 {
79 result := make(map[string]interface{})
80 json.NewDecoder(resp.Body).Decode(&result)
81 fmt.Println(result)
82 fmt.Println(result["data"])
83 fmt.Println("Remove subscription failed. HTTP response status:", resp.Status)
84 return false
85 }
86 fmt.Println("Subscription id", id, "was successfully removed")
87 return true
88}
89