Support for network service for alto-northbound
[alto.git] / alto-northbound / src / main / java / org / opendaylight / alto / northbound / AltoNorthbound.java
index 0c29ab5d472d982901468f1a6d190f232a170bc9..09abd6621fe4fa3a9fc89413c6e45f640bccfb18 100644 (file)
@@ -27,12 +27,18 @@ import org.opendaylight.alto.commons.types.rfc7285.RFC7285CostMap;
 import org.opendaylight.alto.commons.types.rfc7285.RFC7285Endpoint;
 
 import org.opendaylight.alto.services.api.rfc7285.AltoService;
+import org.opendaylight.alto.services.api.rfc7285.IRDService;
+import org.opendaylight.alto.services.api.rfc7285.NetworkMapService;
+import org.opendaylight.alto.services.api.rfc7285.CostMapService;
+import org.opendaylight.alto.services.api.rfc7285.EndpointCostService;
+import org.opendaylight.alto.services.api.rfc7285.EndpointPropertyService;
 
-import org.opendaylight.alto.ext.fake.FakeAltoService;
 
 import org.opendaylight.alto.northbound.exception.AltoBasicException;
 import org.opendaylight.alto.northbound.exception.AltoBadFormatException;
 
+import org.opendaylight.controller.sal.utils.ServiceHelper;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -41,11 +47,18 @@ public class AltoNorthbound {
 
     private static final Logger logger = LoggerFactory.getLogger(AltoNorthbound.class);
 
-    private AltoService altoService = new FakeAltoService();
     private RFC7285JSONMapper mapper = new RFC7285JSONMapper();
 
-    private void checkAltoService() throws Exception {
-        if (altoService == null)
+    private <E> E getService(Class<E> clazz) {
+        E service = (E)ServiceHelper.getGlobalInstance(clazz, this);
+        if (service == null) {
+            service = (E)ServiceHelper.getGlobalInstance(AltoService.class, this);
+        }
+        return service;
+    }
+
+    private void checkService(Object service) throws Exception {
+        if (service == null)
             throw new AltoBasicException(Status.SERVICE_UNAVAILABLE, null);
     }
 
@@ -85,9 +98,10 @@ public class AltoNorthbound {
     @GET
     @Produces({ MediaType.ALTO_DIRECTORY, MediaType.ALTO_ERROR })
     public Response retrieveIRD() throws Exception {
-        checkAltoService();
+        IRDService service = getService(IRDService.class);
+        checkService(service);
 
-        RFC7285IRD ird = altoService.getDefaultIRD();
+        RFC7285IRD ird = service.getDefaultIRD();
         if (ird == null)
             return fail(Status.NOT_FOUND, null);
         return success(ird, MediaType.ALTO_DIRECTORY);
@@ -98,10 +112,11 @@ public class AltoNorthbound {
     @Produces({ MediaType.ALTO_DIRECTORY, MediaType.ALTO_ERROR })
     public Response retrieveIRD(
             @PathParam("id") String id) throws Exception {
-        checkAltoService();
+        IRDService service = getService(IRDService.class);
+        checkService(service);
         checkResourceId(id);
 
-        RFC7285IRD ird = altoService.getIRD(id);
+        RFC7285IRD ird = service.getIRD(id);
         if (ird == null)
             return fail(Status.NOT_FOUND, id);
         return success(ird, MediaType.ALTO_DIRECTORY);
@@ -111,9 +126,10 @@ public class AltoNorthbound {
     @GET
     @Produces({ MediaType.ALTO_NETWORKMAP, MediaType.ALTO_ERROR })
     public Response retrieveDefaultNetworkMap() throws Exception {
-        checkAltoService();
+        NetworkMapService service = getService(NetworkMapService.class);
+        checkService(service);
 
-        RFC7285NetworkMap map = altoService.getDefaultNetworkMap();
+        RFC7285NetworkMap map = service.getDefaultNetworkMap();
         if (map == null)
             return fail(Status.NOT_FOUND, null);
         return success(map, MediaType.ALTO_NETWORKMAP);
@@ -124,10 +140,11 @@ public class AltoNorthbound {
     @Produces({ MediaType.ALTO_NETWORKMAP, MediaType.ALTO_ERROR })
     public Response retrieveNetworkMap(
             @PathParam("id") String id) throws Exception {
-        checkAltoService();
+        NetworkMapService service = getService(NetworkMapService.class);
+        checkService(service);
         checkResourceId(id);
 
-        RFC7285NetworkMap map = altoService.getNetworkMap(id);
+        RFC7285NetworkMap map = service.getNetworkMap(id);
         if (map == null)
             return fail(Status.NOT_FOUND, id);
         return success(map, MediaType.ALTO_NETWORKMAP);
@@ -139,12 +156,13 @@ public class AltoNorthbound {
     public Response retrieveNetworkMap(
             @PathParam("id") String id,
             @PathParam("tag") String tag) throws Exception {
-        checkAltoService();
+        NetworkMapService service = getService(NetworkMapService.class);
+        checkService(service);
         checkResourceId(id);
         checkTag(tag);
 
-       RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
-        RFC7285NetworkMap map = altoService.getNetworkMap(vtag);
+        RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
+        RFC7285NetworkMap map = service.getNetworkMap(vtag);
         if (map == null)
             return fail(Status.NOT_FOUND, vtag);
         return success(map, MediaType.ALTO_NETWORKMAP);
@@ -154,10 +172,11 @@ public class AltoNorthbound {
     @GET
     @Produces({ MediaType.ALTO_COSTMAP, MediaType.ALTO_ERROR})
     public Response retrieveCostMap(@PathParam("id") String id) throws Exception {
-        checkAltoService();
+        CostMapService service = getService(CostMapService.class);
+        checkService(service);
         checkResourceId(id);
 
-        RFC7285CostMap map = altoService.getCostMap(id);
+        RFC7285CostMap map = service.getCostMap(id);
         if (map == null)
             return fail(Status.NOT_FOUND, id);
         return success(map, MediaType.ALTO_COSTMAP);
@@ -169,12 +188,13 @@ public class AltoNorthbound {
     public Response retrieveCostMap(
             @PathParam("id") String id,
             @PathParam("tag") String tag) throws Exception {
-        checkAltoService();
+        CostMapService service = getService(CostMapService.class);
+        checkService(service);
         checkResourceId(id);
         checkTag(tag);
 
-       RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
-        RFC7285CostMap map = altoService.getCostMap(vtag);
+        RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
+        RFC7285CostMap map = service.getCostMap(vtag);
         if (map == null)
             return fail(Status.NOT_FOUND, vtag);
         return success(map, MediaType.ALTO_COSTMAP);
@@ -187,13 +207,14 @@ public class AltoNorthbound {
             @PathParam("id") String id,
             @PathParam("mode") String mode,
             @PathParam("metric") String metric) throws Exception {
-        checkAltoService();
+        CostMapService service = getService(CostMapService.class);
+        checkService(service);
         checkResourceId(id);
 
         RFC7285CostType costType = new RFC7285CostType(mode, metric);
-        if (!altoService.supportCostType(id, costType))
+        if (!service.supportCostType(id, costType))
             return fail(Status.NOT_FOUND, costType);
-        RFC7285CostMap map = altoService.getCostMap(id, costType);
+        RFC7285CostMap map = service.getCostMap(id, costType);
         if (map == null)
             return fail(Status.NOT_FOUND, id);
         return success(map, MediaType.ALTO_COSTMAP);
@@ -207,15 +228,16 @@ public class AltoNorthbound {
             @PathParam("tag") String tag,
             @PathParam("mode") String mode,
             @PathParam("metric") String metric) throws Exception {
-        checkAltoService();
+        CostMapService service = getService(CostMapService.class);
+        checkService(service);
         checkResourceId(id);
         checkTag(tag);
 
-       RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
+        RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
         RFC7285CostType costType = new RFC7285CostType(mode, metric);
-        if (!altoService.supportCostType(vtag, costType))
+        if (!service.supportCostType(vtag, costType))
             return fail(Status.NOT_FOUND, costType);
-        RFC7285CostMap map = altoService.getCostMap(vtag, costType);
+        RFC7285CostMap map = service.getCostMap(vtag, costType);
         if (map == null)
             return fail(Status.NOT_FOUND, vtag);
         return success(map, MediaType.ALTO_COSTMAP);
@@ -227,14 +249,15 @@ public class AltoNorthbound {
     @Produces({ MediaType.ALTO_NETWORKMAP, MediaType.ALTO_ERROR})
     public Response retrieveFilteredNetworkMap(
             @PathParam("id") String id, String filterJSON) throws Exception {
-        checkAltoService();
+        NetworkMapService service = getService(NetworkMapService.class);
+        checkService(service);
         checkResourceId(id);
 
         RFC7285NetworkMap.Filter filter = mapper.asNetworkMapFilter(filterJSON);
 
-        if (!altoService.validateNetworkMapFilter(id, filter))
+        if (!service.validateNetworkMapFilter(id, filter))
             return fail(Status.BAD_REQUEST, filter);
-        RFC7285NetworkMap map = altoService.getNetworkMap(id, filter);
+        RFC7285NetworkMap map = service.getNetworkMap(id, filter);
         if (map == null)
             return fail(Status.NOT_FOUND, id);
         return success(map, MediaType.ALTO_NETWORKMAP);
@@ -248,16 +271,17 @@ public class AltoNorthbound {
             @PathParam("id") String id,
             @PathParam("tag") String tag,
             String filterJSON) throws Exception {
-        checkAltoService();
+        NetworkMapService service = getService(NetworkMapService.class);
+        checkService(service);
         checkResourceId(id);
         checkTag(tag);
 
-       RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
+        RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
         RFC7285NetworkMap.Filter filter = mapper.asNetworkMapFilter(filterJSON);
-        if (!altoService.validateNetworkMapFilter(vtag, filter))
+        if (!service.validateNetworkMapFilter(vtag, filter))
             return fail(Status.BAD_REQUEST, filter);
 
-        RFC7285NetworkMap map = altoService.getNetworkMap(vtag, filter);
+        RFC7285NetworkMap map = service.getNetworkMap(vtag, filter);
         if (map == null)
             return fail(Status.NOT_FOUND, vtag);
         return success(map, MediaType.ALTO_NETWORKMAP);
@@ -269,14 +293,15 @@ public class AltoNorthbound {
     @Produces({ MediaType.ALTO_COSTMAP, MediaType.ALTO_ERROR})
     public Response retrieveFilteredCostMap(
             @PathParam("id") String id, String filterJSON) throws Exception {
-        checkAltoService();
+        CostMapService service = getService(CostMapService.class);
+        checkService(service);
         checkResourceId(id);
 
         RFC7285CostMap.Filter filter = mapper.asCostMapFilter(filterJSON);
-        if (!altoService.validateCostMapFilter(id, filter))
+        if (!service.validateCostMapFilter(id, filter))
             return fail(Status.BAD_REQUEST, filter);
 
-        RFC7285CostMap map = altoService.getCostMap(id, filter);
+        RFC7285CostMap map = service.getCostMap(id, filter);
         if (map == null)
             return fail(Status.NOT_FOUND, id);
         return success(map, MediaType.ALTO_COSTMAP);
@@ -289,16 +314,17 @@ public class AltoNorthbound {
     public Response retrieveFilteredCostMap(
             @PathParam("id") String id,
             @PathParam("tag") String tag, String filterJSON) throws Exception {
-        checkAltoService();
+        CostMapService service = getService(CostMapService.class);
+        checkService(service);
         checkResourceId(id);
         checkTag(tag);
 
-       RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
+        RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
         RFC7285CostMap.Filter filter = mapper.asCostMapFilter(filterJSON);
-        if (!altoService.validateCostMapFilter(vtag, filter))
+        if (!service.validateCostMapFilter(vtag, filter))
             return fail(Status.BAD_REQUEST, filter);
 
-        RFC7285CostMap map = altoService.getCostMap(vtag, filter);
+        RFC7285CostMap map = service.getCostMap(vtag, filter);
         if (map == null)
             return fail(Status.NOT_FOUND, vtag);
         return success(map, MediaType.ALTO_COSTMAP);
@@ -311,11 +337,12 @@ public class AltoNorthbound {
     public Response retrieveEndpointPropMap(
             @PathParam("id") String id,
             String params) throws Exception {
-        checkAltoService();
+        EndpointPropertyService service = getService(EndpointPropertyService.class);
+        checkService(service);
         checkResourceId(id);
 
         RFC7285Endpoint.PropertyRequest request = mapper.asPropertyRequest(params);
-        RFC7285Endpoint.PropertyResponse response = altoService.getEndpointProperty(id, request);
+        RFC7285Endpoint.PropertyResponse response = service.getEndpointProperty(id, request);
         if (response == null)
             return fail(Status.NOT_FOUND, request);
         return success(response, MediaType.ALTO_ENDPOINT_PROP);
@@ -329,13 +356,14 @@ public class AltoNorthbound {
             @PathParam("id") String id,
             @PathParam("tag") String tag,
             String params) throws Exception {
-        checkAltoService();
+        EndpointPropertyService service = getService(EndpointPropertyService.class);
+        checkService(service);
         checkResourceId(id);
         checkTag(tag);
 
-       RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
+        RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
         RFC7285Endpoint.PropertyRequest request = mapper.asPropertyRequest(params);
-        RFC7285Endpoint.PropertyResponse response = altoService.getEndpointProperty(vtag, request);
+        RFC7285Endpoint.PropertyResponse response = service.getEndpointProperty(vtag, request);
         if (response == null)
             return fail(Status.NOT_FOUND, request);
         return success(response, MediaType.ALTO_ENDPOINT_PROP);
@@ -348,11 +376,12 @@ public class AltoNorthbound {
     public Response retrieveEndpointCostMap(
             @PathParam("id") String id,
             String params) throws Exception {
-        checkAltoService();
+        EndpointCostService service = getService(EndpointCostService.class);
+        checkService(service);
         checkResourceId(id);
 
         RFC7285Endpoint.CostRequest request = mapper.asCostRequest(params);
-        RFC7285Endpoint.CostResponse response = altoService.getEndpointCost(id, request);
+        RFC7285Endpoint.CostResponse response = service.getEndpointCost(id, request);
         if (response == null)
             return fail(Status.NOT_FOUND, request);
         return success(response, MediaType.ALTO_ENDPOINT_COST);
@@ -366,13 +395,14 @@ public class AltoNorthbound {
             @PathParam("id") String id,
             @PathParam("tag") String tag,
             String params) throws Exception {
-        checkAltoService();
+        EndpointCostService service = getService(EndpointCostService.class);
+        checkService(service);
         checkResourceId(id);
         checkTag(tag);
 
         RFC7285VersionTag vtag = new RFC7285VersionTag(id, tag);
         RFC7285Endpoint.CostRequest request = mapper.asCostRequest(params);
-        RFC7285Endpoint.CostResponse response = altoService.getEndpointCost(vtag, request);
+        RFC7285Endpoint.CostResponse response = service.getEndpointCost(vtag, request);
         if (response == null)
             return fail(Status.NOT_FOUND, request);
         return success(response, MediaType.ALTO_ENDPOINT_COST);