First commit

This commit is contained in:
Leonardo Bonati
2021-12-08 20:17:46 +00:00
commit 60dffad583
2923 changed files with 463894 additions and 0 deletions

View File

@@ -0,0 +1,47 @@
//
// Copyright 2019 AT&T Intellectual Property
// Copyright 2019 Nokia
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This source code is part of the near-RT RIC (RAN Intelligent Controller)
// platform project (RICP).
package clients
import (
"io"
"net/http"
)
type IHttpClient interface {
Post(url, contentType string, body io.Reader) (resp *http.Response, err error)
Delete(url, contentType string, body io.Reader) (resp *http.Response, err error)
}
type HttpClient struct {
*http.Client
}
func NewHttpClient() *HttpClient {
return &HttpClient{
&http.Client{},
}
}
func (c *HttpClient) Delete(url, contentType string, body io.Reader) (resp *http.Response, err error) {
req, _ := http.NewRequest(http.MethodDelete, url, body)
req.Header.Set("Content-Type", contentType)
return c.Do(req)
}

View File

@@ -0,0 +1,160 @@
//
// Copyright 2019 AT&T Intellectual Property
// Copyright 2019 Nokia
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This source code is part of the near-RT RIC (RAN Intelligent Controller)
// platform project (RICP).
package clients
import (
"bytes"
"e2mgr/configuration"
"e2mgr/e2managererrors"
"e2mgr/logger"
"e2mgr/models"
"encoding/json"
"net/http"
)
const (
AddE2TInstanceApiSuffix = "e2t"
AssociateRanToE2TInstanceApiSuffix = "associate-ran-to-e2t"
DissociateRanE2TInstanceApiSuffix = "dissociate-ran"
DeleteE2TInstanceApiSuffix = "e2t"
)
type RoutingManagerClient struct {
logger *logger.Logger
config *configuration.Configuration
httpClient IHttpClient
}
type IRoutingManagerClient interface {
AddE2TInstance(e2tAddress string) error
AssociateRanToE2TInstance(e2tAddress string, ranName string) error
DissociateRanE2TInstance(e2tAddress string, ranName string) error
DissociateAllRans(e2tAddresses []string) error
DeleteE2TInstance(e2tAddress string, ransToBeDissociated []string) error
}
func NewRoutingManagerClient(logger *logger.Logger, config *configuration.Configuration, httpClient IHttpClient) *RoutingManagerClient {
return &RoutingManagerClient{
logger: logger,
config: config,
httpClient: httpClient,
}
}
func (c *RoutingManagerClient) AddE2TInstance(e2tAddress string) error {
data := models.NewRoutingManagerE2TData(e2tAddress)
url := c.config.RoutingManager.BaseUrl + AddE2TInstanceApiSuffix
return c.PostMessage(url, data)
}
func (c *RoutingManagerClient) AssociateRanToE2TInstance(e2tAddress string, ranName string) error {
data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(e2tAddress, ranName)}
url := c.config.RoutingManager.BaseUrl + AssociateRanToE2TInstanceApiSuffix
return c.PostMessage(url, data)
}
func (c *RoutingManagerClient) DissociateRanE2TInstance(e2tAddress string, ranName string) error {
data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(e2tAddress, ranName)}
url := c.config.RoutingManager.BaseUrl + DissociateRanE2TInstanceApiSuffix
return c.PostMessage(url, data)
}
func (c *RoutingManagerClient) DissociateAllRans(e2tAddresses []string) error {
data := mapE2TAddressesToE2DataList(e2tAddresses)
url := c.config.RoutingManager.BaseUrl + DissociateRanE2TInstanceApiSuffix
return c.PostMessage(url, data)
}
func (c *RoutingManagerClient) DeleteE2TInstance(e2tAddress string, ransTobeDissociated []string) error {
data := models.NewRoutingManagerDeleteRequestModel(e2tAddress, ransTobeDissociated, nil)
url := c.config.RoutingManager.BaseUrl + DeleteE2TInstanceApiSuffix
return c.DeleteMessage(url, data)
}
func (c *RoutingManagerClient) sendMessage(method string, url string, data interface{}) error {
marshaled, err := json.Marshal(data)
if err != nil {
return e2managererrors.NewRoutingManagerError()
}
body := bytes.NewBuffer(marshaled)
c.logger.Infof("[E2 Manager -> Routing Manager] #RoutingManagerClient.sendMessage - %s url: %s, request body: %+v", method, url, body)
var resp *http.Response
if method == http.MethodPost {
resp, err = c.httpClient.Post(url, "application/json", body)
} else if method == http.MethodDelete {
resp, err = c.httpClient.Delete(url, "application/json", body)
}
if err != nil {
c.logger.Errorf("#RoutingManagerClient.sendMessage - failed sending request. error: %s", err)
return e2managererrors.NewRoutingManagerError()
}
defer resp.Body.Close()
if resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices {
c.logger.Infof("[Routing Manager -> E2 Manager] #RoutingManagerClient.sendMessage - success. http status code: %d", resp.StatusCode)
return nil
}
c.logger.Errorf("[Routing Manager -> E2 Manager] #RoutingManagerClient.sendMessage - failure. http status code: %d", resp.StatusCode)
return e2managererrors.NewRoutingManagerError()
}
func (c *RoutingManagerClient) DeleteMessage(url string, data interface{}) error {
return c.sendMessage(http.MethodDelete, url, data)
}
func (c *RoutingManagerClient) PostMessage(url string, data interface{}) error {
return c.sendMessage(http.MethodPost, url, data)
}
func mapE2TAddressesToE2DataList(e2tAddresses []string) models.RoutingManagerE2TDataList {
e2tDataList := make(models.RoutingManagerE2TDataList, len(e2tAddresses))
for i, v := range e2tAddresses {
e2tDataList[i] = models.NewRoutingManagerE2TData(v)
}
return e2tDataList
}
func convertE2TToRansAssociationsMapToE2TDataList(e2tToRansAssociations map[string][]string) models.RoutingManagerE2TDataList {
e2tDataList := make(models.RoutingManagerE2TDataList, len(e2tToRansAssociations))
i := 0
for k, v := range e2tToRansAssociations {
e2tDataList[i] = models.NewRoutingManagerE2TData(k, v...)
i++
}
return e2tDataList
}

View File

@@ -0,0 +1,218 @@
//
// Copyright 2019 AT&T Intellectual Property
// Copyright 2019 Nokia
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This source code is part of the near-RT RIC (RAN Intelligent Controller)
// platform project (RICP).
package clients
import (
"bytes"
"e2mgr/configuration"
"e2mgr/e2managererrors"
"e2mgr/logger"
"e2mgr/mocks"
"e2mgr/models"
"encoding/json"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"testing"
)
const E2TAddress = "10.0.2.15:38000"
const E2TAddress2 = "10.0.2.15:38001"
const RanName = "test1"
func initRoutingManagerClientTest(t *testing.T) (*RoutingManagerClient, *mocks.HttpClientMock, *configuration.Configuration) {
logger := initLog(t)
config := &configuration.Configuration{}
config.RoutingManager.BaseUrl = "http://iltlv740.intl.att.com:8080/ric/v1/handles/"
httpClientMock := &mocks.HttpClientMock{}
rmClient := NewRoutingManagerClient(logger, config, httpClientMock)
return rmClient, httpClientMock, config
}
func TestDeleteE2TInstanceSuccess(t *testing.T) {
rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, []string{"test1"}, nil)
marshaled, _ := json.Marshal(data)
body := bytes.NewBuffer(marshaled)
url := config.RoutingManager.BaseUrl + "e2t"
respBody := ioutil.NopCloser(bytes.NewBufferString(""))
httpClientMock.On("Delete", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusOK, Body: respBody}, nil)
err := rmClient.DeleteE2TInstance(E2TAddress, []string{"test1"})
assert.Nil(t, err)
}
func TestDeleteE2TInstanceFailure(t *testing.T) {
rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, []string{"test1"},nil)
marshaled, _ := json.Marshal(data)
body := bytes.NewBuffer(marshaled)
url := config.RoutingManager.BaseUrl + "e2t"
respBody := ioutil.NopCloser(bytes.NewBufferString(""))
httpClientMock.On("Delete", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusBadRequest, Body: respBody}, nil)
err := rmClient.DeleteE2TInstance(E2TAddress, []string{"test1"})
assert.IsType(t, &e2managererrors.RoutingManagerError{}, err)
}
func TestDeleteE2TInstanceDeleteFailure(t *testing.T) {
rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
data := models.NewRoutingManagerDeleteRequestModel(E2TAddress, []string{"test1"},nil)
marshaled, _ := json.Marshal(data)
body := bytes.NewBuffer(marshaled)
url := config.RoutingManager.BaseUrl + "e2t"
httpClientMock.On("Delete", url, "application/json", body).Return(&http.Response{}, errors.New("error"))
err := rmClient.DeleteE2TInstance(E2TAddress, []string{"test1"})
assert.IsType(t, &e2managererrors.RoutingManagerError{}, err)
}
func TestAddE2TInstanceSuccess(t *testing.T) {
rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
data := models.NewRoutingManagerE2TData(E2TAddress)
marshaled, _ := json.Marshal(data)
body := bytes.NewBuffer(marshaled)
url := config.RoutingManager.BaseUrl + "e2t"
respBody := ioutil.NopCloser(bytes.NewBufferString(""))
httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
err := rmClient.AddE2TInstance(E2TAddress)
assert.Nil(t, err)
}
func TestAddE2TInstanceHttpPostFailure(t *testing.T) {
rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
data := models.NewRoutingManagerE2TData(E2TAddress)
marshaled, _ := json.Marshal(data)
body := bytes.NewBuffer(marshaled)
url := config.RoutingManager.BaseUrl + "e2t"
httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{}, errors.New("error"))
err := rmClient.AddE2TInstance(E2TAddress)
assert.IsType(t, &e2managererrors.RoutingManagerError{}, err)
}
func TestAddE2TInstanceFailure(t *testing.T) {
rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
data := models.NewRoutingManagerE2TData(E2TAddress)
marshaled, _ := json.Marshal(data)
body := bytes.NewBuffer(marshaled)
url := config.RoutingManager.BaseUrl + "e2t"
respBody := ioutil.NopCloser(bytes.NewBufferString(""))
httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusBadRequest, Body: respBody}, nil)
err := rmClient.AddE2TInstance(E2TAddress)
assert.NotNil(t, err)
}
func TestAssociateRanToE2TInstance_Success(t *testing.T) {
rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(E2TAddress, RanName)}
marshaled, _ := json.Marshal(data)
body := bytes.NewBuffer(marshaled)
url := config.RoutingManager.BaseUrl + AssociateRanToE2TInstanceApiSuffix
respBody := ioutil.NopCloser(bytes.NewBufferString(""))
httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
err := rmClient.AssociateRanToE2TInstance(E2TAddress, RanName)
assert.Nil(t, err)
}
func TestAssociateRanToE2TInstance_RoutingManagerError(t *testing.T) {
rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(E2TAddress, RanName)}
marshaled, _ := json.Marshal(data)
body := bytes.NewBuffer(marshaled)
url := config.RoutingManager.BaseUrl + AssociateRanToE2TInstanceApiSuffix
httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{}, errors.New("error"))
err := rmClient.AssociateRanToE2TInstance(E2TAddress, RanName)
assert.IsType(t, &e2managererrors.RoutingManagerError{}, err)
}
func TestAssociateRanToE2TInstance_RoutingManager_400(t *testing.T) {
rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(E2TAddress, RanName)}
marshaled, _ := json.Marshal(data)
body := bytes.NewBuffer(marshaled)
url := config.RoutingManager.BaseUrl + AssociateRanToE2TInstanceApiSuffix
respBody := ioutil.NopCloser(bytes.NewBufferString(""))
httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusBadRequest, Body: respBody}, nil)
err := rmClient.AssociateRanToE2TInstance(E2TAddress, RanName)
assert.IsType(t, &e2managererrors.RoutingManagerError{}, err)
}
func TestDissociateRanE2TInstance_Success(t *testing.T) {
rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(E2TAddress, RanName)}
marshaled, _ := json.Marshal(data)
body := bytes.NewBuffer(marshaled)
url := config.RoutingManager.BaseUrl + DissociateRanE2TInstanceApiSuffix
respBody := ioutil.NopCloser(bytes.NewBufferString(""))
httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusCreated, Body: respBody}, nil)
err := rmClient.DissociateRanE2TInstance(E2TAddress, RanName)
assert.Nil(t, err)
}
func TestDissociateRanE2TInstance_RoutingManagerError(t *testing.T) {
rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(E2TAddress, RanName)}
marshaled, _ := json.Marshal(data)
body := bytes.NewBuffer(marshaled)
url := config.RoutingManager.BaseUrl + DissociateRanE2TInstanceApiSuffix
httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{}, errors.New("error"))
err := rmClient.DissociateRanE2TInstance(E2TAddress, RanName)
assert.IsType(t, &e2managererrors.RoutingManagerError{}, err)
}
func TestDissociateRanE2TInstance_RoutingManager_400(t *testing.T) {
rmClient, httpClientMock, config := initRoutingManagerClientTest(t)
data := models.RoutingManagerE2TDataList{models.NewRoutingManagerE2TData(E2TAddress, RanName)}
marshaled, _ := json.Marshal(data)
body := bytes.NewBuffer(marshaled)
url := config.RoutingManager.BaseUrl + DissociateRanE2TInstanceApiSuffix
respBody := ioutil.NopCloser(bytes.NewBufferString(""))
httpClientMock.On("Post", url, "application/json", body).Return(&http.Response{StatusCode: http.StatusBadRequest, Body: respBody}, nil)
err := rmClient.DissociateRanE2TInstance(E2TAddress, RanName)
assert.IsType(t, &e2managererrors.RoutingManagerError{}, err)
}
// TODO: extract to test_utils
func initLog(t *testing.T) *logger.Logger {
log, err := logger.InitLogger(logger.InfoLevel)
if err != nil {
t.Errorf("#delete_all_request_handler_test.TestHandleSuccessFlow - failed to initialize logger, error: %s", err)
}
return log
}
//func TestAddE2TInstanceInteg(t *testing.T) {
// logger := initLog(t)
// config := configuration.ParseConfiguration()
// httpClient := &http.Client{}
// rmClient := NewRoutingManagerClient(logger, config, httpClient)
// err := rmClient.AddE2TInstance(E2TAddress)
// assert.Nil(t, err)
//}