blob: c87a0a3e8bcc5ba97afb79e6aa4f940a2116727d [file] [log] [blame]
Katri Turunen412df962019-09-16 08:48:18 +03001/*
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.
Roni Riska6ffba082019-11-27 10:59:54 +020016 *
17 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 * platform project (RICP).
19 *
Katri Turunen412df962019-09-16 08:48:18 +030020 */
21
22package main
23
24import (
25 "bytes"
26 "encoding/json"
27 "errors"
28 "fmt"
29 "io/ioutil"
30 "net/http"
31 "time"
32)
33
34// appmgr API
35const appmgrSubsPath = "/ric/v1/subscriptions"
36
Roni Riskafc77ebb2019-09-26 08:20:44 +030037var errPostingFailed = errors.New("Posting subscriptions failed")
38var errWrongStatusCode = errors.New("Wrong subscriptions response StatusCode")
Katri Turunen412df962019-09-16 08:48:18 +030039
Roni Riskafc77ebb2019-09-26 08:20:44 +030040func subscribexAppNotifications(targetURL string, subscriptions chan subscriptionNotification, timeout time.Duration, subsURL string) {
Roni Riskace6ecc62020-01-28 11:58:06 +020041 requestBody := []byte(fmt.Sprintf(`{"Data": {"maxRetries": 5, "retryTimer": 5, "eventType":"all", "targetUrl": "%v"}}`, targetURL))
Roni Riskafc77ebb2019-09-26 08:20:44 +030042 req, err := http.NewRequest("POST", subsURL, bytes.NewBuffer(requestBody))
Katri Turunen412df962019-09-16 08:48:18 +030043 if err != nil {
44 logger.Error("Setting NewRequest failed: %s", err)
Roni Riskafc77ebb2019-09-26 08:20:44 +030045 subscriptions <- subscriptionNotification{false, err, ""}
Katri Turunen412df962019-09-16 08:48:18 +030046 return
47 }
48 req.Header.Set("Content-Type", "application/json")
49 client := &http.Client{}
50 client.Timeout = time.Second * timeout
Roni Riskafc77ebb2019-09-26 08:20:44 +030051 var subsID string
Katri Turunen412df962019-09-16 08:48:18 +030052 for {
Roni Riskafc77ebb2019-09-26 08:20:44 +030053 subsID, err = subscribexAppNotificationsClientDo(req, client)
Katri Turunen412df962019-09-16 08:48:18 +030054 if err == nil {
55 break
56 } else if err != errPostingFailed && err != errWrongStatusCode {
Roni Riskafc77ebb2019-09-26 08:20:44 +030057 subscriptions <- subscriptionNotification{false, err, ""}
Katri Turunen412df962019-09-16 08:48:18 +030058 return
59 }
60 time.Sleep(5 * time.Second)
61 }
Roni Riskafc77ebb2019-09-26 08:20:44 +030062 subscriptions <- subscriptionNotification{true, nil, subsID}
Katri Turunen412df962019-09-16 08:48:18 +030063}
64
Roni Riskafc77ebb2019-09-26 08:20:44 +030065func subscribexAppNotificationsClientDo(req *http.Request, client *http.Client) (string, error) {
Katri Turunen412df962019-09-16 08:48:18 +030066 resp, err := client.Do(req)
67 if err != nil {
Abukar Mohamed267b9092020-03-17 14:00:18 +000068 logger.Error("Posting subscriptions failed: %v", err)
Roni Riskafc77ebb2019-09-26 08:20:44 +030069 return "", errPostingFailed
Katri Turunen412df962019-09-16 08:48:18 +030070 }
Roni Riskafc77ebb2019-09-26 08:20:44 +030071 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
Katri Turunen412df962019-09-16 08:48:18 +030091}