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