Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 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. |
Roni Riska | 6ffba08 | 2019-11-27 10:59:54 +0200 | [diff] [blame] | 16 | * |
| 17 | * This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 18 | * platform project (RICP). |
| 19 | * |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | package main |
| 23 | |
| 24 | import ( |
| 25 | "bytes" |
| 26 | "encoding/json" |
| 27 | "errors" |
| 28 | "fmt" |
| 29 | "io/ioutil" |
| 30 | "net/http" |
| 31 | "time" |
| 32 | ) |
| 33 | |
| 34 | // appmgr API |
| 35 | const appmgrSubsPath = "/ric/v1/subscriptions" |
| 36 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 37 | var errPostingFailed = errors.New("Posting subscriptions failed") |
| 38 | var errWrongStatusCode = errors.New("Wrong subscriptions response StatusCode") |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 39 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 40 | func subscribexAppNotifications(targetURL string, subscriptions chan subscriptionNotification, timeout time.Duration, subsURL string) { |
Roni Riska | ce6ecc6 | 2020-01-28 11:58:06 +0200 | [diff] [blame] | 41 | requestBody := []byte(fmt.Sprintf(`{"Data": {"maxRetries": 5, "retryTimer": 5, "eventType":"all", "targetUrl": "%v"}}`, targetURL)) |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 42 | req, err := http.NewRequest("POST", subsURL, bytes.NewBuffer(requestBody)) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 43 | if err != nil { |
| 44 | logger.Error("Setting NewRequest failed: %s", err) |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 45 | subscriptions <- subscriptionNotification{false, err, ""} |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 46 | return |
| 47 | } |
| 48 | req.Header.Set("Content-Type", "application/json") |
| 49 | client := &http.Client{} |
| 50 | client.Timeout = time.Second * timeout |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 51 | var subsID string |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 52 | for { |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 53 | subsID, err = subscribexAppNotificationsClientDo(req, client) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 54 | if err == nil { |
| 55 | break |
| 56 | } else if err != errPostingFailed && err != errWrongStatusCode { |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 57 | subscriptions <- subscriptionNotification{false, err, ""} |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 58 | return |
| 59 | } |
| 60 | time.Sleep(5 * time.Second) |
| 61 | } |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 62 | subscriptions <- subscriptionNotification{true, nil, subsID} |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 63 | } |
| 64 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 65 | func subscribexAppNotificationsClientDo(req *http.Request, client *http.Client) (string, error) { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 66 | resp, err := client.Do(req) |
| 67 | if err != nil { |
Abukar Mohamed | 267b909 | 2020-03-17 14:00:18 +0000 | [diff] [blame] | 68 | logger.Error("Posting subscriptions failed: %v", err) |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 69 | return "", errPostingFailed |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 70 | } |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame] | 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) |
| 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 Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 91 | } |