6f8ed77f952ad7ebe489c046e1fad1082cc608b7
[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 error = errors.New("Posting subscriptions failed")
34 var errWrongStatusCode error = errors.New("Wrong subscriptions response StatusCode")
35
36 func subscribexAppNotifications(targetUrl string, subscriptions chan subsChannel, 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 <- subsChannel{false, err}
42                 return
43         }
44         req.Header.Set("Content-Type", "application/json")
45         client := &http.Client{}
46         client.Timeout = time.Second * timeout
47         for {
48                 err := subscribexAppNotificationsClientDo(req, client)
49                 if err == nil {
50                         break
51                 } else if err != errPostingFailed && err != errWrongStatusCode {
52                         subscriptions <- subsChannel{false, err}
53                         return
54                 }
55                 time.Sleep(5 * time.Second)
56         }
57         subscriptions <- subsChannel{true, nil}
58 }
59
60 func subscribexAppNotificationsClientDo(req *http.Request, client *http.Client) error {
61         resp, err := client.Do(req)
62         if err != nil {
63                 logger.Error("Posting subscriptions failed: %s", err)
64                 return errPostingFailed
65         } else {
66                 defer resp.Body.Close()
67                 if resp.StatusCode == http.StatusCreated {
68                         logger.Info("Subscriptions response StatusCode: %d", resp.StatusCode)
69                         logger.Info("Subscriptions response headers: %s", resp.Header)
70                         body, err := ioutil.ReadAll(resp.Body)
71                         if err != nil {
72                                 logger.Error("Subscriptions response Body read failed: %s", err)
73                                 return err
74                         }
75                         logger.Info("Response Body: %s", body)
76                         var result map[string]interface{}
77                         if err := json.Unmarshal([]byte(body), &result); err != nil {
78                                 logger.Error("json.Unmarshal failed: %s", err)
79                                 return err
80                         }
81                         logger.Info("Subscription id from the response: %s", result["id"].(string))
82                         vesmgr.appmgrSubsId = result["id"].(string)
83                         return nil
84                 } else {
85                         logger.Error("Wrong subscriptions response StatusCode: %d", resp.StatusCode)
86                         return errWrongStatusCode
87                 }
88         }
89 }