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 DOMEntityOwnershipService domService) {
39 this.domService = Preconditions.checkNotNull(domService);
43 public EntityOwnershipCandidateRegistration registerCandidate(Entity entity)
44 throws CandidateAlreadyRegisteredException {
46 return new EntityOwnershipCandidateRegistrationAdapter(domService.registerCandidate(
47 toDOMEntity(entity)), entity);
48 } catch(org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException e) {
49 throw new CandidateAlreadyRegisteredException(entity);
54 public EntityOwnershipListenerRegistration registerListener(String entityType, EntityOwnershipListener listener) {
55 return new EntityOwnershipListenerRegistrationAdapter(entityType, listener,
56 domService.registerListener(entityType, new DOMEntityOwnershipListenerAdapter(listener)));
60 public Optional<EntityOwnershipState> getOwnershipState(Entity forEntity) {
61 return toEntityOwnershipState(domService.getOwnershipState(toDOMEntity(forEntity)));
65 public boolean isCandidateRegistered(Entity entity) {
66 return domService.isCandidateRegistered(toDOMEntity(entity));
73 private DOMEntity toDOMEntity(Entity from) {
74 return new DOMEntity(from.getType(), from.getId());
77 private Optional<EntityOwnershipState> toEntityOwnershipState(
78 Optional<org.opendaylight.mdsal.eos.common.api.EntityOwnershipState> from) {
79 if(!from.isPresent()) {
80 return Optional.absent();
83 org.opendaylight.mdsal.eos.common.api.EntityOwnershipState fromState = from.get();
84 return Optional.of(new EntityOwnershipState(
85 fromState == org.opendaylight.mdsal.eos.common.api.EntityOwnershipState.IS_OWNER,
86 fromState != org.opendaylight.mdsal.eos.common.api.EntityOwnershipState.NO_OWNER));
89 private static class EntityOwnershipCandidateRegistrationAdapter extends AbstractObjectRegistration<Entity>
90 implements EntityOwnershipCandidateRegistration {
91 private final DOMEntityOwnershipCandidateRegistration domRegistration;
93 EntityOwnershipCandidateRegistrationAdapter(DOMEntityOwnershipCandidateRegistration domRegistration,
96 this.domRegistration = Preconditions.checkNotNull(domRegistration);
100 protected void removeRegistration() {
101 domRegistration.close();
105 private static class EntityOwnershipListenerRegistrationAdapter extends AbstractObjectRegistration<EntityOwnershipListener>
106 implements EntityOwnershipListenerRegistration {
107 private final String entityType;
108 private final DOMEntityOwnershipListenerRegistration domRegistration;
110 EntityOwnershipListenerRegistrationAdapter(String entityType, EntityOwnershipListener listener,
111 DOMEntityOwnershipListenerRegistration domRegistration) {
113 this.entityType = Preconditions.checkNotNull(entityType);
114 this.domRegistration = Preconditions.checkNotNull(domRegistration);
118 public String getEntityType() {
123 protected void removeRegistration() {
124 domRegistration.close();
128 private static class DOMEntityOwnershipListenerAdapter implements DOMEntityOwnershipListener {
129 private final EntityOwnershipListener delegateListener;
131 DOMEntityOwnershipListenerAdapter(EntityOwnershipListener delegateListener) {
132 this.delegateListener = Preconditions.checkNotNull(delegateListener);
136 public void ownershipChanged(DOMEntityOwnershipChange ownershipChange) {
137 Entity entity = new Entity(ownershipChange.getEntity().getType(), ownershipChange.getEntity().
139 delegateListener.ownershipChanged(new EntityOwnershipChange(entity,
140 ownershipChange.getState().wasOwner(), ownershipChange.getState().isOwner(),
141 ownershipChange.getState().hasOwner(), ownershipChange.inJeopardy()));