Bug 5596 Created lifecycle service
[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.mdsal.singleton.common.api.ServiceGroupIdentifier;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
21 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
22 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
23 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
24 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Role context hold information about entity ownership registration,
31  * register and unregister candidate (main and tx)
32  */
33 class RoleContextImpl implements RoleContext {
34
35     private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
36     private static final int TIMEOUT = 12;
37
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     private final DeviceInfo deviceInfo;
51     private CONTEXT_STATE state;
52
53     RoleContextImpl(final DeviceInfo deviceInfo,
54                     final EntityOwnershipService entityOwnershipService,
55                     final Entity entity,
56                     final Entity txEntity,
57                     final LifecycleConductor lifecycleConductor) {
58         this.entityOwnershipService = entityOwnershipService;
59         this.entity = entity;
60         this.txEntity = txEntity;
61         this.conductor = lifecycleConductor;
62         this.deviceInfo = deviceInfo;
63         state = CONTEXT_STATE.INITIALIZATION;
64     }
65
66     @Override
67     public boolean initialization() {
68         LOG.info("Initialization main candidate for node {}", getDeviceInfo().getNodeId());
69         setState(CONTEXT_STATE.WORKING);
70         return registerCandidate(this.entity);
71     }
72
73     @Override
74     public void unregisterAllCandidates() {
75         LOG.info("Role context closed, unregistering all candidates for ownership for node {}", getDeviceInfo().getNodeId());
76         if (isMainCandidateRegistered()) {
77             unregisterCandidate(this.entity);
78         }
79         if (isTxCandidateRegistered()) {
80             unregisterCandidate(this.txEntity);
81         }
82     }
83
84     @Nullable
85     @Override
86     public <T> RequestContext<T> createRequestContext() {
87         return new AbstractRequestContext<T>(conductor.reserveXidForDeviceMessage(getDeviceInfo())) {
88             @Override
89             public void close() {
90             }
91         };
92     }
93
94     @Override
95     public void setSalRoleService(@Nonnull final SalRoleService salRoleService) {
96         Preconditions.checkNotNull(salRoleService);
97         this.salRoleService = salRoleService;
98     }
99
100     @Override
101     public SalRoleService getSalRoleService() {
102         return this.salRoleService;
103     }
104
105     @Override
106     public Entity getEntity() {
107         return this.entity;
108     }
109
110     @Override
111     public Entity getTxEntity() {
112         return this.txEntity;
113     }
114
115     @Override
116     public boolean isMainCandidateRegistered() {
117         return entityOwnershipCandidateRegistration != null;
118     }
119
120     @Override
121     public boolean isTxCandidateRegistered() {
122         return txEntityOwnershipCandidateRegistration != null;
123     }
124
125     @Override
126     public boolean registerCandidate(final Entity entity_) {
127         boolean permit = false;
128         try {
129             permit = roleChangeGuard.tryAcquire(TIMEOUT, TimeUnit.SECONDS);
130             if(permit) {
131                 LOG.debug("Register candidate for entity {}", entity_);
132                 if (entity_.equals(this.entity)) {
133                     entityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(entity_);
134                 } else {
135                     txEntityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(entity_);
136                 }
137             } else {
138                 return false;
139             }
140         } catch (final CandidateAlreadyRegisteredException e) {
141             LOG.warn("Candidate for entity {} is already registered.", entity_.getType());
142             return false;
143         } catch (final InterruptedException e) {
144             LOG.warn("Cannot acquire semaphore for register entity {} candidate.", entity_.getType());
145             return false;
146         } finally {
147             if (permit) {
148                 roleChangeGuard.release();
149             }
150         }
151         return true;
152     }
153
154     @Override
155     public boolean unregisterCandidate(final Entity entity_) {
156         boolean permit = false;
157         try {
158             permit = roleChangeGuard.tryAcquire(TIMEOUT, TimeUnit.SECONDS);
159             if(permit) {
160                 if (entity_.equals(this.entity)) {
161                     if (entityOwnershipCandidateRegistration != null) {
162                         LOG.debug("Unregister candidate for entity {}", entity_);
163                         entityOwnershipCandidateRegistration.close();
164                         entityOwnershipCandidateRegistration = null;
165                     }
166                 } else {
167                     if (txEntityOwnershipCandidateRegistration != null) {
168                         LOG.debug("Unregister candidate for tx entity {}", entity_);
169                         txEntityOwnershipCandidateRegistration.close();
170                         txEntityOwnershipCandidateRegistration = null;
171                     }
172                 }
173             } else {
174                 return false;
175             }
176         } catch (final InterruptedException e) {
177             LOG.warn("Cannot acquire semaphore for unregister entity {} candidate.", entity_.getType());
178             return false;
179         } finally {
180             if (permit) {
181                 roleChangeGuard.release();
182             }
183         }
184         return true;
185     }
186
187     @Override
188     public void close() {
189         setState(CONTEXT_STATE.TERMINATION);
190         unregisterAllCandidates();
191     }
192
193     public boolean isMaster(){
194         return (txEntityOwnershipCandidateRegistration != null && entityOwnershipCandidateRegistration != null);
195     }
196
197     @Override
198     public CONTEXT_STATE getState() {
199         return this.state;
200     }
201
202     @Override
203     public void setState(CONTEXT_STATE state) {
204         this.state = state;
205     }
206
207     @Override
208     public ServiceGroupIdentifier getServiceIdentifier() {
209         return this.deviceInfo.getServiceIdentifier();
210     }
211
212     @Override
213     public DeviceInfo getDeviceInfo() {
214         return this.deviceInfo;
215     }
216
217 }