3d52b8c23e53daa25895ac7885a56ee813445170
[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         // Path parameters
80         private static final String PP_RANNAME = "ranName";
81
82         // Populated by the autowired constructor
83         private final HealthCheckApi e2HealthCheckApi;
84         private final NodebApi e2NodebApi;
85
86         // TODO: remove this when E2 delivers the feature
87         private final List<NodebIdentity> mockNodebIdList;
88
89         @Autowired
90         public E2ManagerController(final HealthCheckApi e2HealthCheckApi, final NodebApi e2NodebApi,
91                         @Value("${e2mgr.mock.rannames:#{null}}") final String mockRanNames) {
92                 Assert.notNull(e2HealthCheckApi, "API must not be null");
93                 Assert.notNull(e2NodebApi, "API must not be null");
94                 this.e2HealthCheckApi = e2HealthCheckApi;
95                 this.e2NodebApi = e2NodebApi;
96                 mockNodebIdList = new ArrayList<>();
97                 if (mockRanNames != null) {
98                         logger.debug("ctor: Mocking RAN names: {}", mockRanNames);
99                         for (String id : mockRanNames.split(",")) {
100                                 NodebIdentityGlobalNbId globalNbId = new NodebIdentityGlobalNbId().nbId("mockNbId").plmnId("mockPlmId");
101                                 mockNodebIdList.add(new NodebIdentity().globalNbId(globalNbId).inventoryName(id.trim()));
102                         }
103                 }
104         }
105
106         @ApiOperation(value = "Gets the E2 manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
107         @RequestMapping(value = DashboardConstants.VERSION_METHOD, method = RequestMethod.GET)
108         public SuccessTransport getE2ManagerClientVersion() {
109                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthCheckApi.class));
110         }
111
112         @ApiOperation(value = "Gets the health from the E2 manager, expressed as the response code.")
113         @RequestMapping(value = HEALTH_METHOD, method = RequestMethod.GET)
114         public void healthGet(HttpServletResponse response) {
115                 logger.debug("healthGet");
116                 e2HealthCheckApi.healthGet();
117                 response.setStatus(e2HealthCheckApi.getApiClient().getStatusCode().value());
118         }
119
120         // This calls other methods to simplify the task of the front-end.
121         @ApiOperation(value = "Gets all RAN identities and statuses from the E2 manager.", response = RanDetailsTransport.class, responseContainer = "List")
122         @RequestMapping(value = RAN_METHOD, method = RequestMethod.GET)
123         public List<RanDetailsTransport> getRanDetails() {
124                 logger.debug("getRanDetails");
125                 // TODO: remove mock when e2mgr delivers the getNodebIdList() method
126                 List<NodebIdentity> nodebIdList = mockNodebIdList.isEmpty() ? e2NodebApi.getNodebIdList() : mockNodebIdList;
127                 List<RanDetailsTransport> details = new ArrayList<>();
128                 for (NodebIdentity nbid : nodebIdList) {
129                         GetNodebResponse nbResp = null;
130                         try {
131                                 // Catch exceptions to keep looping despite failures
132                                 nbResp = e2NodebApi.getNb(nbid.getInventoryName());
133                         } catch (HttpStatusCodeException ex) {
134                                 logger.warn("E2 getNb failed for name {}: {}", nbid.getInventoryName(), ex.toString());
135                                 nbResp = new GetNodebResponse().connectionStatus("UNKNOWN").ip("UNKNOWN").port(-1)
136                                                 .ranName(nbid.getInventoryName());
137                         }
138                         details.add(new RanDetailsTransport(nbid, nbResp));
139                 }
140                 return details;
141         }
142
143         @ApiOperation(value = "Get RAN identities list.", response = NodebIdentity.class, responseContainer = "List")
144         @RequestMapping(value = NODEB_LIST_METHOD, method = RequestMethod.GET)
145         public List<NodebIdentity> getNodebIdList() {
146                 logger.debug("getNodebIdList");
147                 return e2NodebApi.getNodebIdList();
148         }
149
150         @ApiOperation(value = "Get RAN by name.", response = GetNodebResponse.class)
151         @RequestMapping(value = NODEB_METHOD + "/{" + PP_RANNAME + "}", method = RequestMethod.GET)
152         public GetNodebResponse getNb(@PathVariable(PP_RANNAME) String ranName) {
153                 logger.debug("getNb {}", ranName);
154                 return e2NodebApi.getNb(ranName);
155         }
156
157         @ApiOperation(value = "Close all connections to the RANs and delete the data from the nodeb-rnib DB.")
158         @RequestMapping(value = NODEB_METHOD, method = RequestMethod.DELETE)
159         public void nodebDelete(HttpServletResponse response) {
160                 logger.debug("nodebDelete");
161                 e2NodebApi.nodebDelete();
162                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
163         }
164
165         @ApiOperation(value = "Sets up an EN-DC RAN connection via the E2 manager.")
166         @RequestMapping(value = ENDC_SETUP_METHOD, method = RequestMethod.POST)
167         public void endcSetup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
168                 logger.debug("endcSetup {}", setupRequest);
169                 e2NodebApi.endcSetup(setupRequest);
170                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
171         }
172
173         @ApiOperation(value = "Sets up an X2 RAN connection via the E2 manager.")
174         @RequestMapping(value = X2_SETUP_METHOD, method = RequestMethod.POST)
175         public void x2Setup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
176                 logger.debug("x2Setup {}", setupRequest);
177                 e2NodebApi.x2Setup(setupRequest);
178                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
179         }
180
181 }