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.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;
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 NodeId nodeId;
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;
51 public RoleContextImpl(final NodeId nodeId, final EntityOwnershipService entityOwnershipService, final Entity entity, final Entity txEntity, final LifecycleConductor lifecycleConductor) {
52 this.entityOwnershipService = entityOwnershipService;
54 this.txEntity = txEntity;
56 this.conductor = lifecycleConductor;
60 public boolean initialization() {
61 LOG.info("Initialization main candidate for node {}", nodeId);
62 return registerCandidate(this.entity);
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);
71 if (isTxCandidateRegistered()) {
72 unregisterCandidate(this.txEntity);
78 public <T> RequestContext<T> createRequestContext() {
79 return new AbstractRequestContext<T>(conductor.reserveXidForDeviceMessage(nodeId)) {
87 public void setSalRoleService(@Nonnull final SalRoleService salRoleService) {
88 Preconditions.checkNotNull(salRoleService);
89 this.salRoleService = salRoleService;
93 public SalRoleService getSalRoleService() {
94 return this.salRoleService;
98 public Entity getEntity() {
103 public Entity getTxEntity() {
104 return this.txEntity;
108 public NodeId getNodeId() {
113 public boolean isMainCandidateRegistered() {
114 return entityOwnershipCandidateRegistration != null;
118 public boolean isTxCandidateRegistered() {
119 return txEntityOwnershipCandidateRegistration != null;
123 public boolean registerCandidate(final Entity entity_) {
124 boolean permit = false;
126 permit = roleChangeGuard.tryAcquire(TIMEOUT, TimeUnit.SECONDS);
128 LOG.debug("Register candidate for entity {}", entity_);
129 if (entity_.equals(this.entity)) {
130 entityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(entity_);
132 txEntityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(entity_);
137 } catch (final CandidateAlreadyRegisteredException e) {
138 LOG.warn("Candidate for entity {} is already registered.", entity_.getType());
140 } catch (final InterruptedException e) {
141 LOG.warn("Cannot acquire semaphore for register entity {} candidate.", entity_.getType());
145 roleChangeGuard.release();
152 public boolean unregisterCandidate(final Entity entity_) {
153 boolean permit = false;
155 permit = roleChangeGuard.tryAcquire(TIMEOUT, TimeUnit.SECONDS);
157 if (entity_.equals(this.entity)) {
158 if (entityOwnershipCandidateRegistration != null) {
159 LOG.debug("Unregister candidate for entity {}", entity_);
160 entityOwnershipCandidateRegistration.close();
161 entityOwnershipCandidateRegistration = null;
164 if (txEntityOwnershipCandidateRegistration != null) {
165 LOG.debug("Unregister candidate for tx entity {}", entity_);
166 txEntityOwnershipCandidateRegistration.close();
167 txEntityOwnershipCandidateRegistration = null;
173 } catch (final InterruptedException e) {
174 LOG.warn("Cannot acquire semaphore for unregister entity {} candidate.", entity_.getType());
178 roleChangeGuard.release();
185 public void close() {
186 unregisterAllCandidates();
189 public boolean isMaster(){
190 return (txEntityOwnershipCandidateRegistration != null && entityOwnershipCandidateRegistration != null);