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.
18 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 // platform project (RICP).
25 "e2mgr/e2managererrors"
28 "e2mgr/providers/httpmsghandlerprovider"
35 type IE2TController interface {
36 GetE2TInstances(writer http.ResponseWriter, r *http.Request)
39 type E2TController struct {
41 handlerProvider *httpmsghandlerprovider.IncomingRequestHandlerProvider
44 func NewE2TController(logger *logger.Logger, handlerProvider *httpmsghandlerprovider.IncomingRequestHandlerProvider) *E2TController {
45 return &E2TController{
47 handlerProvider: handlerProvider,
51 func (c *E2TController) GetE2TInstances(writer http.ResponseWriter, r *http.Request) {
52 c.logger.Infof("[Client -> E2 Manager] #E2TController.GetE2TInstances - request: %v", c.prettifyRequest(r))
53 c.handleRequest(writer, &r.Header, httpmsghandlerprovider.GetE2TInstancesRequest, nil, false)
56 func (c *E2TController) handleRequest(writer http.ResponseWriter, header *http.Header, requestName httpmsghandlerprovider.IncomingRequest, request models.Request, validateHeader bool) {
58 handler, err := c.handlerProvider.GetHandler(requestName)
61 c.handleErrorResponse(err, writer)
65 response, err := handler.Handle(request)
68 c.handleErrorResponse(err, writer)
72 result, err := response.Marshal()
75 c.handleErrorResponse(err, writer)
79 c.logger.Infof("[E2 Manager -> Client] #E2TController.handleRequest - response: %s", result)
80 writer.Header().Set("Content-Type", "application/json")
84 func (c *E2TController) handleErrorResponse(err error, writer http.ResponseWriter) {
86 var errorResponseDetails models.ErrorResponse
91 case *e2managererrors.RnibDbError:
92 e2Error, _ := err.(*e2managererrors.RnibDbError)
93 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
94 httpError = http.StatusInternalServerError
96 e2Error := e2managererrors.NewInternalError()
97 errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message}
98 httpError = http.StatusInternalServerError
102 errorResponse, _ := json.Marshal(errorResponseDetails)
104 c.logger.Errorf("[E2 Manager -> Client] #E2TController.handleErrorResponse - http status: %d, error response: %+v", httpError, errorResponseDetails)
106 writer.Header().Set("Content-Type", "application/json")
107 writer.WriteHeader(httpError)
108 _, err = writer.Write(errorResponse)
111 func (c *E2TController) prettifyRequest(request *http.Request) string {
112 dump, _ := httputil.DumpRequest(request, true)
113 requestPrettyPrint := strings.Replace(string(dump), "\r\n", " ", -1)
114 return strings.Replace(requestPrettyPrint, "\n", "", -1)