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.services.sal;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext.CONNECTION_STATE;
16 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
18 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
19 import org.opendaylight.openflowplugin.impl.services.AbstractSimpleService;
20 import org.opendaylight.openflowplugin.impl.services.RoleService;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SalRoleService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
27 import org.opendaylight.yangtools.yang.common.RpcResult;
28 import org.opendaylight.yangtools.yang.common.Uint64;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 public final class SalRoleServiceImpl extends AbstractSimpleService<SetRoleInput, SetRoleOutput>
34 implements SalRoleService {
35 private static final Logger LOG = LoggerFactory.getLogger(SalRoleServiceImpl.class);
36 private static final Uint64 MAX_GENERATION_ID = Uint64.valueOf("ffffffffffffffff", 16);
38 private final DeviceContext deviceContext;
39 private final RoleService roleService;
41 public SalRoleServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext) {
42 super(requestContextStack, deviceContext, SetRoleOutput.class);
43 this.deviceContext = requireNonNull(deviceContext);
44 roleService = new RoleService(requestContextStack, deviceContext, RoleRequestOutput.class);
48 protected OfHeader buildRequest(final Xid xid, final SetRoleInput input) {
53 public ListenableFuture<RpcResult<SetRoleOutput>> setRole(final SetRoleInput input) {
54 LOG.info("SetRole called with input:{}", input);
56 // Check current connection state
57 final CONNECTION_STATE state = deviceContext.getPrimaryConnectionContext().getConnectionState();
60 LOG.info("Device {} has been disconnected", input.getNode());
61 return Futures.immediateFailedFuture(new Exception(String
62 .format("Device connection doesn't exist anymore. Primary connection status : %s",
66 LOG.trace("Device {} has been working", input.getNode());
69 LOG.warn("Device {} is in state {}, role change is not allowed", input.getNode(), state);
70 return Futures.immediateFailedFuture(new Exception(String
71 .format("Unexpected device connection status : %s", state)));
74 LOG.info("Requesting state change to {}", input.getControllerRole());
75 return tryToChangeRole(input.getControllerRole());
78 private ListenableFuture<RpcResult<SetRoleOutput>> tryToChangeRole(final OfpRole role) {
79 LOG.info("RoleChangeTask called on device:{} OFPRole:{}", getDeviceInfo().getNodeId().getValue(), role);
81 return Futures.transformAsync(roleService.getGenerationIdFromDevice(getVersion()), generationId -> {
82 LOG.debug("RoleChangeTask, GenerationIdFromDevice from device {} is {}",
83 getDeviceInfo().getNodeId().getValue(), generationId);
84 final Uint64 nextGenerationId = getNextGenerationId(generationId);
85 LOG.debug("nextGenerationId received from device:{} is {}",
86 getDeviceInfo().getNodeId().getValue(), nextGenerationId);
87 return roleService.submitRoleChange(role, getVersion(), nextGenerationId);
88 }, MoreExecutors.directExecutor());
91 private static Uint64 getNextGenerationId(final Uint64 generationId) {
92 if (generationId.compareTo(MAX_GENERATION_ID) < 0) {
93 return Uint64.valueOf(generationId.longValue() + 1);