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.openflow.common.types.rev130731.ControllerRole;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.SetRoleOutputBuilder;
30 import org.opendaylight.yangtools.yang.common.RpcError;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Created by kramesha on 8/24/15.
39 public class RoleService extends AbstractSimpleService<RoleRequestInputBuilder, RoleRequestOutput> {
40 private static final Logger LOG = LoggerFactory.getLogger(RoleService.class);
42 private final DeviceContext deviceContext;
44 protected RoleService(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final Class<RoleRequestOutput> clazz) {
45 super(requestContextStack, deviceContext, clazz);
46 this.deviceContext = deviceContext;
50 protected OfHeader buildRequest(final Xid xid, final RoleRequestInputBuilder input) throws ServiceException {
51 input.setXid(xid.getValue());
55 public Future<BigInteger> getGenerationIdFromDevice(final Short version) {
56 LOG.info("getGenerationIdFromDevice called for device: {}", getDeviceInfo().getNodeId().getValue());
58 // send a dummy no-change role request to get the generation-id of the switch
59 final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
60 roleRequestInputBuilder.setRole(toOFJavaRole(OfpRole.NOCHANGE));
61 roleRequestInputBuilder.setVersion(version);
62 roleRequestInputBuilder.setGenerationId(BigInteger.ZERO);
64 final SettableFuture<BigInteger> finalFuture = SettableFuture.create();
65 final ListenableFuture<RpcResult<RoleRequestOutput>> genIdListenableFuture = handleServiceCall(roleRequestInputBuilder);
66 Futures.addCallback(genIdListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
68 public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
69 if (roleRequestOutputRpcResult.isSuccessful()) {
70 final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
71 if (roleRequestOutput != null) {
72 LOG.debug("roleRequestOutput.getGenerationId()={}", roleRequestOutput.getGenerationId());
73 finalFuture.set(roleRequestOutput.getGenerationId());
75 LOG.info("roleRequestOutput is null in getGenerationIdFromDevice");
76 finalFuture.setException(new RoleChangeException("Exception in getting generationId for device:" + getDeviceInfo().getNodeId().getValue()));
80 LOG.error("getGenerationIdFromDevice RPC error " +
81 roleRequestOutputRpcResult.getErrors().iterator().next().getInfo());
88 public void onFailure(final Throwable throwable) {
89 LOG.info("onFailure - getGenerationIdFromDevice RPC error {}", throwable);
90 finalFuture.setException(new ExecutionException(throwable));
97 public Future<RpcResult<SetRoleOutput>> submitRoleChange(final OfpRole ofpRole, final Short version, final BigInteger generationId) {
98 LOG.info("submitRoleChange called for device:{}, role:{}",
99 getDeviceInfo().getNodeId(), ofpRole);
100 final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
101 roleRequestInputBuilder.setRole(toOFJavaRole(ofpRole));
102 roleRequestInputBuilder.setVersion(version);
103 roleRequestInputBuilder.setGenerationId(generationId);
105 final ListenableFuture<RpcResult<RoleRequestOutput>> roleListenableFuture = handleServiceCall(roleRequestInputBuilder);
107 final SettableFuture<RpcResult<SetRoleOutput>> finalFuture = SettableFuture.create();
108 Futures.addCallback(roleListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
110 public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
111 LOG.info("submitRoleChange onSuccess for device:{}, role:{}",
112 getDeviceInfo().getNodeId(), ofpRole);
113 final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
114 final Collection<RpcError> rpcErrors = roleRequestOutputRpcResult.getErrors();
115 if (roleRequestOutput != null) {
116 final SetRoleOutputBuilder setRoleOutputBuilder = new SetRoleOutputBuilder();
117 setRoleOutputBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(roleRequestOutput.getXid())));
118 finalFuture.set(RpcResultBuilder.<SetRoleOutput>success().withResult(setRoleOutputBuilder.build()).build());
120 } else if (rpcErrors != null) {
121 LOG.trace("roleRequestOutput is null , rpcErrors={}", rpcErrors);
122 for (RpcError rpcError : rpcErrors) {
123 LOG.warn("RpcError on submitRoleChange for {}: {}",
124 deviceContext.getPrimaryConnectionContext().getNodeId(), rpcError.toString());
127 finalFuture.set(RpcResultBuilder.<SetRoleOutput>failed().withRpcErrors(rpcErrors).build());
132 public void onFailure(final Throwable throwable) {
133 LOG.error("submitRoleChange onFailure for device:{}, role:{}",
134 getDeviceInfo().getNodeId(), ofpRole, throwable);
135 finalFuture.setException(throwable);
141 private static ControllerRole toOFJavaRole(final OfpRole role) {
142 ControllerRole ofJavaRole = null;
145 ofJavaRole = ControllerRole.OFPCRROLEMASTER;
148 ofJavaRole = ControllerRole.OFPCRROLESLAVE;
151 ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
155 LOG.warn("given role is not supported by protocol roles: {}", role);