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.
17 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 * platform project (RICP).
35 const appmgrSubsPath = "/ric/v1/subscriptions"
37 var errPostingFailed = errors.New("Posting subscriptions failed")
38 var errWrongStatusCode = errors.New("Wrong subscriptions response StatusCode")
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))
44 logger.Error("Setting NewRequest failed: %s", err)
45 subscriptions <- subscriptionNotification{false, err, ""}
48 req.Header.Set("Content-Type", "application/json")
49 client := &http.Client{}
50 client.Timeout = time.Second * timeout
53 subsID, err = subscribexAppNotificationsClientDo(req, client)
56 } else if err != errPostingFailed && err != errWrongStatusCode {
57 subscriptions <- subscriptionNotification{false, err, ""}
60 time.Sleep(5 * time.Second)
62 subscriptions <- subscriptionNotification{true, nil, subsID}
65 func subscribexAppNotificationsClientDo(req *http.Request, client *http.Client) (string, error) {
66 resp, err := client.Do(req)
68 logger.Error("Posting subscriptions failed: %v", err)
69 return "", errPostingFailed
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)
77 logger.Error("Subscriptions response Body read failed: %s", err)
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)
86 logger.Info("Subscription id from the response: %s", result["id"].(string))
87 return result["id"].(string), nil
89 logger.Error("Wrong subscriptions response StatusCode: %d", resp.StatusCode)
90 return "", errWrongStatusCode