X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflowplugin-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fopenflowplugin%2Fimpl%2Fmastership%2FMastershipChangeServiceManagerImpl.java;h=06ecc97a300da27b04695ade8937063e7cb534dd;hb=ac9b359c3fce7bbb99392841daf776c33a234f6d;hp=64c6497c87e4eb8534ab1bedc0fda73038aa41d3;hpb=10cd6562382140a3f7bd5ed823c8d6de5f2b6918;p=openflowplugin.git diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/mastership/MastershipChangeServiceManagerImpl.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/mastership/MastershipChangeServiceManagerImpl.java index 64c6497c87..06ecc97a30 100644 --- a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/mastership/MastershipChangeServiceManagerImpl.java +++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/mastership/MastershipChangeServiceManagerImpl.java @@ -7,6 +7,7 @@ */ package org.opendaylight.openflowplugin.impl.mastership; +import static java.util.Objects.requireNonNull; import static org.opendaylight.infrautils.utils.concurrent.LoggingFutures.addErrorLogging; import com.google.common.annotations.VisibleForTesting; @@ -21,12 +22,12 @@ import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo; import org.opendaylight.openflowplugin.api.openflow.lifecycle.MasterChecker; import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeException; -import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeRegistration; import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeService; import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager; import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkEvent; -import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkRegistration; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState; +import org.opendaylight.yangtools.concepts.AbstractRegistration; +import org.opendaylight.yangtools.concepts.Registration; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; @@ -35,7 +36,7 @@ import org.slf4j.LoggerFactory; @Singleton @Component(immediate = true) -public final class MastershipChangeServiceManagerImpl implements MastershipChangeServiceManager { +public final class MastershipChangeServiceManagerImpl implements MastershipChangeServiceManager, AutoCloseable { private static final Logger LOG = LoggerFactory.getLogger(MastershipChangeServiceManagerImpl.class); private final List serviceGroup = new CopyOnWriteArrayList<>(); @@ -57,9 +58,15 @@ public final class MastershipChangeServiceManagerImpl implements MastershipChang } @Override - public MastershipChangeRegistration register(final MastershipChangeService service) { - final var registration = new MastershipServiceDelegate(service, () -> serviceGroup.remove(service)); - serviceGroup.add(service); + public Registration register(final MastershipChangeService service) { + serviceGroup.add(requireNonNull(service)); + final var registration = new AbstractRegistration() { + @Override + protected void removeRegistration() { + serviceGroup.remove(service); + } + }; + if (masterChecker != null && masterChecker.isAnyDeviceMastered()) { masterChecker.listOfMasteredDevices().forEach(service::onBecomeOwner); } @@ -67,13 +74,20 @@ public final class MastershipChangeServiceManagerImpl implements MastershipChang } @Override - public ReconciliationFrameworkRegistration reconciliationFrameworkRegistration( + public synchronized Registration reconciliationFrameworkRegistration( final ReconciliationFrameworkEvent reconciliationFrameworkEvent) throws MastershipChangeException { if (rfService != null) { throw new MastershipChangeException("Reconciliation framework already registered."); } - rfService = reconciliationFrameworkEvent; - return new ReconciliationFrameworkServiceDelegate(reconciliationFrameworkEvent, () -> rfService = null); + rfService = requireNonNull(reconciliationFrameworkEvent); + return new AbstractRegistration() { + @Override + protected void removeRegistration() { + synchronized (MastershipChangeServiceManagerImpl.this) { + rfService = null; + } + } + }; } @Override