Inject MD_SAL services directly in sal-binding-it tests
[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 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;
29
30 /**
31  * Adapter that bridges between the legacy pre-Boron EntityOwnershipService and DOMEntityOwnershipService interfaces.
32  *
33  * @author Thomas Pantelis
34  */
35 @Deprecated
36 public class LegacyEntityOwnershipServiceAdapter implements EntityOwnershipService, AutoCloseable {
37     private final DOMEntityOwnershipService domService;
38
39     public LegacyEntityOwnershipServiceAdapter(@Nonnull final DOMEntityOwnershipService domService) {
40         this.domService = Preconditions.checkNotNull(domService);
41     }
42
43     @Override
44     @SuppressWarnings("checkstyle:avoidHidingCauseException")
45     public EntityOwnershipCandidateRegistration registerCandidate(
46             final Entity entity) throws CandidateAlreadyRegisteredException {
47         try {
48             return new EntityOwnershipCandidateRegistrationAdapter(domService.registerCandidate(toDOMEntity(entity)),
49                                                                    entity);
50         } catch (org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException e) {
51             throw new CandidateAlreadyRegisteredException(entity);
52         }
53     }
54
55     @Override
56     public EntityOwnershipListenerRegistration registerListener(final String entityType,
57             final EntityOwnershipListener listener) {
58         return new EntityOwnershipListenerRegistrationAdapter(entityType, listener, domService
59                 .registerListener(entityType, new DOMEntityOwnershipListenerAdapter(listener)));
60     }
61
62     @Override
63     public Optional<EntityOwnershipState> getOwnershipState(final Entity forEntity) {
64         return toEntityOwnershipState(domService.getOwnershipState(toDOMEntity(forEntity)));
65     }
66
67     @Override
68     public boolean isCandidateRegistered(final Entity entity) {
69         return domService.isCandidateRegistered(toDOMEntity(entity));
70     }
71
72     @Override
73     public void close() {
74     }
75
76     private static DOMEntity toDOMEntity(final Entity from) {
77         return new DOMEntity(from.getType(), from.getId());
78     }
79
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();
84         }
85
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));
90     }
91
92     private static class EntityOwnershipCandidateRegistrationAdapter extends AbstractObjectRegistration<Entity>
93             implements EntityOwnershipCandidateRegistration {
94         private final DOMEntityOwnershipCandidateRegistration domRegistration;
95
96         EntityOwnershipCandidateRegistrationAdapter(final DOMEntityOwnershipCandidateRegistration domRegistration,
97                                                     final Entity entity) {
98             super(entity);
99             this.domRegistration = Preconditions.checkNotNull(domRegistration);
100         }
101
102         @Override
103         protected void removeRegistration() {
104             domRegistration.close();
105         }
106     }
107
108     private static class EntityOwnershipListenerRegistrationAdapter extends
109             AbstractObjectRegistration<EntityOwnershipListener> implements EntityOwnershipListenerRegistration {
110         private final String entityType;
111         private final DOMEntityOwnershipListenerRegistration domRegistration;
112
113         EntityOwnershipListenerRegistrationAdapter(final String entityType, final EntityOwnershipListener listener,
114                                                    final DOMEntityOwnershipListenerRegistration domRegistration) {
115             super(listener);
116             this.entityType = Preconditions.checkNotNull(entityType);
117             this.domRegistration = Preconditions.checkNotNull(domRegistration);
118         }
119
120         @Override
121         public String getEntityType() {
122             return entityType;
123         }
124
125         @Override
126         protected void removeRegistration() {
127             domRegistration.close();
128         }
129     }
130
131     private static class DOMEntityOwnershipListenerAdapter implements DOMEntityOwnershipListener {
132         private final EntityOwnershipListener delegateListener;
133
134         DOMEntityOwnershipListenerAdapter(final EntityOwnershipListener delegateListener) {
135             this.delegateListener = Preconditions.checkNotNull(delegateListener);
136         }
137
138         @Override
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()));
148         }
149     }
150 }