ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2019 AT&T Intellectual Property |
| 3 | // Copyright 2019 Nokia |
| 4 | // |
| 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 |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 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. |
| 16 | // |
| 17 | |
ss412g | 697bea2 | 2019-12-24 22:38:19 +0200 | [diff] [blame] | 18 | // This source code is part of the near-RT RIC (RAN Intelligent Controller) |
| 19 | // platform project (RICP). |
| 20 | |
| 21 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 22 | package controllers |
| 23 | |
| 24 | import ( |
| 25 | "e2mgr/e2managererrors" |
| 26 | "e2mgr/logger" |
| 27 | "e2mgr/models" |
| 28 | "e2mgr/providers/httpmsghandlerprovider" |
| 29 | "encoding/json" |
| 30 | "net/http" |
| 31 | "net/http/httputil" |
| 32 | "strings" |
| 33 | ) |
| 34 | |
| 35 | type IE2TController interface { |
| 36 | GetE2TInstances(writer http.ResponseWriter, r *http.Request) |
| 37 | } |
| 38 | |
| 39 | type E2TController struct { |
| 40 | logger *logger.Logger |
| 41 | handlerProvider *httpmsghandlerprovider.IncomingRequestHandlerProvider |
| 42 | } |
| 43 | |
| 44 | func NewE2TController(logger *logger.Logger, handlerProvider *httpmsghandlerprovider.IncomingRequestHandlerProvider) *E2TController { |
| 45 | return &E2TController{ |
| 46 | logger: logger, |
| 47 | handlerProvider: handlerProvider, |
| 48 | } |
| 49 | } |
| 50 | |
| 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) |
| 54 | } |
| 55 | |
| 56 | func (c *E2TController) handleRequest(writer http.ResponseWriter, header *http.Header, requestName httpmsghandlerprovider.IncomingRequest, request models.Request, validateHeader bool) { |
| 57 | |
| 58 | handler, err := c.handlerProvider.GetHandler(requestName) |
| 59 | |
| 60 | if err != nil { |
| 61 | c.handleErrorResponse(err, writer) |
| 62 | return |
| 63 | } |
| 64 | |
| 65 | response, err := handler.Handle(request) |
| 66 | |
| 67 | if err != nil { |
| 68 | c.handleErrorResponse(err, writer) |
| 69 | return |
| 70 | } |
| 71 | |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 72 | result, err := response.Marshal() |
| 73 | |
| 74 | if err != nil { |
| 75 | c.handleErrorResponse(err, writer) |
| 76 | return |
| 77 | } |
| 78 | |
| 79 | c.logger.Infof("[E2 Manager -> Client] #E2TController.handleRequest - response: %s", result) |
| 80 | writer.Header().Set("Content-Type", "application/json") |
| 81 | writer.Write(result) |
| 82 | } |
| 83 | |
| 84 | func (c *E2TController) handleErrorResponse(err error, writer http.ResponseWriter) { |
| 85 | |
| 86 | var errorResponseDetails models.ErrorResponse |
| 87 | var httpError int |
| 88 | |
| 89 | if err != nil { |
| 90 | switch err.(type) { |
| 91 | case *e2managererrors.RnibDbError: |
| 92 | e2Error, _ := err.(*e2managererrors.RnibDbError) |
| 93 | errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message} |
| 94 | httpError = http.StatusInternalServerError |
| 95 | default: |
| 96 | e2Error := e2managererrors.NewInternalError() |
| 97 | errorResponseDetails = models.ErrorResponse{Code: e2Error.Code, Message: e2Error.Message} |
| 98 | httpError = http.StatusInternalServerError |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | errorResponse, _ := json.Marshal(errorResponseDetails) |
| 103 | |
| 104 | c.logger.Errorf("[E2 Manager -> Client] #E2TController.handleErrorResponse - http status: %d, error response: %+v", httpError, errorResponseDetails) |
| 105 | |
| 106 | writer.Header().Set("Content-Type", "application/json") |
| 107 | writer.WriteHeader(httpError) |
| 108 | _, err = writer.Write(errorResponse) |
ss412g | efcb452 | 2019-12-02 16:59:19 +0200 | [diff] [blame] | 109 | } |
| 110 | |
| 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) |
| 115 | } |