2 * Copyright (c) 2019 AT&T Intellectual Property.
3 * Copyright (c) 2018-2019 Nokia.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
31 const appmgrSubsPath = "/ric/v1/subscriptions"
33 var errPostingFailed error = errors.New("Posting subscriptions failed")
34 var errWrongStatusCode error = errors.New("Wrong subscriptions response StatusCode")
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))
40 logger.Error("Setting NewRequest failed: %s", err)
41 subscriptions <- subsChannel{false, err}
44 req.Header.Set("Content-Type", "application/json")
45 client := &http.Client{}
46 client.Timeout = time.Second * timeout
48 err := subscribexAppNotificationsClientDo(req, client)
51 } else if err != errPostingFailed && err != errWrongStatusCode {
52 subscriptions <- subsChannel{false, err}
55 time.Sleep(5 * time.Second)
57 subscriptions <- subsChannel{true, nil}
60 func subscribexAppNotificationsClientDo(req *http.Request, client *http.Client) error {
61 resp, err := client.Do(req)
63 logger.Error("Posting subscriptions failed: %s", err)
64 return errPostingFailed
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)
72 logger.Error("Subscriptions response Body read failed: %s", err)
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)
81 logger.Info("Subscription id from the response: %s", result["id"].(string))
82 vesmgr.appmgrSubsId = result["id"].(string)
85 logger.Error("Wrong subscriptions response StatusCode: %d", resp.StatusCode)
86 return errWrongStatusCode