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.ListenableFuture;
19 import com.google.common.util.concurrent.SettableFuture;
20 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
21 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
22 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
23 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
24 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
27 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
28 import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
29 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
30 import org.opendaylight.openflowplugin.impl.services.SalRoleServiceImpl;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Created by kramesha on 9/12/15.
44 public class RoleContextImpl implements RoleContext {
45 private static final Logger LOG = LoggerFactory.getLogger(RoleContextImpl.class);
47 private final EntityOwnershipService entityOwnershipService;
48 private EntityOwnershipCandidateRegistration entityOwnershipCandidateRegistration;
49 private final DeviceContext deviceContext;
50 private final Entity entity;
51 private SalRoleService salRoleService;
53 private final SettableFuture<OfpRole> initRoleChangeFuture;
55 public RoleContextImpl(final DeviceContext deviceContext, final EntityOwnershipService entityOwnershipService, final Entity entity) {
56 this.entityOwnershipService = Preconditions.checkNotNull(entityOwnershipService);
57 this.deviceContext = Preconditions.checkNotNull(deviceContext);
58 this.entity = Preconditions.checkNotNull(entity);
60 salRoleService = new SalRoleServiceImpl(this, deviceContext);
61 initRoleChangeFuture = SettableFuture.create();
65 public ListenableFuture<OfpRole> initialization() {
66 LOG.debug("Initialization requestOpenflowEntityOwnership for entity {}", entity);
68 entityOwnershipCandidateRegistration = entityOwnershipService.registerCandidate(entity);
69 LOG.debug("RoleContextImpl : Candidate registered with ownership service for device :{}", deviceContext
70 .getPrimaryConnectionContext().getNodeId().getValue());
71 } catch (final CandidateAlreadyRegisteredException e) {
72 completeInitRoleChangeFuture(null, e);
74 return initRoleChangeFuture;
78 public void onRoleChanged(final OfpRole oldRole, final OfpRole newRole) {
79 LOG.trace("onRoleChanged method call for Entity {}", entity);
81 if (!isDeviceConnected()) {
82 // this can happen as after the disconnect, we still get a last messsage from EntityOwnershipService.
83 LOG.info("Device {} is disconnected from this node. Hence not attempting a role change.",
84 deviceContext.getPrimaryConnectionContext().getNodeId());
85 completeInitRoleChangeFuture(null, null);
89 LOG.debug("Role change received from ownership listener from {} to {} for device:{}", oldRole, newRole,
90 deviceContext.getPrimaryConnectionContext().getNodeId());
92 final SetRoleInput setRoleInput = (new SetRoleInputBuilder())
93 .setControllerRole(newRole)
94 .setNode(new NodeRef(deviceContext.getDeviceState().getNodeInstanceIdentifier()))
97 final Future<RpcResult<SetRoleOutput>> setRoleOutputFuture = salRoleService.setRole(setRoleInput);
99 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(setRoleOutputFuture), new FutureCallback<RpcResult<SetRoleOutput>>() {
101 public void onSuccess(final RpcResult<SetRoleOutput> setRoleOutputRpcResult) {
102 LOG.debug("Rolechange {} successful made on switch :{}", newRole,
103 deviceContext.getPrimaryConnectionContext().getNodeId());
104 deviceContext.getDeviceState().setRole(newRole);
105 deviceContext.onClusterRoleChange(newRole);
106 completeInitRoleChangeFuture(newRole, null);
110 public void onFailure(final Throwable throwable) {
111 LOG.error("Error in setRole {} for device {} ", newRole,
112 deviceContext.getPrimaryConnectionContext().getNodeId(), throwable);
113 completeInitRoleChangeFuture(null, throwable);
118 void completeInitRoleChangeFuture(@Nullable final OfpRole role, @Nullable final Throwable throwable) {
119 if (initRoleChangeFuture.isDone()) {
122 if (!isDeviceConnected()) {
123 LOG.debug("Device {} is disconnected from this node. Hence not attempting a role change.", deviceContext
124 .getPrimaryConnectionContext().getNodeId());
125 initRoleChangeFuture.cancel(true);
128 if (throwable != null) {
129 LOG.warn("Connection Role change fail for entity {}", entity);
130 initRoleChangeFuture.setException(throwable);
131 } else if (role != null) {
132 LOG.debug("Initialization Role for entity {} is chosed {}", entity, role);
133 initRoleChangeFuture.set(role);
135 LOG.debug("Unexpected initialization Role Change close for entity {}", entity);
136 initRoleChangeFuture.cancel(true);
141 public void close() {
142 if (entityOwnershipCandidateRegistration != null) {
143 LOG.debug("Closing EntityOwnershipCandidateRegistration for {}", entity);
144 entityOwnershipCandidateRegistration.close();
149 public Entity getEntity() {
153 private boolean isDeviceConnected() {
154 return ConnectionContext.CONNECTION_STATE.WORKING.equals(
155 deviceContext.getPrimaryConnectionContext().getConnectionState());
160 public <T> RequestContext<T> createRequestContext() {
161 final AbstractRequestContext<T> ret = new AbstractRequestContext<T>(deviceContext.getReservedXid()) {
163 public void close() {
170 void setSalRoleService(final SalRoleService salRoleService) {
171 this.salRoleService = salRoleService;
175 public DeviceState getDeviceState() {
176 return deviceContext.getDeviceState();