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 org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
21 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
22 import org.opendaylight.openflowplugin.impl.role.RoleChangeException;
23 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ControllerRole;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutputBuilder;
32 import org.opendaylight.yangtools.yang.common.RpcError;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class RoleService extends AbstractSimpleService<RoleRequestInputBuilder, RoleRequestOutput> {
39 private static final Logger LOG = LoggerFactory.getLogger(RoleService.class);
41 private final DeviceContext deviceContext;
43 public RoleService(final RequestContextStack requestContextStack,
44 final DeviceContext deviceContext,
45 final Class<RoleRequestOutput> clazz) {
46 super(requestContextStack, deviceContext, clazz);
47 this.deviceContext = deviceContext;
51 protected OfHeader buildRequest(final Xid xid, final RoleRequestInputBuilder input) {
52 input.setXid(xid.getValue());
56 public Future<BigInteger> getGenerationIdFromDevice(final Short version) {
57 LOG.info("getGenerationIdFromDevice called for device: {}", getDeviceInfo().getNodeId().getValue());
59 // send a dummy no-change role request to get the generation-id of the switch
60 final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
61 roleRequestInputBuilder.setRole(toOFJavaRole(OfpRole.NOCHANGE));
62 roleRequestInputBuilder.setVersion(version);
63 roleRequestInputBuilder.setGenerationId(BigInteger.ZERO);
65 final SettableFuture<BigInteger> finalFuture = SettableFuture.create();
66 final ListenableFuture<RpcResult<RoleRequestOutput>> genIdListenableFuture =
67 handleServiceCall(roleRequestInputBuilder);
68 Futures.addCallback(genIdListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
70 public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
71 if (roleRequestOutputRpcResult.isSuccessful()) {
72 final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
73 if (roleRequestOutput != null) {
74 LOG.debug("roleRequestOutput.getGenerationId()={}", roleRequestOutput.getGenerationId());
75 finalFuture.set(roleRequestOutput.getGenerationId());
77 LOG.info("roleRequestOutput is null in getGenerationIdFromDevice");
78 finalFuture.setException(new RoleChangeException("Exception in getting generationId for device:"
79 + getDeviceInfo().getNodeId().getValue()));
83 LOG.error("getGenerationIdFromDevice RPC error {}",
84 roleRequestOutputRpcResult.getErrors().iterator().next().getInfo());
85 finalFuture.setException(new RoleChangeException(ErrorUtil
86 .errorsToString(roleRequestOutputRpcResult.getErrors())));
91 public void onFailure(final Throwable throwable) {
92 LOG.info("onFailure - getGenerationIdFromDevice RPC error", throwable);
93 finalFuture.setException(new ExecutionException(throwable));
95 }, MoreExecutors.directExecutor());
100 public Future<RpcResult<SetRoleOutput>> submitRoleChange(final OfpRole ofpRole,
102 final BigInteger generationId) {
103 LOG.info("submitRoleChange called for device:{}, role:{}",
104 getDeviceInfo().getNodeId(), ofpRole);
105 final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
106 roleRequestInputBuilder.setRole(toOFJavaRole(ofpRole));
107 roleRequestInputBuilder.setVersion(version);
108 roleRequestInputBuilder.setGenerationId(generationId);
110 final ListenableFuture<RpcResult<RoleRequestOutput>> roleListenableFuture =
111 handleServiceCall(roleRequestInputBuilder);
113 final SettableFuture<RpcResult<SetRoleOutput>> finalFuture = SettableFuture.create();
114 Futures.addCallback(roleListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
116 public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
117 LOG.info("submitRoleChange onSuccess for device:{}, role:{}",
118 getDeviceInfo().getNodeId(), ofpRole);
119 final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
120 final Collection<RpcError> rpcErrors = roleRequestOutputRpcResult.getErrors();
121 if (roleRequestOutput != null) {
122 final SetRoleOutputBuilder setRoleOutputBuilder = new SetRoleOutputBuilder();
124 .setTransactionId(new TransactionId(BigInteger.valueOf(roleRequestOutput.getXid())));
125 finalFuture.set(RpcResultBuilder.<SetRoleOutput>success()
126 .withResult(setRoleOutputBuilder.build()).build());
128 } else if (rpcErrors != null) {
129 LOG.trace("roleRequestOutput is null , rpcErrors={}", rpcErrors);
130 for (RpcError rpcError : rpcErrors) {
131 LOG.warn("RpcError on submitRoleChange for {}: {}",
132 deviceContext.getPrimaryConnectionContext().getNodeId(), rpcError.toString());
135 finalFuture.set(RpcResultBuilder.<SetRoleOutput>failed().withRpcErrors(rpcErrors).build());
140 public void onFailure(final Throwable throwable) {
141 LOG.error("submitRoleChange onFailure for device:{}, role:{}",
142 getDeviceInfo().getNodeId(), ofpRole, throwable);
143 finalFuture.setException(throwable);
145 }, MoreExecutors.directExecutor());
149 private static ControllerRole toOFJavaRole(final OfpRole role) {
150 ControllerRole ofJavaRole = null;
153 ofJavaRole = ControllerRole.OFPCRROLEMASTER;
156 ofJavaRole = ControllerRole.OFPCRROLESLAVE;
159 ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
163 LOG.warn("given role is not supported by protocol roles: {}", role);