Bug-2827: role switch proposal
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / util / RoleUtil.java
1 /**
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.openflow.md.util;
9
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;
28
29 /**
30  *
31  */
32 public final class RoleUtil {
33
34     private static final Logger LOG = LoggerFactory.getLogger(RoleUtil.class);
35     private static final Function<Exception, RolePushException> exceptionFunction = new Function<Exception, RolePushException>() {
36         @Override
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();
42                 }
43             }
44
45             if (output == null) {
46                 output = new RolePushException(input.getMessage(), input);
47             }
48
49             return output;
50         }
51     };
52
53     private RoleUtil() {
54         throw new UnsupportedOperationException("RoleUtil is not expected to be instantiated.");
55     }
56
57     /**
58      * @param role
59      * @return protocol role
60      */
61     public static ControllerRole toOFJavaRole(OfpRole role) {
62         ControllerRole ofJavaRole = null;
63         switch (role) {
64             case BECOMEMASTER:
65                 ofJavaRole = ControllerRole.OFPCRROLEMASTER;
66                 break;
67             case BECOMESLAVE:
68                 ofJavaRole = ControllerRole.OFPCRROLESLAVE;
69                 break;
70             case NOCHANGE:
71                 ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
72                 break;
73             default:
74                 // no intention
75                 LOG.warn("given role is not supported by protocol roles: {}", role);
76                 break;
77         }
78         return ofJavaRole;
79     }
80
81     /**
82      * @param session
83      * @param role
84      * @param generationId
85      * @return input builder
86      */
87     public static RoleRequestInputBuilder createRoleRequestInput(
88             final SessionContext session, OfpRole role, BigInteger generationId) {
89
90         ControllerRole ofJavaRole = RoleUtil.toOFJavaRole(role);
91
92         return new RoleRequestInputBuilder()
93                 .setGenerationId(generationId)
94                 .setRole(ofJavaRole)
95                 .setVersion(session.getFeatures().getVersion())
96                 .setXid(session.getNextXid());
97     }
98
99     /**
100      * @param sessionContext
101      * @param ofpRole
102      * @param generationId
103      * @return roleRequest future result
104      */
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());
109         return roleReply;
110     }
111
112     /**
113      * @param sessionContext
114      * @return generationId from future RpcResult
115      */
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>() {
122                     @Override
123                     public BigInteger apply(RpcResult<RoleRequestOutput> input) {
124                         return input.getResult().getGenerationId();
125                     }
126                 });
127
128         return generationIdFuture;
129     }
130
131     /**
132      * @param generationId
133      * @return next (incremented value)
134      */
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);
139         } else {
140             nextGenerationId = BigInteger.ZERO;
141         }
142
143         return nextGenerationId;
144     }
145
146     /**
147      * @param rolePushResult
148      * @return future which throws {@link RolePushException}
149      */
150     public static CheckedFuture<Boolean, RolePushException> makeCheckedRuleRequestFxResult(
151             ListenableFuture<Boolean> rolePushResult) {
152         return Futures.makeChecked(
153                 rolePushResult, exceptionFunction
154         );
155     }
156 }