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 java.math.BigInteger;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Future;
18 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
19 import org.opendaylight.openflowplugin.openflow.md.core.session.OFRoleManager;
20 import org.opendaylight.openflowplugin.openflow.md.core.session.RolePushException;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ControllerRole;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.common.config.impl.rev140326.OfpRole;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
32 public final class RoleUtil {
34 private static final Logger LOG = LoggerFactory.getLogger(RoleUtil.class);
35 private static final Function<Exception, RolePushException> exceptionFunction = new Function<Exception, RolePushException>() {
37 public RolePushException apply(Exception input) {
38 RolePushException output = null;
39 if (input instanceof ExecutionException) {
40 if (input.getCause() instanceof RolePushException) {
41 output = (RolePushException) input.getCause();
46 output = new RolePushException(input.getMessage(), input);
54 throw new UnsupportedOperationException("RoleUtil is not expected to be instantiated.");
59 * @return protocol role
61 public static ControllerRole toOFJavaRole(OfpRole role) {
62 ControllerRole ofJavaRole = null;
65 ofJavaRole = ControllerRole.OFPCRROLEMASTER;
68 ofJavaRole = ControllerRole.OFPCRROLESLAVE;
71 ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
75 LOG.warn("given role is not supported by protocol roles: {}", role);
85 * @return input builder
87 public static RoleRequestInputBuilder createRoleRequestInput(
88 final SessionContext session, OfpRole role, BigInteger generationId) {
90 ControllerRole ofJavaRole = RoleUtil.toOFJavaRole(role);
92 return new RoleRequestInputBuilder()
93 .setGenerationId(generationId)
95 .setVersion(session.getFeatures().getVersion())
96 .setXid(session.getNextXid());
100 * @param sessionContext
102 * @param generationId
103 * @return roleRequest future result
105 public static Future<RpcResult<RoleRequestOutput>> sendRoleChangeRequest(SessionContext sessionContext, OfpRole ofpRole, BigInteger generationId) {
106 RoleRequestInputBuilder ruleRequestInputBld = RoleUtil.createRoleRequestInput(sessionContext, ofpRole, generationId);
107 Future<RpcResult<RoleRequestOutput>> roleReply = sessionContext.getPrimaryConductor().getConnectionAdapter()
108 .roleRequest(ruleRequestInputBld.build());
113 * @param sessionContext
114 * @return generationId from future RpcResult
116 public static Future<BigInteger> readGenerationIdFromDevice(SessionContext sessionContext) {
117 Future<BigInteger> generationIdFuture = null;
118 Future<RpcResult<RoleRequestOutput>> roleReply = sendRoleChangeRequest(sessionContext, OfpRole.NOCHANGE, BigInteger.ZERO);
119 generationIdFuture = Futures.transform(
120 JdkFutureAdapters.listenInPoolThread(roleReply),
121 new Function<RpcResult<RoleRequestOutput>, BigInteger>() {
123 public BigInteger apply(RpcResult<RoleRequestOutput> input) {
124 return input.getResult().getGenerationId();
128 return generationIdFuture;
132 * @param generationId
133 * @return next (incremented value)
135 public static BigInteger getNextGenerationId(BigInteger generationId) {
136 BigInteger nextGenerationId = null;
137 if (generationId.compareTo(OFRoleManager.MAX_GENERATION_ID) < 0) {
138 nextGenerationId = generationId.add(BigInteger.ONE);
140 nextGenerationId = BigInteger.ZERO;
143 return nextGenerationId;
147 * @param rolePushResult
148 * @return future which throws {@link RolePushException}
150 public static CheckedFuture<Boolean, RolePushException> makeCheckedRuleRequestFxResult(
151 ListenableFuture<Boolean> rolePushResult) {
152 return Futures.makeChecked(
153 rolePushResult, exceptionFunction