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 LOG.info("getGenerationIdFromDevice called for device:{}", 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 = handleServiceCall(roleRequestInputBuilder);
67 Futures.addCallback(genIdListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
69 public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
70 if (roleRequestOutputRpcResult.isSuccessful()) {
71 final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
72 if (roleRequestOutput != null) {
73 LOG.debug("roleRequestOutput.getGenerationId()={}", roleRequestOutput.getGenerationId());
74 finalFuture.set(roleRequestOutput.getGenerationId());
76 LOG.info("roleRequestOutput is null in getGenerationIdFromDevice");
77 finalFuture.setException(new RoleChangeException("Exception in getting generationId for device:" + getNodeId().getValue()));
81 LOG.error("getGenerationIdFromDevice RPC error " +
82 roleRequestOutputRpcResult.getErrors().iterator().next().getInfo());
89 public void onFailure(final Throwable throwable) {
90 LOG.info("onFailure - getGenerationIdFromDevice RPC error {}", throwable);
91 finalFuture.setException(new ExecutionException(throwable));
98 public Future<RpcResult<SetRoleOutput>> submitRoleChange(final OfpRole ofpRole, final Short version, final BigInteger generationId) {
99 LOG.info("submitRoleChange called for device:{}, role:{}",
100 getNodeId(), ofpRole);
101 final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
102 roleRequestInputBuilder.setRole(toOFJavaRole(ofpRole));
103 roleRequestInputBuilder.setVersion(version);
104 roleRequestInputBuilder.setGenerationId(generationId);
106 final ListenableFuture<RpcResult<RoleRequestOutput>> roleListenableFuture = handleServiceCall(roleRequestInputBuilder);
108 final SettableFuture<RpcResult<SetRoleOutput>> finalFuture = SettableFuture.create();
109 Futures.addCallback(roleListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
111 public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
112 LOG.info("submitRoleChange onSuccess for device:{}, role:{}",
113 getNodeId(), ofpRole);
114 final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
115 final Collection<RpcError> rpcErrors = roleRequestOutputRpcResult.getErrors();
116 if (roleRequestOutput != null) {
117 final SetRoleOutputBuilder setRoleOutputBuilder = new SetRoleOutputBuilder();
118 setRoleOutputBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(roleRequestOutput.getXid())));
119 finalFuture.set(RpcResultBuilder.<SetRoleOutput>success().withResult(setRoleOutputBuilder.build()).build());
121 } else if (rpcErrors != null) {
122 LOG.trace("roleRequestOutput is null , rpcErrors={}", rpcErrors);
123 for (RpcError rpcError : rpcErrors) {
124 LOG.warn("RpcError on submitRoleChange for {}: {}",
125 deviceContext.getPrimaryConnectionContext().getNodeId(), rpcError.toString());
128 finalFuture.set(RpcResultBuilder.<SetRoleOutput>failed().withRpcErrors(rpcErrors).build());
133 public void onFailure(final Throwable throwable) {
134 LOG.error("submitRoleChange onFailure for device:{}, role:{}",
135 getNodeId(), ofpRole, throwable);
136 finalFuture.setException(throwable);
142 private static ControllerRole toOFJavaRole(final OfpRole role) {
143 ControllerRole ofJavaRole = null;
146 ofJavaRole = ControllerRole.OFPCRROLEMASTER;
149 ofJavaRole = ControllerRole.OFPCRROLESLAVE;
152 ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
156 LOG.warn("given role is not supported by protocol roles: {}", role);