Merge "BUG-4148: Improving the logging in flow/group/meter to provide more contextual...
[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
12 import java.util.concurrent.Semaphore;
13 import java.util.concurrent.TimeUnit;
14 import javax.annotation.Nonnull;
15 import javax.annotation.Nullable;
16
17 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
18 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
19 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
20 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
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.LifecycleConductorImpl;
25 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Role context hold information about entity ownership registration,
33  * register and unregister candidate (main and tx)
34  */
35 class RoleContextImpl implements RoleContext {
36
37     private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
38     private static final int TIMEOUT = 12;
39
40     private final NodeId nodeId;
41     private final EntityOwnershipService entityOwnershipService;
42     private volatile EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration = null;
43     private volatile EntityOwnershipCandidateRegistration txEntityOwnershipCandidateRegistration = null;
44
45     private final Entity entity;
46     private final Entity txEntity;
47
48     private SalRoleService salRoleService = null;
49
50     private final Semaphore roleChangeGuard = new Semaphore(1, true);
51
52     private final LifecycleConductor conductor;
53
54     public RoleContextImpl(final NodeId nodeId, final EntityOwnershipService entityOwnershipService, final Entity entity, final Entity txEntity, final LifecycleConductor lifecycleConductor) {
55         this.entityOwnershipService = entityOwnershipService;
56         this.entity = entity;
57         this.txEntity = txEntity;
58         this.nodeId = nodeId;
59         this.conductor = lifecycleConductor;
60     }
61
62     @Override
63     public boolean initialization() {
64         LOG.info("Initialization main candidate for node {}", nodeId);
65         return registerCandidate(this.entity);
66     }
67
68     @Override
69     public void unregisterAllCandidates() {
70         LOG.info("Role context closed, unregistering all candidates for ownership for node {}", nodeId);
71         if (isMainCandidateRegistered()) {
72             unregisterCandidate(this.entity);
73         }
74         if (isTxCandidateRegistered()) {
75             unregisterCandidate(this.txEntity);
76         }
77     }
78
79     @Nullable
80     @Override
81     public <T> RequestContext<T> createRequestContext() {
82         return new AbstractRequestContext<T>(conductor.reserveXidForDeviceMessage(nodeId)) {
83             @Override
84             public void close() {
85             }
86         };
87     }
88
89     @Override
90     public void setSalRoleService(@Nonnull final SalRoleService salRoleService) {
91         Preconditions.checkNotNull(salRoleService);
92         this.salRoleService = salRoleService;
93     }
94
95     @Override
96     public SalRoleService getSalRoleService() {
97         return this.salRoleService;
98     }
99
100     @Override
101     public Entity getEntity() {
102         return this.entity;
103     }
104
105     @Override
106     public Entity getTxEntity() {
107         return this.txEntity;
108     }
109
110     @Override
111     public NodeId getNodeId() {
112         return nodeId;
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         unregisterAllCandidates();
190     }
191
192     public boolean isMaster(){
193         return (txEntityOwnershipCandidateRegistration != null && entityOwnershipCandidateRegistration != null);
194     }
195 }