Proxy DOMMountPointServiceImpl to MD-SAL API 58/73858/3
authorTom Pantelis <tompantelis@gmail.com>
Mon, 9 Jul 2018 17:07:56 +0000 (13:07 -0400)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 11 Jul 2018 10:38:09 +0000 (12:38 +0200)
Change-Id: Ic16ce1a850340482688c492045101af45ca7e521
Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/md/sal/dom/api/DOMService.java
opendaylight/md-sal/sal-dom-api/src/main/java/org/opendaylight/controller/sal/core/api/mount/MountProvisionListener.java
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/mount/DOMMountPointServiceImpl.java
opendaylight/md-sal/sal-dom-broker/src/main/resources/org/opendaylight/blueprint/dom-broker.xml
opendaylight/md-sal/sal-dom-spi/src/main/java/org/opendaylight/controller/md/sal/dom/broker/spi/mount/SimpleDOMMountPoint.java

index 74f4634..740cea8 100644 (file)
@@ -7,10 +7,13 @@
  */
 package org.opendaylight.controller.md.sal.dom.api;
 
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+
 /**
  * Marker interface for services which can be obtained from a {@link DOMMountPoint} instance. No further semantics are
  * implied.
  */
-public interface DOMService {
+@SuppressFBWarnings(value = "NM_SAME_SIMPLE_NAME_AS_INTERFACE", justification = "Migration")
+public interface DOMService extends org.opendaylight.mdsal.dom.api.DOMService {
 
 }
index c4d0062..f368dbb 100644 (file)
@@ -8,13 +8,5 @@
 
 package org.opendaylight.controller.sal.core.api.mount;
 
-import java.util.EventListener;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-
-public interface MountProvisionListener extends EventListener {
-
-    void onMountPointCreated(YangInstanceIdentifier path);
-
-    void onMountPointRemoved(YangInstanceIdentifier path);
-
+public interface MountProvisionListener extends org.opendaylight.mdsal.dom.api.DOMMountPointListener {
 }
index c80d75f..4966348 100644 (file)
 
 package org.opendaylight.controller.md.sal.dom.broker.impl.mount;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ClassToInstanceMap;
 import com.google.common.collect.MutableClassToInstanceMap;
-import java.util.HashMap;
-import java.util.Map;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
 import org.opendaylight.controller.md.sal.dom.api.DOMService;
 import org.opendaylight.controller.md.sal.dom.broker.spi.mount.SimpleDOMMountPoint;
 import org.opendaylight.controller.sal.core.api.mount.MountProvisionListener;
+import org.opendaylight.mdsal.dom.api.DOMMountPointListener;
+import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.concepts.ObjectRegistration;
-import org.opendaylight.yangtools.util.ListenerRegistry;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 
 public class DOMMountPointServiceImpl implements DOMMountPointService {
 
-    private final Map<YangInstanceIdentifier, DOMMountPoint> mountPoints = new HashMap<>();
+    private final org.opendaylight.mdsal.dom.api.DOMMountPointService delegate;
 
-    private final ListenerRegistry<MountProvisionListener> listeners = ListenerRegistry.create();
+    @VisibleForTesting
+    public DOMMountPointServiceImpl() {
+        this(new org.opendaylight.mdsal.dom.broker.DOMMountPointServiceImpl());
+    }
 
-    @Override
-    public Optional<DOMMountPoint> getMountPoint(final YangInstanceIdentifier path) {
-        return Optional.fromNullable(mountPoints.get(path));
+    public DOMMountPointServiceImpl(org.opendaylight.mdsal.dom.api.DOMMountPointService delegate) {
+        this.delegate = delegate;
     }
 
     @Override
-    public DOMMountPointBuilder createMountPoint(final YangInstanceIdentifier path) {
-        Preconditions.checkState(!mountPoints.containsKey(path), "Mount point already exists");
-        return new DOMMountPointBuilderImpl(path);
+    public Optional<DOMMountPoint> getMountPoint(final YangInstanceIdentifier path) {
+        final Optional<org.opendaylight.mdsal.dom.api.DOMMountPoint> delegateMountPoint = delegate.getMountPoint(path);
+        return delegateMountPoint.isPresent() ? Optional.of(convert(delegateMountPoint.get())) : Optional.absent();
     }
 
-    public void notifyMountCreated(final YangInstanceIdentifier identifier) {
-        for (final ListenerRegistration<MountProvisionListener> listener : listeners
-                .getListeners()) {
-            listener.getInstance().onMountPointCreated(identifier);
-        }
+    private DOMMountPoint convert(final org.opendaylight.mdsal.dom.api.DOMMountPoint from) {
+        return new DOMMountPoint() {
+            @Override
+            public YangInstanceIdentifier getIdentifier() {
+                return from.getIdentifier();
+            }
+
+            @Override
+            public <T extends DOMService> Optional<T> getService(Class<T> cls) {
+                return from.getService(cls);
+            }
+
+            @Override
+            public SchemaContext getSchemaContext() {
+                return from.getSchemaContext();
+            }
+
+            @Override
+            public int hashCode() {
+                return from.getIdentifier().hashCode();
+            }
+
+            @Override
+            public boolean equals(Object obj) {
+                if (this == obj) {
+                    return true;
+                }
+
+                if (!(obj instanceof DOMMountPoint)) {
+                    return false;
+                }
+
+                DOMMountPoint other = (DOMMountPoint) obj;
+                return from.getIdentifier().equals(other.getIdentifier());
+            }
+        };
     }
 
-    public void notifyMountRemoved(final YangInstanceIdentifier identifier) {
-        for (final ListenerRegistration<MountProvisionListener> listener : listeners
-                .getListeners()) {
-            listener.getInstance().onMountPointRemoved(identifier);
-        }
+    @Override
+    public DOMMountPointBuilder createMountPoint(final YangInstanceIdentifier path) {
+        return new DOMMountPointBuilderImpl(path);
     }
 
     @Override
     public ListenerRegistration<MountProvisionListener> registerProvisionListener(
             final MountProvisionListener listener) {
-        return listeners.register(listener);
+        final ListenerRegistration<DOMMountPointListener> delegateReg = delegate.registerProvisionListener(listener);
+        return new AbstractListenerRegistration<MountProvisionListener>(listener) {
+            @Override
+            protected void removeRegistration() {
+                delegateReg.close();
+            }
+        };
     }
 
+    /**
+     * Deprecated.
+
+     * @deprecated this method should never have been exposed publicly - registration should be done via the
+     *         public {@link #createMountPoint} interface. As such, this method expects the {@code mountPoint} param
+     *         to be of type {@link SimpleDOMMountPoint}.
+     */
+    @SuppressWarnings("unchecked")
+    @Deprecated
     public ObjectRegistration<DOMMountPoint> registerMountPoint(final DOMMountPoint mountPoint) {
-        synchronized (mountPoints) {
-            Preconditions
-                    .checkState(!mountPoints.containsKey(mountPoint.getIdentifier()), "Mount point already exists");
-            mountPoints.put(mountPoint.getIdentifier(), mountPoint);
-        }
-        notifyMountCreated(mountPoint.getIdentifier());
+        Preconditions.checkArgument(mountPoint instanceof SimpleDOMMountPoint, "Expected SimpleDOMMountPoint");
 
-        return new MountRegistration(mountPoint);
-    }
+        final org.opendaylight.mdsal.dom.api.DOMMountPointService.DOMMountPointBuilder delegateBuilder =
+            delegate.createMountPoint(mountPoint.getIdentifier());
 
-    public void unregisterMountPoint(final YangInstanceIdentifier mountPointId) {
-        synchronized (mountPoints) {
-            Preconditions.checkState(mountPoints.containsKey(mountPointId), "Mount point does not exist");
-            mountPoints.remove(mountPointId);
+        if (mountPoint.getSchemaContext() != null) {
+            delegateBuilder.addInitialSchemaContext(mountPoint.getSchemaContext());
         }
-        notifyMountRemoved(mountPointId);
+
+        ((SimpleDOMMountPoint)mountPoint).getAllServices().forEach(
+            entry -> delegateBuilder.addService((Class<DOMService>)entry.getKey(), entry.getValue()));
+
+        final ObjectRegistration<org.opendaylight.mdsal.dom.api.DOMMountPoint> delegateReg = delegateBuilder.register();
+        return new ObjectRegistration<DOMMountPoint>() {
+            @Override
+            public void close() {
+                delegateReg.close();
+            }
+
+            @Override
+            public DOMMountPoint getInstance() {
+                return mountPoint;
+            }
+        };
     }
 
     public class DOMMountPointBuilderImpl implements DOMMountPointBuilder {
@@ -107,26 +161,8 @@ public class DOMMountPointServiceImpl implements DOMMountPointService {
         @Override
         public ObjectRegistration<DOMMountPoint> register() {
             Preconditions.checkState(mountPoint == null, "Mount point is already built.");
-            mountPoint = SimpleDOMMountPoint.create(path, services,schemaContext);
+            mountPoint = SimpleDOMMountPoint.create(path, services, schemaContext);
             return registerMountPoint(mountPoint);
         }
     }
-
-    private final class MountRegistration implements ObjectRegistration<DOMMountPoint> {
-        private final DOMMountPoint mountPoint;
-
-        MountRegistration(final DOMMountPoint mountPoint) {
-            this.mountPoint = mountPoint;
-        }
-
-        @Override
-        public DOMMountPoint getInstance() {
-            return mountPoint;
-        }
-
-        @Override
-        public void close() {
-            unregisterMountPoint(mountPoint.getIdentifier());
-        }
-    }
 }
index f77b793..67ebcf5 100644 (file)
 
   <!-- DOM MountPoint Service -->
 
-  <bean id="domMountPointService" class="org.opendaylight.controller.md.sal.dom.broker.impl.mount.DOMMountPointServiceImpl"/>
+  <reference id="delegateDomMountPointService" interface="org.opendaylight.mdsal.dom.api.DOMMountPointService"
+      odl:type="default"/>
+
+  <bean id="domMountPointService" class="org.opendaylight.controller.md.sal.dom.broker.impl.mount.DOMMountPointServiceImpl">
+    <argument ref="delegateDomMountPointService"/>
+  </bean>
 
   <service ref="domMountPointService" interface="org.opendaylight.controller.md.sal.dom.api.DOMMountPointService"
         odl:type="default"/>
index 11d9cab..bd0b6fb 100644 (file)
@@ -11,6 +11,8 @@ package org.opendaylight.controller.md.sal.dom.broker.spi.mount;
 import com.google.common.base.Optional;
 import com.google.common.collect.ClassToInstanceMap;
 import com.google.common.collect.ImmutableClassToInstanceMap;
+import java.util.Map.Entry;
+import java.util.Set;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
 import org.opendaylight.controller.md.sal.dom.api.DOMService;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
@@ -48,4 +50,18 @@ public final class SimpleDOMMountPoint implements DOMMountPoint {
     public <T extends DOMService> Optional<T> getService(final Class<T> cls) {
         return Optional.fromNullable(services.getInstance(cls));
     }
+
+    public Set<Entry<Class<? extends DOMService>, DOMService>> getAllServices() {
+        return services.entrySet();
+    }
+
+    @Override
+    public int hashCode() {
+        return identifier.hashCode();
+    }
+
+    @Override
+    public boolean equals(final Object obj) {
+        return this == obj || obj instanceof DOMMountPoint && identifier.equals(((DOMMountPoint) obj).getIdentifier());
+    }
 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.