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.services.util.ServiceException;
25 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ControllerRole;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
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.SetRoleOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutputBuilder;
34 import org.opendaylight.yangtools.yang.common.RpcError;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class RoleService extends AbstractSimpleService<RoleRequestInputBuilder, RoleRequestOutput> {
41 private static final Logger LOG = LoggerFactory.getLogger(RoleService.class);
43 private final DeviceContext deviceContext;
45 public RoleService(final RequestContextStack requestContextStack,
46 final DeviceContext deviceContext,
47 final Class<RoleRequestOutput> clazz) {
48 super(requestContextStack, deviceContext, clazz);
49 this.deviceContext = deviceContext;
53 protected OfHeader buildRequest(final Xid xid, final RoleRequestInputBuilder input) throws ServiceException {
54 input.setXid(xid.getValue());
58 public Future<BigInteger> getGenerationIdFromDevice(final Short version) {
59 LOG.info("getGenerationIdFromDevice called for device: {}", getDeviceInfo().getNodeId().getValue());
61 // send a dummy no-change role request to get the generation-id of the switch
62 final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
63 roleRequestInputBuilder.setRole(toOFJavaRole(OfpRole.NOCHANGE));
64 roleRequestInputBuilder.setVersion(version);
65 roleRequestInputBuilder.setGenerationId(BigInteger.ZERO);
67 final SettableFuture<BigInteger> finalFuture = SettableFuture.create();
68 final ListenableFuture<RpcResult<RoleRequestOutput>> genIdListenableFuture =
69 handleServiceCall(roleRequestInputBuilder);
70 Futures.addCallback(genIdListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
72 public void onSuccess(@Nonnull final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
73 if (roleRequestOutputRpcResult.isSuccessful()) {
74 final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
75 if (roleRequestOutput != null) {
76 LOG.debug("roleRequestOutput.getGenerationId()={}", roleRequestOutput.getGenerationId());
77 finalFuture.set(roleRequestOutput.getGenerationId());
79 LOG.info("roleRequestOutput is null in getGenerationIdFromDevice");
80 finalFuture.setException(new RoleChangeException("Exception in getting generationId for device:"
81 + getDeviceInfo().getNodeId().getValue()));
85 LOG.error("getGenerationIdFromDevice RPC error "
86 + roleRequestOutputRpcResult.getErrors().iterator().next().getInfo());
87 finalFuture.setException(new RoleChangeException(ErrorUtil
88 .errorsToString(roleRequestOutputRpcResult.getErrors())));
93 public void onFailure(final Throwable throwable) {
94 LOG.info("onFailure - getGenerationIdFromDevice RPC error {}", throwable);
95 finalFuture.setException(new ExecutionException(throwable));
97 }, MoreExecutors.directExecutor());
102 public Future<RpcResult<SetRoleOutput>> submitRoleChange(final OfpRole ofpRole,
104 final BigInteger generationId) {
105 LOG.info("submitRoleChange called for device:{}, role:{}",
106 getDeviceInfo().getNodeId(), ofpRole);
107 final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
108 roleRequestInputBuilder.setRole(toOFJavaRole(ofpRole));
109 roleRequestInputBuilder.setVersion(version);
110 roleRequestInputBuilder.setGenerationId(generationId);
112 final ListenableFuture<RpcResult<RoleRequestOutput>> roleListenableFuture =
113 handleServiceCall(roleRequestInputBuilder);
115 final SettableFuture<RpcResult<SetRoleOutput>> finalFuture = SettableFuture.create();
116 Futures.addCallback(roleListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
118 public void onSuccess(@Nonnull final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
119 LOG.info("submitRoleChange onSuccess for device:{}, role:{}",
120 getDeviceInfo().getNodeId(), ofpRole);
121 final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
122 final Collection<RpcError> rpcErrors = roleRequestOutputRpcResult.getErrors();
123 if (roleRequestOutput != null) {
124 final SetRoleOutputBuilder setRoleOutputBuilder = new SetRoleOutputBuilder();
126 .setTransactionId(new TransactionId(BigInteger.valueOf(roleRequestOutput.getXid())));
127 finalFuture.set(RpcResultBuilder.<SetRoleOutput>success()
128 .withResult(setRoleOutputBuilder.build()).build());
130 } else if (rpcErrors != null) {
131 LOG.trace("roleRequestOutput is null , rpcErrors={}", rpcErrors);
132 for (RpcError rpcError : rpcErrors) {
133 LOG.warn("RpcError on submitRoleChange for {}: {}",
134 deviceContext.getPrimaryConnectionContext().getNodeId(), rpcError.toString());
137 finalFuture.set(RpcResultBuilder.<SetRoleOutput>failed().withRpcErrors(rpcErrors).build());
142 public void onFailure(final Throwable throwable) {
143 LOG.error("submitRoleChange onFailure for device:{}, role:{}",
144 getDeviceInfo().getNodeId(), ofpRole, throwable);
145 finalFuture.setException(throwable);
147 }, MoreExecutors.directExecutor());
151 private static ControllerRole toOFJavaRole(final OfpRole role) {
152 ControllerRole ofJavaRole = null;
155 ofJavaRole = ControllerRole.OFPCRROLEMASTER;
158 ofJavaRole = ControllerRole.OFPCRROLESLAVE;
161 ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
165 LOG.warn("given role is not supported by protocol roles: {}", role);