// This source code is part of the near-RT RIC (RAN Intelligent Controller)
// platform project (RICP).
-
package controllers
import (
"e2mgr/models"
"e2mgr/providers/httpmsghandlerprovider"
"encoding/json"
+ "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
+ "github.com/golang/protobuf/jsonpb"
+ "github.com/golang/protobuf/proto"
"github.com/gorilla/mux"
"io"
"io/ioutil"
ParamRanName = "ranName"
LimitRequest = 2000
)
+const ApplicationJson = "application/json"
+const ContentType = "Content-Type"
type INodebController interface {
Shutdown(writer http.ResponseWriter, r *http.Request)
X2Reset(writer http.ResponseWriter, r *http.Request)
- X2Setup(writer http.ResponseWriter, r *http.Request)
- EndcSetup(writer http.ResponseWriter, r *http.Request)
GetNodeb(writer http.ResponseWriter, r *http.Request)
+ UpdateGnb(writer http.ResponseWriter, r *http.Request)
GetNodebIdList(writer http.ResponseWriter, r *http.Request)
+ SetGeneralConfiguration(writer http.ResponseWriter, r *http.Request)
+ AddEnb(writer http.ResponseWriter, r *http.Request)
}
type NodebController struct {
func (c *NodebController) GetNodebIdList(writer http.ResponseWriter, r *http.Request) {
c.logger.Infof("[Client -> E2 Manager] #NodebController.GetNodebIdList - request: %v", c.prettifyRequest(r))
- c.handleRequest(writer, &r.Header, httpmsghandlerprovider.GetNodebIdListRequest, nil, false)
+ c.handleRequest(writer, &r.Header, httpmsghandlerprovider.GetNodebIdListRequest, nil, false, http.StatusOK)
}
func (c *NodebController) GetNodeb(writer http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
ranName := vars["ranName"]
request := models.GetNodebRequest{RanName: ranName}
- c.handleRequest(writer, &r.Header, httpmsghandlerprovider.GetNodebRequest, request, false)
+ c.handleRequest(writer, &r.Header, httpmsghandlerprovider.GetNodebRequest, request, false, http.StatusOK)
+}
+
+func (c *NodebController) UpdateGnb(writer http.ResponseWriter, r *http.Request) {
+ c.logger.Infof("[Client -> E2 Manager] #NodebController.UpdateGnb - request: %v", c.prettifyRequest(r))
+ vars := mux.Vars(r)
+ ranName := vars[ParamRanName]
+
+ request := models.UpdateGnbRequest{}
+
+ gnb := entities.Gnb{}
+
+ if !c.extractRequestBodyToProto(r, &gnb, writer) {
+ return
+ }
+
+ request.Gnb = &gnb
+ request.RanName = ranName
+ c.handleRequest(writer, &r.Header, httpmsghandlerprovider.UpdateGnbRequest, request, true, http.StatusOK)
+}
+
+func (c *NodebController) AddEnb(writer http.ResponseWriter, r *http.Request) {
+ c.logger.Infof("[Client -> E2 Manager] #NodebController.AddEnb - request: %v", c.prettifyRequest(r))
+
+ defer r.Body.Close()
+ body, err := ioutil.ReadAll(io.LimitReader(r.Body, LimitRequest))
+
+ if err != nil {
+ c.logger.Errorf("[Client -> E2 Manager] #NodebController.AddEnb - unable to read request body - error: %s", err)
+ c.handleErrorResponse(e2managererrors.NewInvalidJsonError(), writer)
+ return
+ }
+
+ addEnbRequest := models.AddEnbRequest{}
+ err = json.Unmarshal(body, &addEnbRequest)
+
+ if err != nil {
+ c.logger.Errorf("[Client -> E2 Manager] #NodebController.AddEnb - unable to unmarshal json - error: %s", err)
+ c.handleErrorResponse(e2managererrors.NewInvalidJsonError(), writer)
+ return
+ }
+
+ c.handleRequest(writer, &r.Header, httpmsghandlerprovider.AddEnbRequest, &addEnbRequest, true, http.StatusCreated)
+}
+
+func (c *NodebController) SetGeneralConfiguration(writer http.ResponseWriter, r *http.Request) {
+ c.logger.Infof("[Client -> E2 Manager] #NodebController.SetGeneralConfiguration - request: %v", c.prettifyRequest(r))
+
+ request := models.GeneralConfigurationRequest{}
+
+ if !c.extractJsonBodyDisallowUnknownFields(r, &request, writer) {
+ return
+ }
+ c.handleRequest(writer, &r.Header, httpmsghandlerprovider.SetGeneralConfigurationRequest, request, false, http.StatusOK)
}
func (c *NodebController) Shutdown(writer http.ResponseWriter, r *http.Request) {
c.logger.Infof("[Client -> E2 Manager] #NodebController.Shutdown - request: %v", c.prettifyRequest(r))
- c.handleRequest(writer, &r.Header, httpmsghandlerprovider.ShutdownRequest, nil, false)
+ c.handleRequest(writer, &r.Header, httpmsghandlerprovider.ShutdownRequest, nil, false, http.StatusNoContent)
}
func (c *NodebController) X2Reset(writer http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
ranName := vars[ParamRanName]
- if r.ContentLength > 0 && !c.extractJsonBody(r, &request, writer) {
+ if err := c.extractJsonBody(r, &request); err != nil {
+ c.handleErrorResponse(err, writer)
return
}
request.RanName = ranName
- c.handleRequest(writer, &r.Header, httpmsghandlerprovider.ResetRequest, request, false)
+ c.handleRequest(writer, &r.Header, httpmsghandlerprovider.ResetRequest, request, false, http.StatusNoContent)
}
-func (c *NodebController) X2Setup(writer http.ResponseWriter, r *http.Request) {
- c.logger.Infof("[Client -> E2 Manager] #NodebController.X2Setup - request: %v", c.prettifyRequest(r))
+func (c *NodebController) extractRequestBodyToProto(r *http.Request, pb proto.Message, writer http.ResponseWriter) bool {
+ defer r.Body.Close()
- request := models.SetupRequest{}
+ err := jsonpb.Unmarshal(r.Body, pb)
- if !c.extractJsonBody(r, &request, writer) {
- return
+ if err != nil {
+ c.logger.Errorf("[Client -> E2 Manager] #NodebController.extractJsonBody - unable to extract json body - error: %s", err)
+ c.handleErrorResponse(e2managererrors.NewInvalidJsonError(), writer)
+ return false
}
- c.handleRequest(writer, &r.Header, httpmsghandlerprovider.X2SetupRequest, request, true)
+ return true
}
-func (c *NodebController) EndcSetup(writer http.ResponseWriter, r *http.Request) {
- c.logger.Infof("[Client -> E2 Manager] #NodebController.EndcSetup - request: %v", c.prettifyRequest(r))
+func (c *NodebController) extractJsonBodyDisallowUnknownFields(r *http.Request, request models.Request, writer http.ResponseWriter) bool {
+ defer r.Body.Close()
- request := models.SetupRequest{}
+ decoder := json.NewDecoder(r.Body)
+ decoder.DisallowUnknownFields()
- if !c.extractJsonBody(r, &request, writer) {
- return
+ if err := decoder.Decode(&request); err != nil {
+ c.logger.Errorf("[Client -> E2 Manager] #NodebController.extractJsonBody - unable to extract json body - error: %s", err)
+ c.handleErrorResponse(e2managererrors.NewInvalidJsonError(), writer)
+ return false
}
- c.handleRequest(writer, &r.Header, httpmsghandlerprovider.EndcSetupRequest, request, true)
+ return true
}
-func (c *NodebController) extractJsonBody(r *http.Request, request models.Request, writer http.ResponseWriter) bool {
+func (c *NodebController) extractJsonBody(r *http.Request, request models.Request) error {
defer r.Body.Close()
body, err := ioutil.ReadAll(io.LimitReader(r.Body, LimitRequest))
if err != nil {
c.logger.Errorf("[Client -> E2 Manager] #NodebController.extractJsonBody - unable to extract json body - error: %s", err)
- c.handleErrorResponse(e2managererrors.NewInvalidJsonError(), writer)
- return false
+ return e2managererrors.NewInvalidJsonError()
}
err = json.Unmarshal(body, &request)
if err != nil {
c.logger.Errorf("[Client -> E2 Manager] #NodebController.extractJsonBody - unable to extract json body - error: %s", err)
- c.handleErrorResponse(e2managererrors.NewInvalidJsonError(), writer)
- return false
+ return e2managererrors.NewInvalidJsonError()
}
- return true
+ return nil
}
-func (c *NodebController) handleRequest(writer http.ResponseWriter, header *http.Header, requestName httpmsghandlerprovider.IncomingRequest, request models.Request, validateHeader bool) {
+func (c *NodebController) handleRequest(writer http.ResponseWriter, header *http.Header, requestName httpmsghandlerprovider.IncomingRequest, request models.Request, validateRequestHeaders bool, successStatusCode int) {
- if validateHeader {
+ if validateRequestHeaders {
err := c.validateRequestHeader(header)
if err != nil {
return
}
- if response == nil {
- writer.WriteHeader(http.StatusNoContent)
+ if successStatusCode == http.StatusNoContent {
+ writer.WriteHeader(successStatusCode)
c.logger.Infof("[E2 Manager -> Client] #NodebController.handleRequest - status response: %v", http.StatusNoContent)
return
}
}
c.logger.Infof("[E2 Manager -> Client] #NodebController.handleRequest - response: %s", result)
- writer.Header().Set("Content-Type", "application/json")
+ writer.Header().Set(ContentType, ApplicationJson)
+ writer.WriteHeader(successStatusCode)
writer.Write(result)
}
func (c *NodebController) validateRequestHeader(header *http.Header) error {
- if header.Get("Content-Type") != "application/json" {
+ if header.Get(ContentType) != ApplicationJson {
c.logger.Errorf("#NodebController.validateRequestHeader - validation failure, incorrect content type")
return e2managererrors.NewHeaderValidationError()
e2Error, _ := err.(*e2managererrors.E2TInstanceAbsenceError)
errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
httpError = http.StatusServiceUnavailable
+ case *e2managererrors.RoutingManagerError:
+ e2Error, _ := err.(*e2managererrors.RoutingManagerError)
+ errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
+ httpError = http.StatusServiceUnavailable
default:
e2Error := e2managererrors.NewInternalError()
errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
c.logger.Errorf("[E2 Manager -> Client] #NodebController.handleErrorResponse - http status: %d, error response: %+v", httpError, errorResponseDetails)
- writer.Header().Set("Content-Type", "application/json")
+ writer.Header().Set(ContentType, ApplicationJson)
writer.WriteHeader(httpError)
_, err = writer.Write(errorResponse)
-
- if err != nil {
- c.logger.Errorf("#NodebController.handleErrorResponse - Cannot send response. writer:%v", writer)
- }
}
func (c *NodebController) prettifyRequest(request *http.Request) string {