Revise controller error handling
[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.security.access.annotation.Secured;
40 import org.springframework.util.Assert;
41 import org.springframework.web.bind.annotation.DeleteMapping;
42 import org.springframework.web.bind.annotation.GetMapping;
43 import org.springframework.web.bind.annotation.PathVariable;
44 import org.springframework.web.bind.annotation.PutMapping;
45 import org.springframework.web.bind.annotation.RequestBody;
46 import org.springframework.web.bind.annotation.RequestMapping;
47 import org.springframework.web.bind.annotation.RequestParam;
48 import org.springframework.web.bind.annotation.RestController;
49
50 import io.swagger.annotations.ApiOperation;
51
52 /**
53  * Proxies calls from the front end to the ANR xApp, which manages a Neighbor
54  * Cell Relation Table (NCRT).
55  * 
56  * If a method throws RestClientResponseException, it is handled by
57  * {@link CustomResponseEntityExceptionHandler#handleProxyMethodException(Exception, org.springframework.web.context.request.WebRequest)}
58  * which returns status 502. All other exceptions are handled by Spring which
59  * returns status 500.
60  */
61 @Configuration
62 @RestController
63 @RequestMapping(value = AnrXappController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
64 public class AnrXappController {
65
66         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
67
68         // Publish paths in constants so tests are easy to write
69         public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/xapp/anr";
70         // Endpoints
71         public static final String HEALTH_ALIVE_METHOD = "/health/alive";
72         public static final String HEALTH_READY_METHOD = "/health/ready";
73         public static final String GNODEBS_METHOD = "/gnodebs";
74         public static final String NCRT_METHOD = "/ncrt";
75         public static final String VERSION_METHOD = DashboardConstants.VERSION_METHOD;
76
77         // Path parameters
78         public static final String PP_SERVING = "servingcells";
79         public static final String PP_NEIGHBOR = "neighborcells";
80
81         // Populated by the autowired constructor
82         private final HealthApi healthApi;
83         private final NcrtApi ncrtApi;
84
85         @Autowired
86         public AnrXappController(final HealthApi anrHealthApi, final NcrtApi anrNcrtApi) {
87                 Assert.notNull(anrHealthApi, "API must not be null");
88                 Assert.notNull(anrNcrtApi, "API must not be null");
89                 this.healthApi = anrHealthApi;
90                 this.ncrtApi = anrNcrtApi;
91                 if (logger.isDebugEnabled())
92                         logger.debug("ctor: configured with client types {} and {}", anrHealthApi.getClass().getName(),
93                                         anrNcrtApi.getClass().getName());
94         }
95
96         @ApiOperation(value = "Gets the ANR client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
97         @GetMapping(VERSION_METHOD)
98         // No role required
99         public SuccessTransport getClientVersion() {
100                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthApi.class));
101         }
102
103         @ApiOperation(value = "Performs a liveness probe on the ANR xApp, result expressed as the response code.")
104         @GetMapping(HEALTH_ALIVE_METHOD)
105         // No role required
106         public void getHealthAlive(HttpServletResponse response) {
107                 logger.debug("getHealthAlive");
108                 healthApi.getHealthAlive();
109                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
110         }
111
112         @ApiOperation(value = "Performs a readiness probe on the ANR xApp, result expressed as the response code.")
113         @GetMapping(HEALTH_READY_METHOD)
114         // No role required
115         public void getHealthReady(HttpServletResponse response) {
116                 logger.debug("getHealthReady");
117                 healthApi.getHealthReady();
118                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
119         }
120
121         @ApiOperation(value = "Returns list of gNodeB IDs based on NCRT in ANR", response = GgNodeBTable.class)
122         @GetMapping(GNODEBS_METHOD)
123         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
124         public GgNodeBTable getGnodebs() {
125                 logger.debug("getGnodebs");
126                 return ncrtApi.getgNodeB();
127         }
128
129         @ApiOperation(value = "Returns neighbor cell relation table for all gNodeBs or based on query parameters", response = NeighborCellRelationTable.class)
130         @GetMapping(NCRT_METHOD)
131         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
132         public NeighborCellRelationTable getNcrt( //
133                         @RequestParam(required = false) String ggnodeb, //
134                         @RequestParam(required = false) String servingCellNrcgi, //
135                         @RequestParam(required = false) String neighborCellNrpci) {
136                 logger.debug("getNcrt: ggnbid {}, servingCellNrpci {}, neighborCellNrcgi {}", ggnodeb, servingCellNrcgi,
137                                 neighborCellNrpci);
138                 return ncrtApi.getNcrt(ggnodeb, servingCellNrcgi, neighborCellNrpci);
139         }
140
141         // /ncrt/servingcells/{servCellNrcgi}/neighborcells/{neighCellNrpci} :
142         @ApiOperation(value = "Modify neighbor cell relation based on Serving Cell NRCGI and Neighbor Cell NRPCI")
143         @PutMapping(NCRT_METHOD + "/" + PP_SERVING + "/{" + PP_SERVING + "}/" + PP_NEIGHBOR + "/{" + PP_NEIGHBOR + "}")
144         @Secured({ DashboardConstants.ROLE_ADMIN })
145         public void modifyNcrt(@PathVariable(PP_SERVING) String servingCellNrcgi, //
146                         @PathVariable(PP_NEIGHBOR) String neighborCellNrpci, //
147                         @RequestBody NeighborCellRelationMod ncrMod, HttpServletResponse response) {
148                 logger.debug("modifyNcrt: servingCellNrcgi {}, neighborCellNrpci {}, ncrMod {}", servingCellNrcgi,
149                                 neighborCellNrpci, ncrMod);
150                 ncrtApi.modifyNcrt(servingCellNrcgi, neighborCellNrpci, ncrMod);
151                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
152         }
153
154         @ApiOperation(value = "Delete neighbor cell relation based on Serving Cell NRCGI and Neighbor Cell NRPCI")
155         @DeleteMapping(NCRT_METHOD + "/" + PP_SERVING + "/{" + PP_SERVING + "}/" + PP_NEIGHBOR + "/{" + PP_NEIGHBOR + "}")
156         @Secured({ DashboardConstants.ROLE_ADMIN })
157         public void deleteNcrt(@PathVariable(PP_SERVING) String servingCellNrcgi, //
158                         @PathVariable(PP_NEIGHBOR) String neighborCellNrpci, //
159                         HttpServletResponse response) {
160                 logger.debug("deleteNcrt: servingCellNrcgi {}, neighborCellNrpci {}", servingCellNrcgi, neighborCellNrpci);
161                 ncrtApi.deleteNcrt(servingCellNrcgi, neighborCellNrpci);
162                 response.setStatus(healthApi.getApiClient().getStatusCode().value());
163         }
164
165 }