2 * Copyright (c) 2014 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.openflow.md.util;
10 import com.google.common.base.Function;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.JdkFutureAdapters;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.SettableFuture;
17 import java.math.BigInteger;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.Future;
20 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
21 import org.opendaylight.openflowplugin.openflow.md.core.session.OFRoleManager;
22 import org.opendaylight.openflowplugin.openflow.md.core.session.RolePushException;
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.RoleRequestInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.common.config.impl.rev140326.OfpRole;
27 import org.opendaylight.yangtools.yang.common.RpcResult;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
34 public final class RoleUtil {
36 private static final Logger LOG = LoggerFactory.getLogger(RoleUtil.class);
37 private static final Function<Exception, RolePushException> exceptionFunction = new Function<Exception, RolePushException>() {
39 public RolePushException apply(Exception input) {
40 RolePushException output = null;
41 if (input instanceof ExecutionException) {
42 if (input.getCause() instanceof RolePushException) {
43 output = (RolePushException) input.getCause();
48 output = new RolePushException(input.getMessage(), input);
56 throw new UnsupportedOperationException("RoleUtil is not expected to be instantiated.");
60 * @param role openflow role for controller
61 * @return protocol role
63 public static ControllerRole toOFJavaRole(OfpRole role) {
64 ControllerRole ofJavaRole = null;
67 ofJavaRole = ControllerRole.OFPCRROLEMASTER;
70 ofJavaRole = ControllerRole.OFPCRROLESLAVE;
73 ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
77 LOG.warn("given role is not supported by protocol roles: {}", role);
84 * @param session switch session context
85 * @param role controller openflow role
86 * @param generationId generate id for role negotiation
87 * @return input builder
89 public static RoleRequestInputBuilder createRoleRequestInput(
90 final SessionContext session, OfpRole role, BigInteger generationId) {
92 ControllerRole ofJavaRole = RoleUtil.toOFJavaRole(role);
94 return new RoleRequestInputBuilder()
95 .setGenerationId(generationId)
97 .setVersion(session.getFeatures().getVersion())
98 .setXid(session.getNextXid());
102 * @param sessionContext switch session context
103 * @param ofpRole controller openflow role
104 * @param generationId generate id for role negotiation
105 * @return roleRequest future result
107 public static Future<RpcResult<RoleRequestOutput>> sendRoleChangeRequest(SessionContext sessionContext, OfpRole ofpRole, BigInteger generationId) {
108 RoleRequestInputBuilder ruleRequestInputBld = RoleUtil.createRoleRequestInput(sessionContext, ofpRole, generationId);
109 Future<RpcResult<RoleRequestOutput>> roleReply = sessionContext.getPrimaryConductor().getConnectionAdapter()
110 .roleRequest(ruleRequestInputBld.build());
115 * @param sessionContext switch session context
116 * @return generationId from future RpcResult
118 public static Future<BigInteger> readGenerationIdFromDevice(SessionContext sessionContext) {
119 Future<RpcResult<RoleRequestOutput>> roleReply = sendRoleChangeRequest(sessionContext, OfpRole.NOCHANGE, BigInteger.ZERO);
120 final SettableFuture<BigInteger> result = SettableFuture.create();
122 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(roleReply), new FutureCallback<RpcResult<RoleRequestOutput>>() {
124 public void onSuccess(RpcResult<RoleRequestOutput> input) {
125 if(input != null && input.getResult() != null) {
126 result.set(input.getResult().getGenerationId());
130 public void onFailure(Throwable t) {
138 * @param generationId generate id for role negotiation
139 * @return next (incremented value)
141 public static BigInteger getNextGenerationId(BigInteger generationId) {
142 BigInteger nextGenerationId = null;
143 if (generationId.compareTo(OFRoleManager.MAX_GENERATION_ID) < 0) {
144 nextGenerationId = generationId.add(BigInteger.ONE);
146 nextGenerationId = BigInteger.ZERO;
149 return nextGenerationId;
153 * @param rolePushResult result of role push request
154 * @return future which throws {@link RolePushException}
156 public static CheckedFuture<Boolean, RolePushException> makeCheckedRuleRequestFxResult(
157 ListenableFuture<Boolean> rolePushResult) {
158 return Futures.makeChecked(
159 rolePushResult, exceptionFunction