Fix checkstyle issues in module sal-dom-broker
[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 DOMEntityOwnershipService domService) {
39         this.domService = Preconditions.checkNotNull(domService);
40     }
41
42     @Override
43     public EntityOwnershipCandidateRegistration registerCandidate(
44             Entity entity) throws CandidateAlreadyRegisteredException {
45         try {
46             return new EntityOwnershipCandidateRegistrationAdapter(domService.registerCandidate(toDOMEntity(entity)),
47                                                                    entity);
48         } catch (org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException e) {
49             throw new CandidateAlreadyRegisteredException(entity);
50         }
51     }
52
53     @Override
54     public EntityOwnershipListenerRegistration registerListener(String entityType, EntityOwnershipListener listener) {
55         return new EntityOwnershipListenerRegistrationAdapter(entityType, listener, domService
56                 .registerListener(entityType, new DOMEntityOwnershipListenerAdapter(listener)));
57     }
58
59     @Override
60     public Optional<EntityOwnershipState> getOwnershipState(Entity forEntity) {
61         return toEntityOwnershipState(domService.getOwnershipState(toDOMEntity(forEntity)));
62     }
63
64     @Override
65     public boolean isCandidateRegistered(Entity entity) {
66         return domService.isCandidateRegistered(toDOMEntity(entity));
67     }
68
69     @Override
70     public void close() {
71     }
72
73     private DOMEntity toDOMEntity(Entity from) {
74         return new DOMEntity(from.getType(), from.getId());
75     }
76
77     private Optional<EntityOwnershipState> toEntityOwnershipState(
78             Optional<org.opendaylight.mdsal.eos.common.api.EntityOwnershipState> from) {
79         if (!from.isPresent()) {
80             return Optional.absent();
81         }
82
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));
87     }
88
89     private static class EntityOwnershipCandidateRegistrationAdapter extends AbstractObjectRegistration<Entity>
90             implements EntityOwnershipCandidateRegistration {
91         private final DOMEntityOwnershipCandidateRegistration domRegistration;
92
93         EntityOwnershipCandidateRegistrationAdapter(DOMEntityOwnershipCandidateRegistration domRegistration,
94                                                     Entity entity) {
95             super(entity);
96             this.domRegistration = Preconditions.checkNotNull(domRegistration);
97         }
98
99         @Override
100         protected void removeRegistration() {
101             domRegistration.close();
102         }
103     }
104
105     private static class EntityOwnershipListenerRegistrationAdapter extends
106             AbstractObjectRegistration<EntityOwnershipListener> implements EntityOwnershipListenerRegistration {
107         private final String entityType;
108         private final DOMEntityOwnershipListenerRegistration domRegistration;
109
110         EntityOwnershipListenerRegistrationAdapter(String entityType, EntityOwnershipListener listener,
111                                                    DOMEntityOwnershipListenerRegistration domRegistration) {
112             super(listener);
113             this.entityType = Preconditions.checkNotNull(entityType);
114             this.domRegistration = Preconditions.checkNotNull(domRegistration);
115         }
116
117         @Override
118         public String getEntityType() {
119             return entityType;
120         }
121
122         @Override
123         protected void removeRegistration() {
124             domRegistration.close();
125         }
126     }
127
128     private static class DOMEntityOwnershipListenerAdapter implements DOMEntityOwnershipListener {
129         private final EntityOwnershipListener delegateListener;
130
131         DOMEntityOwnershipListenerAdapter(EntityOwnershipListener delegateListener) {
132             this.delegateListener = Preconditions.checkNotNull(delegateListener);
133         }
134
135         @Override
136         public void ownershipChanged(DOMEntityOwnershipChange ownershipChange) {
137             Entity entity = new Entity(ownershipChange.getEntity().getType(),
138                                        ownershipChange.getEntity().getIdentifier());
139             delegateListener.ownershipChanged(new EntityOwnershipChange(entity, ownershipChange.getState().wasOwner(),
140                                                                         ownershipChange.getState().isOwner(),
141                                                                         ownershipChange.getState().hasOwner(),
142                                                                         ownershipChange.inJeopardy()));
143         }
144     }
145 }