Integrate EPSDK-FW library for auth and users
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / E2ManagerController.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 import java.util.ArrayList;
24 import java.util.List;
25
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.oransc.ric.e2mgr.client.api.HealthCheckApi;
29 import org.oransc.ric.e2mgr.client.api.NodebApi;
30 import org.oransc.ric.e2mgr.client.model.GetNodebResponse;
31 import org.oransc.ric.e2mgr.client.model.NodebIdentity;
32 import org.oransc.ric.e2mgr.client.model.SetupRequest;
33 import org.oransc.ric.portal.dashboard.DashboardApplication;
34 import org.oransc.ric.portal.dashboard.DashboardConstants;
35 import org.oransc.ric.portal.dashboard.model.RanDetailsTransport;
36 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.context.annotation.Configuration;
41 import org.springframework.http.MediaType;
42 import org.springframework.security.access.annotation.Secured;
43 import org.springframework.util.Assert;
44 import org.springframework.web.bind.annotation.DeleteMapping;
45 import org.springframework.web.bind.annotation.GetMapping;
46 import org.springframework.web.bind.annotation.PathVariable;
47 import org.springframework.web.bind.annotation.PostMapping;
48 import org.springframework.web.bind.annotation.RequestBody;
49 import org.springframework.web.bind.annotation.RequestMapping;
50 import org.springframework.web.bind.annotation.RestController;
51 import org.springframework.web.client.HttpStatusCodeException;
52
53 import io.swagger.annotations.ApiOperation;
54
55 /**
56  * Proxies calls from the front end to the E2 Manager API. All methods answer
57  * 502 on failure and wrap the remote details: <blockquote>HTTP server received
58  * an invalid response from a server it consulted when acting as a proxy or
59  * gateway.</blockquote>
60  */
61 @Configuration
62 @RestController
63 @RequestMapping(value = E2ManagerController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
64 public class E2ManagerController {
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 + "/e2mgr";
70         // Endpoints
71         public static final String HEALTH_METHOD = "health";
72         public static final String NODEB_METHOD = "/nodeb";
73         public static final String NODEB_LIST_METHOD = "/nodeb-ids";
74         public static final String RAN_METHOD = "/ran";
75         public static final String ENDC_SETUP_METHOD = "/endcSetup";
76         public static final String X2_SETUP_METHOD = "/x2Setup";
77         public static final String VERSION_METHOD = DashboardConstants.VERSION_METHOD;
78         // Path parameters
79         private static final String PP_RANNAME = "ranName";
80
81         // Populated by the autowired constructor
82         private final HealthCheckApi e2HealthCheckApi;
83         private final NodebApi e2NodebApi;
84
85         @Autowired
86         public E2ManagerController(final HealthCheckApi e2HealthCheckApi, final NodebApi e2NodebApi) {
87                 Assert.notNull(e2HealthCheckApi, "API must not be null");
88                 Assert.notNull(e2NodebApi, "API must not be null");
89                 this.e2HealthCheckApi = e2HealthCheckApi;
90                 this.e2NodebApi = e2NodebApi;
91         }
92
93         @ApiOperation(value = "Gets the E2 manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
94         @GetMapping(VERSION_METHOD)
95         // No role required
96         public SuccessTransport getClientVersion() {
97                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthCheckApi.class));
98         }
99
100         @ApiOperation(value = "Gets the health from the E2 manager, expressed as the response code.")
101         @GetMapping(HEALTH_METHOD)
102         // No role required
103         public void healthGet(HttpServletResponse response) {
104                 logger.debug("healthGet");
105                 e2HealthCheckApi.healthGet();
106                 response.setStatus(e2HealthCheckApi.getApiClient().getStatusCode().value());
107         }
108
109         // This calls other methods to simplify the task of the front-end.
110         @ApiOperation(value = "Gets all RAN identities and statuses from the E2 manager.", response = RanDetailsTransport.class, responseContainer = "List")
111         @GetMapping(RAN_METHOD)
112         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
113         public List<RanDetailsTransport> getRanDetails() {
114                 logger.debug("getRanDetails");
115                 List<NodebIdentity> nodebIdList = e2NodebApi.getNodebIdList();
116                 List<RanDetailsTransport> details = new ArrayList<>();
117                 for (NodebIdentity nbid : nodebIdList) {
118                         GetNodebResponse nbResp = null;
119                         try {
120                                 // Catch exceptions to keep looping despite failures
121                                 nbResp = e2NodebApi.getNb(nbid.getInventoryName());
122                         } catch (HttpStatusCodeException ex) {
123                                 logger.warn("E2 getNb failed for name {}: {}", nbid.getInventoryName(), ex.toString());
124                                 nbResp = new GetNodebResponse().connectionStatus("UNKNOWN").ip("UNKNOWN").port(-1)
125                                                 .ranName(nbid.getInventoryName());
126                         }
127                         details.add(new RanDetailsTransport(nbid, nbResp));
128                 }
129                 return details;
130         }
131
132         @ApiOperation(value = "Get RAN identities list.", response = NodebIdentity.class, responseContainer = "List")
133         @GetMapping(NODEB_LIST_METHOD)
134         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
135         public List<NodebIdentity> getNodebIdList() {
136                 logger.debug("getNodebIdList");
137                 return e2NodebApi.getNodebIdList();
138         }
139
140         @ApiOperation(value = "Get RAN by name.", response = GetNodebResponse.class)
141         @GetMapping(NODEB_METHOD + "/{" + PP_RANNAME + "}")
142         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
143         public GetNodebResponse getNb(@PathVariable(PP_RANNAME) String ranName) {
144                 logger.debug("getNb {}", ranName);
145                 return e2NodebApi.getNb(ranName);
146         }
147
148         @ApiOperation(value = "Close all connections to the RANs and delete the data from the nodeb-rnib DB.")
149         @DeleteMapping(NODEB_METHOD)
150         @Secured({ DashboardConstants.ROLE_ADMIN })
151         public void nodebDelete(HttpServletResponse response) {
152                 logger.debug("nodebDelete");
153                 e2NodebApi.nodebDelete();
154                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
155         }
156
157         @ApiOperation(value = "Sets up an EN-DC RAN connection via the E2 manager.")
158         @PostMapping(ENDC_SETUP_METHOD)
159         @Secured({ DashboardConstants.ROLE_ADMIN })
160         public void endcSetup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
161                 logger.debug("endcSetup {}", setupRequest);
162                 e2NodebApi.endcSetup(setupRequest);
163                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
164         }
165
166         @ApiOperation(value = "Sets up an X2 RAN connection via the E2 manager.")
167         @PostMapping(X2_SETUP_METHOD)
168         @Secured({ DashboardConstants.ROLE_ADMIN })
169         public void x2Setup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
170                 logger.debug("x2Setup {}", setupRequest);
171                 e2NodebApi.x2Setup(setupRequest);
172                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
173         }
174
175 }