Drop e2.mock.rannames feature
[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.util.Assert;
43 import org.springframework.web.bind.annotation.DeleteMapping;
44 import org.springframework.web.bind.annotation.GetMapping;
45 import org.springframework.web.bind.annotation.PathVariable;
46 import org.springframework.web.bind.annotation.PostMapping;
47 import org.springframework.web.bind.annotation.RequestBody;
48 import org.springframework.web.bind.annotation.RequestMapping;
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 @Configuration
61 @RestController
62 @RequestMapping(value = E2ManagerController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
63 public class E2ManagerController {
64
65         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
66
67         // Publish paths in constants so tests are easy to write
68         public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/e2mgr";
69         // Endpoints
70         public static final String HEALTH_METHOD = "health";
71         public static final String NODEB_METHOD = "/nodeb";
72         public static final String NODEB_LIST_METHOD = "/nodeb-ids";
73         public static final String RAN_METHOD = "/ran";
74         public static final String ENDC_SETUP_METHOD = "/endcSetup";
75         public static final String X2_SETUP_METHOD = "/x2Setup";
76         public static final String VERSION_METHOD = DashboardConstants.VERSION_METHOD;
77         // Path parameters
78         private static final String PP_RANNAME = "ranName";
79
80         // Populated by the autowired constructor
81         private final HealthCheckApi e2HealthCheckApi;
82         private final NodebApi e2NodebApi;
83
84         @Autowired
85         public E2ManagerController(final HealthCheckApi e2HealthCheckApi, final NodebApi e2NodebApi) {
86                 Assert.notNull(e2HealthCheckApi, "API must not be null");
87                 Assert.notNull(e2NodebApi, "API must not be null");
88                 this.e2HealthCheckApi = e2HealthCheckApi;
89                 this.e2NodebApi = e2NodebApi;
90         }
91
92         @ApiOperation(value = "Gets the E2 manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
93         @GetMapping(VERSION_METHOD)
94         public SuccessTransport getClientVersion() {
95                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthCheckApi.class));
96         }
97
98         @ApiOperation(value = "Gets the health from the E2 manager, expressed as the response code.")
99         @GetMapping(HEALTH_METHOD)
100         public void healthGet(HttpServletResponse response) {
101                 logger.debug("healthGet");
102                 e2HealthCheckApi.healthGet();
103                 response.setStatus(e2HealthCheckApi.getApiClient().getStatusCode().value());
104         }
105
106         // This calls other methods to simplify the task of the front-end.
107         @ApiOperation(value = "Gets all RAN identities and statuses from the E2 manager.", response = RanDetailsTransport.class, responseContainer = "List")
108         @GetMapping(RAN_METHOD)
109         public List<RanDetailsTransport> getRanDetails() {
110                 logger.debug("getRanDetails");
111                 List<NodebIdentity> nodebIdList = e2NodebApi.getNodebIdList();
112                 List<RanDetailsTransport> details = new ArrayList<>();
113                 for (NodebIdentity nbid : nodebIdList) {
114                         GetNodebResponse nbResp = null;
115                         try {
116                                 // Catch exceptions to keep looping despite failures
117                                 nbResp = e2NodebApi.getNb(nbid.getInventoryName());
118                         } catch (HttpStatusCodeException ex) {
119                                 logger.warn("E2 getNb failed for name {}: {}", nbid.getInventoryName(), ex.toString());
120                                 nbResp = new GetNodebResponse().connectionStatus("UNKNOWN").ip("UNKNOWN").port(-1)
121                                                 .ranName(nbid.getInventoryName());
122                         }
123                         details.add(new RanDetailsTransport(nbid, nbResp));
124                 }
125                 return details;
126         }
127
128         @ApiOperation(value = "Get RAN identities list.", response = NodebIdentity.class, responseContainer = "List")
129         @GetMapping(NODEB_LIST_METHOD)
130         public List<NodebIdentity> getNodebIdList() {
131                 logger.debug("getNodebIdList");
132                 return e2NodebApi.getNodebIdList();
133         }
134
135         @ApiOperation(value = "Get RAN by name.", response = GetNodebResponse.class)
136         @GetMapping(NODEB_METHOD + "/{" + PP_RANNAME + "}")
137         public GetNodebResponse getNb(@PathVariable(PP_RANNAME) String ranName) {
138                 logger.debug("getNb {}", ranName);
139                 return e2NodebApi.getNb(ranName);
140         }
141
142         @ApiOperation(value = "Close all connections to the RANs and delete the data from the nodeb-rnib DB.")
143         @DeleteMapping(NODEB_METHOD)
144         public void nodebDelete(HttpServletResponse response) {
145                 logger.debug("nodebDelete");
146                 e2NodebApi.nodebDelete();
147                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
148         }
149
150         @ApiOperation(value = "Sets up an EN-DC RAN connection via the E2 manager.")
151         @PostMapping(ENDC_SETUP_METHOD)
152         public void endcSetup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
153                 logger.debug("endcSetup {}", setupRequest);
154                 e2NodebApi.endcSetup(setupRequest);
155                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
156         }
157
158         @ApiOperation(value = "Sets up an X2 RAN connection via the E2 manager.")
159         @PostMapping(X2_SETUP_METHOD)
160         public void x2Setup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
161                 logger.debug("x2Setup {}", setupRequest);
162                 e2NodebApi.x2Setup(setupRequest);
163                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
164         }
165
166 }