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.Futures;
13 import com.google.common.util.concurrent.JdkFutureAdapters;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.FutureCallback;
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;
30 import java.util.Date;
35 public final class RoleUtil {
37 private static final Logger LOG = LoggerFactory.getLogger(RoleUtil.class);
38 private static final Function<Exception, RolePushException> exceptionFunction = new Function<Exception, RolePushException>() {
40 public RolePushException apply(Exception input) {
41 RolePushException output = null;
42 if (input instanceof ExecutionException) {
43 if (input.getCause() instanceof RolePushException) {
44 output = (RolePushException) input.getCause();
49 output = new RolePushException(input.getMessage(), input);
57 throw new UnsupportedOperationException("RoleUtil is not expected to be instantiated.");
61 * @param role openflow role for controller
62 * @return protocol role
64 public static ControllerRole toOFJavaRole(OfpRole role) {
65 ControllerRole ofJavaRole = null;
68 ofJavaRole = ControllerRole.OFPCRROLEMASTER;
71 ofJavaRole = ControllerRole.OFPCRROLESLAVE;
74 ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
78 LOG.warn("given role is not supported by protocol roles: {}", role);
85 * @param session switch session context
86 * @param role controller openflow role
87 * @param generationId generate id for role negotiation
88 * @return input builder
90 public static RoleRequestInputBuilder createRoleRequestInput(
91 final SessionContext session, OfpRole role, BigInteger generationId) {
93 ControllerRole ofJavaRole = RoleUtil.toOFJavaRole(role);
95 return new RoleRequestInputBuilder()
96 .setGenerationId(generationId)
98 .setVersion(session.getFeatures().getVersion())
99 .setXid(session.getNextXid());
103 * @param sessionContext switch session context
104 * @param ofpRole controller openflow role
105 * @param generationId generate id for role negotiation
106 * @return roleRequest future result
108 public static Future<RpcResult<RoleRequestOutput>> sendRoleChangeRequest(SessionContext sessionContext, OfpRole ofpRole, BigInteger generationId) {
109 RoleRequestInputBuilder ruleRequestInputBld = RoleUtil.createRoleRequestInput(sessionContext, ofpRole, generationId);
110 Future<RpcResult<RoleRequestOutput>> roleReply = sessionContext.getPrimaryConductor().getConnectionAdapter()
111 .roleRequest(ruleRequestInputBld.build());
116 * @param sessionContext switch session context
117 * @return generationId from future RpcResult
119 public static Future<BigInteger> readGenerationIdFromDevice(SessionContext sessionContext) {
120 Future<RpcResult<RoleRequestOutput>> roleReply = sendRoleChangeRequest(sessionContext, OfpRole.NOCHANGE, BigInteger.ZERO);
121 final SettableFuture<BigInteger> result = SettableFuture.create();
123 Futures.addCallback(JdkFutureAdapters.listenInPoolThread(roleReply), new FutureCallback<RpcResult<RoleRequestOutput>>() {
125 public void onSuccess(RpcResult<RoleRequestOutput> input) {
126 if(input != null && input.getResult() != null) {
127 result.set(input.getResult().getGenerationId());
131 public void onFailure(Throwable t) {
139 * @param generationId generate id for role negotiation
140 * @return next (incremented value)
142 public static BigInteger getNextGenerationId(BigInteger generationId) {
143 BigInteger nextGenerationId = null;
144 if (generationId.compareTo(OFRoleManager.MAX_GENERATION_ID) < 0) {
145 nextGenerationId = generationId.add(BigInteger.ONE);
147 nextGenerationId = BigInteger.ZERO;
150 return nextGenerationId;
154 * @param rolePushResult result of role push request
155 * @return future which throws {@link RolePushException}
157 public static CheckedFuture<Boolean, RolePushException> makeCheckedRuleRequestFxResult(
158 ListenableFuture<Boolean> rolePushResult) {
159 return Futures.makeChecked(
160 rolePushResult, exceptionFunction