[RICPLT-2590] Update Red Button Flow with MultiE2T support + UTs
[ric-plt/e2mgr.git] / E2Manager / clients / routing_manager_client.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 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 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 //  platform project (RICP).
19
20 package clients
21
22 import (
23         "bytes"
24         "e2mgr/configuration"
25         "e2mgr/e2managererrors"
26         "e2mgr/logger"
27         "e2mgr/models"
28         "encoding/json"
29         "net/http"
30 )
31
32 const (
33         AddE2TInstanceApiSuffix            = "e2t"
34         AssociateRanToE2TInstanceApiSuffix = "associate-ran-to-e2t"
35         DissociateRanE2TInstanceApiSuffix  = "dissociate-ran"
36 )
37
38 type RoutingManagerClient struct {
39         logger     *logger.Logger
40         config     *configuration.Configuration
41         httpClient HttpClient
42 }
43
44 type IRoutingManagerClient interface {
45         AddE2TInstance(e2tAddress string) error
46         AssociateRanToE2TInstance(e2tAddress string, ranName string) error
47         DissociateRanE2TInstance(e2tAddress string, ranName string) error
48         DissociateAllRans(e2tAddresses []string) error
49 }
50
51 func NewRoutingManagerClient(logger *logger.Logger, config *configuration.Configuration, httpClient HttpClient) *RoutingManagerClient {
52         return &RoutingManagerClient{
53                 logger:     logger,
54                 config:     config,
55                 httpClient: httpClient,
56         }
57 }
58
59 func (c *RoutingManagerClient) AddE2TInstance(e2tAddress string) error {
60
61         data := models.NewRoutingManagerE2TData(e2tAddress)
62         url := c.config.RoutingManager.BaseUrl + AddE2TInstanceApiSuffix
63
64         return c.PostMessage(data, url)
65 }
66
67 func (c *RoutingManagerClient) AssociateRanToE2TInstance(e2tAddress string, ranName string) error {
68
69         data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(e2tAddress, ranName)}
70         url := c.config.RoutingManager.BaseUrl + AssociateRanToE2TInstanceApiSuffix
71
72         return c.PostMessage(data, url)
73 }
74
75 func (c *RoutingManagerClient) DissociateRanE2TInstance(e2tAddress string, ranName string) error {
76
77         data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(e2tAddress, ranName)}
78         url := c.config.RoutingManager.BaseUrl + DissociateRanE2TInstanceApiSuffix
79
80         return c.PostMessage(data, url)
81 }
82
83 func (c *RoutingManagerClient) DissociateAllRans(e2tAddresses []string) error {
84
85         data := mapE2TAddressesToE2DataList(e2tAddresses)
86         url := c.config.RoutingManager.BaseUrl + DissociateRanE2TInstanceApiSuffix
87
88         return c.PostMessage(data, url)
89 }
90
91 func (c *RoutingManagerClient) PostMessage(data interface{}, url string) error {
92         marshaled, err := json.Marshal(data)
93
94         if err != nil {
95                 return e2managererrors.NewRoutingManagerError()
96         }
97
98         body := bytes.NewBuffer(marshaled)
99         c.logger.Infof("[E2 Manager -> Routing Manager] #RoutingManagerClient.PostMessage - url: %s, request body: %+v", url, body)
100
101         resp, err := c.httpClient.Post(url, "application/json", body)
102
103         if err != nil {
104                 c.logger.Errorf("#RoutingManagerClient.PostMessage - failed sending request. error: %s", err)
105                 return e2managererrors.NewRoutingManagerError()
106         }
107
108         defer resp.Body.Close()
109
110         if resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices {
111                 c.logger.Infof("[Routing Manager -> E2 Manager] #RoutingManagerClient.PostMessage - success. http status code: %d", resp.StatusCode)
112                 return nil
113         }
114
115         c.logger.Errorf("[Routing Manager -> E2 Manager] #RoutingManagerClient.PostMessage - failure. http status code: %d", resp.StatusCode)
116         return e2managererrors.NewRoutingManagerError()
117 }
118
119 func mapE2TAddressesToE2DataList(e2tAddresses []string) models.RoutingManagerE2TDataList {
120         e2tDataList := make(models.RoutingManagerE2TDataList, len(e2tAddresses))
121
122         for i, v := range e2tAddresses {
123                 e2tDataList[i] = models.NewRoutingManagerE2TData(v)
124         }
125
126         return e2tDataList
127 }