blob: 1d3cc22e4de6759f3ad298b3b7fa37c8e59fd956 [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.
16 */
17
18package main
19
20import (
21 "bytes"
22 "encoding/json"
23 "errors"
24 "fmt"
25 "io/ioutil"
26 "net/http"
27 "time"
28)
29
30// appmgr API
31const appmgrSubsPath = "/ric/v1/subscriptions"
32
Roni Riskafc77ebb2019-09-26 08:20:44 +030033var errPostingFailed = errors.New("Posting subscriptions failed")
34var errWrongStatusCode = errors.New("Wrong subscriptions response StatusCode")
Katri Turunen412df962019-09-16 08:48:18 +030035
Roni Riskafc77ebb2019-09-26 08:20:44 +030036func 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))
Katri Turunen412df962019-09-16 08:48:18 +030039 if err != nil {
40 logger.Error("Setting NewRequest failed: %s", err)
Roni Riskafc77ebb2019-09-26 08:20:44 +030041 subscriptions <- subscriptionNotification{false, err, ""}
Katri Turunen412df962019-09-16 08:48:18 +030042 return
43 }
44 req.Header.Set("Content-Type", "application/json")
45 client := &http.Client{}
46 client.Timeout = time.Second * timeout
Roni Riskafc77ebb2019-09-26 08:20:44 +030047 var subsID string
Katri Turunen412df962019-09-16 08:48:18 +030048 for {
Roni Riskafc77ebb2019-09-26 08:20:44 +030049 subsID, err = subscribexAppNotificationsClientDo(req, client)
Katri Turunen412df962019-09-16 08:48:18 +030050 if err == nil {
51 break
52 } else if err != errPostingFailed && err != errWrongStatusCode {
Roni Riskafc77ebb2019-09-26 08:20:44 +030053 subscriptions <- subscriptionNotification{false, err, ""}
Katri Turunen412df962019-09-16 08:48:18 +030054 return
55 }
56 time.Sleep(5 * time.Second)
57 }
Roni Riskafc77ebb2019-09-26 08:20:44 +030058 subscriptions <- subscriptionNotification{true, nil, subsID}
Katri Turunen412df962019-09-16 08:48:18 +030059}
60
Roni Riskafc77ebb2019-09-26 08:20:44 +030061func subscribexAppNotificationsClientDo(req *http.Request, client *http.Client) (string, error) {
Katri Turunen412df962019-09-16 08:48:18 +030062 resp, err := client.Do(req)
63 if err != nil {
64 logger.Error("Posting subscriptions failed: %s", err)
Roni Riskafc77ebb2019-09-26 08:20:44 +030065 return "", errPostingFailed
Katri Turunen412df962019-09-16 08:48:18 +030066 }
Roni Riskafc77ebb2019-09-26 08:20:44 +030067 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
Katri Turunen412df962019-09-16 08:48:18 +030087}