Refactor the code
[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
18 package main
19
20 import (
21         "bytes"
22         "encoding/json"
23         "errors"
24         "fmt"
25         "io/ioutil"
26         "net/http"
27         "time"
28 )
29
30 // appmgr API
31 const appmgrSubsPath = "/ric/v1/subscriptions"
32
33 var errPostingFailed = errors.New("Posting subscriptions failed")
34 var errWrongStatusCode = errors.New("Wrong subscriptions response StatusCode")
35
36 func subscribexAppNotifications(targetURL string, subscriptions chan subscriptionNotification, timeout time.Duration, subsURL string) {
37         requestBody := []byte(fmt.Sprintf(`{"maxRetries": 5, "retryTimer": 5, "eventType":"all", "targetUrl": "%v"}`, targetURL))
38         req, err := http.NewRequest("POST", subsURL, bytes.NewBuffer(requestBody))
39         if err != nil {
40                 logger.Error("Setting NewRequest failed: %s", err)
41                 subscriptions <- subscriptionNotification{false, err, ""}
42                 return
43         }
44         req.Header.Set("Content-Type", "application/json")
45         client := &http.Client{}
46         client.Timeout = time.Second * timeout
47         var subsID string
48         for {
49                 subsID, err = subscribexAppNotificationsClientDo(req, client)
50                 if err == nil {
51                         break
52                 } else if err != errPostingFailed && err != errWrongStatusCode {
53                         subscriptions <- subscriptionNotification{false, err, ""}
54                         return
55                 }
56                 time.Sleep(5 * time.Second)
57         }
58         subscriptions <- subscriptionNotification{true, nil, subsID}
59 }
60
61 func subscribexAppNotificationsClientDo(req *http.Request, client *http.Client) (string, error) {
62         resp, err := client.Do(req)
63         if err != nil {
64                 logger.Error("Posting subscriptions failed: %s", err)
65                 return "", errPostingFailed
66         }
67         defer resp.Body.Close()
68         if resp.StatusCode == http.StatusCreated {
69                 logger.Info("Subscriptions response StatusCode: %d", resp.StatusCode)
70                 logger.Info("Subscriptions response headers: %s", resp.Header)
71                 body, err := ioutil.ReadAll(resp.Body)
72                 if err != nil {
73                         logger.Error("Subscriptions response Body read failed: %s", err)
74                         return "", err
75                 }
76                 logger.Info("Response Body: %s", body)
77                 var result map[string]interface{}
78                 if err := json.Unmarshal([]byte(body), &result); err != nil {
79                         logger.Error("json.Unmarshal failed: %s", err)
80                         return "", err
81                 }
82                 logger.Info("Subscription id from the response: %s", result["id"].(string))
83                 return result["id"].(string), nil
84         }
85         logger.Error("Wrong subscriptions response StatusCode: %d", resp.StatusCode)
86         return "", errWrongStatusCode
87 }