+++ /dev/null
-/*
- * Copyright (c) 2015 Brocade Communications Systems, Inc. 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.mdsal.eos.binding.dom.adapter;
-
-import static java.util.Objects.requireNonNull;
-
-import org.opendaylight.mdsal.eos.binding.api.Entity;
-import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
-import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration;
-import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
-
-/**
- * Implementation of EntityOwnershipCandidateRegistration whose instances are returned from the
- * {@link BindingDOMEntityOwnershipServiceAdapter}.
- *
- * @author Thomas Pantelis
- */
-class BindingEntityOwnershipCandidateRegistration extends AbstractObjectRegistration<Entity>
- implements EntityOwnershipCandidateRegistration {
- private final DOMEntityOwnershipCandidateRegistration domRegistration;
-
- BindingEntityOwnershipCandidateRegistration(DOMEntityOwnershipCandidateRegistration domRegistration,
- Entity entity) {
- super(entity);
- this.domRegistration = requireNonNull(domRegistration);
- }
-
- @Override
- protected void removeRegistration() {
- domRegistration.close();
- }
-}
\ No newline at end of file
/**
* Implementation of EntityOwnershipListenerRegistration whose instances are returned from the
- * {@link BindingDOMEntityOwnershipServiceAdapter}.
+ * {@link DefaultEntityOwnershipService}.
*
* @author Thomas Pantelis
*/
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.mdsal.binding.dom.adapter.AdapterContext;
import org.opendaylight.mdsal.eos.binding.api.Entity;
-import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService;
+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.Reference;
/**
* Adapter that bridges between the binding and DOM EntityOwnershipService interfaces.
* @author Thomas Pantelis
*/
@Singleton
-public class BindingDOMEntityOwnershipServiceAdapter implements EntityOwnershipService, AutoCloseable {
+@Component
+public final class DefaultEntityOwnershipService implements EntityOwnershipService {
private final @NonNull DOMEntityOwnershipService domService;
private final @NonNull AdapterContext adapterContext;
@Inject
- public BindingDOMEntityOwnershipServiceAdapter(final @NonNull DOMEntityOwnershipService domService,
- final @NonNull AdapterContext adapterContext) {
+ @Activate
+ public DefaultEntityOwnershipService(@Reference final @NonNull DOMEntityOwnershipService domService,
+ @Reference final @NonNull AdapterContext adapterContext) {
this.domService = requireNonNull(domService);
this.adapterContext = requireNonNull(adapterContext);
}
@Override
- public EntityOwnershipCandidateRegistration registerCandidate(final Entity entity)
- throws CandidateAlreadyRegisteredException {
- return new BindingEntityOwnershipCandidateRegistration(
- domService.registerCandidate(toDOMEntity(entity)), entity);
+ public Registration registerCandidate(final Entity entity) throws CandidateAlreadyRegisteredException {
+ return domService.registerCandidate(toDOMEntity(entity));
}
@Override
return new DOMEntity(entity.getType(),
adapterContext.currentSerializer().toYangInstanceIdentifier(entity.getIdentifier()));
}
-
- @Override
- public void close() {
- }
}
+++ /dev/null
-/*
- * Copyright (c) 2020 PANTHEON.tech, 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.mdsal.eos.binding.dom.adapter;
-
-import com.google.common.annotations.Beta;
-import java.util.Optional;
-import org.opendaylight.mdsal.binding.dom.adapter.AdapterContext;
-import org.opendaylight.mdsal.eos.binding.api.Entity;
-import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
-import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
-import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
-import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
-import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
-import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
-import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService;
-import org.osgi.service.component.annotations.Activate;
-import org.osgi.service.component.annotations.Component;
-import org.osgi.service.component.annotations.Deactivate;
-import org.osgi.service.component.annotations.Reference;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-@Beta
-@Component(immediate = true, property = "type=default")
-public final class OSGiEntityOwnershipService implements EntityOwnershipService {
- private static final Logger LOG = LoggerFactory.getLogger(OSGiEntityOwnershipService.class);
-
- @Reference
- DOMEntityOwnershipService domService = null;
- @Reference
- AdapterContext adapterContext = null;
-
- private BindingDOMEntityOwnershipServiceAdapter delegate;
-
- @Override
- public EntityOwnershipCandidateRegistration registerCandidate(final Entity entity)
- throws CandidateAlreadyRegisteredException {
- return delegate.registerCandidate(entity);
- }
-
- @Override
- public EntityOwnershipListenerRegistration registerListener(final String entityType,
- final EntityOwnershipListener listener) {
- return delegate.registerListener(entityType, listener);
- }
-
- @Override
- public Optional<EntityOwnershipState> getOwnershipState(final Entity forEntity) {
- return delegate.getOwnershipState(forEntity);
- }
-
- @Override
- public boolean isCandidateRegistered(final Entity forEntity) {
- return delegate.isCandidateRegistered(forEntity);
- }
-
- @Activate
- void activate() {
- LOG.info("Entity Ownership Service adapter starting");
- delegate = new BindingDOMEntityOwnershipServiceAdapter(domService, adapterContext);
- LOG.info("Entity Ownership Service adapter started");
- }
-
- @Deactivate
- void deactivate() {
- LOG.info("Entity Ownership Service adapter stopping");
- delegate.close();
- delegate = null;
- LOG.info("Entity Ownership Service adapter stopped");
-
- }
-}
import org.opendaylight.mdsal.binding.dom.adapter.ConstantAdapterContext;
import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecServices;
import org.opendaylight.mdsal.eos.binding.api.Entity;
-import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListener;
import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
import org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState;
import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
-import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration;
import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange;
import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener;
import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListenerRegistration;
import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService;
+import org.opendaylight.yangtools.concepts.Registration;
/**
* Unit tests for BindingDOMEntityOwnershipServiceAdapter.
* @author Thomas Pantelis
*/
@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class BindingDOMEntityOwnershipServiceAdapterTest {
- static Entity BINDING_ENTITY = new Entity("foo", "bar");
- static DOMEntity DOM_ENTITY = new DOMEntity("foo", "bar");
+public class DefaultEntityOwnershipServiceTest {
+ static final Entity BINDING_ENTITY = new Entity("foo", "bar");
+ static final DOMEntity DOM_ENTITY = new DOMEntity("foo", "bar");
@Mock
private BindingDOMCodecServices mockCodecRegistry;
@Mock
private DOMEntityOwnershipService mockDOMService;
- private BindingDOMEntityOwnershipServiceAdapter adapter;
+ private DefaultEntityOwnershipService adapter;
@Before
public void setup() {
doReturn(BINDING_ENTITY.getIdentifier()).when(mockCodecRegistry).fromYangInstanceIdentifier(
DOM_ENTITY.getIdentifier());
- this.adapter = new BindingDOMEntityOwnershipServiceAdapter(mockDOMService,
+ adapter = new DefaultEntityOwnershipService(mockDOMService,
new ConstantAdapterContext(mockCodecRegistry));
}
@Test
public void testRegisterCandidate() throws CandidateAlreadyRegisteredException {
- final DOMEntityOwnershipCandidateRegistration mockDOMReg = mock(DOMEntityOwnershipCandidateRegistration.class);
- doReturn(mockDOMReg).when(this.mockDOMService).registerCandidate(DOM_ENTITY);
+ final var mockDOMReg = mock(Registration.class);
+ doReturn(mockDOMReg).when(mockDOMService).registerCandidate(DOM_ENTITY);
- final EntityOwnershipCandidateRegistration reg = this.adapter.registerCandidate(BINDING_ENTITY);
-
- assertNotNull("registerCandidate returned null", reg);
- assertEquals("getInstance", BINDING_ENTITY, reg.getInstance());
-
- reg.close();
+ try (var reg = adapter.registerCandidate(BINDING_ENTITY)) {
+ assertNotNull("registerCandidate returned null", reg);
+ }
verify(mockDOMReg).close();
}
@Test
public void testRegisterListener() {
final DOMEntityOwnershipListenerRegistration mockDOMReg = mock(DOMEntityOwnershipListenerRegistration.class);
- doReturn(mockDOMReg).when(this.mockDOMService).registerListener(eq(DOM_ENTITY.getType()),
+ doReturn(mockDOMReg).when(mockDOMService).registerListener(eq(DOM_ENTITY.getType()),
any(DOMEntityOwnershipListener.class));
final EntityOwnershipListener mockListener = mock(EntityOwnershipListener.class);
- final EntityOwnershipListenerRegistration reg = this.adapter.registerListener(
+ final EntityOwnershipListenerRegistration reg = adapter.registerListener(
BINDING_ENTITY.getType(), mockListener);
assertNotNull("registerListener returned null", reg);
final ArgumentCaptor<DOMEntityOwnershipListener> domListenerCaptor = ArgumentCaptor.forClass(
DOMEntityOwnershipListener.class);
- verify(this.mockDOMService).registerListener(eq(DOM_ENTITY.getType()), domListenerCaptor.capture());
+ verify(mockDOMService).registerListener(eq(DOM_ENTITY.getType()), domListenerCaptor.capture());
final DOMEntityOwnershipChange domOwnershipChange = new DOMEntityOwnershipChange(DOM_ENTITY,
EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED, true);
@Test
public void testGetOwnershipState() {
- final Optional<EntityOwnershipState> expectedState = Optional.of(EntityOwnershipState.IS_OWNER);
- doReturn(expectedState).when(this.mockDOMService).getOwnershipState(DOM_ENTITY);
-
- final Optional<EntityOwnershipState> actualState = this.adapter.getOwnershipState(BINDING_ENTITY);
+ final var expectedState = Optional.of(EntityOwnershipState.IS_OWNER);
+ doReturn(expectedState).when(mockDOMService).getOwnershipState(DOM_ENTITY);
+ final var actualState = adapter.getOwnershipState(BINDING_ENTITY);
assertSame("getOwnershipState", expectedState, actualState);
}
@Test
public void testIsCandidateRegistered() {
- doReturn(true).when(this.mockDOMService).isCandidateRegistered(DOM_ENTITY);
- assertEquals("isCandidateRegistered", true, this.adapter.isCandidateRegistered(BINDING_ENTITY));
+ doReturn(true).when(mockDOMService).isCandidateRegistered(DOM_ENTITY);
+ assertEquals("isCandidateRegistered", true, adapter.isCandidateRegistered(BINDING_ENTITY));
}
public void testOwnershipChangeWithException() {
- final DOMEntityOwnershipListenerAdapter domEntityOwnershipListenerAdapter =
- new DOMEntityOwnershipListenerAdapter(mock(EntityOwnershipListener.class),
- new ConstantAdapterContext(mockCodecRegistry));
- final DOMEntityOwnershipChange domOwnershipChange = mock(DOMEntityOwnershipChange.class);
+ final var domEntityOwnershipListenerAdapter =
+ new DOMEntityOwnershipListenerAdapter(mock(EntityOwnershipListener.class),
+ new ConstantAdapterContext(mockCodecRegistry));
+ final var domOwnershipChange = mock(DOMEntityOwnershipChange.class);
doThrow(IllegalStateException.class).when(domOwnershipChange).getEntity();
assertThrows(IllegalStateException.class,
() -> domEntityOwnershipListenerAdapter.ownershipChanged(domOwnershipChange));
+++ /dev/null
-/*
- * Copyright (c) 2015 Brocade Communications Systems, Inc. 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.mdsal.eos.binding.api;
-
-import com.google.common.annotations.Beta;
-import org.opendaylight.mdsal.eos.common.api.GenericEntityOwnershipCandidateRegistration;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-
-/**
- * Binding version of {@link GenericEntityOwnershipCandidateRegistration}.
- *
- * @author Thomas Pantelis
- */
-@Beta
-public interface EntityOwnershipCandidateRegistration extends
- GenericEntityOwnershipCandidateRegistration<InstanceIdentifier<?>, Entity> {
-}
import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
import org.opendaylight.mdsal.eos.common.api.GenericEntityOwnershipService;
+import org.opendaylight.yangtools.concepts.Registration;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
/**
GenericEntityOwnershipService<InstanceIdentifier<?>, Entity, EntityOwnershipListener> {
@Override
- EntityOwnershipCandidateRegistration registerCandidate(Entity entity)
- throws CandidateAlreadyRegisteredException;
+ Registration registerCandidate(Entity entity) throws CandidateAlreadyRegisteredException;
@Override
- EntityOwnershipListenerRegistration registerListener(String entityType,
- EntityOwnershipListener listener);
+ EntityOwnershipListenerRegistration registerListener(String entityType, EntityOwnershipListener listener);
@Override
Optional<EntityOwnershipState> getOwnershipState(Entity forEntity);
+++ /dev/null
-/*
- * Copyright (c) 2015 Brocade Communications Systems, Inc. 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.mdsal.eos.common.api;
-
-import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
-import org.opendaylight.yangtools.concepts.ObjectRegistration;
-
-/**
- * An interface that records a request to register a Candidate for a given Entity. Calling close on the
- * registration will remove the Candidate from any future ownership considerations for that Entity.
- *
- * @author Thomas Pantelis
- *
- * @param <P> the instance identifier type
- * @param <E> the GenericEntity type
- */
-public interface GenericEntityOwnershipCandidateRegistration<P extends HierarchicalIdentifier<P>,
- E extends GenericEntity<P>> extends ObjectRegistration<E> {
-
- /**
- * Unregister the candidate.
- */
- @Override
- void close();
-}
import java.util.Optional;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
+import org.opendaylight.yangtools.concepts.Registration;
/**
* <p>
*/
public interface GenericEntityOwnershipService<P extends HierarchicalIdentifier<P>, E extends GenericEntity<P>,
L extends GenericEntityOwnershipListener<P, ? extends GenericEntityOwnershipChange<P, E>>> {
-
/**
* Registers a candidate for ownership of the given entity. Only one such request can be made per entity
* per process. If multiple requests for registering a candidate for a given entity are received in the
* @return a registration object that can be used to unregister the Candidate
* @throws CandidateAlreadyRegisteredException if the candidate was already registered
*/
- GenericEntityOwnershipCandidateRegistration<P, E> registerCandidate(@NonNull E entity)
- throws CandidateAlreadyRegisteredException;
+ @NonNull Registration registerCandidate(@NonNull E entity) throws CandidateAlreadyRegisteredException;
/**
* Registers a listener that is interested in ownership changes for entities of the given entity type. The
+++ /dev/null
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. 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.mdsal.eos.common.spi;
-
-import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.mdsal.eos.common.api.GenericEntity;
-import org.opendaylight.mdsal.eos.common.api.GenericEntityOwnershipCandidateRegistration;
-import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
-import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
-
-/**
- * Abstract base class for an EntityOwnershipCandidateRegistration.
- *
- * @param <P> the instance identifier path type
- * @param <E> the GenericEntity type
- */
-public abstract class AbstractGenericEntityOwnershipCandidateRegistration<P extends HierarchicalIdentifier<P>,
- E extends GenericEntity<P>>
- extends AbstractObjectRegistration<E> implements GenericEntityOwnershipCandidateRegistration<P, E> {
-
- protected AbstractGenericEntityOwnershipCandidateRegistration(final @NonNull E entity) {
- super(entity);
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. 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.mdsal.eos.common.spi;
-
-import static org.junit.Assert.assertNotNull;
-import static org.mockito.Mockito.mock;
-
-import org.junit.Test;
-import org.opendaylight.mdsal.eos.common.api.GenericEntity;
-
-public class AbstractGenericEntityOwnershipCandidateRegistrationTest
- extends AbstractGenericEntityOwnershipCandidateRegistration {
-
- @Test
- public void basicTest() {
- assertNotNull(this);
- }
-
- public AbstractGenericEntityOwnershipCandidateRegistrationTest() {
- super(mock(GenericEntity.class));
- }
-
- @Override
- protected void removeRegistration() {
- //NOOP
- }
-}
\ No newline at end of file
+++ /dev/null
-/*
- * Copyright (c) 2015 Brocade Communications Systems, Inc. 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.mdsal.eos.dom.api;
-
-import com.google.common.annotations.Beta;
-import org.opendaylight.mdsal.eos.common.api.GenericEntityOwnershipCandidateRegistration;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-
-/**
- * DOM version of {@link GenericEntityOwnershipCandidateRegistration}.
- *
- * @author Thomas Pantelis
- */
-@Beta
-public interface DOMEntityOwnershipCandidateRegistration extends
- GenericEntityOwnershipCandidateRegistration<YangInstanceIdentifier, DOMEntity> {
-}
import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
import org.opendaylight.mdsal.eos.common.api.GenericEntityOwnershipService;
+import org.opendaylight.yangtools.concepts.Registration;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
/**
public interface DOMEntityOwnershipService extends
GenericEntityOwnershipService<YangInstanceIdentifier, DOMEntity, DOMEntityOwnershipListener> {
@Override
- DOMEntityOwnershipCandidateRegistration registerCandidate(DOMEntity entity)
- throws CandidateAlreadyRegisteredException;
+ Registration registerCandidate(DOMEntity entity) throws CandidateAlreadyRegisteredException;
@Override
DOMEntityOwnershipListenerRegistration registerListener(String entityType,
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.MoreObjects;
-import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.ImmutableList;
import org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState;
import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
-import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration;
import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange;
import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener;
import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListenerRegistration;
import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService;
+import org.opendaylight.mdsal.eos.dom.simple.di.LocalDOMEntityOwnershipService;
import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
+import org.opendaylight.yangtools.concepts.AbstractRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+@Component
@MetaInfServices
-@Component(immediate = true)
-public class SimpleDOMEntityOwnershipService implements DOMEntityOwnershipService {
+public sealed class SimpleDOMEntityOwnershipService implements DOMEntityOwnershipService
+ permits LocalDOMEntityOwnershipService {
private static final Logger LOG = LoggerFactory.getLogger(SimpleDOMEntityOwnershipService.class);
@GuardedBy("entities")
private final UUID uuid;
+ @Activate
+ public SimpleDOMEntityOwnershipService() {
+ this(UUID.randomUUID());
+ }
+
@VisibleForTesting
SimpleDOMEntityOwnershipService(final UUID uuid) {
this.uuid = requireNonNull(uuid);
}
- public SimpleDOMEntityOwnershipService() {
- this(UUID.randomUUID());
- }
-
@Override
- public DOMEntityOwnershipCandidateRegistration registerCandidate(final DOMEntity entity)
- throws CandidateAlreadyRegisteredException {
+ public Registration registerCandidate(final DOMEntity entity) throws CandidateAlreadyRegisteredException {
synchronized (entities) {
- final DOMEntity prev = entities.get(entity.getType(), entity.getIdentifier());
+ final var prev = entities.get(entity.getType(), entity.getIdentifier());
if (prev != null) {
throw new CandidateAlreadyRegisteredException(prev);
}
}
notifyListeners(entity, LOCAL_OWNERSHIP_GRANTED);
- return new EntityRegistration(entity);
+ return new AbstractRegistration() {
+ @Override
+ protected void removeRegistration() {
+ removeEntity(entity);
+ }
+ };
}
@Override
@Override
public String toString() {
- final ToStringHelper h = MoreObjects.toStringHelper(SimpleDOMEntityOwnershipService.class).add("uuid", uuid);
-
+ final var helper = MoreObjects.toStringHelper(SimpleDOMEntityOwnershipService.class).add("uuid", uuid);
synchronized (entities) {
- h.add("entities", entities);
+ helper.add("entities", entities);
}
synchronized (listeners) {
- h.add("listeners", listeners);
- }
-
- return h.toString();
- }
-
- private final class EntityRegistration extends AbstractObjectRegistration<DOMEntity> implements
- DOMEntityOwnershipCandidateRegistration {
- EntityRegistration(final DOMEntity entity) {
- super(entity);
- }
-
- @Override
- protected void removeRegistration() {
- removeEntity(getInstance());
+ helper.add("listeners", listeners);
}
+ return helper.toString();
}
private final class ListenerRegistration extends AbstractObjectRegistration<DOMEntityOwnershipListener>
/**
* Simple {@link DOMEntityOwnershipService} operating as an isolated island. It has no awareness of the world outside
* of itself.
- *
- * @author Robert Varga
*/
@Beta
@Singleton
-public class LocalDOMEntityOwnershipService extends SimpleDOMEntityOwnershipService {
+public final class LocalDOMEntityOwnershipService extends SimpleDOMEntityOwnershipService {
@Inject
public LocalDOMEntityOwnershipService() {
// Exposed for DI
*/
package org.opendaylight.mdsal.eos.dom.simple;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import java.util.Optional;
import java.util.UUID;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
-import org.mockito.junit.MockitoJUnitRunner;
+import org.mockito.junit.jupiter.MockitoExtension;
import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
import org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState;
import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
-import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration;
import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange;
import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener;
-import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListenerRegistration;
import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService;
-@RunWith(MockitoJUnitRunner.StrictStubs.class)
-public class SimpleDOMEntityOwnershipServiceTest {
- public static final String FOO_TYPE = "foo";
- public static final String BAR_TYPE = "bar";
+@ExtendWith(MockitoExtension.class)
+class SimpleDOMEntityOwnershipServiceTest {
+ private static final String FOO_TYPE = "foo";
+ private static final String BAR_TYPE = "bar";
+ private static final DOMEntity FOO_FOO_ENTITY = new DOMEntity(FOO_TYPE, "foo");
+ private static final DOMEntity FOO_BAR_ENTITY = new DOMEntity(FOO_TYPE, "bar");
- public static final DOMEntity FOO_FOO_ENTITY = new DOMEntity(FOO_TYPE, "foo");
- public static final DOMEntity FOO_BAR_ENTITY = new DOMEntity(FOO_TYPE, "bar");
-
- public final DOMEntityOwnershipService service = new SimpleDOMEntityOwnershipService();
+ final DOMEntityOwnershipService service = new SimpleDOMEntityOwnershipService();
@Test
- public void testNonExistingEntity() {
+ void testNonExistingEntity() {
assertFalse(service.isCandidateRegistered(FOO_FOO_ENTITY));
- final Optional<EntityOwnershipState> state = service.getOwnershipState(FOO_FOO_ENTITY);
- assertNotNull(state);
- assertFalse(state.isPresent());
+ assertEquals(Optional.empty(), service.getOwnershipState(FOO_FOO_ENTITY));
}
@Test
- public void testExistingEntity() throws CandidateAlreadyRegisteredException {
- final DOMEntityOwnershipCandidateRegistration reg = service.registerCandidate(FOO_FOO_ENTITY);
- assertNotNull(reg);
-
- assertTrue(service.isCandidateRegistered(FOO_FOO_ENTITY));
- assertFalse(service.isCandidateRegistered(FOO_BAR_ENTITY));
+ void testExistingEntity() throws Exception {
+ try (var reg = service.registerCandidate(FOO_FOO_ENTITY)) {
+ assertNotNull(reg);
- final Optional<EntityOwnershipState> state = service.getOwnershipState(FOO_FOO_ENTITY);
- assertNotNull(state);
- assertTrue(state.isPresent());
- assertEquals(EntityOwnershipState.IS_OWNER, state.orElseThrow());
+ assertTrue(service.isCandidateRegistered(FOO_FOO_ENTITY));
+ assertFalse(service.isCandidateRegistered(FOO_BAR_ENTITY));
- reg.close();
+ assertEquals(Optional.of(EntityOwnershipState.IS_OWNER), service.getOwnershipState(FOO_FOO_ENTITY));
+ }
assertFalse(service.isCandidateRegistered(FOO_FOO_ENTITY));
}
- @Test(expected = CandidateAlreadyRegisteredException.class)
- public void testDuplicateRegistration() throws CandidateAlreadyRegisteredException {
- final DOMEntityOwnershipCandidateRegistration reg = service.registerCandidate(FOO_FOO_ENTITY);
- assertNotNull(reg);
+ @Test
+ void testDuplicateRegistration() throws Exception {
+ assertNotNull(service.registerCandidate(FOO_FOO_ENTITY));
// Should throw
- service.registerCandidate(FOO_FOO_ENTITY);
+ assertThrows(CandidateAlreadyRegisteredException.class, () -> service.registerCandidate(FOO_FOO_ENTITY));
}
@Test
- public void testListener() throws CandidateAlreadyRegisteredException {
- final DOMEntityOwnershipCandidateRegistration entityReg = service.registerCandidate(FOO_FOO_ENTITY);
+ void testListener() throws Exception {
+ final var entityReg = service.registerCandidate(FOO_FOO_ENTITY);
assertNotNull(entityReg);
// Mismatched type, not triggered
- final DOMEntityOwnershipListener barListener = mock(DOMEntityOwnershipListener.class);
- final DOMEntityOwnershipListenerRegistration barReg = service.registerListener(BAR_TYPE, barListener);
-
- // Matching type should be triggered
- final DOMEntityOwnershipListener fooListener = mock(DOMEntityOwnershipListener.class);
- doNothing().when(fooListener).ownershipChanged(any(DOMEntityOwnershipChange.class));
- final DOMEntityOwnershipListenerRegistration fooReg = service.registerListener(FOO_TYPE, fooListener);
- final ArgumentCaptor<DOMEntityOwnershipChange> fooCaptor = ArgumentCaptor.forClass(
- DOMEntityOwnershipChange.class);
- verify(fooListener).ownershipChanged(fooCaptor.capture());
-
- DOMEntityOwnershipChange fooChange = fooCaptor.getValue();
- assertEquals(FOO_FOO_ENTITY, fooChange.getEntity());
- assertEquals(EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED, fooChange.getState());
-
- reset(fooListener);
- doNothing().when(fooListener).ownershipChanged(any(DOMEntityOwnershipChange.class));
- entityReg.close();
- verify(fooListener).ownershipChanged(fooCaptor.capture());
- fooChange = fooCaptor.getValue();
- assertEquals(FOO_FOO_ENTITY, fooChange.getEntity());
- assertEquals(EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NO_OWNER, fooChange.getState());
-
- fooReg.close();
- barReg.close();
+ final var barListener = mock(DOMEntityOwnershipListener.class);
+ try (var barReg = service.registerListener(BAR_TYPE, barListener)) {
+ // Matching type should be triggered
+ final var fooListener = mock(DOMEntityOwnershipListener.class);
+ doNothing().when(fooListener).ownershipChanged(any(DOMEntityOwnershipChange.class));
+ try (var fooReg = service.registerListener(FOO_TYPE, fooListener)) {
+ final var fooCaptor = ArgumentCaptor.forClass(DOMEntityOwnershipChange.class);
+ verify(fooListener).ownershipChanged(fooCaptor.capture());
+
+ var fooChange = fooCaptor.getValue();
+ assertEquals(FOO_FOO_ENTITY, fooChange.getEntity());
+ assertEquals(EntityOwnershipChangeState.LOCAL_OWNERSHIP_GRANTED, fooChange.getState());
+
+ reset(fooListener);
+ doNothing().when(fooListener).ownershipChanged(any(DOMEntityOwnershipChange.class));
+ entityReg.close();
+ verify(fooListener).ownershipChanged(fooCaptor.capture());
+ fooChange = fooCaptor.getValue();
+ assertEquals(FOO_FOO_ENTITY, fooChange.getEntity());
+ assertEquals(EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NO_OWNER, fooChange.getState());
+ }
+ }
}
@Test
- public void testToString() throws CandidateAlreadyRegisteredException {
- final UUID uuid = UUID.randomUUID();
- final String expected = String.format("SimpleDOMEntityOwnershipService{uuid=%s, entities={}, listeners={}}",
- uuid);
+ void testToString() throws Exception {
+ final var uuid = UUID.randomUUID();
+ final var expected = String.format("SimpleDOMEntityOwnershipService{uuid=%s, entities={}, listeners={}}", uuid);
assertEquals(expected, new SimpleDOMEntityOwnershipService(uuid).toString());
}
}
import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
import org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState;
import org.opendaylight.mdsal.eos.common.api.GenericEntity;
-import org.opendaylight.mdsal.eos.common.api.GenericEntityOwnershipCandidateRegistration;
import org.opendaylight.mdsal.eos.common.api.GenericEntityOwnershipChange;
import org.opendaylight.mdsal.eos.common.api.GenericEntityOwnershipListener;
import org.opendaylight.mdsal.eos.common.api.GenericEntityOwnershipService;
import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
import org.opendaylight.yangtools.concepts.HierarchicalIdentifier;
+import org.opendaylight.yangtools.concepts.Registration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
* acquire {@link #cleanupEntity}.
*/
@GuardedBy("this")
- private GenericEntityOwnershipCandidateRegistration<P, E> serviceEntityReg = null;
+ private Registration serviceEntityReg = null;
/**
* Service (base) entity last reported state.
*/
* and startup.
*/
@GuardedBy("this")
- private GenericEntityOwnershipCandidateRegistration<P, E> cleanupEntityReg;
+ private Registration cleanupEntityReg;
/**
* Cleanup (owner) entity last reported state.
*/
import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
import org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState;
import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
-import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration;
import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange;
import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListenerRegistration;
import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService;
import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
+import org.opendaylight.yangtools.concepts.Registration;
/**
* Abstract {@link DOMClusterSingletonServiceProviderImpl} testing substrate.
@Override
public final void instantiateServiceInstance() {
- this.serviceState = TestClusterSingletonServiceState.STARTED;
+ serviceState = TestClusterSingletonServiceState.STARTED;
}
final TestClusterSingletonServiceState getServiceState() {
@Override
public ListenableFuture<Void> closeServiceInstance() {
- this.serviceState = TestClusterSingletonServiceState.DESTROYED;
+ serviceState = TestClusterSingletonServiceState.DESTROYED;
return Futures.immediateFuture(null);
}
}
@Mock
public DOMEntityOwnershipService mockEos;
@Mock
- public DOMEntityOwnershipCandidateRegistration mockEntityCandReg;
+ public Registration mockEntityCandReg;
@Mock
- public DOMEntityOwnershipCandidateRegistration mockDoubleEntityCandReg;
+ public Registration mockDoubleEntityCandReg;
@Mock
public DOMEntityOwnershipListenerRegistration mockEosEntityListReg;
@Mock
import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
import org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState;
-import org.opendaylight.mdsal.eos.common.api.GenericEntityOwnershipCandidateRegistration;
import org.opendaylight.mdsal.eos.common.api.GenericEntityOwnershipChange;
import org.opendaylight.mdsal.eos.common.api.GenericEntityOwnershipListener;
import org.opendaylight.mdsal.eos.common.api.GenericEntityOwnershipService;
import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
import org.opendaylight.mdsal.singleton.dom.impl.util.TestEntity;
import org.opendaylight.mdsal.singleton.dom.impl.util.TestInstanceIdentifier;
+import org.opendaylight.yangtools.concepts.Registration;
/**
* Testing {@link ClusterSingletonServiceGroupImpl}.
@Mock
public ClusterSingletonService mockClusterSingletonServiceSecond;
@Mock
- public GenericEntityOwnershipCandidateRegistration<?, ?> mockEntityCandReg;
+ public Registration mockEntityCandReg;
@Mock
- public GenericEntityOwnershipCandidateRegistration<?, ?> mockCloseEntityCandReg;
+ public Registration mockCloseEntityCandReg;
@Mock
public GenericEntityOwnershipListener<TestInstanceIdentifier,
GenericEntityOwnershipChange<TestInstanceIdentifier, TestEntity>> mockEosListener;