df12b9341d4bd90bd8c5603ef56afa7997fa0cfc
[ric-plt/vespamgr.git] / cmd / vesmgr / subscribexAPPNotifications.go
1 /*
2  *  Copyright (c) 2019 AT&T Intellectual Property.
3  *  Copyright (c) 2018-2019 Nokia.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18  *  platform project (RICP).
19  *
20  */
21
22 package main
23
24 import (
25         "bytes"
26         "encoding/json"
27         "errors"
28         "fmt"
29         "io/ioutil"
30         "net/http"
31         "time"
32 )
33
34 // appmgr API
35 const appmgrSubsPath = "/ric/v1/subscriptions"
36
37 var errPostingFailed = errors.New("Posting subscriptions failed")
38 var errWrongStatusCode = errors.New("Wrong subscriptions response StatusCode")
39
40 func subscribexAppNotifications(targetURL string, subscriptions chan subscriptionNotification, timeout time.Duration, subsURL string) {
41         requestBody := []byte(fmt.Sprintf(`{"Data": {"maxRetries": 5, "retryTimer": 5, "eventType":"all", "targetUrl": "%v"}}`, targetURL))
42         req, err := http.NewRequest("POST", subsURL, bytes.NewBuffer(requestBody))
43         if err != nil {
44                 logger.Error("Setting NewRequest failed: %s", err)
45                 subscriptions <- subscriptionNotification{false, err, ""}
46                 return
47         }
48         req.Header.Set("Content-Type", "application/json")
49         client := &http.Client{}
50         client.Timeout = time.Second * timeout
51         var subsID string
52         for {
53                 subsID, err = subscribexAppNotificationsClientDo(req, client)
54                 if err == nil {
55                         break
56                 } else if err != errPostingFailed && err != errWrongStatusCode {
57                         subscriptions <- subscriptionNotification{false, err, ""}
58                         return
59                 }
60                 time.Sleep(5 * time.Second)
61         }
62         subscriptions <- subscriptionNotification{true, nil, subsID}
63 }
64
65 func subscribexAppNotificationsClientDo(req *http.Request, client *http.Client) (string, error) {
66         resp, err := client.Do(req)
67         if err != nil {
68                 logger.Error("Posting subscriptions failed: %s", err)
69                 return "", errPostingFailed
70         }
71         defer resp.Body.Close()
72         if resp.StatusCode == http.StatusCreated {
73                 logger.Info("Subscriptions response StatusCode: %d", resp.StatusCode)
74                 logger.Info("Subscriptions response headers: %s", resp.Header)
75                 body, err := ioutil.ReadAll(resp.Body)
76                 if err != nil {
77                         logger.Error("Subscriptions response Body read failed: %s", err)
78                         return "", err
79                 }
80                 logger.Info("Response Body: %s", body)
81                 var result map[string]interface{}
82                 if err := json.Unmarshal([]byte(body), &result); err != nil {
83                         logger.Error("json.Unmarshal failed: %s", err)
84                         return "", err
85                 }
86                 logger.Info("Subscription id from the response: %s", result["id"].(string))
87                 return result["id"].(string), nil
88         }
89         logger.Error("Wrong subscriptions response StatusCode: %d", resp.StatusCode)
90         return "", errWrongStatusCode
91 }