Pass thru error details from remote APIs
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / AnrXappController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 AT&T Intellectual Property and Nokia
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20 package org.oransc.ric.portal.dashboard.controller;
21
22 import java.lang.invoke.MethodHandles;
23
24 import javax.servlet.http.HttpServletResponse;
25
26 import org.oransc.ric.anrxapp.client.api.HealthApi;
27 import org.oransc.ric.anrxapp.client.api.NcrtApi;
28 import org.oransc.ric.anrxapp.client.model.GgNodeBTable;
29 import org.oransc.ric.anrxapp.client.model.NeighborCellRelationMod;
30 import org.oransc.ric.anrxapp.client.model.NeighborCellRelationTable;
31 import org.oransc.ric.portal.dashboard.DashboardApplication;
32 import org.oransc.ric.portal.dashboard.DashboardConstants;
33 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.context.annotation.Configuration;
38 import org.springframework.http.MediaType;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.util.Assert;
41 import org.springframework.web.bind.annotation.PathVariable;
42 import org.springframework.web.bind.annotation.RequestBody;
43 import org.springframework.web.bind.annotation.RequestMapping;
44 import org.springframework.web.bind.annotation.RequestMethod;
45 import org.springframework.web.bind.annotation.RequestParam;
46 import org.springframework.web.bind.annotation.RestController;
47 import org.springframework.web.client.HttpStatusCodeException;
48
49 import io.swagger.annotations.ApiOperation;
50
51 /**
52  * Provides methods to contact the ANR xApp which manages a Neighbor Cell
53  * Relation Table (NCRT).
54  */
55 @Configuration
56 @RestController
57 @RequestMapping(value = DashboardConstants.ENDPOINT_PREFIX + "/xapp/anr", produces = MediaType.APPLICATION_JSON_VALUE)
58 public class AnrXappController {
59
60         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
61
62         // Query parameters
63         private static final String QP_NODEB = "ggnodeb";
64         private static final String QP_SERVING = "servingCellNrcgi";
65         private static final String QP_NEIGHBOR = "neighborCellNrpci";
66         // Path parameters
67         private static final String PP_SERVING = "servingcells";
68         private static final String PP_NEIGHBOR = "neighborcells";
69
70         // Populated by the autowired constructor
71         private final HealthApi healthApi;
72         private final NcrtApi ncrtApi;
73
74         @Autowired
75         public AnrXappController(final HealthApi anrHealthApi, final NcrtApi anrNcrtApi) {
76                 Assert.notNull(anrHealthApi, "API must not be null");
77                 Assert.notNull(anrNcrtApi, "API must not be null");
78                 this.healthApi = anrHealthApi;
79                 this.ncrtApi = anrNcrtApi;
80                 if (logger.isDebugEnabled())
81                         logger.debug("ctor: configured with client types {} and {}", anrHealthApi.getClass().getName(),
82                                         anrNcrtApi.getClass().getName());
83         }
84
85         @ApiOperation(value = "Gets the ANR client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
86         @RequestMapping(value = DashboardConstants.VERSION_PATH, method = RequestMethod.GET)
87         public SuccessTransport getAnrXappClientVersion() {
88                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthApi.class));
89         }
90
91         @ApiOperation(value = "Performs a liveness probe on the ANR xApp, result expressed as the response code.")
92         @RequestMapping(value = "/health/alive", method = RequestMethod.GET)
93         public Object getHealthAlive(HttpServletResponse response) {
94                 logger.debug("getHealthAlive");
95                 try {
96                         healthApi.getHealthAlive();
97                         response.setStatus(healthApi.getApiClient().getStatusCode().value());
98                         return null;
99                 } catch (HttpStatusCodeException ex) {
100                         logger.warn("getHealthAlive failed: {}", ex.toString());
101                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
102                 }
103         }
104
105         @ApiOperation(value = "Performs a readiness probe on the ANR xApp, result expressed as the response code.")
106         @RequestMapping(value = "/health/ready", method = RequestMethod.GET)
107         public Object getHealthReady(HttpServletResponse response) {
108                 logger.debug("getHealthReady");
109                 try {
110                         healthApi.getHealthReady();
111                         response.setStatus(healthApi.getApiClient().getStatusCode().value());
112                         return null;
113                 } catch (HttpStatusCodeException ex) {
114                         logger.warn("getHealthAlive failed: {}", ex.toString());
115                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
116                 }
117         }
118
119         @ApiOperation(value = "Returns list of gNodeB IDs based on NCRT in ANR", response = GgNodeBTable.class)
120         @RequestMapping(value = "/gnodebs", method = RequestMethod.GET)
121         public Object getGnodebs() {
122                 logger.debug("getGnodebs");
123                 try {
124                         return ncrtApi.getgNodeB();
125                 } catch (HttpStatusCodeException ex) {
126                         logger.warn("getGnodebs failed: {}", ex.toString());
127                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
128                 }
129         }
130
131         @ApiOperation(value = "Returns neighbor cell relation table for all gNodeBs or based on query parameters", response = NeighborCellRelationTable.class)
132         @RequestMapping(value = "/ncrt", method = RequestMethod.GET)
133         public Object getNcrt( //
134                         @RequestParam(name = QP_NODEB, required = false) String ggnbId, //
135                         @RequestParam(name = QP_SERVING, required = false) String servingCellNrcgi, //
136                         @RequestParam(name = QP_NEIGHBOR, required = false) String neighborCellNrpci) {
137                 logger.debug("getNcrt: ggnbid {}, servingCellNrpci {}, neighborCellNrcgi {}", ggnbId, servingCellNrcgi,
138                                 neighborCellNrpci);
139                 try {
140                         return ncrtApi.getNcrt(ggnbId, servingCellNrcgi, neighborCellNrpci);
141                 } catch (HttpStatusCodeException ex) {
142                         logger.warn("getNcrt failed: {}", ex.toString());
143                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
144                 }
145         }
146
147         // /ncrt/servingcells/{servCellNrcgi}/neighborcells/{neighCellNrpci} :
148         @ApiOperation(value = "Modify neighbor cell relation based on Serving Cell NRCGI and Neighbor Cell NRPCI")
149         @RequestMapping(value = "/ncrt/" + PP_SERVING + "/{" + PP_SERVING + "}/" + PP_NEIGHBOR + "/{" + PP_NEIGHBOR
150                         + "}", method = RequestMethod.PUT)
151         public Object modifyNcrt(@PathVariable(PP_SERVING) String servingCellNrcgi, //
152                         @PathVariable(PP_NEIGHBOR) String neighborCellNrpci, //
153                         @RequestBody NeighborCellRelationMod ncrMod, HttpServletResponse response) {
154                 logger.debug("modifyNcrt: servingCellNrcgi {}, neighborCellNrpci {}, ncrMod {}", servingCellNrcgi,
155                                 neighborCellNrpci, ncrMod);
156                 try {
157                         ncrtApi.modifyNcrt(servingCellNrcgi, neighborCellNrpci, ncrMod);
158                         response.setStatus(healthApi.getApiClient().getStatusCode().value());
159                         return null;
160                 } catch (HttpStatusCodeException ex) {
161                         logger.warn("modifyNcrt failed: {}", ex.toString());
162                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
163                 }
164         }
165
166         @ApiOperation(value = "Delete neighbor cell relation based on Serving Cell NRCGI and Neighbor Cell NRPCI")
167         @RequestMapping(value = "/ncrt/" + PP_SERVING + "/{" + PP_SERVING + "}/" + PP_NEIGHBOR + "/{" + PP_NEIGHBOR
168                         + "}", method = RequestMethod.DELETE)
169         public Object deleteNcrt(@PathVariable(PP_SERVING) String servingCellNrcgi, //
170                         @PathVariable(PP_NEIGHBOR) String neighborCellNrpci, //
171                         HttpServletResponse response) {
172                 logger.debug("deleteNcrt: servingCellNrcgi {}, neighborCellNrpci {}", servingCellNrcgi, neighborCellNrpci);
173                 try {
174                         ncrtApi.deleteNcrt(servingCellNrcgi, neighborCellNrpci);
175                         response.setStatus(healthApi.getApiClient().getStatusCode().value());
176                         return null;
177                 } catch (HttpStatusCodeException ex) {
178                         logger.warn("modifyNcrt failed: {}", ex.toString());
179                         return ResponseEntity.status(HttpServletResponse.SC_BAD_GATEWAY).body(ex.getResponseBodyAsString());
180                 }
181         }
182
183 }