Fix unused import warnings
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / role / RoleContextImpl.java
1 /**
2  * Copyright (c) 2015 Cisco 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.openflowplugin.impl.role;
9
10 import com.google.common.base.Preconditions;
11 import java.util.concurrent.Semaphore;
12 import java.util.concurrent.TimeUnit;
13 import javax.annotation.Nonnull;
14 import javax.annotation.Nullable;
15 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
16 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
17 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
18 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
19 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
20 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
21 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
22 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Role context hold information about entity ownership registration,
30  * register and unregister candidate (main and tx)
31  */
32 class RoleContextImpl implements RoleContext {
33
34     private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
35     private static final int TIMEOUT = 12;
36
37     private final NodeId nodeId;
38     private final EntityOwnershipService entityOwnershipService;
39     private volatile EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration = null;
40     private volatile EntityOwnershipCandidateRegistration txEntityOwnershipCandidateRegistration = null;
41
42     private final Entity entity;
43     private final Entity txEntity;
44
45     private SalRoleService salRoleService = null;
46
47     private final Semaphore roleChangeGuard = new Semaphore(1, true);
48
49     private final LifecycleConductor conductor;
50
51     public RoleContextImpl(final NodeId nodeId, final EntityOwnershipService entityOwnershipService, final Entity entity, final Entity txEntity, final LifecycleConductor lifecycleConductor) {
52         this.entityOwnershipService = entityOwnershipService;
53         this.entity = entity;
54         this.txEntity = txEntity;
55         this.nodeId = nodeId;
56         this.conductor = lifecycleConductor;
57     }
58
59     @Override
60     public boolean initialization() {
61         LOG.info("Initialization main candidate for node {}", nodeId);
62         return registerCandidate(this.entity);
63     }
64
65     @Override
66     public void unregisterAllCandidates() {
67         LOG.info("Role context closed, unregistering all candidates for ownership for node {}", nodeId);
68         if (isMainCandidateRegistered()) {
69             unregisterCandidate(this.entity);
70         }
71         if (isTxCandidateRegistered()) {
72             unregisterCandidate(this.txEntity);
73         }
74     }
75
76     @Nullable
77     @Override
78     public <T> RequestContext<T> createRequestContext() {
79         return new AbstractRequestContext<T>(conductor.reserveXidForDeviceMessage(nodeId)) {
80             @Override
81             public void close() {
82             }
83         };
84     }
85
86     @Override
87     public void setSalRoleService(@Nonnull final SalRoleService salRoleService) {
88         Preconditions.checkNotNull(salRoleService);
89         this.salRoleService = salRoleService;
90     }
91
92     @Override
93     public SalRoleService getSalRoleService() {
94         return this.salRoleService;
95     }
96
97     @Override
98     public Entity getEntity() {
99         return this.entity;
100     }
101
102     @Override
103     public Entity getTxEntity() {
104         return this.txEntity;
105     }
106
107     @Override
108     public NodeId getNodeId() {
109         return nodeId;
110     }
111
112     @Override
113     public boolean isMainCandidateRegistered() {
114         return entityOwnershipCandidateRegistration != null;
115     }
116
117     @Override
118     public boolean isTxCandidateRegistered() {
119         return txEntityOwnershipCandidateRegistration != null;
120     }
121
122     @Override
123     public boolean registerCandidate(final Entity entity_) {
124         boolean permit = false;
125         try {
126             permit = roleChangeGuard.tryAcquire(TIMEOUT, TimeUnit.SECONDS);
127             if(permit) {
128                 LOG.debug("Register candidate for entity {}", entity_);
129                 if (entity_.equals(this.entity)) {
130                     entityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(entity_);
131                 } else {
132                     txEntityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(entity_);
133                 }
134             } else {
135                 return false;
136             }
137         } catch (final CandidateAlreadyRegisteredException e) {
138             LOG.warn("Candidate for entity {} is already registered.", entity_.getType());
139             return false;
140         } catch (final InterruptedException e) {
141             LOG.warn("Cannot acquire semaphore for register entity {} candidate.", entity_.getType());
142             return false;
143         } finally {
144             if (permit) {
145                 roleChangeGuard.release();
146             }
147         }
148         return true;
149     }
150
151     @Override
152     public boolean unregisterCandidate(final Entity entity_) {
153         boolean permit = false;
154         try {
155             permit = roleChangeGuard.tryAcquire(TIMEOUT, TimeUnit.SECONDS);
156             if(permit) {
157                 if (entity_.equals(this.entity)) {
158                     if (entityOwnershipCandidateRegistration != null) {
159                         LOG.debug("Unregister candidate for entity {}", entity_);
160                         entityOwnershipCandidateRegistration.close();
161                         entityOwnershipCandidateRegistration = null;
162                     }
163                 } else {
164                     if (txEntityOwnershipCandidateRegistration != null) {
165                         LOG.debug("Unregister candidate for tx entity {}", entity_);
166                         txEntityOwnershipCandidateRegistration.close();
167                         txEntityOwnershipCandidateRegistration = null;
168                     }
169                 }
170             } else {
171                 return false;
172             }
173         } catch (final InterruptedException e) {
174             LOG.warn("Cannot acquire semaphore for unregister entity {} candidate.", entity_.getType());
175             return false;
176         } finally {
177             if (permit) {
178                 roleChangeGuard.release();
179             }
180         }
181         return true;
182     }
183
184     @Override
185     public void close() {
186         unregisterAllCandidates();
187     }
188
189     public boolean isMaster(){
190         return (txEntityOwnershipCandidateRegistration != null && entityOwnershipCandidateRegistration != null);
191     }
192 }