Integrate EPSDK-FW library for auth and users
[portal/ric-dashboard.git] / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / controller / E2ManagerController.java
index fb1add8..b200c9a 100644 (file)
 package org.oransc.ric.portal.dashboard.controller;
 
 import java.lang.invoke.MethodHandles;
-import java.util.HashSet;
-import java.util.Set;
+import java.util.ArrayList;
+import java.util.List;
 
 import javax.servlet.http.HttpServletResponse;
 
-import org.oransc.ric.e2mgr.client.api.E2ManagerApi;
+import org.oransc.ric.e2mgr.client.api.HealthCheckApi;
+import org.oransc.ric.e2mgr.client.api.NodebApi;
+import org.oransc.ric.e2mgr.client.model.GetNodebResponse;
+import org.oransc.ric.e2mgr.client.model.NodebIdentity;
 import org.oransc.ric.e2mgr.client.model.SetupRequest;
+import org.oransc.ric.portal.dashboard.DashboardApplication;
 import org.oransc.ric.portal.dashboard.DashboardConstants;
+import org.oransc.ric.portal.dashboard.model.RanDetailsTransport;
+import org.oransc.ric.portal.dashboard.model.SuccessTransport;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Configuration;
 import org.springframework.http.MediaType;
+import org.springframework.security.access.annotation.Secured;
 import org.springframework.util.Assert;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.client.HttpStatusCodeException;
 
 import io.swagger.annotations.ApiOperation;
 
 /**
- * Provides methods to contact the E2 Manager.
- * 
- * As of this writing the E2 interface only supports setup connection and check
- * health actions; it does not support query or close operations on existing
- * connections. So this class mocks up some of that needed functionality.
+ * Proxies calls from the front end to the E2 Manager API. All methods answer
+ * 502 on failure and wrap the remote details: <blockquote>HTTP server received
+ * an invalid response from a server it consulted when acting as a proxy or
+ * gateway.</blockquote>
  */
 @Configuration
 @RestController
-@RequestMapping(value = DashboardConstants.ENDPOINT_PREFIX + "/e2mgr", produces = MediaType.APPLICATION_JSON_VALUE)
+@RequestMapping(value = E2ManagerController.CONTROLLER_PATH, produces = MediaType.APPLICATION_JSON_VALUE)
 public class E2ManagerController {
 
        private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
 
-       // Populated by the autowired constructor
-       private final E2ManagerApi e2ManagerApi;
+       // Publish paths in constants so tests are easy to write
+       public static final String CONTROLLER_PATH = DashboardConstants.ENDPOINT_PREFIX + "/e2mgr";
+       // Endpoints
+       public static final String HEALTH_METHOD = "health";
+       public static final String NODEB_METHOD = "/nodeb";
+       public static final String NODEB_LIST_METHOD = "/nodeb-ids";
+       public static final String RAN_METHOD = "/ran";
+       public static final String ENDC_SETUP_METHOD = "/endcSetup";
+       public static final String X2_SETUP_METHOD = "/x2Setup";
+       public static final String VERSION_METHOD = DashboardConstants.VERSION_METHOD;
+       // Path parameters
+       private static final String PP_RANNAME = "ranName";
 
-       // Tracks the requests previously submitted.
-       // TODO remove when the E2 manager is extended.
-       private Set<SetupRequest> requests = new HashSet<>();
+       // Populated by the autowired constructor
+       private final HealthCheckApi e2HealthCheckApi;
+       private final NodebApi e2NodebApi;
 
        @Autowired
-       public E2ManagerController(final E2ManagerApi e2ManagerApi) {
-               Assert.notNull(e2ManagerApi, "API must not be null");
-               this.e2ManagerApi = e2ManagerApi;
+       public E2ManagerController(final HealthCheckApi e2HealthCheckApi, final NodebApi e2NodebApi) {
+               Assert.notNull(e2HealthCheckApi, "API must not be null");
+               Assert.notNull(e2NodebApi, "API must not be null");
+               this.e2HealthCheckApi = e2HealthCheckApi;
+               this.e2NodebApi = e2NodebApi;
        }
 
-       private void assertNotNull(Object o) {
-               if (o == null)
-                       throw new IllegalArgumentException("Null not permitted");
+       @ApiOperation(value = "Gets the E2 manager client library MANIFEST.MF property Implementation-Version.", response = SuccessTransport.class)
+       @GetMapping(VERSION_METHOD)
+       // No role required
+       public SuccessTransport getClientVersion() {
+               return new SuccessTransport(200, DashboardApplication.getImplementationVersion(HealthCheckApi.class));
        }
 
-       private void assertNotEmpty(String s) {
-               assertNotNull(s);
-               if (s.isEmpty())
-                       throw new IllegalArgumentException("Empty not permitted");
+       @ApiOperation(value = "Gets the health from the E2 manager, expressed as the response code.")
+       @GetMapping(HEALTH_METHOD)
+       // No role required
+       public void healthGet(HttpServletResponse response) {
+               logger.debug("healthGet");
+               e2HealthCheckApi.healthGet();
+               response.setStatus(e2HealthCheckApi.getApiClient().getStatusCode().value());
        }
 
-       @ApiOperation(value = "Gets the health from the E2 manager, expressed as the response code.")
-       @RequestMapping(value = "/health", method = RequestMethod.GET)
-       public void getHealth(HttpServletResponse response) {
-               logger.debug("getHealth");
-               e2ManagerApi.healthCheck();
-               response.setStatus(e2ManagerApi.getApiClient().getStatusCode().value());
+       // This calls other methods to simplify the task of the front-end.
+       @ApiOperation(value = "Gets all RAN identities and statuses from the E2 manager.", response = RanDetailsTransport.class, responseContainer = "List")
+       @GetMapping(RAN_METHOD)
+       @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
+       public List<RanDetailsTransport> getRanDetails() {
+               logger.debug("getRanDetails");
+               List<NodebIdentity> nodebIdList = e2NodebApi.getNodebIdList();
+               List<RanDetailsTransport> details = new ArrayList<>();
+               for (NodebIdentity nbid : nodebIdList) {
+                       GetNodebResponse nbResp = null;
+                       try {
+                               // Catch exceptions to keep looping despite failures
+                               nbResp = e2NodebApi.getNb(nbid.getInventoryName());
+                       } catch (HttpStatusCodeException ex) {
+                               logger.warn("E2 getNb failed for name {}: {}", nbid.getInventoryName(), ex.toString());
+                               nbResp = new GetNodebResponse().connectionStatus("UNKNOWN").ip("UNKNOWN").port(-1)
+                                               .ranName(nbid.getInventoryName());
+                       }
+                       details.add(new RanDetailsTransport(nbid, nbResp));
+               }
+               return details;
        }
 
-       @ApiOperation(value = "Gets the unique requests submitted to the E2 manager.", response = SetupRequest.class, responseContainer = "List")
-       @RequestMapping(value = "/setup", method = RequestMethod.GET)
-       public Iterable<SetupRequest> getRequests() {
-               logger.debug("getRequests");
-               return requests;
+       @ApiOperation(value = "Get RAN identities list.", response = NodebIdentity.class, responseContainer = "List")
+       @GetMapping(NODEB_LIST_METHOD)
+       @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
+       public List<NodebIdentity> getNodebIdList() {
+               logger.debug("getNodebIdList");
+               return e2NodebApi.getNodebIdList();
        }
 
-       @ApiOperation(value = "Sets up a RAN connection via the E2 manager.")
-       @RequestMapping(value = "/setup", method = RequestMethod.POST)
-       public void setup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
-               logger.debug("setup {}", setupRequest);
-               try {
-                       assertNotEmpty(setupRequest.getRanIp());
-                       assertNotEmpty(setupRequest.getRanName());
-                       assertNotNull(setupRequest.getRanPort());
-               } catch (Exception ex) {
-                       logger.error("Bad request", ex);
-                       response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
-               }
-               try {
-                       requests.add(setupRequest);
-                       e2ManagerApi.setup(setupRequest);
-               } catch (Exception ex) {
-                       logger.error("Failed", ex);
-                       response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
-               }
+       @ApiOperation(value = "Get RAN by name.", response = GetNodebResponse.class)
+       @GetMapping(NODEB_METHOD + "/{" + PP_RANNAME + "}")
+       @Secured({ DashboardConstants.ROLE_ADMIN, DashboardConstants.ROLE_STANDARD })
+       public GetNodebResponse getNb(@PathVariable(PP_RANNAME) String ranName) {
+               logger.debug("getNb {}", ranName);
+               return e2NodebApi.getNb(ranName);
+       }
+
+       @ApiOperation(value = "Close all connections to the RANs and delete the data from the nodeb-rnib DB.")
+       @DeleteMapping(NODEB_METHOD)
+       @Secured({ DashboardConstants.ROLE_ADMIN })
+       public void nodebDelete(HttpServletResponse response) {
+               logger.debug("nodebDelete");
+               e2NodebApi.nodebDelete();
+               response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
+       }
+
+       @ApiOperation(value = "Sets up an EN-DC RAN connection via the E2 manager.")
+       @PostMapping(ENDC_SETUP_METHOD)
+       @Secured({ DashboardConstants.ROLE_ADMIN })
+       public void endcSetup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
+               logger.debug("endcSetup {}", setupRequest);
+               e2NodebApi.endcSetup(setupRequest);
+               response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
+       }
+
+       @ApiOperation(value = "Sets up an X2 RAN connection via the E2 manager.")
+       @PostMapping(X2_SETUP_METHOD)
+       @Secured({ DashboardConstants.ROLE_ADMIN })
+       public void x2Setup(@RequestBody SetupRequest setupRequest, HttpServletResponse response) {
+               logger.debug("x2Setup {}", setupRequest);
+               e2NodebApi.x2Setup(setupRequest);
+               response.setStatus(e2NodebApi.getApiClient().getStatusCode().value());
        }
 
 }