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 javax.annotation.Nullable;
11 import java.util.concurrent.Future;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.base.Preconditions;
15 import com.google.common.util.concurrent.FutureCallback;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.JdkFutureAdapters;
18 import com.google.common.util.concurrent.SettableFuture;
19 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
20 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
21 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
23 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
24 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
26 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
27 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
28 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
29 import org.opendaylight.openflowplugin.impl.services.SalRoleServiceImpl;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * Created by kramesha on 9/12/15.
43 public class RoleContextImpl implements RoleContext {
44 private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
46 private final EntityOwnershipService entityOwnershipService;
47 private EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration;
48 private final DeviceContext deviceContext;
49 private final Entity entity;
50 private SalRoleService salRoleService;
52 private final SettableFuture<OfpRole> initRoleChangeFuture;
54 public RoleContextImpl(final DeviceContext deviceContext, final EntityOwnershipService entityOwnershipService, final Entity entity) {
55 this.entityOwnershipService = Preconditions.checkNotNull(entityOwnershipService);
56 this.deviceContext = Preconditions.checkNotNull(deviceContext);
57 this.entity = Preconditions.checkNotNull(entity);
59 salRoleService = new SalRoleServiceImpl(this, deviceContext);
61 initRoleChangeFuture = SettableFuture.create();
65 public Future<OfpRole> initialization() throws CandidateAlreadyRegisteredException {
66 LOG.debug("Initialization requestOpenflowEntityOwnership for entity {}", entity);
67 entityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(entity);
68 LOG.info("RoleContextImpl : Candidate registered with ownership service for device :{}", deviceContext
69 .getPrimaryConnectionContext().getNodeId().getValue());
70 return initRoleChangeFuture;
74 public void onRoleChanged(final OfpRole oldRole, final OfpRole newRole) {
75 LOG.trace("onRoleChanged method call for Entity {}", entity);
77 if (!isDeviceConnected()) {
78 // this can happen as after the disconnect, we still get a last messsage from EntityOwnershipService.
79 LOG.info("Device {} is disconnected from this node. Hence not attempting a role change.",
80 deviceContext.getPrimaryConnectionContext().getNodeId());
81 if (!initRoleChangeFuture.isDone()) {
82 LOG.debug("RoleChange is not valid for initialization Entity {} anymore - Device is disconnected", entity);
83 initRoleChangeFuture.cancel(true);
88 if (!initRoleChangeFuture.isDone()) {
89 LOG.debug("Initialization Role for entity {} is chosed {}", entity, newRole);
90 // we don't want to wait for Device RoleChangeResponse in initial phase
91 deviceContext.onClusterRoleChange(newRole);
92 initRoleChangeFuture.set(newRole);
95 LOG.debug("Role change received from ownership listener from {} to {} for device:{}", oldRole, newRole,
96 deviceContext.getPrimaryConnectionContext().getNodeId());
98 final SetRoleInput setRoleInput = (new SetRoleInputBuilder())
99 .setControllerRole(newRole)
100 .setNode(new NodeRef(deviceContext.getDeviceState().getNodeInstanceIdentifier()))
103 final Future<RpcResult<SetRoleOutput>> setRoleOutputFuture = salRoleService.setRole(setRoleInput);
105 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(setRoleOutputFuture), new FutureCallback<RpcResult<SetRoleOutput>>() {
107 public void onSuccess(final RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
108 LOG.debug("Rolechange {} successful made on switch :{}", newRole,
109 deviceContext.getPrimaryConnectionContext().getNodeId());
110 deviceContext.getDeviceState().setRole(newRole);
111 deviceContext.onClusterRoleChange(newRole);
115 public void onFailure(final Throwable throwable) {
116 LOG.error("Error in setRole {} for device {} ", newRole,
117 deviceContext.getPrimaryConnectionContext().getNodeId(), throwable);
123 public void close() {
124 if (entityOwnershipCandidateRegistration != null) {
125 LOG.debug("Closing EntityOwnershipCandidateRegistration for {}", entity);
126 entityOwnershipCandidateRegistration.close();
131 public Entity getEntity() {
135 private boolean isDeviceConnected() {
136 return ConnectionContext.CONNECTION_STATE.WORKING.equals(
137 deviceContext.getPrimaryConnectionContext().getConnectionState());
142 public <T> RequestContext<T> createRequestContext() {
143 final AbstractRequestContext<T> ret = new AbstractRequestContext<T>(deviceContext.getReservedXid()) {
145 public void close() {
152 void setSalRoleService(final SalRoleService salRoleService) {
153 this.salRoleService = salRoleService;
157 public DeviceState getDeviceState() {
158 return deviceContext.getDeviceState();