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;
10 import com.google.common.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.math.BigInteger;
16 import java.util.Collection;
17 import java.util.concurrent.ExecutionException;
18 import java.util.concurrent.Future;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
21 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
22 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
23 import org.opendaylight.openflowplugin.impl.role.RoleChangeException;
24 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ControllerRole;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutputBuilder;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class RoleService extends AbstractSimpleService<RoleRequestInputBuilder, RoleRequestOutput> {
40 private static final Logger LOG = LoggerFactory.getLogger(RoleService.class);
42 private final DeviceContext deviceContext;
44 public RoleService(final RequestContextStack requestContextStack,
45 final DeviceContext deviceContext,
46 final Class<RoleRequestOutput> clazz) {
47 super(requestContextStack, deviceContext, clazz);
48 this.deviceContext = deviceContext;
52 protected OfHeader buildRequest(final Xid xid, final RoleRequestInputBuilder input) {
53 input.setXid(xid.getValue());
57 public Future<BigInteger> getGenerationIdFromDevice(final Short version) {
58 LOG.info("getGenerationIdFromDevice called for device: {}", getDeviceInfo().getNodeId().getValue());
60 // send a dummy no-change role request to get the generation-id of the switch
61 final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
62 roleRequestInputBuilder.setRole(toOFJavaRole(OfpRole.NOCHANGE));
63 roleRequestInputBuilder.setVersion(version);
64 roleRequestInputBuilder.setGenerationId(BigInteger.ZERO);
66 final SettableFuture<BigInteger> finalFuture = SettableFuture.create();
67 final ListenableFuture<RpcResult<RoleRequestOutput>> genIdListenableFuture =
68 handleServiceCall(roleRequestInputBuilder);
69 Futures.addCallback(genIdListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
71 public void onSuccess(@Nonnull final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
72 if (roleRequestOutputRpcResult.isSuccessful()) {
73 final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
74 if (roleRequestOutput != null) {
75 LOG.debug("roleRequestOutput.getGenerationId()={}", roleRequestOutput.getGenerationId());
76 finalFuture.set(roleRequestOutput.getGenerationId());
78 LOG.info("roleRequestOutput is null in getGenerationIdFromDevice");
79 finalFuture.setException(new RoleChangeException("Exception in getting generationId for device:"
80 + getDeviceInfo().getNodeId().getValue()));
84 LOG.error("getGenerationIdFromDevice RPC error {}",
85 roleRequestOutputRpcResult.getErrors().iterator().next().getInfo());
86 finalFuture.setException(new RoleChangeException(ErrorUtil
87 .errorsToString(roleRequestOutputRpcResult.getErrors())));
92 public void onFailure(final Throwable throwable) {
93 LOG.info("onFailure - getGenerationIdFromDevice RPC error", throwable);
94 finalFuture.setException(new ExecutionException(throwable));
96 }, MoreExecutors.directExecutor());
101 public Future<RpcResult<SetRoleOutput>> submitRoleChange(final OfpRole ofpRole,
103 final BigInteger generationId) {
104 LOG.info("submitRoleChange called for device:{}, role:{}",
105 getDeviceInfo().getNodeId(), ofpRole);
106 final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
107 roleRequestInputBuilder.setRole(toOFJavaRole(ofpRole));
108 roleRequestInputBuilder.setVersion(version);
109 roleRequestInputBuilder.setGenerationId(generationId);
111 final ListenableFuture<RpcResult<RoleRequestOutput>> roleListenableFuture =
112 handleServiceCall(roleRequestInputBuilder);
114 final SettableFuture<RpcResult<SetRoleOutput>> finalFuture = SettableFuture.create();
115 Futures.addCallback(roleListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
117 public void onSuccess(@Nonnull final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
118 LOG.info("submitRoleChange onSuccess for device:{}, role:{}",
119 getDeviceInfo().getNodeId(), ofpRole);
120 final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
121 final Collection<RpcError> rpcErrors = roleRequestOutputRpcResult.getErrors();
122 if (roleRequestOutput != null) {
123 final SetRoleOutputBuilder setRoleOutputBuilder = new SetRoleOutputBuilder();
125 .setTransactionId(new TransactionId(BigInteger.valueOf(roleRequestOutput.getXid())));
126 finalFuture.set(RpcResultBuilder.<SetRoleOutput>success()
127 .withResult(setRoleOutputBuilder.build()).build());
129 } else if (rpcErrors != null) {
130 LOG.trace("roleRequestOutput is null , rpcErrors={}", rpcErrors);
131 for (RpcError rpcError : rpcErrors) {
132 LOG.warn("RpcError on submitRoleChange for {}: {}",
133 deviceContext.getPrimaryConnectionContext().getNodeId(), rpcError.toString());
136 finalFuture.set(RpcResultBuilder.<SetRoleOutput>failed().withRpcErrors(rpcErrors).build());
141 public void onFailure(final Throwable throwable) {
142 LOG.error("submitRoleChange onFailure for device:{}, role:{}",
143 getDeviceInfo().getNodeId(), ofpRole, throwable);
144 finalFuture.setException(throwable);
146 }, MoreExecutors.directExecutor());
150 private static ControllerRole toOFJavaRole(final OfpRole role) {
151 ControllerRole ofJavaRole = null;
154 ofJavaRole = ControllerRole.OFPCRROLEMASTER;
157 ofJavaRole = ControllerRole.OFPCRROLESLAVE;
160 ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
164 LOG.warn("given role is not supported by protocol roles: {}", role);