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.util.Collection;
16 import java.util.concurrent.ExecutionException;
17 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
19 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
20 import org.opendaylight.openflowplugin.impl.role.RoleChangeException;
21 import org.opendaylight.openflowplugin.impl.util.ErrorUtil;
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.opendaylight.yangtools.yang.common.Uint64;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class RoleService extends AbstractSimpleService<RoleRequestInputBuilder, RoleRequestOutput> {
38 private static final Logger LOG = LoggerFactory.getLogger(RoleService.class);
40 private final DeviceContext deviceContext;
42 public RoleService(final RequestContextStack requestContextStack,
43 final DeviceContext deviceContext,
44 final Class<RoleRequestOutput> clazz) {
45 super(requestContextStack, deviceContext, clazz);
46 this.deviceContext = deviceContext;
50 protected OfHeader buildRequest(final Xid xid, final RoleRequestInputBuilder input) {
51 input.setXid(xid.getValue());
55 public ListenableFuture<Uint64> 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(Uint64.ZERO);
64 final SettableFuture<Uint64> finalFuture = SettableFuture.create();
65 final ListenableFuture<RpcResult<RoleRequestOutput>> genIdListenableFuture =
66 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:"
78 + getDeviceInfo().getNodeId().getValue()));
82 LOG.error("getGenerationIdFromDevice RPC error {}",
83 roleRequestOutputRpcResult.getErrors().iterator().next().getInfo());
84 finalFuture.setException(new RoleChangeException(ErrorUtil
85 .errorsToString(roleRequestOutputRpcResult.getErrors())));
90 public void onFailure(final Throwable throwable) {
91 LOG.info("onFailure - getGenerationIdFromDevice RPC error", throwable);
92 finalFuture.setException(new ExecutionException(throwable));
94 }, MoreExecutors.directExecutor());
99 public ListenableFuture<RpcResult<SetRoleOutput>> submitRoleChange(final OfpRole ofpRole, final Short version,
100 final Uint64 generationId) {
101 LOG.info("submitRoleChange called for device:{}, role:{}",
102 getDeviceInfo().getNodeId(), ofpRole);
103 final RoleRequestInputBuilder roleRequestInputBuilder = new RoleRequestInputBuilder();
104 roleRequestInputBuilder.setRole(toOFJavaRole(ofpRole));
105 roleRequestInputBuilder.setVersion(version);
106 roleRequestInputBuilder.setGenerationId(generationId);
108 final ListenableFuture<RpcResult<RoleRequestOutput>> roleListenableFuture =
109 handleServiceCall(roleRequestInputBuilder);
111 final SettableFuture<RpcResult<SetRoleOutput>> finalFuture = SettableFuture.create();
112 Futures.addCallback(roleListenableFuture, new FutureCallback<RpcResult<RoleRequestOutput>>() {
114 public void onSuccess(final RpcResult<RoleRequestOutput> roleRequestOutputRpcResult) {
115 LOG.info("submitRoleChange onSuccess for device:{}, role:{}",
116 getDeviceInfo().getNodeId(), ofpRole);
117 final RoleRequestOutput roleRequestOutput = roleRequestOutputRpcResult.getResult();
118 final Collection<RpcError> rpcErrors = roleRequestOutputRpcResult.getErrors();
119 if (roleRequestOutput != null) {
120 final SetRoleOutputBuilder setRoleOutputBuilder = new SetRoleOutputBuilder();
122 .setTransactionId(new TransactionId(Uint64.valueOf(roleRequestOutput.getXid())));
123 finalFuture.set(RpcResultBuilder.<SetRoleOutput>success()
124 .withResult(setRoleOutputBuilder.build()).build());
126 } else if (rpcErrors != null) {
127 LOG.trace("roleRequestOutput is null , rpcErrors={}", rpcErrors);
128 for (RpcError rpcError : rpcErrors) {
129 LOG.warn("RpcError on submitRoleChange for {}: {}",
130 deviceContext.getPrimaryConnectionContext().getNodeId(), rpcError.toString());
133 finalFuture.set(RpcResultBuilder.<SetRoleOutput>failed().withRpcErrors(rpcErrors).build());
138 public void onFailure(final Throwable throwable) {
139 LOG.error("submitRoleChange onFailure for device:{}, role:{}",
140 getDeviceInfo().getNodeId(), ofpRole, throwable);
141 finalFuture.setException(throwable);
143 }, MoreExecutors.directExecutor());
147 private static ControllerRole toOFJavaRole(final OfpRole role) {
148 ControllerRole ofJavaRole = null;
151 ofJavaRole = ControllerRole.OFPCRROLEMASTER;
154 ofJavaRole = ControllerRole.OFPCRROLESLAVE;
157 ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
161 LOG.warn("given role is not supported by protocol roles: {}", role);