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.SettableFuture;
14 import java.math.BigInteger;
15 import java.util.Collection;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Future;
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.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
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.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Created by kramesha on 8/24/15.
40 public class RoleService extends AbstractSimpleService<RoleRequestInputBuilder, RoleRequestOutput> {
41 private static final Logger LOG = LoggerFactory.getLogger(RoleService.class);
43 private final DeviceContext deviceContext;
45 protected RoleService(final RequestContextStack requestContextStack, final DeviceContext deviceContext, 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 final NodeId nodeId = deviceContext.getPrimaryConnectionContext().getNodeId();
58 LOG.info("getGenerationIdFromDevice called for device:{}", nodeId.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 = 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:" + nodeId.getValue()));
82 LOG.error("getGenerationIdFromDevice RPC error " +
83 roleRequestOutputRpcResult.getErrors().iterator().next().getInfo());
90 public void onFailure(final Throwable throwable) {
91 LOG.info("onFailure - getGenerationIdFromDevice RPC error {}", throwable);
92 finalFuture.setException(new ExecutionException(throwable));
99 public Future<RpcResult<SetRoleOutput>> submitRoleChange(final OfpRole ofpRole, final Short version, final BigInteger generationId) {
100 LOG.info("submitRoleChange called for device:{}, role:{}",
101 deviceContext.getPrimaryConnectionContext().getNodeId(), ofpRole);
102 final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
103 roleRequestInputBuilder.setRole(toOFJavaRole(ofpRole));
104 roleRequestInputBuilder.setVersion(version);
105 roleRequestInputBuilder.setGenerationId(generationId);
107 final ListenableFuture<RpcResult<RoleRequestOutput>> roleListenableFuture = handleServiceCall(roleRequestInputBuilder);
109 final SettableFuture<RpcResult<SetRoleOutput>> finalFuture = SettableFuture.create();
110 Futures.addCallback(roleListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
112 public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
113 LOG.info("submitRoleChange onSuccess for device:{}, role:{}",
114 deviceContext.getPrimaryConnectionContext().getNodeId(), ofpRole);
115 final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
116 final Collection<RpcError> rpcErrors = roleRequestOutputRpcResult.getErrors();
117 if (roleRequestOutput != null) {
118 final SetRoleOutputBuilder setRoleOutputBuilder = new SetRoleOutputBuilder();
119 setRoleOutputBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(roleRequestOutput.getXid())));
120 finalFuture.set(RpcResultBuilder.<SetRoleOutput>success().withResult(setRoleOutputBuilder.build()).build());
122 } else if (rpcErrors != null) {
123 LOG.trace("roleRequestOutput is null , rpcErrors={}", rpcErrors);
124 for (RpcError rpcError : rpcErrors) {
125 LOG.warn("RpcError on submitRoleChange for {}: {}",
126 deviceContext.getPrimaryConnectionContext().getNodeId(), rpcError.toString());
129 finalFuture.set(RpcResultBuilder.<SetRoleOutput>failed().withRpcErrors(rpcErrors).build());
134 public void onFailure(final Throwable throwable) {
135 LOG.error("submitRoleChange onFailure for device:{}, role:{}",
136 deviceContext.getPrimaryConnectionContext().getNodeId(), ofpRole, throwable);
137 finalFuture.setException(throwable);
143 private static ControllerRole toOFJavaRole(final OfpRole role) {
144 ControllerRole ofJavaRole = null;
147 ofJavaRole = ControllerRole.OFPCRROLEMASTER;
150 ofJavaRole = ControllerRole.OFPCRROLESLAVE;
153 ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
157 LOG.warn("given role is not supported by protocol roles: {}", role);