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 javax.annotation.Nonnull;
13 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
14 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
15 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
16 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
17 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
18 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
19 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
20 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
21 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
22 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration;
23 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange;
24 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener;
25 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListenerRegistration;
26 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService;
27 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
30 * Adapter that bridges between the legacy pre-Boron EntityOwnershipService and DOMEntityOwnershipService interfaces.
32 * @author Thomas Pantelis
35 public class LegacyEntityOwnershipServiceAdapter implements EntityOwnershipService, AutoCloseable {
36 private final DOMEntityOwnershipService domService;
38 public LegacyEntityOwnershipServiceAdapter(@Nonnull final DOMEntityOwnershipService domService) {
39 this.domService = Preconditions.checkNotNull(domService);
43 @SuppressWarnings("checkstyle:avoidHidingCauseException")
44 public EntityOwnershipCandidateRegistration registerCandidate(
45 final Entity entity) throws CandidateAlreadyRegisteredException {
47 return new EntityOwnershipCandidateRegistrationAdapter(domService.registerCandidate(toDOMEntity(entity)),
49 } catch (org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException e) {
50 throw new CandidateAlreadyRegisteredException(entity);
55 public EntityOwnershipListenerRegistration registerListener(final String entityType,
56 final EntityOwnershipListener listener) {
57 return new EntityOwnershipListenerRegistrationAdapter(entityType, listener, domService
58 .registerListener(entityType, new DOMEntityOwnershipListenerAdapter(listener)));
62 public Optional<EntityOwnershipState> getOwnershipState(final Entity forEntity) {
63 return toEntityOwnershipState(domService.getOwnershipState(toDOMEntity(forEntity)));
67 public boolean isCandidateRegistered(final Entity entity) {
68 return domService.isCandidateRegistered(toDOMEntity(entity));
75 private static DOMEntity toDOMEntity(final Entity from) {
76 return new DOMEntity(from.getType(), from.getId());
79 private static Optional<EntityOwnershipState> toEntityOwnershipState(
80 final Optional<org.opendaylight.mdsal.eos.common.api.EntityOwnershipState> from) {
81 if (!from.isPresent()) {
82 return Optional.absent();
85 org.opendaylight.mdsal.eos.common.api.EntityOwnershipState fromState = from.get();
86 return Optional.of(new EntityOwnershipState(
87 fromState == org.opendaylight.mdsal.eos.common.api.EntityOwnershipState.IS_OWNER,
88 fromState != org.opendaylight.mdsal.eos.common.api.EntityOwnershipState.NO_OWNER));
91 private static class EntityOwnershipCandidateRegistrationAdapter extends AbstractObjectRegistration<Entity>
92 implements EntityOwnershipCandidateRegistration {
93 private final DOMEntityOwnershipCandidateRegistration domRegistration;
95 EntityOwnershipCandidateRegistrationAdapter(final DOMEntityOwnershipCandidateRegistration domRegistration,
96 final Entity entity) {
98 this.domRegistration = Preconditions.checkNotNull(domRegistration);
102 protected void removeRegistration() {
103 domRegistration.close();
107 private static class EntityOwnershipListenerRegistrationAdapter extends
108 AbstractObjectRegistration<EntityOwnershipListener> implements EntityOwnershipListenerRegistration {
109 private final String entityType;
110 private final DOMEntityOwnershipListenerRegistration domRegistration;
112 EntityOwnershipListenerRegistrationAdapter(final String entityType, final EntityOwnershipListener listener,
113 final DOMEntityOwnershipListenerRegistration domRegistration) {
115 this.entityType = Preconditions.checkNotNull(entityType);
116 this.domRegistration = Preconditions.checkNotNull(domRegistration);
120 public String getEntityType() {
125 protected void removeRegistration() {
126 domRegistration.close();
130 private static class DOMEntityOwnershipListenerAdapter implements DOMEntityOwnershipListener {
131 private final EntityOwnershipListener delegateListener;
133 DOMEntityOwnershipListenerAdapter(final EntityOwnershipListener delegateListener) {
134 this.delegateListener = Preconditions.checkNotNull(delegateListener);
138 public void ownershipChanged(final DOMEntityOwnershipChange ownershipChange) {
139 Entity entity = new Entity(ownershipChange.getEntity().getType(),
140 ownershipChange.getEntity().getIdentifier());
141 delegateListener.ownershipChanged(new EntityOwnershipChange(entity, ownershipChange.getState().wasOwner(),
142 ownershipChange.getState().isOwner(),
143 ownershipChange.getState().hasOwner(),
144 ownershipChange.inJeopardy()));