Non-functional changes to silence Sonar
[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.NodebIdentityGlobalNbId;
33 import org.oransc.ric.e2mgr.client.model.SetupRequest;
34 import org.oransc.ric.portal.dashboard.DashboardApplication;
35 import org.oransc.ric.portal.dashboard.DashboardConstants;
36 import org.oransc.ric.portal.dashboard.model.RanDetailsTransport;
37 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.beans.factory.annotation.Value;
42 import org.springframework.context.annotation.Configuration;
43 import org.springframework.http.MediaType;
44 import org.springframework.util.Assert;
45 import org.springframework.web.bind.annotation.DeleteMapping;
46 import org.springframework.web.bind.annotation.GetMapping;
47 import org.springframework.web.bind.annotation.PathVariable;
48 import org.springframework.web.bind.annotation.PostMapping;
49 import org.springframework.web.bind.annotation.RequestBody;
50 import org.springframework.web.bind.annotation.RequestMapping;
51 import org.springframework.web.bind.annotation.RestController;
52 import org.springframework.web.client.HttpStatusCodeException;
53
54 import io.swagger.annotations.ApiOperation;
55
56 /**
57  * Proxies calls from the front end to the E2 Manager API. All methods answer
58  * 502 on failure and wrap the remote details: <blockquote>HTTP server received
59  * an invalid response from a server it consulted when acting as a proxy or
60  * gateway.</blockquote>
61  * 
62  * In R1 the E2 interface does not yet implement the get-ID-list method, so this
63  * class mocks up some functionality.
64  */
65 @Configuration
66 @RestController
67 @RequestMapping(value = E2ManagerController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
68 public class E2ManagerController {
69
70         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
71
72         // Publish paths in constants so tests are easy to write
73         public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/e2mgr";
74         // Endpoints
75         public static final String HEALTH_METHOD = "health";
76         public static final String NODEB_METHOD = "/nodeb";
77         public static final String NODEB_LIST_METHOD = "/nodeb-ids";
78         public static final String RAN_METHOD = "/ran";
79         public static final String ENDC_SETUP_METHOD = "/endcSetup";
80         public static final String X2_SETUP_METHOD = "/x2Setup";
81         public static final String VERSION_METHOD = DashboardConstants.VERSION_METHOD;
82         // Path parameters
83         private static final String PP_RANNAME = "ranName";
84
85         // Populated by the autowired constructor
86         private final HealthCheckApi e2HealthCheckApi;
87         private final NodebApi e2NodebApi;
88
89         // TODO: remove this when E2 delivers the feature
90         private final List<NodebIdentity> mockNodebIdList;
91
92         @Autowired
93         public E2ManagerController(final HealthCheckApi e2HealthCheckApi, final NodebApi e2NodebApi,
94                         @Value("${e2mgr.mock.rannames:#{null}}") final String mockRanNames) {
95                 Assert.notNull(e2HealthCheckApi, "API must not be null");
96                 Assert.notNull(e2NodebApi, "API must not be null");
97                 this.e2HealthCheckApi = e2HealthCheckApi;
98                 this.e2NodebApi = e2NodebApi;
99                 mockNodebIdList = new ArrayList<>();
100                 if (mockRanNames != null) {
101                         logger.debug("ctor: Mocking RAN names: {}", mockRanNames);
102                         for (String id : mockRanNames.split(",")) {
103                                 NodebIdentityGlobalNbId globalNbId = new NodebIdentityGlobalNbId().nbId("mockNbId").plmnId("mockPlmId");
104                                 mockNodebIdList.add(new NodebIdentity().globalNbId(globalNbId).inventoryName(id.trim()));
105                         }
106                 }
107         }
108
109         @ApiOperation(value = "Gets the E2 manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
110         @GetMapping(VERSION_METHOD)
111         public SuccessTransport getClientVersion() {
112                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthCheckApi.class));
113         }
114
115         @ApiOperation(value = "Gets the health from the E2 manager, expressed as the response code.")
116         @GetMapping(HEALTH_METHOD)
117         public void healthGet(HttpServletResponse response) {
118                 logger.debug("healthGet");
119                 e2HealthCheckApi.healthGet();
120                 response.setStatus(e2HealthCheckApi.getApiClient().getStatusCode().value());
121         }
122
123         // This calls other methods to simplify the task of the front-end.
124         @ApiOperation(value = "Gets all RAN identities and statuses from the E2 manager.", response = RanDetailsTransport.class, responseContainer = "List")
125         @GetMapping(RAN_METHOD)
126         public List<RanDetailsTransport> getRanDetails() {
127                 logger.debug("getRanDetails");
128                 // TODO: remove mock when e2mgr delivers the getNodebIdList() method
129                 List<NodebIdentity> nodebIdList = mockNodebIdList.isEmpty() ? e2NodebApi.getNodebIdList() : mockNodebIdList;
130                 List<RanDetailsTransport> details = new ArrayList<>();
131                 for (NodebIdentity nbid : nodebIdList) {
132                         GetNodebResponse nbResp = null;
133                         try {
134                                 // Catch exceptions to keep looping despite failures
135                                 nbResp = e2NodebApi.getNb(nbid.getInventoryName());
136                         } catch (HttpStatusCodeException ex) {
137                                 logger.warn("E2 getNb failed for name {}: {}", nbid.getInventoryName(), ex.toString());
138                                 nbResp = new GetNodebResponse().connectionStatus("UNKNOWN").ip("UNKNOWN").port(-1)
139                                                 .ranName(nbid.getInventoryName());
140                         }
141                         details.add(new RanDetailsTransport(nbid, nbResp));
142                 }
143                 return details;
144         }
145
146         @ApiOperation(value = "Get RAN identities list.", response = NodebIdentity.class, responseContainer = "List")
147         @GetMapping(NODEB_LIST_METHOD)
148         public List<NodebIdentity> getNodebIdList() {
149                 logger.debug("getNodebIdList");
150                 return e2NodebApi.getNodebIdList();
151         }
152
153         @ApiOperation(value = "Get RAN by name.", response = GetNodebResponse.class)
154         @GetMapping(NODEB_METHOD + "/{" + PP_RANNAME + "}")
155         public GetNodebResponse getNb(@PathVariable(PP_RANNAME) String ranName) {
156                 logger.debug("getNb {}", ranName);
157                 return e2NodebApi.getNb(ranName);
158         }
159
160         @ApiOperation(value = "Close all connections to the RANs and delete the data from the nodeb-rnib DB.")
161         @DeleteMapping(NODEB_METHOD)
162         public void nodebDelete(HttpServletResponse response) {
163                 logger.debug("nodebDelete");
164                 e2NodebApi.nodebDelete();
165                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
166         }
167
168         @ApiOperation(value = "Sets up an EN-DC RAN connection via the E2 manager.")
169         @PostMapping(ENDC_SETUP_METHOD)
170         public void endcSetup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
171                 logger.debug("endcSetup {}", setupRequest);
172                 e2NodebApi.endcSetup(setupRequest);
173                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
174         }
175
176         @ApiOperation(value = "Sets up an X2 RAN connection via the E2 manager.")
177         @PostMapping(X2_SETUP_METHOD)
178         public void x2Setup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
179                 logger.debug("x2Setup {}", setupRequest);
180                 e2NodebApi.x2Setup(setupRequest);
181                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
182         }
183
184 }