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).
24 "e2mgr/e2managererrors"
27 "e2mgr/providers/httpmsghandlerprovider"
29 "github.com/gorilla/mux"
38 ParamRanName = "ranName"
42 type INodebController interface {
43 Shutdown(writer http.ResponseWriter, r *http.Request)
44 X2Reset(writer http.ResponseWriter, r *http.Request)
45 X2Setup(writer http.ResponseWriter, r *http.Request)
46 EndcSetup(writer http.ResponseWriter, r *http.Request)
47 GetNodeb(writer http.ResponseWriter, r *http.Request)
48 GetNodebIdList(writer http.ResponseWriter, r *http.Request)
51 type NodebController struct {
53 handlerProvider *httpmsghandlerprovider.IncomingRequestHandlerProvider
56 func NewNodebController(logger *logger.Logger, handlerProvider *httpmsghandlerprovider.IncomingRequestHandlerProvider) *NodebController {
57 return &NodebController{
59 handlerProvider: handlerProvider,
63 func (c *NodebController) GetNodebIdList(writer http.ResponseWriter, r *http.Request) {
64 c.logger.Infof("[Client -> E2 Manager] #NodebController.GetNodebIdList - request: %v", c.prettifyRequest(r))
66 c.handleRequest(writer, &r.Header, httpmsghandlerprovider.GetNodebIdListRequest, nil, false)
69 func (c *NodebController) GetNodeb(writer http.ResponseWriter, r *http.Request) {
70 c.logger.Infof("[Client -> E2 Manager] #NodebController.GetNodeb - request: %v", c.prettifyRequest(r))
72 ranName := vars["ranName"]
73 request := models.GetNodebRequest{RanName: ranName}
74 c.handleRequest(writer, &r.Header, httpmsghandlerprovider.GetNodebRequest, request, false)
77 func (c *NodebController) Shutdown(writer http.ResponseWriter, r *http.Request) {
78 c.logger.Infof("[Client -> E2 Manager] #NodebController.Shutdown - request: %v", c.prettifyRequest(r))
79 c.handleRequest(writer, &r.Header, httpmsghandlerprovider.ShutdownRequest, nil, false)
82 func (c *NodebController) X2Reset(writer http.ResponseWriter, r *http.Request) {
83 c.logger.Infof("[Client -> E2 Manager] #NodebController.X2Reset - request: %v", c.prettifyRequest(r))
84 request := models.ResetRequest{}
86 ranName := vars[ParamRanName]
88 if r.ContentLength > 0 && !c.extractJsonBody(r, &request, writer) {
91 request.RanName = ranName
92 c.handleRequest(writer, &r.Header, httpmsghandlerprovider.ResetRequest, request, false)
95 func (c *NodebController) X2Setup(writer http.ResponseWriter, r *http.Request) {
96 c.logger.Infof("[Client -> E2 Manager] #NodebController.X2Setup - request: %v", c.prettifyRequest(r))
98 request := models.SetupRequest{}
100 if !c.extractJsonBody(r, &request, writer) {
104 c.handleRequest(writer, &r.Header, httpmsghandlerprovider.X2SetupRequest, request, true)
107 func (c *NodebController) EndcSetup(writer http.ResponseWriter, r *http.Request) {
108 c.logger.Infof("[Client -> E2 Manager] #NodebController.EndcSetup - request: %v", c.prettifyRequest(r))
110 request := models.SetupRequest{}
112 if !c.extractJsonBody(r, &request, writer) {
116 c.handleRequest(writer, &r.Header, httpmsghandlerprovider.EndcSetupRequest, request, true)
119 func (c *NodebController) extractJsonBody(r *http.Request, request models.Request, writer http.ResponseWriter) bool {
121 body, err := ioutil.ReadAll(io.LimitReader(r.Body, LimitRequest))
124 c.logger.Errorf("[Client -> E2 Manager] #NodebController.extractJsonBody - unable to extract json body - error: %s", err)
125 c.handleErrorResponse(e2managererrors.NewInvalidJsonError(), writer)
129 err = json.Unmarshal(body, &request)
131 c.logger.Errorf("[Client -> E2 Manager] #NodebController.extractJsonBody - unable to extract json body - error: %s", err)
132 c.handleErrorResponse(e2managererrors.NewInvalidJsonError(), writer)
139 func (c *NodebController) handleRequest(writer http.ResponseWriter, header *http.Header, requestName httpmsghandlerprovider.IncomingRequest, request models.Request, validateHeader bool) {
143 err := c.validateRequestHeader(header)
145 c.handleErrorResponse(err, writer)
150 handler, err := c.handlerProvider.GetHandler(requestName)
153 c.handleErrorResponse(err, writer)
157 response, err := handler.Handle(request)
160 c.handleErrorResponse(err, writer)
165 writer.WriteHeader(http.StatusNoContent)
166 c.logger.Infof("[E2 Manager -> Client] #NodebController.handleRequest - status response: %v", http.StatusNoContent)
170 result, err := response.Marshal()
173 c.handleErrorResponse(err, writer)
177 c.logger.Infof("[E2 Manager -> Client] #NodebController.handleRequest - response: %s", result)
178 writer.Header().Set("Content-Type", "application/json")
182 func (c *NodebController) validateRequestHeader(header *http.Header) error {
184 if header.Get("Content-Type") != "application/json" {
185 c.logger.Errorf("#NodebController.validateRequestHeader - validation failure, incorrect content type")
187 return e2managererrors.NewHeaderValidationError()
192 func (c *NodebController) handleErrorResponse(err error, writer http.ResponseWriter) {
194 var errorResponseDetails models.ErrorResponse
199 case *e2managererrors.RnibDbError:
200 e2Error, _ := err.(*e2managererrors.RnibDbError)
201 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
202 httpError = http.StatusInternalServerError
203 case *e2managererrors.CommandAlreadyInProgressError:
204 e2Error, _ := err.(*e2managererrors.CommandAlreadyInProgressError)
205 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
206 httpError = http.StatusMethodNotAllowed
207 case *e2managererrors.HeaderValidationError:
208 e2Error, _ := err.(*e2managererrors.HeaderValidationError)
209 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
210 httpError = http.StatusUnsupportedMediaType
211 case *e2managererrors.WrongStateError:
212 e2Error, _ := err.(*e2managererrors.WrongStateError)
213 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
214 httpError = http.StatusBadRequest
215 case *e2managererrors.RequestValidationError:
216 e2Error, _ := err.(*e2managererrors.RequestValidationError)
217 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
218 httpError = http.StatusBadRequest
219 case *e2managererrors.InvalidJsonError:
220 e2Error, _ := err.(*e2managererrors.InvalidJsonError)
221 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
222 httpError = http.StatusBadRequest
223 case *e2managererrors.RmrError:
224 e2Error, _ := err.(*e2managererrors.RmrError)
225 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
226 httpError = http.StatusInternalServerError
227 case *e2managererrors.ResourceNotFoundError:
228 e2Error, _ := err.(*e2managererrors.ResourceNotFoundError)
229 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
230 httpError = http.StatusNotFound
231 case *e2managererrors.E2TInstanceAbsenceError:
232 e2Error, _ := err.(*e2managererrors.E2TInstanceAbsenceError)
233 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
234 httpError = http.StatusServiceUnavailable
235 case *e2managererrors.RoutingManagerError:
236 e2Error, _ := err.(*e2managererrors.RoutingManagerError)
237 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
238 httpError = http.StatusServiceUnavailable
240 e2Error := e2managererrors.NewInternalError()
241 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
242 httpError = http.StatusInternalServerError
245 errorResponse, _ := json.Marshal(errorResponseDetails)
247 c.logger.Errorf("[E2 Manager -> Client] #NodebController.handleErrorResponse - http status: %d, error response: %+v", httpError, errorResponseDetails)
249 writer.Header().Set("Content-Type", "application/json")
250 writer.WriteHeader(httpError)
251 _, err = writer.Write(errorResponse)
254 func (c *NodebController) prettifyRequest(request *http.Request) string {
255 dump, _ := httputil.DumpRequest(request, true)
256 requestPrettyPrint := strings.Replace(string(dump), "\r\n", " ", -1)
257 return strings.Replace(requestPrettyPrint, "\n", "", -1)