[RIC-346] Refactor E2 Setup flow and update EnGnb xml
[ric-plt/e2mgr.git] / E2Manager / controllers / nodeb_controller_test.go
index 47b217b..a9bccdc 100644 (file)
 //  This source code is part of the near-RT RIC (RAN Intelligent Controller)
 //  platform project (RICP).
 
-
 package controllers
 
 import (
        "bytes"
+       "e2mgr/clients"
        "e2mgr/configuration"
        "e2mgr/e2managererrors"
        "e2mgr/e2pdus"
@@ -30,7 +30,6 @@ import (
        "e2mgr/mocks"
        "e2mgr/models"
        "e2mgr/providers/httpmsghandlerprovider"
-       "e2mgr/rNibWriter"
        "e2mgr/rmrCgo"
        "e2mgr/services"
        "e2mgr/services/rmrsender"
@@ -39,7 +38,7 @@ import (
        "fmt"
        "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/common"
        "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
-       "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/reader"
+       "github.com/golang/protobuf/jsonpb"
        "github.com/gorilla/mux"
        "github.com/pkg/errors"
        "github.com/stretchr/testify/assert"
@@ -50,6 +49,21 @@ import (
        "net/http/httptest"
        "strings"
        "testing"
+       "unsafe"
+)
+
+const (
+       RanName                      = "test"
+       AssociatedE2TInstanceAddress = "10.0.2.15:38000"
+       ValidationFailureJson        = "{\"errorCode\":402,\"errorMessage\":\"Validation error\"}"
+       ResourceNotFoundJson         = "{\"errorCode\":404,\"errorMessage\":\"Resource not found\"}"
+       RnibErrorJson                = "{\"errorCode\":500,\"errorMessage\":\"RNIB error\"}"
+       InternalErrorJson            = "{\"errorCode\":501,\"errorMessage\":\"Internal Server Error. Please try again later\"}"
+)
+
+var (
+       ServedNrCellInformationRequiredFields = []string{"cellId", "choiceNrMode", "nrMode", "nrPci", "servedPlmns"}
+       NrNeighbourInformationRequiredFields  = []string{"nrCgi", "choiceNrMode", "nrMode", "nrPci"}
 )
 
 type controllerGetNodebTestContext struct {
@@ -67,30 +81,111 @@ type controllerGetNodebIdListTestContext struct {
        expectedJsonResponse string
 }
 
-func setupControllerTest(t *testing.T) (*NodebController, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *mocks.RmrMessengerMock) {
+type getNodebInfoResult struct {
+       nodebInfo *entities.NodebInfo
+       rnibError error
+}
+
+type updateGnbCellsParams struct {
+       err error
+}
+
+type removeServedNrCellsParams struct {
+       servedNrCells []*entities.ServedNRCell
+       err           error
+}
+
+type controllerUpdateGnbTestContext struct {
+       getNodebInfoResult        *getNodebInfoResult
+       removeServedNrCellsParams *removeServedNrCellsParams
+       updateGnbCellsParams      *updateGnbCellsParams
+       requestBody               map[string]interface{}
+       expectedStatusCode        int
+       expectedJsonResponse      string
+}
+
+func generateServedNrCells(cellIds ...string) []*entities.ServedNRCell {
+
+       servedNrCells := []*entities.ServedNRCell{}
+
+       for _, v := range cellIds {
+               servedNrCells = append(servedNrCells, &entities.ServedNRCell{ServedNrCellInformation: &entities.ServedNRCellInformation{
+                       CellId: v,
+                       ChoiceNrMode: &entities.ServedNRCellInformation_ChoiceNRMode{
+                               Fdd: &entities.ServedNRCellInformation_ChoiceNRMode_FddInfo{
+
+                               },
+                       },
+                       NrMode:      entities.Nr_FDD,
+                       NrPci:       5,
+                       ServedPlmns: []string{"whatever"},
+               }})
+       }
+
+       return servedNrCells
+}
+
+func buildNrNeighbourInformation(propToOmit string) map[string]interface{} {
+       ret := map[string]interface{}{
+               "nrCgi": "whatever",
+               "choiceNrMode": map[string]interface{}{
+                       "tdd": map[string]interface{}{},
+               },
+               "nrMode": 1,
+               "nrPci":  1,
+       }
+
+       if len(propToOmit) != 0 {
+               delete(ret, propToOmit)
+       }
+
+       return ret
+}
+
+func buildServedNrCellInformation(propToOmit string) map[string]interface{} {
+       ret := map[string]interface{}{
+               "cellId": "whatever",
+               "choiceNrMode": map[string]interface{}{
+                       "fdd": map[string]interface{}{},
+               },
+               "nrMode": 1,
+               "nrPci":  1,
+               "servedPlmns": []interface{}{
+                       "whatever",
+               },
+       }
+
+       if len(propToOmit) != 0 {
+               delete(ret, propToOmit)
+       }
+
+       return ret
+}
+
+func setupControllerTest(t *testing.T) (*NodebController, *mocks.RnibReaderMock, *mocks.RnibWriterMock, *mocks.RmrMessengerMock, *mocks.E2TInstancesManagerMock) {
        log := initLog(t)
        config := configuration.ParseConfiguration()
 
        rmrMessengerMock := &mocks.RmrMessengerMock{}
        readerMock := &mocks.RnibReaderMock{}
-       readerProvider := func() reader.RNibReader {
-               return readerMock
-       }
+
        writerMock := &mocks.RnibWriterMock{}
-       writerProvider := func() rNibWriter.RNibWriter {
-               return writerMock
-       }
-       rnibDataService := services.NewRnibDataService(log, config, readerProvider, writerProvider)
+
+       rnibDataService := services.NewRnibDataService(log, config, readerMock, writerMock)
        rmrSender := getRmrSender(rmrMessengerMock, log)
        ranSetupManager := managers.NewRanSetupManager(log, rmrSender, rnibDataService)
-       handlerProvider := httpmsghandlerprovider.NewIncomingRequestHandlerProvider(log, rmrSender, config, rnibDataService, ranSetupManager)
+       e2tInstancesManager := &mocks.E2TInstancesManagerMock{}
+       httpClientMock := &mocks.HttpClientMock{}
+       rmClient := clients.NewRoutingManagerClient(log, config, httpClientMock)
+       e2tAssociationManager := managers.NewE2TAssociationManager(log, rnibDataService, e2tInstancesManager, rmClient)
+       handlerProvider := httpmsghandlerprovider.NewIncomingRequestHandlerProvider(log, rmrSender, config, rnibDataService, ranSetupManager, e2tInstancesManager, e2tAssociationManager, rmClient)
        controller := NewNodebController(log, handlerProvider)
-       return controller, readerMock, writerMock, rmrMessengerMock
+       return controller, readerMock, writerMock, rmrMessengerMock, e2tInstancesManager
 }
 
 func TestX2SetupInvalidBody(t *testing.T) {
 
-       controller, _, _, _ := setupControllerTest(t)
+       controller, _, _, _, _ := setupControllerTest(t)
 
        header := http.Header{}
        header.Set("Content-Type", "application/json")
@@ -108,20 +203,23 @@ func TestX2SetupInvalidBody(t *testing.T) {
 
 func TestX2SetupSuccess(t *testing.T) {
 
-       controller, readerMock, writerMock, rmrMessengerMock := setupControllerTest(t)
+       controller, readerMock, writerMock, rmrMessengerMock, _ := setupControllerTest(t)
 
        ranName := "test"
-       nb := &entities.NodebInfo{RanName: ranName, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST}
+       nb := &entities.NodebInfo{RanName: ranName, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST, AssociatedE2TInstanceAddress: "10.0.2.15:8989"}
        readerMock.On("GetNodeb", ranName).Return(nb, nil)
+       var nbUpdated = *nb
+       writerMock.On("UpdateNodebInfo", &nbUpdated).Return(nil)
 
-       var nbUpdated = &entities.NodebInfo{RanName: ranName, ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST, ConnectionAttempts: 1}
-       writerMock.On("UpdateNodebInfo", nbUpdated).Return(nil)
+       var nbUpdated2 = &entities.NodebInfo{RanName: ranName, ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_X2_SETUP_REQUEST, AssociatedE2TInstanceAddress: "10.0.2.15:8989"}
+       writerMock.On("UpdateNodebInfo", nbUpdated2).Return(nil)
 
        payload := e2pdus.PackedX2setupRequest
-       xaction := []byte(ranName)
-       msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ranName, &payload, &xaction)
+       var xAction []byte
+       var msgSrc unsafe.Pointer
+       msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_SETUP_REQ, len(payload), ranName, &payload, &xAction, msgSrc)
 
-       rmrMessengerMock.On("SendMsg", mock.Anything).Return(msg, nil)
+       rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(msg, nil)
 
        header := http.Header{}
        header.Set("Content-Type", "application/json")
@@ -136,20 +234,23 @@ func TestX2SetupSuccess(t *testing.T) {
 
 func TestEndcSetupSuccess(t *testing.T) {
 
-       controller, readerMock, writerMock, rmrMessengerMock := setupControllerTest(t)
+       controller, readerMock, writerMock, rmrMessengerMock, _ := setupControllerTest(t)
 
        ranName := "test"
-       nb := &entities.NodebInfo{RanName: ranName, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST}
+       nb := &entities.NodebInfo{RanName: ranName, ConnectionStatus: entities.ConnectionStatus_DISCONNECTED, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST, AssociatedE2TInstanceAddress: "10.0.2.15:8989"}
        readerMock.On("GetNodeb", ranName).Return(nb, nil)
+       var nbUpdated = *nb
+       writerMock.On("UpdateNodebInfo", &nbUpdated).Return(nil)
 
-       var nbUpdated = &entities.NodebInfo{RanName: ranName, ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST, ConnectionAttempts: 1}
-       writerMock.On("UpdateNodebInfo", nbUpdated).Return(nil)
+       var nbUpdated2 = &entities.NodebInfo{RanName: ranName, ConnectionStatus: entities.ConnectionStatus_CONNECTING, E2ApplicationProtocol: entities.E2ApplicationProtocol_ENDC_X2_SETUP_REQUEST, AssociatedE2TInstanceAddress: "10.0.2.15:8989"}
+       writerMock.On("UpdateNodebInfo", nbUpdated2).Return(nil)
 
        payload := e2pdus.PackedEndcX2setupRequest
-       xaction := []byte(ranName)
-       msg := rmrCgo.NewMBuf(rmrCgo.RIC_ENDC_X2_SETUP_REQ, len(payload), ranName, &payload, &xaction)
+       var xAction []byte
+       var msgSrc unsafe.Pointer
+       msg := rmrCgo.NewMBuf(rmrCgo.RIC_ENDC_X2_SETUP_REQ, len(payload), ranName, &payload, &xAction, msgSrc)
 
-       rmrMessengerMock.On("SendMsg", mock.Anything).Return(msg, nil)
+       rmrMessengerMock.On("SendMsg", mock.Anything, true).Return(msg, nil)
 
        header := http.Header{}
        header.Set("Content-Type", "application/json")
@@ -163,11 +264,8 @@ func TestEndcSetupSuccess(t *testing.T) {
 }
 
 func TestShutdownHandlerRnibError(t *testing.T) {
-       controller, readerMock, _, _ := setupControllerTest(t)
-
-       rnibErr := &common.ResourceNotFoundError{}
-       var nbIdentityList []*entities.NbIdentity
-       readerMock.On("GetListNodebIds").Return(nbIdentityList, rnibErr)
+       controller, _, _, _, e2tInstancesManagerMock := setupControllerTest(t)
+       e2tInstancesManagerMock.On("GetE2TAddresses").Return([]string{}, e2managererrors.NewRnibDbError())
 
        writer := httptest.NewRecorder()
 
@@ -180,10 +278,10 @@ func TestShutdownHandlerRnibError(t *testing.T) {
 }
 
 func controllerGetNodebTestExecuter(t *testing.T, context *controllerGetNodebTestContext) {
-       controller, readerMock, _, _ := setupControllerTest(t)
+       controller, readerMock, _, _, _ := setupControllerTest(t)
        writer := httptest.NewRecorder()
        readerMock.On("GetNodeb", context.ranName).Return(context.nodebInfo, context.rnibError)
-       req, _ := http.NewRequest("GET", "/nodeb", nil)
+       req, _ := http.NewRequest(http.MethodGet, "/nodeb", nil)
        req = mux.SetURLVars(req, map[string]string{"ranName": context.ranName})
        controller.GetNodeb(writer, req)
        assert.Equal(t, context.expectedStatusCode, writer.Result().StatusCode)
@@ -192,16 +290,366 @@ func controllerGetNodebTestExecuter(t *testing.T, context *controllerGetNodebTes
 }
 
 func controllerGetNodebIdListTestExecuter(t *testing.T, context *controllerGetNodebIdListTestContext) {
-       controller, readerMock, _, _ := setupControllerTest(t)
+       controller, readerMock, _, _, _ := setupControllerTest(t)
        writer := httptest.NewRecorder()
        readerMock.On("GetListNodebIds").Return(context.nodebIdList, context.rnibError)
-       req, _ := http.NewRequest("GET", "/nodeb/ids", nil)
+       req, _ := http.NewRequest(http.MethodGet, "/nodeb/ids", nil)
        controller.GetNodebIdList(writer, req)
        assert.Equal(t, context.expectedStatusCode, writer.Result().StatusCode)
        bodyBytes, _ := ioutil.ReadAll(writer.Body)
        assert.Equal(t, context.expectedJsonResponse, string(bodyBytes))
 }
 
+func activateControllerUpdateGnbMocks(context *controllerUpdateGnbTestContext, readerMock *mocks.RnibReaderMock, writerMock *mocks.RnibWriterMock) {
+       if context.getNodebInfoResult != nil {
+               readerMock.On("GetNodeb", RanName).Return(context.getNodebInfoResult.nodebInfo, context.getNodebInfoResult.rnibError)
+       }
+
+       if context.removeServedNrCellsParams != nil {
+               writerMock.On("RemoveServedNrCells", RanName, context.removeServedNrCellsParams.servedNrCells).Return(context.removeServedNrCellsParams.err)
+       }
+
+       if context.updateGnbCellsParams != nil {
+               updatedNodebInfo := *context.getNodebInfoResult.nodebInfo
+               gnb := entities.Gnb{}
+               _ = jsonpb.Unmarshal(getJsonRequestAsBuffer(context.requestBody), &gnb)
+               updatedGnb := *updatedNodebInfo.GetGnb()
+               updatedGnb.ServedNrCells = gnb.ServedNrCells
+               writerMock.On("UpdateGnbCells", &updatedNodebInfo, gnb.ServedNrCells).Return(context.updateGnbCellsParams.err)
+       }
+}
+
+func assertControllerUpdateGnb(t *testing.T, context *controllerUpdateGnbTestContext, writer *httptest.ResponseRecorder, readerMock *mocks.RnibReaderMock, writerMock *mocks.RnibWriterMock) {
+       assert.Equal(t, context.expectedStatusCode, writer.Result().StatusCode)
+       bodyBytes, _ := ioutil.ReadAll(writer.Body)
+       assert.Equal(t, context.expectedJsonResponse, string(bodyBytes))
+       readerMock.AssertExpectations(t)
+       writerMock.AssertExpectations(t)
+
+       if context.getNodebInfoResult != nil {
+               readerMock.AssertNotCalled(t, "GetNodeb")
+       }
+
+       if context.updateGnbCellsParams != nil {
+               writerMock.AssertNotCalled(t, "UpdateGnb")
+       }
+
+       if context.removeServedNrCellsParams != nil {
+               writerMock.AssertNotCalled(t, "RemoveServedNrCells")
+       }
+}
+
+func buildUpdateGnbRequest(context *controllerUpdateGnbTestContext) *http.Request {
+       updateGnbUrl := fmt.Sprintf("/nodeb/%s/update", RanName)
+       requestBody := getJsonRequestAsBuffer(context.requestBody)
+       req, _ := http.NewRequest(http.MethodGet, updateGnbUrl, requestBody)
+       req.Header.Set("Content-Type", "application/json")
+       req = mux.SetURLVars(req, map[string]string{"ranName": RanName})
+       return req
+}
+
+func controllerUpdateGnbTestExecuter(t *testing.T, context *controllerUpdateGnbTestContext) {
+       controller, readerMock, writerMock, _, _ := setupControllerTest(t)
+       writer := httptest.NewRecorder()
+
+       activateControllerUpdateGnbMocks(context, readerMock, writerMock)
+       req := buildUpdateGnbRequest(context)
+       controller.UpdateGnb(writer, req)
+       assertControllerUpdateGnb(t, context, writer, readerMock, writerMock)
+}
+
+func TestControllerUpdateGnbEmptyServedNrCells(t *testing.T) {
+       context := controllerUpdateGnbTestContext{
+               getNodebInfoResult: nil,
+               requestBody: map[string]interface{}{
+                       "servedNrCells": []interface{}{
+                       },
+               },
+               expectedStatusCode:   http.StatusBadRequest,
+               expectedJsonResponse: ValidationFailureJson,
+       }
+
+       controllerUpdateGnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateGnbMissingServedNrCellInformation(t *testing.T) {
+       context := controllerUpdateGnbTestContext{
+               getNodebInfoResult: nil,
+               requestBody: map[string]interface{}{
+                       "servedNrCells": []interface{}{
+                               map[string]interface{}{
+                                       "servedNrCellInformation": nil,
+                               },
+                       },
+               },
+               expectedStatusCode:   http.StatusBadRequest,
+               expectedJsonResponse: ValidationFailureJson,
+       }
+
+       controllerUpdateGnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateGnbMissingServedNrCellRequiredProp(t *testing.T) {
+
+       for _, v := range ServedNrCellInformationRequiredFields {
+               context := controllerUpdateGnbTestContext{
+                       getNodebInfoResult: nil,
+                       requestBody: map[string]interface{}{
+                               "servedNrCells": []interface{}{
+                                       map[string]interface{}{
+                                               "servedNrCellInformation": buildServedNrCellInformation(v),
+                                       },
+                               },
+                       },
+                       expectedStatusCode:   http.StatusBadRequest,
+                       expectedJsonResponse: ValidationFailureJson,
+               }
+
+               controllerUpdateGnbTestExecuter(t, &context)
+       }
+}
+
+func TestControllerUpdateGnbMissingServedNrCellFddOrTdd(t *testing.T) {
+
+       servedNrCellInformation := buildServedNrCellInformation("")
+       servedNrCellInformation["choiceNrMode"] = map[string]interface{}{}
+
+       context := controllerUpdateGnbTestContext{
+               getNodebInfoResult: nil,
+               requestBody: map[string]interface{}{
+                       "servedNrCells": []interface{}{
+                               map[string]interface{}{
+                                       "servedNrCellInformation": servedNrCellInformation,
+                               },
+                       },
+               },
+               expectedStatusCode:   http.StatusBadRequest,
+               expectedJsonResponse: ValidationFailureJson,
+       }
+
+       controllerUpdateGnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateGnbMissingNeighbourInfoFddOrTdd(t *testing.T) {
+
+       nrNeighbourInfo := buildNrNeighbourInformation("")
+       nrNeighbourInfo["choiceNrMode"] = map[string]interface{}{}
+
+       context := controllerUpdateGnbTestContext{
+               getNodebInfoResult: nil,
+               requestBody: map[string]interface{}{
+                       "servedNrCells": []interface{}{
+                               map[string]interface{}{
+                                       "servedNrCellInformation": buildServedNrCellInformation(""),
+                                       "nrNeighbourInfos": []interface{}{
+                                               nrNeighbourInfo,
+                                       },
+                               },
+                       },
+               },
+               expectedStatusCode:   http.StatusBadRequest,
+               expectedJsonResponse: ValidationFailureJson,
+       }
+
+       controllerUpdateGnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateGnbMissingNrNeighbourInformationRequiredProp(t *testing.T) {
+
+       for _, v := range NrNeighbourInformationRequiredFields {
+               context := controllerUpdateGnbTestContext{
+                       getNodebInfoResult: nil,
+                       requestBody: map[string]interface{}{
+                               "servedNrCells": []interface{}{
+                                       map[string]interface{}{
+                                               "servedNrCellInformation": buildServedNrCellInformation(""),
+                                               "nrNeighbourInfos": []interface{}{
+                                                       buildNrNeighbourInformation(v),
+                                               },
+                                       },
+                               },
+                       },
+                       expectedStatusCode:   http.StatusBadRequest,
+                       expectedJsonResponse: ValidationFailureJson,
+               }
+
+               controllerUpdateGnbTestExecuter(t, &context)
+       }
+}
+
+func TestControllerUpdateGnbValidServedNrCellInformationGetNodebNotFound(t *testing.T) {
+       context := controllerUpdateGnbTestContext{
+               getNodebInfoResult: &getNodebInfoResult{
+                       nodebInfo: nil,
+                       rnibError: common.NewResourceNotFoundError("#reader.GetNodeb - Not found Error"),
+               },
+               requestBody: map[string]interface{}{
+                       "servedNrCells": []interface{}{
+                               map[string]interface{}{
+                                       "servedNrCellInformation": buildServedNrCellInformation(""),
+                               },
+                       },
+               },
+               expectedStatusCode:   http.StatusNotFound,
+               expectedJsonResponse: ResourceNotFoundJson,
+       }
+
+       controllerUpdateGnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateGnbValidServedNrCellInformationGetNodebInternalError(t *testing.T) {
+       context := controllerUpdateGnbTestContext{
+               getNodebInfoResult: &getNodebInfoResult{
+                       nodebInfo: nil,
+                       rnibError: common.NewInternalError(errors.New("#reader.GetNodeb - Internal Error")),
+               },
+               requestBody: map[string]interface{}{
+                       "servedNrCells": []interface{}{
+                               map[string]interface{}{
+                                       "servedNrCellInformation": buildServedNrCellInformation(""),
+                               },
+                       },
+               },
+               expectedStatusCode:   http.StatusInternalServerError,
+               expectedJsonResponse: RnibErrorJson,
+       }
+
+       controllerUpdateGnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateGnbGetNodebSuccessInvalidGnbConfiguration(t *testing.T) {
+       context := controllerUpdateGnbTestContext{
+               getNodebInfoResult: &getNodebInfoResult{
+                       nodebInfo: &entities.NodebInfo{
+                               RanName:                      RanName,
+                               ConnectionStatus:             entities.ConnectionStatus_CONNECTED,
+                               AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
+                       },
+                       rnibError: nil,
+               },
+               requestBody: map[string]interface{}{
+                       "servedNrCells": []interface{}{
+                               map[string]interface{}{
+                                       "servedNrCellInformation": buildServedNrCellInformation(""),
+                                       "nrNeighbourInfos": []interface{}{
+                                               buildNrNeighbourInformation(""),
+                                       },
+                               },
+                       },
+               },
+               expectedStatusCode:   http.StatusInternalServerError,
+               expectedJsonResponse: InternalErrorJson,
+       }
+
+       controllerUpdateGnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateGnbGetNodebSuccessRemoveServedNrCellsFailure(t *testing.T) {
+       oldServedNrCells := generateServedNrCells("whatever1", "whatever2")
+       context := controllerUpdateGnbTestContext{
+               removeServedNrCellsParams: &removeServedNrCellsParams{
+                       err:           common.NewInternalError(errors.New("#writer.UpdateGnbCells - Internal Error")),
+                       servedNrCells: oldServedNrCells,
+               },
+               getNodebInfoResult: &getNodebInfoResult{
+                       nodebInfo: &entities.NodebInfo{
+                               RanName:                      RanName,
+                               ConnectionStatus:             entities.ConnectionStatus_CONNECTED,
+                               AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
+                               Configuration:                &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{ServedNrCells: oldServedNrCells}},
+                       },
+                       rnibError: nil,
+               },
+               requestBody: map[string]interface{}{
+                       "servedNrCells": []interface{}{
+                               map[string]interface{}{
+                                       "servedNrCellInformation": buildServedNrCellInformation(""),
+                                       "nrNeighbourInfos": []interface{}{
+                                               buildNrNeighbourInformation(""),
+                                       },
+                               },
+                       },
+               },
+               expectedStatusCode:   http.StatusInternalServerError,
+               expectedJsonResponse: RnibErrorJson,
+       }
+
+       controllerUpdateGnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateGnbGetNodebSuccessUpdateGnbCellsFailure(t *testing.T) {
+       oldServedNrCells := generateServedNrCells("whatever1", "whatever2")
+       context := controllerUpdateGnbTestContext{
+               removeServedNrCellsParams: &removeServedNrCellsParams{
+                       err:           nil,
+                       servedNrCells: oldServedNrCells,
+               },
+               updateGnbCellsParams: &updateGnbCellsParams{
+                       err: common.NewInternalError(errors.New("#writer.UpdateGnbCells - Internal Error")),
+               },
+               getNodebInfoResult: &getNodebInfoResult{
+                       nodebInfo: &entities.NodebInfo{
+                               RanName:                      RanName,
+                               ConnectionStatus:             entities.ConnectionStatus_CONNECTED,
+                               AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
+                               Configuration:                &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{ServedNrCells: oldServedNrCells}},
+                       },
+                       rnibError: nil,
+               },
+               requestBody: map[string]interface{}{
+                       "servedNrCells": []interface{}{
+                               map[string]interface{}{
+                                       "servedNrCellInformation": buildServedNrCellInformation(""),
+                                       "nrNeighbourInfos": []interface{}{
+                                               buildNrNeighbourInformation(""),
+                                       },
+                               },
+                       },
+               },
+               expectedStatusCode:   http.StatusInternalServerError,
+               expectedJsonResponse: RnibErrorJson,
+       }
+
+       controllerUpdateGnbTestExecuter(t, &context)
+}
+
+func TestControllerUpdateGnbSuccess(t *testing.T) {
+       context := controllerUpdateGnbTestContext{
+               updateGnbCellsParams: &updateGnbCellsParams{
+                       err: nil,
+               },
+               getNodebInfoResult: &getNodebInfoResult{
+                       nodebInfo: &entities.NodebInfo{
+                               RanName:                      RanName,
+                               ConnectionStatus:             entities.ConnectionStatus_CONNECTED,
+                               AssociatedE2TInstanceAddress: AssociatedE2TInstanceAddress,
+                               Configuration:                &entities.NodebInfo_Gnb{Gnb: &entities.Gnb{}},
+                       },
+                       rnibError: nil,
+               },
+               requestBody: map[string]interface{}{
+                       "servedNrCells": []interface{}{
+                               map[string]interface{}{
+                                       "servedNrCellInformation": buildServedNrCellInformation(""),
+                                       "nrNeighbourInfos": []interface{}{
+                                               buildNrNeighbourInformation(""),
+                                       },
+                               },
+                       },
+               },
+               expectedStatusCode:   http.StatusOK,
+               expectedJsonResponse: "{\"ranName\":\"test\",\"connectionStatus\":\"CONNECTED\",\"gnb\":{\"servedNrCells\":[{\"servedNrCellInformation\":{\"nrPci\":1,\"cellId\":\"whatever\",\"servedPlmns\":[\"whatever\"],\"nrMode\":\"FDD\",\"choiceNrMode\":{\"fdd\":{}}},\"nrNeighbourInfos\":[{\"nrPci\":1,\"nrCgi\":\"whatever\",\"nrMode\":\"FDD\",\"choiceNrMode\":{\"tdd\":{}}}]}]},\"associatedE2tInstanceAddress\":\"10.0.2.15:38000\"}",
+       }
+
+       controllerUpdateGnbTestExecuter(t, &context)
+}
+
+func getJsonRequestAsBuffer(requestJson map[string]interface{}) *bytes.Buffer {
+       b := new(bytes.Buffer)
+       _ = json.NewEncoder(b).Encode(requestJson)
+       return b
+}
+
 func TestControllerGetNodebSuccess(t *testing.T) {
        ranName := "test"
        var rnibError error
@@ -225,7 +673,7 @@ func TestControllerGetNodebNotFound(t *testing.T) {
                nodebInfo:            nodebInfo,
                rnibError:            common.NewResourceNotFoundError("#reader.GetNodeb - Not found Error"),
                expectedStatusCode:   http.StatusNotFound,
-               expectedJsonResponse: "{\"errorCode\":404,\"errorMessage\":\"Resource not found\"}",
+               expectedJsonResponse: ResourceNotFoundJson,
        }
 
        controllerGetNodebTestExecuter(t, &context)
@@ -239,7 +687,7 @@ func TestControllerGetNodebInternal(t *testing.T) {
                nodebInfo:            nodebInfo,
                rnibError:            common.NewInternalError(errors.New("#reader.GetNodeb - Internal Error")),
                expectedStatusCode:   http.StatusInternalServerError,
-               expectedJsonResponse: "{\"errorCode\":500,\"errorMessage\":\"RNIB error\"}",
+               expectedJsonResponse: RnibErrorJson,
        }
 
        controllerGetNodebTestExecuter(t, &context)
@@ -282,14 +730,14 @@ func TestControllerGetNodebIdListInternal(t *testing.T) {
                nodebIdList:          nodebIdList,
                rnibError:            common.NewInternalError(errors.New("#reader.GetNodeb - Internal Error")),
                expectedStatusCode:   http.StatusInternalServerError,
-               expectedJsonResponse: "{\"errorCode\":500,\"errorMessage\":\"RNIB error\"}",
+               expectedJsonResponse: RnibErrorJson,
        }
 
        controllerGetNodebIdListTestExecuter(t, &context)
 }
 
 func TestHeaderValidationFailed(t *testing.T) {
-       controller, _, _, _ := setupControllerTest(t)
+       controller, _, _, _, _ := setupControllerTest(t)
 
        writer := httptest.NewRecorder()
 
@@ -306,11 +754,9 @@ func TestHeaderValidationFailed(t *testing.T) {
 }
 
 func TestShutdownStatusNoContent(t *testing.T) {
-       controller, readerMock, _, _ := setupControllerTest(t)
-
-       var rnibError error
-       nbIdentityList := []*entities.NbIdentity{}
-       readerMock.On("GetListNodebIds").Return(nbIdentityList, rnibError)
+       controller, readerMock, _, _, e2tInstancesManagerMock := setupControllerTest(t)
+       e2tInstancesManagerMock.On("GetE2TAddresses").Return([]string{}, nil)
+       readerMock.On("GetListNodebIds").Return([]*entities.NbIdentity{}, nil)
 
        writer := httptest.NewRecorder()
        controller.Shutdown(writer, tests.GetHttpRequest())
@@ -319,7 +765,7 @@ func TestShutdownStatusNoContent(t *testing.T) {
 }
 
 func TestHandleInternalError(t *testing.T) {
-       controller, _, _, _ := setupControllerTest(t)
+       controller, _, _, _, _ := setupControllerTest(t)
 
        writer := httptest.NewRecorder()
        err := e2managererrors.NewInternalError()
@@ -333,7 +779,7 @@ func TestHandleInternalError(t *testing.T) {
 }
 
 func TestHandleCommandAlreadyInProgressError(t *testing.T) {
-       controller, _, _, _ := setupControllerTest(t)
+       controller, _, _, _, _ := setupControllerTest(t)
        writer := httptest.NewRecorder()
        err := e2managererrors.NewCommandAlreadyInProgressError()
 
@@ -345,8 +791,35 @@ func TestHandleCommandAlreadyInProgressError(t *testing.T) {
        assert.Equal(t, errorResponse.Message, err.Message)
 }
 
+func TestHandleRoutingManagerError(t *testing.T) {
+       controller, _, _, _, _ := setupControllerTest(t)
+       writer := httptest.NewRecorder()
+       err := e2managererrors.NewRoutingManagerError()
+
+       controller.handleErrorResponse(err, writer)
+       var errorResponse = parseJsonRequest(t, writer.Body)
+
+       assert.Equal(t, http.StatusServiceUnavailable, writer.Result().StatusCode)
+       assert.Equal(t, errorResponse.Code, err.Code)
+       assert.Equal(t, errorResponse.Message, err.Message)
+}
+
+func TestHandleE2TInstanceAbsenceError(t *testing.T) {
+       controller, _, _, _, _ := setupControllerTest(t)
+
+       writer := httptest.NewRecorder()
+       err := e2managererrors.NewE2TInstanceAbsenceError()
+
+       controller.handleErrorResponse(err, writer)
+       var errorResponse = parseJsonRequest(t, writer.Body)
+
+       assert.Equal(t, http.StatusServiceUnavailable, writer.Result().StatusCode)
+       assert.Equal(t, errorResponse.Code, err.Code)
+       assert.Equal(t, errorResponse.Message, err.Message)
+}
+
 func TestValidateHeaders(t *testing.T) {
-       controller, _, _, _ := setupControllerTest(t)
+       controller, _, _, _, _ := setupControllerTest(t)
 
        header := http.Header{}
        header.Set("Content-Type", "application/json")
@@ -362,7 +835,7 @@ func parseJsonRequest(t *testing.T, r io.Reader) models.ErrorResponse {
        if err != nil {
                t.Errorf("Error cannot deserialize json request")
        }
-       json.Unmarshal(body, &errorResponse)
+       _ = json.Unmarshal(body, &errorResponse)
 
        return errorResponse
 }
@@ -376,12 +849,13 @@ func initLog(t *testing.T) *logger.Logger {
 }
 
 func TestX2ResetHandleSuccessfulRequestedCause(t *testing.T) {
-       controller, readerMock, _, rmrMessengerMock := setupControllerTest(t)
+       controller, readerMock, _, rmrMessengerMock, _ := setupControllerTest(t)
 
        ranName := "test1"
        payload := []byte{0x00, 0x07, 0x00, 0x08, 0x00, 0x00, 0x01, 0x00, 0x05, 0x40, 0x01, 0x40}
-       xaction := []byte(ranName)
-       msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_RESET, len(payload), ranName, &payload, &xaction)
+       var xAction []byte
+       var msgSrc unsafe.Pointer
+       msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_RESET, len(payload), ranName, &payload, &xAction, msgSrc)
        rmrMessengerMock.On("SendMsg", msg, mock.Anything).Return(msg, nil)
 
        writer := httptest.NewRecorder()
@@ -401,14 +875,15 @@ func TestX2ResetHandleSuccessfulRequestedCause(t *testing.T) {
 }
 
 func TestX2ResetHandleSuccessfulRequestedDefault(t *testing.T) {
-       controller, readerMock, _, rmrMessengerMock := setupControllerTest(t)
+       controller, readerMock, _, rmrMessengerMock, _ := setupControllerTest(t)
 
        ranName := "test1"
        // o&m intervention
        payload := []byte{0x00, 0x07, 0x00, 0x08, 0x00, 0x00, 0x01, 0x00, 0x05, 0x40, 0x01, 0x64}
-       xaction := []byte(ranName)
-       msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_RESET, len(payload), ranName, &payload, &xaction)
-       rmrMessengerMock.On("SendMsg", msg).Return(msg, nil)
+       var xAction []byte
+       var msgSrc unsafe.Pointer
+       msg := rmrCgo.NewMBuf(rmrCgo.RIC_X2_RESET, len(payload), ranName, &payload, &xAction, msgSrc)
+       rmrMessengerMock.On("SendMsg", msg, true).Return(msg, nil)
 
        writer := httptest.NewRecorder()
 
@@ -426,7 +901,7 @@ func TestX2ResetHandleSuccessfulRequestedDefault(t *testing.T) {
 }
 
 func TestX2ResetHandleFailureInvalidBody(t *testing.T) {
-       controller, _, _, _ := setupControllerTest(t)
+       controller, _, _, _, _ := setupControllerTest(t)
 
        ranName := "test1"
 
@@ -443,7 +918,7 @@ func TestX2ResetHandleFailureInvalidBody(t *testing.T) {
 }
 
 func TestHandleErrorResponse(t *testing.T) {
-       controller, _, _, _ := setupControllerTest(t)
+       controller, _, _, _, _ := setupControllerTest(t)
 
        writer := httptest.NewRecorder()
        controller.handleErrorResponse(e2managererrors.NewRnibDbError(), writer)
@@ -481,5 +956,5 @@ func TestHandleErrorResponse(t *testing.T) {
 func getRmrSender(rmrMessengerMock *mocks.RmrMessengerMock, log *logger.Logger) *rmrsender.RmrSender {
        rmrMessenger := rmrCgo.RmrMessenger(rmrMessengerMock)
        rmrMessengerMock.On("Init", tests.GetPort(), tests.MaxMsgSize, tests.Flags, log).Return(&rmrMessenger)
-       return rmrsender.NewRmrSender(log, &rmrMessenger)
+       return rmrsender.NewRmrSender(log, rmrMessenger)
 }