Extend Mock E2Mgr configuration to save state
[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.ResetRequest;
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.context.annotation.Configuration;
42 import org.springframework.http.MediaType;
43 import org.springframework.security.access.annotation.Secured;
44 import org.springframework.util.Assert;
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.PutMapping;
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 @Configuration
63 @RestController
64 @RequestMapping(value = E2ManagerController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
65 public class E2ManagerController {
66
67         private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
68
69         // Publish paths in constants so tests are easy to write
70         public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/e2mgr";
71         // Endpoints
72         public static final String HEALTH_METHOD = "health";
73         public static final String NODEB_SHUTDOWN_METHOD = "/nodebShutdownPut";
74         public static final String NODEB_LIST_METHOD = "/nodeb-ids";
75         public static final String RAN_METHOD = "/ran";
76         public static final String RESET_METHOD = "/reset";
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         @Autowired
88         public E2ManagerController(final HealthCheckApi e2HealthCheckApi, final NodebApi e2NodebApi) {
89                 Assert.notNull(e2HealthCheckApi, "API must not be null");
90                 Assert.notNull(e2NodebApi, "API must not be null");
91                 this.e2HealthCheckApi = e2HealthCheckApi;
92                 this.e2NodebApi = e2NodebApi;
93         }
94
95         @ApiOperation(value = "Gets the E2 manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
96         @GetMapping(VERSION_METHOD)
97         // No role required
98         public SuccessTransport getClientVersion() {
99                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthCheckApi.class));
100         }
101
102         @ApiOperation(value = "Gets the health from the E2 manager, expressed as the response code.")
103         @GetMapping(HEALTH_METHOD)
104         // No role required
105         public void healthGet(HttpServletResponse response) {
106                 logger.debug("healthGet");
107                 e2HealthCheckApi.healthGet();
108                 response.setStatus(e2HealthCheckApi.getApiClient().getStatusCode().value());
109         }
110
111         // This calls other methods to simplify the task of the front-end.
112         @ApiOperation(value = "Gets all RAN identities and statuses from the E2 manager.", response = RanDetailsTransport.class, responseContainer = "List")
113         @GetMapping(RAN_METHOD)
114         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
115         public List<RanDetailsTransport> getRanDetails() {
116                 logger.debug("getRanDetails");
117                 List<NodebIdentity> nodebIdList = e2NodebApi.getNodebIdList();
118                 List<RanDetailsTransport> details = new ArrayList<>();
119                 for (NodebIdentity nbid : nodebIdList) {
120                         GetNodebResponse nbResp = null;
121                         try {
122                                 // Catch exceptions to keep looping despite failures
123                                 nbResp = e2NodebApi.getNb(nbid.getInventoryName());
124                         } catch (HttpStatusCodeException ex) {
125                                 logger.warn("E2 getNb failed for name {}: {}", nbid.getInventoryName(), ex.toString());
126                                 nbResp = new GetNodebResponse().connectionStatus("UNKNOWN").ip("UNKNOWN").port(-1)
127                                                 .ranName(nbid.getInventoryName());
128                         }
129                         details.add(new RanDetailsTransport(nbid, nbResp));
130                 }
131                 return details;
132         }
133
134         @ApiOperation(value = "Get RAN identities list.", response = NodebIdentity.class, responseContainer = "List")
135         @GetMapping(NODEB_LIST_METHOD)
136         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
137         public List<NodebIdentity> getNodebIdList() {
138                 logger.debug("getNodebIdList");
139                 return e2NodebApi.getNodebIdList();
140         }
141
142         @ApiOperation(value = "Get RAN by name.", response = GetNodebResponse.class)
143         @GetMapping(NODEB_SHUTDOWN_METHOD + "/{" + PP_RANNAME + "}")
144         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
145         public GetNodebResponse getNb(@PathVariable(PP_RANNAME) String ranName) {
146                 logger.debug("getNb {}", ranName);
147                 return e2NodebApi.getNb(ranName);
148         }
149
150         @ApiOperation(value = "Sets up an EN-DC RAN connection via the E2 manager.")
151         @PostMapping(ENDC_SETUP_METHOD)
152         @Secured({ DashboardConstants.ROLE_ADMIN })
153         public void endcSetup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
154                 logger.debug("endcSetup {}", setupRequest);
155                 e2NodebApi.endcSetup(setupRequest);
156                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
157         }
158
159         @ApiOperation(value = "Sets up an X2 RAN connection via the E2 manager.")
160         @PostMapping(X2_SETUP_METHOD)
161         @Secured({ DashboardConstants.ROLE_ADMIN })
162         public void x2Setup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
163                 logger.debug("x2Setup {}", setupRequest);
164                 e2NodebApi.x2Setup(setupRequest);
165                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
166         }
167
168         @ApiOperation(value = "Close all connections to the RANs and delete the data from the nodeb-rnib DB.")
169         @PutMapping(NODEB_SHUTDOWN_METHOD)
170         @Secured({ DashboardConstants.ROLE_ADMIN })
171         public void nodebShutdownPut(HttpServletResponse response) {
172                 logger.debug("nodebShutdownPut");
173                 e2NodebApi.nodebShutdownPut();
174                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
175         }
176
177         @ApiOperation(value = "Abort any other ongoing procedures over X2 between the RIC and the RAN.")
178         @PutMapping(RESET_METHOD + "/{" + PP_RANNAME + "}")
179         @Secured({ DashboardConstants.ROLE_ADMIN })
180         public void reset(@PathVariable(PP_RANNAME) String ranName, @RequestBody ResetRequest resetRequest,
181                         HttpServletResponse response) {
182                 logger.debug("reset");
183                 e2NodebApi.reset(ranName, resetRequest);
184                 response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
185         }
186
187 }