Upgrade to E2Manager API version 4.4.4
[portal/ric-dashboard.git] / dashboard / 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
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 org.oransc.ric.portal.dashboard.DashboardApplication;
27 import org.oransc.ric.portal.dashboard.DashboardConstants;
28 import org.oransc.ric.portal.dashboard.config.E2ManagerApiBuilder;
29 import org.oransc.ric.portal.dashboard.model.RanDetailsTransport;
30 import org.oransc.ric.portal.dashboard.model.SuccessTransport;
31 import org.oransc.ricplt.e2mgr.client.api.HealthCheckApi;
32 import org.oransc.ricplt.e2mgr.client.api.NodebApi;
33 import org.oransc.ricplt.e2mgr.client.model.GetNodebResponse;
34 import org.oransc.ricplt.e2mgr.client.model.NodebIdentity;
35 import org.oransc.ricplt.e2mgr.client.model.UpdateGnbRequest;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.context.annotation.Configuration;
40 import org.springframework.http.MediaType;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.security.access.annotation.Secured;
43 import org.springframework.util.Assert;
44 import org.springframework.web.bind.annotation.GetMapping;
45 import org.springframework.web.bind.annotation.PathVariable;
46 import org.springframework.web.bind.annotation.PutMapping;
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.
56  * 
57  * If a method throws RestClientResponseException, it is handled by
58  * {@link CustomResponseEntityExceptionHandler#handleProxyMethodException(Exception, org.springframework.web.context.request.WebRequest)}
59  * which returns status 502. All other exceptions are handled by Spring which
60  * returns status 500.
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 for tests
70         public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/e2mgr";
71         // Dashboard only
72         public static final String HEALTH_METHOD = "health";
73         // Keep these consistent with the E2M implementation
74         /* package */ static final String NODEB_PREFIX = "nodeb";
75         public static final String RAN_METHOD = NODEB_PREFIX + "/ran";
76         public static final String NODEB_SHUTDOWN_METHOD = NODEB_PREFIX + "/shutdown";
77         public static final String NODEB_LIST_METHOD = NODEB_PREFIX + "/ids";
78         // Path parameters
79         private static final String PP_RANNAME = "ranName";
80
81         // Populated by the autowired constructor
82         private final E2ManagerApiBuilder e2ManagerApiBuilder;
83
84         @Autowired
85         public E2ManagerController(final E2ManagerApiBuilder e2ManagerApiBuilder) {
86                 Assert.notNull(e2ManagerApiBuilder, "builder must not be null");
87                 this.e2ManagerApiBuilder = e2ManagerApiBuilder;
88                 if (logger.isDebugEnabled())
89                         logger.debug("ctor: configured with builder type {}", e2ManagerApiBuilder.getClass().getName());
90         }
91
92         @ApiOperation(value = "Gets the E2 manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
93         @GetMapping(DashboardConstants.VERSION_METHOD)
94         // No role required
95         public SuccessTransport getClientVersion() {
96                 return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthCheckApi.class));
97         }
98
99         @ApiOperation(value = "Gets the health from the E2 manager, expressed as the response code.")
100         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + HEALTH_METHOD)
101         // No role required
102         public ResponseEntity<String> healthGet(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey) {
103                 logger.debug("healthGet instance {}", instanceKey);
104                 HealthCheckApi api = e2ManagerApiBuilder.getHealthCheckApi(instanceKey);
105                 api.healthGet();
106                 return ResponseEntity.status(api.getApiClient().getStatusCode().value()).body(null);
107         }
108
109         // This calls other methods to simplify the task of the front-end.
110         @ApiOperation(value = "Gets all RAN identities and statuses from the E2 manager.", response = RanDetailsTransport.class, responseContainer = "List")
111         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + RAN_METHOD)
112         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
113         public List<RanDetailsTransport> getRanDetails(
114                         @PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey) {
115                 logger.debug("getRanDetails instance {}", instanceKey);
116                 NodebApi api = e2ManagerApiBuilder.getNodebApi(instanceKey);
117                 List<NodebIdentity> nodebIdList = api.getNodebIdList();
118                 logger.debug("getRanDetails: nodebIdList {}", nodebIdList);
119                 List<RanDetailsTransport> details = new ArrayList<>();
120                 for (NodebIdentity nbid : nodebIdList) {
121                         GetNodebResponse nbResp = null;
122                         try {
123                                 // Catch exceptions to keep looping despite failures
124                                 nbResp = api.getNb(nbid.getInventoryName());
125                         } catch (HttpStatusCodeException ex) {
126                                 logger.warn("E2 getNb failed for name {}: {}", nbid.getInventoryName(), ex.toString());
127                                 nbResp = new GetNodebResponse().connectionStatus("UNKNOWN").ip("UNKNOWN").port(-1)
128                                                 .ranName(nbid.getInventoryName());
129                         }
130                         details.add(new RanDetailsTransport(nbid, nbResp));
131                 }
132                 return details;
133         }
134
135         @ApiOperation(value = "Get RAN identities list.", response = NodebIdentity.class, responseContainer = "List")
136         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/"
137                         + NODEB_LIST_METHOD)
138         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
139         public List<NodebIdentity> getNodebIdList(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey) {
140                 logger.debug("getNodebIdList instance {}", instanceKey);
141                 return e2ManagerApiBuilder.getNodebApi(instanceKey).getNodebIdList();
142         }
143
144         @ApiOperation(value = "Get RAN by name.", response = GetNodebResponse.class)
145         @GetMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + NODEB_PREFIX
146                         + "/{" + PP_RANNAME + "}")
147         @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
148         public GetNodebResponse getNb(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
149                         @PathVariable(PP_RANNAME) String ranName) {
150                 logger.debug("getNb instance {} name {}", instanceKey, ranName);
151                 return e2ManagerApiBuilder.getNodebApi(instanceKey).getNb(ranName);
152         }
153
154         @ApiOperation(value = "Close all connections to the RANs and delete the data from the nodeb-rnib DB.")
155         @PutMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/"
156                         + NODEB_SHUTDOWN_METHOD)
157         @Secured({ DashboardConstants.ROLE_ADMIN })
158         public ResponseEntity<String> nodebShutdownPut(
159                         @PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey) {
160                 logger.debug("nodebShutdownPut instance {}", instanceKey);
161                 NodebApi api = e2ManagerApiBuilder.getNodebApi(instanceKey);
162                 api.nodebShutdownPut();
163                 return ResponseEntity.status(api.getApiClient().getStatusCode().value()).body(null);
164         }
165
166         @ApiOperation(value = "Update GNB.")
167         @PutMapping(DashboardConstants.RIC_INSTANCE_KEY + "/{" + DashboardConstants.RIC_INSTANCE_KEY + "}/" + NODEB_PREFIX
168                         + "/{" + PP_RANNAME + "}")
169         @Secured({ DashboardConstants.ROLE_ADMIN })
170         public ResponseEntity<String> updateGnb(@PathVariable(DashboardConstants.RIC_INSTANCE_KEY) String instanceKey,
171                         @PathVariable(PP_RANNAME) String ranName, @RequestBody UpdateGnbRequest updateGnbRequest) {
172                 logger.debug("updateGnb instance {} ran {}", instanceKey, ranName);
173                 NodebApi api = e2ManagerApiBuilder.getNodebApi(instanceKey);
174                 api.updateGnb(updateGnbRequest, ranName);
175                 return ResponseEntity.status(api.getApiClient().getStatusCode().value()).body(null);
176         }
177
178 }