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