SRM refactoring
authorFaseela K <faseela.k@ericsson.com>
Fri, 23 Feb 2018 12:27:19 +0000 (17:57 +0530)
committerFaseela K <faseela.k@ericsson.com>
Wed, 28 Feb 2018 09:26:40 +0000 (14:56 +0530)
Realized that some code that went in interface-manager for service
recovery can be reused across modules, and hence moving some of those
common utilities to srm.

Change-Id: I65dbb9b27bddbe11f7ca4a30a3a27838eef8cefc
Signed-off-by: Faseela K <faseela.k@ericsson.com>
api/src/main/java/org/opendaylight/genius/srm/RecoverableListener.java [new file with mode: 0644]
api/src/main/java/org/opendaylight/genius/srm/ServiceRecoveryInterface.java [new file with mode: 0644]
api/src/main/java/org/opendaylight/genius/srm/ServiceRecoveryRegistry.java [new file with mode: 0644]
impl/pom.xml
impl/src/main/java/org/opendaylight/genius/srm/impl/ServiceRecoveryListener.java [new file with mode: 0644]
impl/src/main/java/org/opendaylight/genius/srm/impl/ServiceRecoveryManager.java [new file with mode: 0644]
impl/src/main/java/org/opendaylight/genius/srm/impl/ServiceRecoveryRegistryImpl.java [new file with mode: 0644]
impl/src/main/java/org/opendaylight/genius/srm/impl/SrmRpcProvider.java [moved from impl/src/main/java/org/opendaylight/genius/srm/SrmRpcProvider.java with 98% similarity]
impl/src/main/java/org/opendaylight/genius/srm/impl/SrmRpcUtils.java [moved from impl/src/main/java/org/opendaylight/genius/srm/SrmRpcUtils.java with 99% similarity]

diff --git a/api/src/main/java/org/opendaylight/genius/srm/RecoverableListener.java b/api/src/main/java/org/opendaylight/genius/srm/RecoverableListener.java
new file mode 100644 (file)
index 0000000..1c4a790
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. 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.genius.srm;
+
+public interface RecoverableListener {
+    /**
+     * register a recoverable listener.
+     */
+    void registerListener();
+
+    /**
+     * Deregister a recoverable listener.
+     */
+    void deregisterListener();
+}
\ No newline at end of file
diff --git a/api/src/main/java/org/opendaylight/genius/srm/ServiceRecoveryInterface.java b/api/src/main/java/org/opendaylight/genius/srm/ServiceRecoveryInterface.java
new file mode 100644 (file)
index 0000000..f732223
--- /dev/null
@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. 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.genius.srm;
+
+public interface ServiceRecoveryInterface {
+    /**
+     * Initiates recovery mechanism for a particular interface-manager entity.
+     *
+     * @param entityId
+     *            The unique identifier for the service instance.
+     */
+    void recoverService(String entityId);
+}
\ No newline at end of file
diff --git a/api/src/main/java/org/opendaylight/genius/srm/ServiceRecoveryRegistry.java b/api/src/main/java/org/opendaylight/genius/srm/ServiceRecoveryRegistry.java
new file mode 100644 (file)
index 0000000..ea1bab2
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. 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.genius.srm;
+
+import java.util.List;
+
+public interface ServiceRecoveryRegistry {
+
+    void registerServiceRecoveryRegistry(String entityName,
+                                         ServiceRecoveryInterface serviceRecoveryHandler);
+
+    void addRecoverableListener(String serviceName, RecoverableListener recoverableListener);
+
+    void removeRecoverableListener(String serviceName, RecoverableListener recoverableListener);
+
+    List<RecoverableListener> getRecoverableListeners(String serviceName);
+}
\ No newline at end of file
index 9b5b4226c6197fcdf1064f6eb663c12dd11bb347..55f3eb61b6aa67f4127460d5b8c572a489528fcc 100644 (file)
@@ -45,7 +45,11 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
       <groupId>javax.inject</groupId>
       <artifactId>javax.inject</artifactId>
     </dependency>
-
+    <dependency>
+      <groupId>org.ops4j.pax.cdi</groupId>
+      <artifactId>pax-cdi-api</artifactId>
+      <optional>true</optional>
+    </dependency>
   </dependencies>
 
   <build>
diff --git a/impl/src/main/java/org/opendaylight/genius/srm/impl/ServiceRecoveryListener.java b/impl/src/main/java/org/opendaylight/genius/srm/impl/ServiceRecoveryListener.java
new file mode 100644 (file)
index 0000000..be7923d
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. 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.genius.srm.impl;
+
+import javax.annotation.Nonnull;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.genius.datastoreutils.listeners.AbstractClusteredSyncDataTreeChangeListener;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.srm.ops.rev170711.ServiceOps;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.srm.ops.rev170711.service.ops.Services;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.srm.ops.rev170711.service.ops.services.Operations;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Singleton
+public class ServiceRecoveryListener extends AbstractClusteredSyncDataTreeChangeListener<Operations> {
+
+    private static final Logger LOG = LoggerFactory.getLogger(ServiceRecoveryListener.class);
+
+    private final ServiceRecoveryManager serviceRecoveryManager;
+
+    @Inject
+    public ServiceRecoveryListener(DataBroker dataBroker, ServiceRecoveryManager serviceRecoveryManager) {
+        super(dataBroker, LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(ServiceOps.class)
+                .child(Services.class).child(Operations.class));
+        this.serviceRecoveryManager = serviceRecoveryManager;
+    }
+
+    @Override
+    public void add(@Nonnull Operations operations) {
+        LOG.info("Service Recovery operation triggered for service: {}", operations);
+        serviceRecoveryManager.recoverService(operations.getEntityType(), operations.getEntityName(),
+                operations.getEntityId());
+    }
+
+    @Override
+    public void remove(@Nonnull Operations removedDataObject) {
+
+    }
+
+    @Override
+    public void update(@Nonnull Operations originalDataObject, @Nonnull Operations updatedDataObject) {
+        add(updatedDataObject);
+    }
+}
diff --git a/impl/src/main/java/org/opendaylight/genius/srm/impl/ServiceRecoveryManager.java b/impl/src/main/java/org/opendaylight/genius/srm/impl/ServiceRecoveryManager.java
new file mode 100644 (file)
index 0000000..1040bb6
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. 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.genius.srm.impl;
+
+import javax.inject.Inject;
+import javax.inject.Singleton;
+
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.srm.types.rev170711.EntityNameBase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.srm.types.rev170711.EntityTypeBase;
+
+@Singleton
+public final class ServiceRecoveryManager {
+
+    private final ServiceRecoveryRegistryImpl serviceRecoveryRegistry;
+
+    @Inject
+    public ServiceRecoveryManager(ServiceRecoveryRegistryImpl serviceRecoveryRegistry) {
+        this.serviceRecoveryRegistry = serviceRecoveryRegistry;
+    }
+
+    private String getServiceRegistryKey(Class<? extends EntityNameBase> entityName) {
+        return entityName.toString();
+    }
+
+    /**
+     * Initiates recovery mechanism for a particular interface-manager entity.
+     * This method tries to check whether there is a registered handler for the incoming
+     * service recovery request within interface-manager and redirects the call
+     * to the respective handler if found.
+     *  @param entityType
+     *            The type of service recovery. eg :SERVICE or INSTANCE.
+     * @param entityName
+     *            The type entity for which recovery has to be started. eg : INTERFACE or DPN.
+     * @param entityId
+     *            The unique id to represent the entity to be recovered
+     */
+    public void recoverService(Class<? extends EntityTypeBase> entityType,
+                                      Class<? extends EntityNameBase> entityName, String entityId) {
+        String serviceRegistryKey = getServiceRegistryKey(entityName);
+        serviceRecoveryRegistry.getRegisteredServiceRecoveryHandler(serviceRegistryKey).recoverService(entityId);
+    }
+}
diff --git a/impl/src/main/java/org/opendaylight/genius/srm/impl/ServiceRecoveryRegistryImpl.java b/impl/src/main/java/org/opendaylight/genius/srm/impl/ServiceRecoveryRegistryImpl.java
new file mode 100644 (file)
index 0000000..0cc256f
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2018 Ericsson India Global Services Pvt Ltd. 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.genius.srm.impl;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import javax.inject.Singleton;
+
+import org.opendaylight.genius.srm.RecoverableListener;
+import org.opendaylight.genius.srm.ServiceRecoveryInterface;
+import org.opendaylight.genius.srm.ServiceRecoveryRegistry;
+import org.ops4j.pax.cdi.api.OsgiServiceProvider;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Singleton
+@OsgiServiceProvider(classes = ServiceRecoveryRegistry.class)
+public class ServiceRecoveryRegistryImpl implements ServiceRecoveryRegistry {
+
+    private static final Logger LOG = LoggerFactory.getLogger(ServiceRecoveryRegistryImpl.class);
+
+    private final Map<String, ServiceRecoveryInterface> serviceRecoveryRegistry = new ConcurrentHashMap<>();
+
+    private final Map<String, List<RecoverableListener>> recoverableListenersMap =
+            new ConcurrentHashMap<String, List<RecoverableListener>>();
+
+    @Override
+    public void registerServiceRecoveryRegistry(String entityName,
+                                                ServiceRecoveryInterface serviceRecoveryHandler) {
+        serviceRecoveryRegistry.put(entityName, serviceRecoveryHandler);
+        LOG.trace("Registered service recovery handler for {}", entityName);
+    }
+
+    @Override
+    public synchronized void addRecoverableListener(String serviceName, final RecoverableListener recoverableListener) {
+        List<RecoverableListener> recoverableListeners = recoverableListenersMap.get(serviceName);
+        if (recoverableListeners == null) {
+            recoverableListeners = Collections.synchronizedList(new ArrayList<>());
+            recoverableListenersMap.put(serviceName, recoverableListeners);
+        }
+
+        recoverableListeners.add(recoverableListener);
+    }
+
+    @Override
+    public void removeRecoverableListener(String serviceName, final RecoverableListener recoverableListener) {
+        if (recoverableListenersMap.get(serviceName) != null) {
+            recoverableListenersMap.get(serviceName).remove(recoverableListener);
+        }
+    }
+
+    @Override
+    public List<RecoverableListener> getRecoverableListeners(String serviceName) {
+        return recoverableListenersMap.get(serviceName);
+    }
+
+    ServiceRecoveryInterface getRegisteredServiceRecoveryHandler(String entityName) {
+        return serviceRecoveryRegistry.get(entityName);
+    }
+}
\ No newline at end of file
similarity index 98%
rename from impl/src/main/java/org/opendaylight/genius/srm/SrmRpcProvider.java
rename to impl/src/main/java/org/opendaylight/genius/srm/impl/SrmRpcProvider.java
index a612b4de5437ec4d021046da586ac90475a7ca9c..d758d645e7cf3e709a5ee8d0b5a0f6d5eaeddb05 100644 (file)
@@ -6,7 +6,7 @@
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 
-package org.opendaylight.genius.srm;
+package org.opendaylight.genius.srm.impl;
 
 import com.google.common.util.concurrent.Futures;
 import java.util.concurrent.Future;
similarity index 99%
rename from impl/src/main/java/org/opendaylight/genius/srm/SrmRpcUtils.java
rename to impl/src/main/java/org/opendaylight/genius/srm/impl/SrmRpcUtils.java
index 9267ca5e06913146f869cf1d02133b42ca6515c9..ff159ca2ce0fbfa4b1f3d30a76ab539f8f48a450 100644 (file)
@@ -6,7 +6,7 @@
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
 
-package org.opendaylight.genius.srm;
+package org.opendaylight.genius.srm.impl;
 
 import com.google.common.collect.ImmutableMap;
 import java.util.concurrent.ExecutionException;