eebd31fd49b53e8691194641f7d537b9a708bfbf
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / clustering / impl / LegacyEntityOwnershipServiceAdapter.java
1 /*
2  * Copyright (c) 2016 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.sal.dom.clustering.impl;
9
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;
28
29 /**
30  * Adapter that bridges between the legacy pre-Boron EntityOwnershipService and DOMEntityOwnershipService interfaces.
31  *
32  * @author Thomas Pantelis
33  */
34 public class LegacyEntityOwnershipServiceAdapter implements EntityOwnershipService, AutoCloseable {
35     private final DOMEntityOwnershipService domService;
36
37     public LegacyEntityOwnershipServiceAdapter(@Nonnull DOMEntityOwnershipService domService) {
38         this.domService = Preconditions.checkNotNull(domService);
39     }
40
41     @Override
42     public EntityOwnershipCandidateRegistration registerCandidate(Entity entity)
43             throws CandidateAlreadyRegisteredException {
44         try {
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);
49         }
50     }
51
52     @Override
53     public EntityOwnershipListenerRegistration registerListener(String entityType, EntityOwnershipListener listener) {
54         return new EntityOwnershipListenerRegistrationAdapter(entityType, listener,
55                 domService.registerListener(entityType, new DOMEntityOwnershipListenerAdapter(listener)));
56     }
57
58     @Override
59     public Optional<EntityOwnershipState> getOwnershipState(Entity forEntity) {
60         return toEntityOwnershipState(domService.getOwnershipState(toDOMEntity(forEntity)));
61     }
62
63     @Override
64     public boolean isCandidateRegistered(Entity entity) {
65         return domService.isCandidateRegistered(toDOMEntity(entity));
66     }
67
68     @Override
69     public void close() {
70     }
71
72     private DOMEntity toDOMEntity(Entity from) {
73         return new DOMEntity(from.getType(), from.getId());
74     }
75
76     private Optional<EntityOwnershipState> toEntityOwnershipState(
77             Optional<org.opendaylight.mdsal.eos.common.api.EntityOwnershipState> from) {
78         if(!from.isPresent()) {
79             return Optional.absent();
80         }
81
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));
86     }
87
88     private static class EntityOwnershipCandidateRegistrationAdapter extends AbstractObjectRegistration<Entity>
89             implements EntityOwnershipCandidateRegistration {
90         private final DOMEntityOwnershipCandidateRegistration domRegistration;
91
92         EntityOwnershipCandidateRegistrationAdapter(DOMEntityOwnershipCandidateRegistration domRegistration,
93                 Entity entity) {
94             super(entity);
95             this.domRegistration = Preconditions.checkNotNull(domRegistration);
96         }
97
98         @Override
99         protected void removeRegistration() {
100             domRegistration.close();
101         }
102     }
103
104     private static class EntityOwnershipListenerRegistrationAdapter extends AbstractObjectRegistration<EntityOwnershipListener>
105             implements EntityOwnershipListenerRegistration {
106         private final String entityType;
107         private final DOMEntityOwnershipListenerRegistration domRegistration;
108
109         EntityOwnershipListenerRegistrationAdapter(String entityType, EntityOwnershipListener listener,
110                 DOMEntityOwnershipListenerRegistration domRegistration) {
111             super(listener);
112             this.entityType = Preconditions.checkNotNull(entityType);
113             this.domRegistration = Preconditions.checkNotNull(domRegistration);
114         }
115
116         @Override
117         public String getEntityType() {
118             return entityType;
119         }
120
121         @Override
122         protected void removeRegistration() {
123             domRegistration.close();
124         }
125     }
126
127     private static class DOMEntityOwnershipListenerAdapter implements DOMEntityOwnershipListener {
128         private final EntityOwnershipListener delegateListener;
129
130         DOMEntityOwnershipListenerAdapter(EntityOwnershipListener delegateListener) {
131             this.delegateListener = Preconditions.checkNotNull(delegateListener);
132         }
133
134         @Override
135         public void ownershipChanged(DOMEntityOwnershipChange ownershipChange) {
136             Entity entity = new Entity(ownershipChange.getEntity().getType(), ownershipChange.getEntity().
137                     getIdentifier());
138             delegateListener.ownershipChanged(new EntityOwnershipChange(entity,
139                     ownershipChange.getState().wasOwner(), ownershipChange.getState().isOwner(),
140                     ownershipChange.getState().hasOwner(), ownershipChange.inJeopardy()));
141         }
142     }
143 }