2 * Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.md.sal.dom.clustering.impl;
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
15 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
16 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
17 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
18 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
19 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
20 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
22 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
23 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration;
24 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange;
25 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener;
26 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListenerRegistration;
27 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService;
28 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
31 * Adapter that bridges between the legacy pre-Boron EntityOwnershipService and DOMEntityOwnershipService interfaces.
33 * @author Thomas Pantelis
36 public class LegacyEntityOwnershipServiceAdapter implements EntityOwnershipService, AutoCloseable {
37 private final DOMEntityOwnershipService domService;
39 public LegacyEntityOwnershipServiceAdapter(@Nonnull final DOMEntityOwnershipService domService) {
40 this.domService = Preconditions.checkNotNull(domService);
44 @SuppressWarnings("checkstyle:avoidHidingCauseException")
45 public EntityOwnershipCandidateRegistration registerCandidate(
46 final Entity entity) throws CandidateAlreadyRegisteredException {
48 return new EntityOwnershipCandidateRegistrationAdapter(domService.registerCandidate(toDOMEntity(entity)),
50 } catch (org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException e) {
51 throw new CandidateAlreadyRegisteredException(entity);
56 public EntityOwnershipListenerRegistration registerListener(final String entityType,
57 final EntityOwnershipListener listener) {
58 return new EntityOwnershipListenerRegistrationAdapter(entityType, listener, domService
59 .registerListener(entityType, new DOMEntityOwnershipListenerAdapter(listener)));
63 public Optional<EntityOwnershipState> getOwnershipState(final Entity forEntity) {
64 return toEntityOwnershipState(domService.getOwnershipState(toDOMEntity(forEntity)));
68 public boolean isCandidateRegistered(final Entity entity) {
69 return domService.isCandidateRegistered(toDOMEntity(entity));
76 private static DOMEntity toDOMEntity(final Entity from) {
77 return new DOMEntity(from.getType(), from.getId());
80 private static Optional<EntityOwnershipState> toEntityOwnershipState(
81 final Optional<org.opendaylight.mdsal.eos.common.api.EntityOwnershipState> from) {
82 if (!from.isPresent()) {
83 return Optional.absent();
86 org.opendaylight.mdsal.eos.common.api.EntityOwnershipState fromState = from.get();
87 return Optional.of(new EntityOwnershipState(
88 fromState == org.opendaylight.mdsal.eos.common.api.EntityOwnershipState.IS_OWNER,
89 fromState != org.opendaylight.mdsal.eos.common.api.EntityOwnershipState.NO_OWNER));
92 private static class EntityOwnershipCandidateRegistrationAdapter extends AbstractObjectRegistration<Entity>
93 implements EntityOwnershipCandidateRegistration {
94 private final DOMEntityOwnershipCandidateRegistration domRegistration;
96 EntityOwnershipCandidateRegistrationAdapter(final DOMEntityOwnershipCandidateRegistration domRegistration,
97 final Entity entity) {
99 this.domRegistration = Preconditions.checkNotNull(domRegistration);
103 protected void removeRegistration() {
104 domRegistration.close();
108 private static class EntityOwnershipListenerRegistrationAdapter extends
109 AbstractObjectRegistration<EntityOwnershipListener> implements EntityOwnershipListenerRegistration {
110 private final String entityType;
111 private final DOMEntityOwnershipListenerRegistration domRegistration;
113 EntityOwnershipListenerRegistrationAdapter(final String entityType, final EntityOwnershipListener listener,
114 final DOMEntityOwnershipListenerRegistration domRegistration) {
116 this.entityType = Preconditions.checkNotNull(entityType);
117 this.domRegistration = Preconditions.checkNotNull(domRegistration);
121 public String getEntityType() {
126 protected void removeRegistration() {
127 domRegistration.close();
131 private static class DOMEntityOwnershipListenerAdapter implements DOMEntityOwnershipListener {
132 private final EntityOwnershipListener delegateListener;
134 DOMEntityOwnershipListenerAdapter(final EntityOwnershipListener delegateListener) {
135 this.delegateListener = Preconditions.checkNotNull(delegateListener);
139 @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE")
140 public void ownershipChanged(final DOMEntityOwnershipChange ownershipChange) {
141 final DOMEntity domEntity = ownershipChange.getEntity();
142 Entity entity = new Entity(domEntity.getType(),
143 domEntity.getIdentifier());
144 delegateListener.ownershipChanged(new EntityOwnershipChange(entity, ownershipChange.getState().wasOwner(),
145 ownershipChange.getState().isOwner(),
146 ownershipChange.getState().hasOwner(),
147 ownershipChange.inJeopardy()));