2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
23 "e2mgr/e2managererrors"
26 "e2mgr/providers/httpmsghandlerprovider"
28 "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
29 "github.com/golang/protobuf/jsonpb"
30 "github.com/golang/protobuf/proto"
31 "github.com/gorilla/mux"
40 ParamRanName = "ranName"
43 const ApplicationJson = "application/json"
44 const ContentType = "Content-Type"
46 type INodebController interface {
47 Shutdown(writer http.ResponseWriter, r *http.Request)
48 X2Reset(writer http.ResponseWriter, r *http.Request)
49 GetNodeb(writer http.ResponseWriter, r *http.Request)
50 UpdateGnb(writer http.ResponseWriter, r *http.Request)
51 UpdateEnb(writer http.ResponseWriter, r *http.Request)
52 GetNodebIdList(writer http.ResponseWriter, r *http.Request)
53 SetGeneralConfiguration(writer http.ResponseWriter, r *http.Request)
54 AddEnb(writer http.ResponseWriter, r *http.Request)
55 DeleteEnb(writer http.ResponseWriter, r *http.Request)
58 type NodebController struct {
60 handlerProvider *httpmsghandlerprovider.IncomingRequestHandlerProvider
63 func NewNodebController(logger *logger.Logger, handlerProvider *httpmsghandlerprovider.IncomingRequestHandlerProvider) *NodebController {
64 return &NodebController{
66 handlerProvider: handlerProvider,
70 func (c *NodebController) GetNodebIdList(writer http.ResponseWriter, r *http.Request) {
71 c.logger.Infof("[Client -> E2 Manager] #NodebController.GetNodebIdList - request: %v", c.prettifyRequest(r))
73 c.handleRequest(writer, &r.Header, httpmsghandlerprovider.GetNodebIdListRequest, nil, false, http.StatusOK)
76 func (c *NodebController) GetNodeb(writer http.ResponseWriter, r *http.Request) {
77 c.logger.Infof("[Client -> E2 Manager] #NodebController.GetNodeb - request: %v", c.prettifyRequest(r))
79 ranName := vars["ranName"]
80 request := models.GetNodebRequest{RanName: ranName}
81 c.handleRequest(writer, &r.Header, httpmsghandlerprovider.GetNodebRequest, request, false, http.StatusOK)
84 func (c *NodebController) UpdateGnb(writer http.ResponseWriter, r *http.Request) {
85 c.logger.Infof("[Client -> E2 Manager] #NodebController.UpdateGnb - request: %v", c.prettifyRequest(r))
87 ranName := vars[ParamRanName]
89 request := models.UpdateGnbRequest{}
93 if !c.extractRequestBodyToProto(r, &gnb, writer) {
98 request.RanName = ranName
99 c.handleRequest(writer, &r.Header, httpmsghandlerprovider.UpdateGnbRequest, &request, true, http.StatusOK)
102 func (c *NodebController) UpdateEnb(writer http.ResponseWriter, r *http.Request) {
103 c.logger.Infof("[Client -> E2 Manager] #NodebController.UpdateEnb - request: %v", c.prettifyRequest(r))
106 body, err := ioutil.ReadAll(r.Body)
109 c.logger.Errorf("[Client -> E2 Manager] #NodebController.UpdateEnb - unable to read request body - error: %s", err)
110 c.handleErrorResponse(e2managererrors.NewInvalidJsonError(), writer)
114 updateEnbRequest := models.UpdateEnbRequest{}
115 err = json.Unmarshal(body, &updateEnbRequest)
118 c.logger.Errorf("[Client -> E2 Manager] #NodebController.UpdateEnb - unable to unmarshal json - error: %s", err)
119 c.handleErrorResponse(e2managererrors.NewInvalidJsonError(), writer)
124 ranName := vars[ParamRanName]
126 updateEnbRequest.RanName = ranName
128 c.handleRequest(writer, &r.Header, httpmsghandlerprovider.UpdateEnbRequest, &updateEnbRequest, true, http.StatusOK)
131 func (c *NodebController) AddEnb(writer http.ResponseWriter, r *http.Request) {
132 c.logger.Infof("[Client -> E2 Manager] #NodebController.AddEnb - request: %v", c.prettifyRequest(r))
135 body, err := ioutil.ReadAll(r.Body)
138 c.logger.Errorf("[Client -> E2 Manager] #NodebController.AddEnb - unable to read request body - error: %s", err)
139 c.handleErrorResponse(e2managererrors.NewInvalidJsonError(), writer)
143 addEnbRequest := models.AddEnbRequest{}
144 err = json.Unmarshal(body, &addEnbRequest)
147 c.logger.Errorf("[Client -> E2 Manager] #NodebController.AddEnb - unable to unmarshal json - error: %s", err)
148 c.handleErrorResponse(e2managererrors.NewInvalidJsonError(), writer)
152 c.handleRequest(writer, &r.Header, httpmsghandlerprovider.AddEnbRequest, &addEnbRequest, true, http.StatusCreated)
155 func (c *NodebController) DeleteEnb(writer http.ResponseWriter, r *http.Request) {
156 c.logger.Infof("[Client -> E2 Manager] #NodebController.DeleteEnb - request: %v", c.prettifyRequest(r))
158 ranName := vars["ranName"]
159 request := &models.DeleteEnbRequest{RanName: ranName}
160 c.handleRequest(writer, &r.Header, httpmsghandlerprovider.DeleteEnbRequest, request, true, http.StatusNoContent)
163 func (c *NodebController) SetGeneralConfiguration(writer http.ResponseWriter, r *http.Request) {
164 c.logger.Infof("[Client -> E2 Manager] #NodebController.SetGeneralConfiguration - request: %v", c.prettifyRequest(r))
166 request := models.GeneralConfigurationRequest{}
168 if !c.extractJsonBodyDisallowUnknownFields(r, &request, writer) {
171 c.handleRequest(writer, &r.Header, httpmsghandlerprovider.SetGeneralConfigurationRequest, request, false, http.StatusOK)
174 func (c *NodebController) Shutdown(writer http.ResponseWriter, r *http.Request) {
175 c.logger.Infof("[Client -> E2 Manager] #NodebController.Shutdown - request: %v", c.prettifyRequest(r))
176 c.handleRequest(writer, &r.Header, httpmsghandlerprovider.ShutdownRequest, nil, false, http.StatusNoContent)
179 func (c *NodebController) X2Reset(writer http.ResponseWriter, r *http.Request) {
180 c.logger.Infof("[Client -> E2 Manager] #NodebController.X2Reset - request: %v", c.prettifyRequest(r))
181 request := models.ResetRequest{}
183 ranName := vars[ParamRanName]
185 if err := c.extractJsonBody(r, &request); err != nil {
186 c.handleErrorResponse(err, writer)
189 request.RanName = ranName
190 c.handleRequest(writer, &r.Header, httpmsghandlerprovider.ResetRequest, request, false, http.StatusNoContent)
193 func (c *NodebController) extractRequestBodyToProto(r *http.Request, pb proto.Message, writer http.ResponseWriter) bool {
196 err := jsonpb.Unmarshal(r.Body, pb)
199 c.logger.Errorf("[Client -> E2 Manager] #NodebController.extractJsonBody - unable to extract json body - error: %s", err)
200 c.handleErrorResponse(e2managererrors.NewInvalidJsonError(), writer)
207 func (c *NodebController) extractJsonBodyDisallowUnknownFields(r *http.Request, request models.Request, writer http.ResponseWriter) bool {
210 decoder := json.NewDecoder(r.Body)
211 decoder.DisallowUnknownFields()
213 if err := decoder.Decode(&request); err != nil {
214 c.logger.Errorf("[Client -> E2 Manager] #NodebController.extractJsonBody - unable to extract json body - error: %s", err)
215 c.handleErrorResponse(e2managererrors.NewInvalidJsonError(), writer)
222 func (c *NodebController) extractJsonBody(r *http.Request, request models.Request) error {
224 body, err := ioutil.ReadAll(io.LimitReader(r.Body, LimitRequest))
227 c.logger.Errorf("[Client -> E2 Manager] #NodebController.extractJsonBody - unable to extract json body - error: %s", err)
228 return e2managererrors.NewInvalidJsonError()
231 err = json.Unmarshal(body, &request)
233 c.logger.Errorf("[Client -> E2 Manager] #NodebController.extractJsonBody - unable to extract json body - error: %s", err)
234 return e2managererrors.NewInvalidJsonError()
240 func (c *NodebController) handleRequest(writer http.ResponseWriter, header *http.Header, requestName httpmsghandlerprovider.IncomingRequest, request models.Request, validateRequestHeaders bool, successStatusCode int) {
242 if validateRequestHeaders {
244 err := c.validateRequestHeader(header)
246 c.handleErrorResponse(err, writer)
251 handler, err := c.handlerProvider.GetHandler(requestName)
254 c.handleErrorResponse(err, writer)
258 response, err := handler.Handle(request)
261 c.handleErrorResponse(err, writer)
265 if successStatusCode == http.StatusNoContent {
266 writer.WriteHeader(successStatusCode)
267 c.logger.Infof("[E2 Manager -> Client] #NodebController.handleRequest - status response: %v", http.StatusNoContent)
271 result, err := response.Marshal()
274 c.handleErrorResponse(err, writer)
278 c.logger.Infof("[E2 Manager -> Client] #NodebController.handleRequest - response: %s", result)
279 writer.Header().Set(ContentType, ApplicationJson)
280 writer.WriteHeader(successStatusCode)
284 func (c *NodebController) validateRequestHeader(header *http.Header) error {
286 if header.Get(ContentType) != ApplicationJson {
287 c.logger.Errorf("#NodebController.validateRequestHeader - validation failure, incorrect content type")
289 return e2managererrors.NewHeaderValidationError()
294 func (c *NodebController) handleErrorResponse(err error, writer http.ResponseWriter) {
296 var errorResponseDetails models.ErrorResponse
301 case *e2managererrors.RnibDbError:
302 e2Error, _ := err.(*e2managererrors.RnibDbError)
303 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
304 httpError = http.StatusInternalServerError
305 case *e2managererrors.CommandAlreadyInProgressError:
306 e2Error, _ := err.(*e2managererrors.CommandAlreadyInProgressError)
307 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
308 httpError = http.StatusMethodNotAllowed
309 case *e2managererrors.HeaderValidationError:
310 e2Error, _ := err.(*e2managererrors.HeaderValidationError)
311 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
312 httpError = http.StatusUnsupportedMediaType
313 case *e2managererrors.WrongStateError:
314 e2Error, _ := err.(*e2managererrors.WrongStateError)
315 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
316 httpError = http.StatusBadRequest
317 case *e2managererrors.RequestValidationError:
318 e2Error, _ := err.(*e2managererrors.RequestValidationError)
319 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
320 httpError = http.StatusBadRequest
321 case *e2managererrors.InvalidJsonError:
322 e2Error, _ := err.(*e2managererrors.InvalidJsonError)
323 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
324 httpError = http.StatusBadRequest
325 case *e2managererrors.RmrError:
326 e2Error, _ := err.(*e2managererrors.RmrError)
327 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
328 httpError = http.StatusInternalServerError
329 case *e2managererrors.ResourceNotFoundError:
330 e2Error, _ := err.(*e2managererrors.ResourceNotFoundError)
331 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
332 httpError = http.StatusNotFound
333 case *e2managererrors.E2TInstanceAbsenceError:
334 e2Error, _ := err.(*e2managererrors.E2TInstanceAbsenceError)
335 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
336 httpError = http.StatusServiceUnavailable
337 case *e2managererrors.RoutingManagerError:
338 e2Error, _ := err.(*e2managererrors.RoutingManagerError)
339 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
340 httpError = http.StatusServiceUnavailable
341 case *e2managererrors.NodebExistsError:
342 e2Error, _ := err.(*e2managererrors.NodebExistsError)
343 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
344 httpError = http.StatusBadRequest
346 e2Error := e2managererrors.NewInternalError()
347 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
348 httpError = http.StatusInternalServerError
351 errorResponse, _ := json.Marshal(errorResponseDetails)
353 c.logger.Errorf("[E2 Manager -> Client] #NodebController.handleErrorResponse - http status: %d, error response: %+v", httpError, errorResponseDetails)
355 writer.Header().Set(ContentType, ApplicationJson)
356 writer.WriteHeader(httpError)
357 _, err = writer.Write(errorResponse)
360 func (c *NodebController) prettifyRequest(request *http.Request) string {
361 dump, _ := httputil.DumpRequest(request, true)
362 requestPrettyPrint := strings.Replace(string(dump), "\r\n", " ", -1)
363 return strings.Replace(requestPrettyPrint, "\n", "", -1)