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
34 public class LegacyEntityOwnershipServiceAdapter implements EntityOwnershipService, AutoCloseable {
35 private final DOMEntityOwnershipService domService;
37 public LegacyEntityOwnershipServiceAdapter(@Nonnull DOMEntityOwnershipService domService) {
38 this.domService = Preconditions.checkNotNull(domService);
42 public EntityOwnershipCandidateRegistration registerCandidate(Entity entity)
43 throws CandidateAlreadyRegisteredException {
45 return new EntityOwnershipCandidateRegistrationAdapter(domService.registerCandidate(
46 toDOMEntity(entity)), entity);
47 } catch(org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException e) {
48 throw new CandidateAlreadyRegisteredException(entity);
53 public EntityOwnershipListenerRegistration registerListener(String entityType, EntityOwnershipListener listener) {
54 return new EntityOwnershipListenerRegistrationAdapter(entityType, listener,
55 domService.registerListener(entityType, new DOMEntityOwnershipListenerAdapter(listener)));
59 public Optional<EntityOwnershipState> getOwnershipState(Entity forEntity) {
60 return toEntityOwnershipState(domService.getOwnershipState(toDOMEntity(forEntity)));
64 public boolean isCandidateRegistered(Entity entity) {
65 return domService.isCandidateRegistered(toDOMEntity(entity));
72 private DOMEntity toDOMEntity(Entity from) {
73 return new DOMEntity(from.getType(), from.getId());
76 private Optional<EntityOwnershipState> toEntityOwnershipState(
77 Optional<org.opendaylight.mdsal.eos.common.api.EntityOwnershipState> from) {
78 if(!from.isPresent()) {
79 return Optional.absent();
82 org.opendaylight.mdsal.eos.common.api.EntityOwnershipState fromState = from.get();
83 return Optional.of(new EntityOwnershipState(
84 fromState == org.opendaylight.mdsal.eos.common.api.EntityOwnershipState.IS_OWNER,
85 fromState != org.opendaylight.mdsal.eos.common.api.EntityOwnershipState.NO_OWNER));
88 private static class EntityOwnershipCandidateRegistrationAdapter extends AbstractObjectRegistration<Entity>
89 implements EntityOwnershipCandidateRegistration {
90 private final DOMEntityOwnershipCandidateRegistration domRegistration;
92 EntityOwnershipCandidateRegistrationAdapter(DOMEntityOwnershipCandidateRegistration domRegistration,
95 this.domRegistration = Preconditions.checkNotNull(domRegistration);
99 protected void removeRegistration() {
100 domRegistration.close();
104 private static class EntityOwnershipListenerRegistrationAdapter extends AbstractObjectRegistration<EntityOwnershipListener>
105 implements EntityOwnershipListenerRegistration {
106 private final String entityType;
107 private final DOMEntityOwnershipListenerRegistration domRegistration;
109 EntityOwnershipListenerRegistrationAdapter(String entityType, EntityOwnershipListener listener,
110 DOMEntityOwnershipListenerRegistration domRegistration) {
112 this.entityType = Preconditions.checkNotNull(entityType);
113 this.domRegistration = Preconditions.checkNotNull(domRegistration);
117 public String getEntityType() {
122 protected void removeRegistration() {
123 domRegistration.close();
127 private static class DOMEntityOwnershipListenerAdapter implements DOMEntityOwnershipListener {
128 private final EntityOwnershipListener delegateListener;
130 DOMEntityOwnershipListenerAdapter(EntityOwnershipListener delegateListener) {
131 this.delegateListener = Preconditions.checkNotNull(delegateListener);
135 public void ownershipChanged(DOMEntityOwnershipChange ownershipChange) {
136 Entity entity = new Entity(ownershipChange.getEntity().getType(), ownershipChange.getEntity().
138 delegateListener.ownershipChanged(new EntityOwnershipChange(entity,
139 ownershipChange.getState().wasOwner(), ownershipChange.getState().isOwner(),
140 ownershipChange.getState().hasOwner(), ownershipChange.inJeopardy()));