2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.role;
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.DeviceInfo;
20 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
21 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
22 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
23 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Role context hold information about entity ownership registration,
30 * register and unregister candidate (main and tx)
32 class RoleContextImpl implements RoleContext {
34 private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
35 private static final int TIMEOUT = 12;
37 private final DeviceInfo deviceInfo;
38 private final EntityOwnershipService entityOwnershipService;
39 private volatile EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration = null;
40 private volatile EntityOwnershipCandidateRegistration txEntityOwnershipCandidateRegistration = null;
42 private final Entity entity;
43 private final Entity txEntity;
45 private SalRoleService salRoleService = null;
47 private final Semaphore roleChangeGuard = new Semaphore(1, true);
49 private final LifecycleConductor conductor;
50 private volatile CONTEXT_STATE contextState;
52 RoleContextImpl(final DeviceInfo deviceInfo, final EntityOwnershipService entityOwnershipService, final Entity entity, final Entity txEntity, final LifecycleConductor lifecycleConductor) {
53 this.entityOwnershipService = entityOwnershipService;
55 this.txEntity = txEntity;
56 this.deviceInfo = deviceInfo;
57 this.conductor = lifecycleConductor;
58 contextState = CONTEXT_STATE.INITIALIZATION;
62 public boolean initialization() {
63 LOG.info("Initialization main candidate for node {}", deviceInfo.getNodeId());
64 contextState = CONTEXT_STATE.WORKING;
65 return registerCandidate(this.entity);
69 public void unregisterAllCandidates() {
70 LOG.info("Role context closed, unregistering all candidates for ownership for node {}", deviceInfo.getNodeId());
71 if (isMainCandidateRegistered()) {
72 unregisterCandidate(this.entity);
74 if (isTxCandidateRegistered()) {
75 unregisterCandidate(this.txEntity);
81 public <T> RequestContext<T> createRequestContext() {
82 return new AbstractRequestContext<T>(conductor.reserveXidForDeviceMessage(deviceInfo)) {
90 public void setSalRoleService(@Nonnull final SalRoleService salRoleService) {
91 Preconditions.checkNotNull(salRoleService);
92 this.salRoleService = salRoleService;
96 public SalRoleService getSalRoleService() {
97 return this.salRoleService;
101 public Entity getEntity() {
106 public Entity getTxEntity() {
107 return this.txEntity;
111 public DeviceInfo getDeviceInfo() {
116 public boolean isMainCandidateRegistered() {
117 return entityOwnershipCandidateRegistration != null;
121 public boolean isTxCandidateRegistered() {
122 return txEntityOwnershipCandidateRegistration != null;
126 public boolean registerCandidate(final Entity entity_) {
127 boolean permit = false;
129 permit = roleChangeGuard.tryAcquire(TIMEOUT, TimeUnit.SECONDS);
131 LOG.debug("Register candidate for entity {}", entity_);
132 if (entity_.equals(this.entity)) {
133 entityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(entity_);
135 txEntityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(entity_);
140 } catch (final CandidateAlreadyRegisteredException e) {
141 LOG.warn("Candidate for entity {} is already registered.", entity_.getType());
143 } catch (final InterruptedException e) {
144 LOG.warn("Cannot acquire semaphore for register entity {} candidate.", entity_.getType());
148 roleChangeGuard.release();
155 public boolean unregisterCandidate(final Entity entity_) {
156 boolean permit = false;
158 permit = roleChangeGuard.tryAcquire(TIMEOUT, TimeUnit.SECONDS);
160 if (entity_.equals(this.entity)) {
161 if (entityOwnershipCandidateRegistration != null) {
162 LOG.debug("Unregister candidate for entity {}", entity_);
163 entityOwnershipCandidateRegistration.close();
164 entityOwnershipCandidateRegistration = null;
167 if (txEntityOwnershipCandidateRegistration != null) {
168 LOG.debug("Unregister candidate for tx entity {}", entity_);
169 txEntityOwnershipCandidateRegistration.close();
170 txEntityOwnershipCandidateRegistration = null;
176 } catch (final InterruptedException e) {
177 LOG.warn("Cannot acquire semaphore for unregister entity {} candidate.", entity_.getType());
181 roleChangeGuard.release();
188 public void close() {
189 contextState = CONTEXT_STATE.TERMINATION;
190 unregisterAllCandidates();
193 public boolean isMaster(){
194 return (txEntityOwnershipCandidateRegistration != null && entityOwnershipCandidateRegistration != null);
198 public CONTEXT_STATE getState() {
203 public void setState(CONTEXT_STATE contextState) {
204 this.contextState = contextState;