Add update method of handlers for REST services 58/65458/6
authorJakub Toth <jakub.toth@pantheon.tech>
Sun, 12 Nov 2017 17:04:52 +0000 (18:04 +0100)
committerJakub Toth <jakub.toth@pantheon.tech>
Mon, 20 Nov 2017 23:36:44 +0000 (00:36 +0100)
Change-Id: I64a7f7a47f43566a50839ce1a83ba0fdd4034ed9
Signed-off-by: Jakub Toth <jakub.toth@pantheon.tech>
14 files changed:
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/RestConnectorProvider.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/api/RestconfDataService.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/api/RestconfInvokeOperationsService.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/api/RestconfStreamsSubscriptionService.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfInvokeOperationsServiceImpl.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfStreamsSubscriptionServiceImpl.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/services/simple/api/RestconfOperationsService.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/services/simple/api/RestconfSchemaService.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/services/simple/api/RestconfService.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/services/simple/api/UpdateHandlers.java [new file with mode: 0644]
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/services/simple/impl/RestconfImpl.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/services/simple/impl/RestconfOperationsServiceImpl.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/services/simple/impl/RestconfSchemaServiceImpl.java

index 6ad199e7aceb1b4e1ab99d0cedec417e67194d12..be7d7a878ec228444f7aa0cc447baaca522e411b 100644 (file)
@@ -9,6 +9,9 @@
 package org.opendaylight.restconf.nb.rfc8040;
 
 import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSet.Builder;
+import java.util.Set;
 import org.opendaylight.controller.md.sal.common.api.data.AsyncTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionChain;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
@@ -61,16 +64,26 @@ public class RestConnectorProvider<T extends ServiceWrapper> implements Restconf
     private final DOMRpcService rpcService;
     private final DOMNotificationService notificationService;
     private final DOMMountPointService mountPointService;
-    private final T wrapperServices;
+    private final Builder<Object> servicesProperties;
 
     private ListenerRegistration<SchemaContextListener> listenerRegistration;
     private SchemaContextHandler schemaCtxHandler;
+    private T wrapperServices;
 
+    // FIXME: refactor this class and its users to interact via builder pattern, where individual
+    // services are injected and then the provider is created
     public RestConnectorProvider(final DOMDataBroker domDataBroker,
             final SchemaService schemaService, final DOMRpcService rpcService,
+            final DOMNotificationService notificationService, final DOMMountPointService mountPointService) {
+        this(domDataBroker, schemaService, rpcService, notificationService, mountPointService, null);
+    }
+
+    public RestConnectorProvider(final DOMDataBroker domDataBroker, final SchemaService schemaService,
+            final DOMRpcService rpcService,
             final DOMNotificationService notificationService, final DOMMountPointService mountPointService,
             final T wrapperServices) {
-        this.wrapperServices = Preconditions.checkNotNull(wrapperServices);
+        this.servicesProperties = ImmutableSet.<Object>builder();
+        this.wrapperServices = wrapperServices;
         this.schemaService = Preconditions.checkNotNull(schemaService);
         this.rpcService = Preconditions.checkNotNull(rpcService);
         this.notificationService = Preconditions.checkNotNull(notificationService);
@@ -79,25 +92,33 @@ public class RestConnectorProvider<T extends ServiceWrapper> implements Restconf
         RestConnectorProvider.dataBroker = Preconditions.checkNotNull(domDataBroker);
     }
 
-    public void start() {
+    public synchronized void start() {
         mountPointServiceHandler = new DOMMountPointServiceHandler(mountPointService);
+        servicesProperties.add(mountPointServiceHandler);
 
         final DOMDataBrokerHandler brokerHandler = new DOMDataBrokerHandler(dataBroker);
+        servicesProperties.add(brokerHandler);
 
         RestConnectorProvider.transactionChainHandler = new TransactionChainHandler(dataBroker
                 .createTransactionChain(RestConnectorProvider.TRANSACTION_CHAIN_LISTENER));
+        servicesProperties.add(transactionChainHandler);
 
         this.schemaCtxHandler = new SchemaContextHandler(transactionChainHandler);
+        servicesProperties.add(schemaCtxHandler);
         this.listenerRegistration = schemaService.registerSchemaContextListener(this.schemaCtxHandler);
 
         final RpcServiceHandler rpcServiceHandler = new RpcServiceHandler(rpcService);
+        servicesProperties.add(rpcServiceHandler);
 
         final NotificationServiceHandler notificationServiceHandler =
                 new NotificationServiceHandler(notificationService);
+        servicesProperties.add(notificationServiceHandler);
 
-        wrapperServices.setHandlers(this.schemaCtxHandler, RestConnectorProvider.mountPointServiceHandler,
+        if (wrapperServices != null) {
+            wrapperServices.setHandlers(this.schemaCtxHandler, RestConnectorProvider.mountPointServiceHandler,
                 RestConnectorProvider.transactionChainHandler, brokerHandler, rpcServiceHandler,
                 notificationServiceHandler);
+        }
     }
 
     public DOMMountPointServiceHandler getMountPointServiceHandler() {
@@ -143,4 +164,8 @@ public class RestConnectorProvider<T extends ServiceWrapper> implements Restconf
         mountPointServiceHandler = null;
         dataBroker = null;
     }
+
+    public final synchronized Set<Object> getServicesProperties() {
+        return servicesProperties.build();
+    }
 }
index 46314c57663106234a1324c47ce70f948c9bc099..87407ac46308a595ccb3ca039ac093d211e29e2b 100644 (file)
@@ -25,6 +25,7 @@ import org.opendaylight.restconf.common.patch.Patch;
 import org.opendaylight.restconf.common.patch.PatchContext;
 import org.opendaylight.restconf.common.patch.PatchStatusContext;
 import org.opendaylight.restconf.nb.rfc8040.Rfc8040;
+import org.opendaylight.restconf.nb.rfc8040.services.simple.api.UpdateHandlers;
 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
 
 /**
@@ -32,7 +33,7 @@ import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
  * is a collection of configuration data and state data nodes.
  *
  */
-public interface RestconfDataService {
+public interface RestconfDataService extends UpdateHandlers {
 
     /**
      * Get target data resource.
index 1de502b25cfdd66ce7a06c651f968592b822450f..faa0a4a1e722b2b7ac0edfcb312fe9ffec1a2803 100644 (file)
@@ -18,6 +18,7 @@ import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.UriInfo;
 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.nb.rfc8040.Rfc8040;
+import org.opendaylight.restconf.nb.rfc8040.services.simple.api.UpdateHandlers;
 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
 
 /**
@@ -25,7 +26,7 @@ import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
  * "rpc" statement. It is invoked using a POST method on the operation resource.
  *
  */
-public interface RestconfInvokeOperationsService {
+public interface RestconfInvokeOperationsService extends UpdateHandlers {
 
     /**
      * Invoke RPC operation.
index d131c2c80a7d59f04136d617d0da06d56bf1c782..978e3f1f280139a395cf8bf0735f07ad16a8f272 100644 (file)
@@ -14,12 +14,13 @@ import javax.ws.rs.PathParam;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.UriInfo;
 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
+import org.opendaylight.restconf.nb.rfc8040.services.simple.api.UpdateHandlers;
 
 /**
  * Subscribing to streams.
  *
  */
-public interface RestconfStreamsSubscriptionService {
+public interface RestconfStreamsSubscriptionService extends UpdateHandlers {
 
     /**
      * Subscribing to receive notification from stream support.
index a1424316e07492ec4a641268ccdf6ed358a177c0..0e8971ff4cac835b7619d96b3c104b87a59b0197 100644 (file)
@@ -61,9 +61,9 @@ public class RestconfDataServiceImpl implements RestconfDataService {
     private static final Logger LOG = LoggerFactory.getLogger(RestconfDataServiceImpl.class);
     private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss");
 
-    private final SchemaContextHandler schemaContextHandler;
-    private final TransactionChainHandler transactionChainHandler;
-    private final DOMMountPointServiceHandler mountPointServiceHandler;
+    private SchemaContextHandler schemaContextHandler;
+    private TransactionChainHandler transactionChainHandler;
+    private DOMMountPointServiceHandler mountPointServiceHandler;
 
     private final RestconfStreamsSubscriptionService delegRestconfSubscrService;
 
@@ -77,6 +77,19 @@ public class RestconfDataServiceImpl implements RestconfDataService {
         this.delegRestconfSubscrService = delegRestconfSubscrService;
     }
 
+    @Override
+    public synchronized void updateHandlers(final Object... handlers) {
+        for (final Object object : handlers) {
+            if (object instanceof SchemaContextHandler) {
+                schemaContextHandler = (SchemaContextHandler) object;
+            } else if (object instanceof DOMMountPointServiceHandler) {
+                mountPointServiceHandler = (DOMMountPointServiceHandler) object;
+            } else if (object instanceof TransactionChainHandler) {
+                transactionChainHandler = (TransactionChainHandler) object;
+            }
+        }
+    }
+
     @Override
     public Response readData(final UriInfo uriInfo) {
         return readData(null, uriInfo);
index 71c6c6c3b0a19cb48db3922ccc41534dd3732879..34c76dc3ae63d4d2737923b75e323bcd8c5a353d 100644 (file)
@@ -33,8 +33,8 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath;
  */
 public class RestconfInvokeOperationsServiceImpl implements RestconfInvokeOperationsService {
 
-    private final RpcServiceHandler rpcServiceHandler;
-    private final SchemaContextHandler schemaContextHandler;
+    private RpcServiceHandler rpcServiceHandler;
+    private SchemaContextHandler schemaContextHandler;
 
     public RestconfInvokeOperationsServiceImpl(final RpcServiceHandler rpcServiceHandler,
             final SchemaContextHandler schemaContextHandler) {
@@ -42,6 +42,17 @@ public class RestconfInvokeOperationsServiceImpl implements RestconfInvokeOperat
         this.schemaContextHandler = schemaContextHandler;
     }
 
+    @Override
+    public synchronized void updateHandlers(final Object... handlers) {
+        for (final Object object : handlers) {
+            if (object instanceof SchemaContextHandler) {
+                schemaContextHandler = (SchemaContextHandler) object;
+            } else if (object instanceof RpcServiceHandler) {
+                rpcServiceHandler = (RpcServiceHandler) object;
+            }
+        }
+    }
+
     @Override
     public NormalizedNodeContext invokeRpc(final String identifier, final NormalizedNodeContext payload,
                                            final UriInfo uriInfo) {
index 547c318ff33d8febcd032b101342f167bce7bc8b..f106fc634a6799510e0f53eb2b8e429c2524da7c 100644 (file)
@@ -46,7 +46,7 @@ public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSu
 
     private static final Logger LOG = LoggerFactory.getLogger(RestconfStreamsSubscriptionServiceImpl.class);
 
-    private final HandlersHolder handlersHolder;
+    private HandlersHolder handlersHolder;
 
     /**
      * Initialize holder of handlers with holders as parameters.
@@ -67,6 +67,15 @@ public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSu
                 transactionChainHandler, schemaHandler);
     }
 
+    @Override
+    public synchronized void updateHandlers(final Object... handlers) {
+        for (final Object object : handlers) {
+            if (object instanceof HandlersHolder) {
+                handlersHolder = (HandlersHolder) object;
+            }
+        }
+    }
+
     @Override
     public NormalizedNodeContext subscribeToStream(final String identifier, final UriInfo uriInfo) {
         final NotificationQueryParams notificationQueryParams = NotificationQueryParams.fromUriInfo(uriInfo);
index c313277eca8da3ab25b431c11d2cd151738f4942..3027def8e0720dbe4ae5b850f61082403633fef9 100644 (file)
@@ -23,7 +23,7 @@ import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
  * supported by the server.
  *
  */
-public interface RestconfOperationsService {
+public interface RestconfOperationsService extends UpdateHandlers {
 
     /**
      * List of rpc or action operations supported by the server.
index 2a87fd41d4d0a5ba469fcafbac489f879b7ad7ef..cffb2e9e61f0f4f7b1a5812621aba3a7479bec3d 100644 (file)
@@ -20,7 +20,7 @@ import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
  *
  */
 @Path("/")
-public interface RestconfSchemaService {
+public interface RestconfSchemaService extends UpdateHandlers {
 
     /**
      * Get schema of specific module.
index cd028fd86ac6bd86be887e2b087510f94c0665dd..844669eb89e8ab0b867e2ca5c4ed25a2f8c9a635 100644 (file)
@@ -19,7 +19,7 @@ import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
  * Service for getting yang library version.
  *
  */
-public interface RestconfService {
+public interface RestconfService extends UpdateHandlers {
 
     /**
      * Get yang library version.
diff --git a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/services/simple/api/UpdateHandlers.java b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/services/simple/api/UpdateHandlers.java
new file mode 100644 (file)
index 0000000..0dfcfac
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2017 Pantheon technologies s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.restconf.nb.rfc8040.services.simple.api;
+
+/**
+ * Allow update of handlers in web application services, if needed.
+ */
+public interface UpdateHandlers {
+
+    /**
+     * Update method for handlers in specific service (resource) of web application.
+     * Has to be implemented as synchronized to avoid conflict of update variables in multithreaded application.
+     *
+     * @param handlers
+     *            array of handlers
+     */
+    default void updateHandlers(final Object... handlers) {
+        throw new UnsupportedOperationException("This method it's not allowed for this service by default.");
+    }
+}
index bb84a0921679bbcc90f8fbc6dad81f14e10e9e2e..386c991ad387e6ecd34e11fb2104a4b68ad8cff7 100644 (file)
@@ -25,7 +25,7 @@ import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 
 public class RestconfImpl implements RestconfService {
 
-    private final SchemaContextHandler schemaContextHandler;
+    private SchemaContextHandler schemaContextHandler;
 
     public RestconfImpl(final SchemaContextHandler schemaContextHandler) {
         this.schemaContextHandler = schemaContextHandler;
@@ -51,4 +51,13 @@ public class RestconfImpl implements RestconfService {
                 Builders.leafBuilder((LeafSchemaNode) schemaNode).withValue(IetfYangLibrary.REVISION).build();
         return new NormalizedNodeContext(iid, data);
     }
+
+    @Override
+    public synchronized void updateHandlers(final Object... handlers) {
+        for (final Object object : handlers) {
+            if (object instanceof SchemaContextHandler) {
+                schemaContextHandler = (SchemaContextHandler) object;
+            }
+        }
+    }
 }
index d2b383bad1721b358b32681414ca3b8ae63deaaf..93c99146c49bf1a43ef030ad5b7b8b01342677ea 100644 (file)
@@ -46,8 +46,8 @@ public class RestconfOperationsServiceImpl implements RestconfOperationsService
 
     private static final Logger LOG = LoggerFactory.getLogger(RestconfOperationsServiceImpl.class);
 
-    private final SchemaContextHandler schemaContextHandler;
-    private final DOMMountPointServiceHandler domMountPointServiceHandler;
+    private SchemaContextHandler schemaContextHandler;
+    private DOMMountPointServiceHandler domMountPointServiceHandler;
 
     /**
      * Set {@link SchemaContextHandler} for getting actual {@link SchemaContext}.
@@ -63,6 +63,17 @@ public class RestconfOperationsServiceImpl implements RestconfOperationsService
         this.domMountPointServiceHandler = domMountPointServiceHandler;
     }
 
+    @Override
+    public synchronized void updateHandlers(final Object... handlers) {
+        for (final Object object : handlers) {
+            if (object instanceof SchemaContextHandler) {
+                schemaContextHandler = (SchemaContextHandler) object;
+            } else if (object instanceof DOMMountPointServiceHandler) {
+                domMountPointServiceHandler = (DOMMountPointServiceHandler) object;
+            }
+        }
+    }
+
     @Override
     public NormalizedNodeContext getOperations(final UriInfo uriInfo) {
         final SchemaContextRef ref = new SchemaContextRef(this.schemaContextHandler.get());
index e19e9db38945b78de8b1e349d102f30dfdfcd1c3..231b436c5e2e6d8400ee8701f6d91d6c8839750d 100644 (file)
@@ -21,8 +21,8 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext;
  */
 public class RestconfSchemaServiceImpl implements RestconfSchemaService {
 
-    private final SchemaContextHandler schemaContextHandler;
-    private final DOMMountPointServiceHandler domMountPointServiceHandler;
+    private SchemaContextHandler schemaContextHandler;
+    private DOMMountPointServiceHandler domMountPointServiceHandler;
 
     /**
      * Set {@link SchemaContextHandler} for getting actual {@link SchemaContext}
@@ -45,4 +45,15 @@ public class RestconfSchemaServiceImpl implements RestconfSchemaService {
         return ParserIdentifier.toSchemaExportContextFromIdentifier(schemaContextRef.get(), identifier,
                 this.domMountPointServiceHandler.get());
     }
+
+    @Override
+    public synchronized void updateHandlers(final Object... handlers) {
+        for (final Object object : handlers) {
+            if (object instanceof SchemaContextHandler) {
+                schemaContextHandler = (SchemaContextHandler) object;
+            } else if (object instanceof DOMMountPointServiceHandler) {
+                domMountPointServiceHandler = (DOMMountPointServiceHandler) object;
+            }
+        }
+    }
 }