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 org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
20 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
21 import org.opendaylight.openflowplugin.impl.role.RoleChangeException;
22 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ControllerRole;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutputBuilder;
31 import org.opendaylight.yangtools.yang.common.RpcError;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
34 import org.opendaylight.yangtools.yang.common.Uint64;
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 ListenableFuture<Uint64> 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<Uint64> 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 ListenableFuture<RpcResult<SetRoleOutput>> submitRoleChange(final OfpRole ofpRole, final Short version,
101 final Uint64 generationId) {
102 LOG.info("submitRoleChange called for device:{}, role:{}",
103 getDeviceInfo().getNodeId(), ofpRole);
104 final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
105 roleRequestInputBuilder.setRole(toOFJavaRole(ofpRole));
106 roleRequestInputBuilder.setVersion(version);
107 roleRequestInputBuilder.setGenerationId(generationId);
109 final ListenableFuture<RpcResult<RoleRequestOutput>> roleListenableFuture =
110 handleServiceCall(roleRequestInputBuilder);
112 final SettableFuture<RpcResult<SetRoleOutput>> finalFuture = SettableFuture.create();
113 Futures.addCallback(roleListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
115 public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
116 LOG.info("submitRoleChange onSuccess for device:{}, role:{}",
117 getDeviceInfo().getNodeId(), ofpRole);
118 final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
119 final Collection<RpcError> rpcErrors = roleRequestOutputRpcResult.getErrors();
120 if (roleRequestOutput != null) {
121 final SetRoleOutputBuilder setRoleOutputBuilder = new SetRoleOutputBuilder();
123 .setTransactionId(new TransactionId(Uint64.valueOf(roleRequestOutput.getXid())));
124 finalFuture.set(RpcResultBuilder.<SetRoleOutput>success()
125 .withResult(setRoleOutputBuilder.build()).build());
127 } else if (rpcErrors != null) {
128 LOG.trace("roleRequestOutput is null , rpcErrors={}", rpcErrors);
129 for (RpcError rpcError : rpcErrors) {
130 LOG.warn("RpcError on submitRoleChange for {}: {}",
131 deviceContext.getPrimaryConnectionContext().getNodeId(), rpcError.toString());
134 finalFuture.set(RpcResultBuilder.<SetRoleOutput>failed().withRpcErrors(rpcErrors).build());
139 public void onFailure(final Throwable throwable) {
140 LOG.error("submitRoleChange onFailure for device:{}, role:{}",
141 getDeviceInfo().getNodeId(), ofpRole, throwable);
142 finalFuture.setException(throwable);
144 }, MoreExecutors.directExecutor());
148 private static ControllerRole toOFJavaRole(final OfpRole role) {
149 ControllerRole ofJavaRole = null;
152 ofJavaRole = ControllerRole.OFPCRROLEMASTER;
155 ofJavaRole = ControllerRole.OFPCRROLESLAVE;
158 ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
162 LOG.warn("given role is not supported by protocol roles: {}", role);