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