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. |
| 16 | */ |
| 17 | |
| 18 | package main |
| 19 | |
| 20 | import ( |
| 21 | "bytes" |
| 22 | "encoding/json" |
| 23 | "errors" |
| 24 | "fmt" |
| 25 | "io/ioutil" |
| 26 | "net/http" |
| 27 | "time" |
| 28 | ) |
| 29 | |
| 30 | // appmgr API |
| 31 | const appmgrSubsPath = "/ric/v1/subscriptions" |
| 32 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame^] | 33 | var errPostingFailed = errors.New("Posting subscriptions failed") |
| 34 | var errWrongStatusCode = errors.New("Wrong subscriptions response StatusCode") |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 35 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame^] | 36 | func 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 Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 39 | if err != nil { |
| 40 | logger.Error("Setting NewRequest failed: %s", err) |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame^] | 41 | subscriptions <- subscriptionNotification{false, err, ""} |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 42 | return |
| 43 | } |
| 44 | req.Header.Set("Content-Type", "application/json") |
| 45 | client := &http.Client{} |
| 46 | client.Timeout = time.Second * timeout |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame^] | 47 | var subsID string |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 48 | for { |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame^] | 49 | subsID, err = subscribexAppNotificationsClientDo(req, client) |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 50 | if err == nil { |
| 51 | break |
| 52 | } else if err != errPostingFailed && err != errWrongStatusCode { |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame^] | 53 | subscriptions <- subscriptionNotification{false, err, ""} |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 54 | return |
| 55 | } |
| 56 | time.Sleep(5 * time.Second) |
| 57 | } |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame^] | 58 | subscriptions <- subscriptionNotification{true, nil, subsID} |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 59 | } |
| 60 | |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame^] | 61 | func subscribexAppNotificationsClientDo(req *http.Request, client *http.Client) (string, error) { |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 62 | resp, err := client.Do(req) |
| 63 | if err != nil { |
| 64 | logger.Error("Posting subscriptions failed: %s", err) |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame^] | 65 | return "", errPostingFailed |
Katri Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 66 | } |
Roni Riska | fc77ebb | 2019-09-26 08:20:44 +0300 | [diff] [blame^] | 67 | 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 Turunen | 412df96 | 2019-09-16 08:48:18 +0300 | [diff] [blame] | 87 | } |