Merge "Bug 4957 TxChainManager lifecycle startup cleaning"
[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 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;
31
32 /**
33  *
34  */
35 public final class RoleUtil {
36
37     private static final Logger LOG = LoggerFactory.getLogger(RoleUtil.class);
38     private static final Function<Exception, RolePushException> exceptionFunction = new Function<Exception, RolePushException>() {
39         @Override
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();
45                 }
46             }
47
48             if (output == null) {
49                 output = new RolePushException(input.getMessage(), input);
50             }
51
52             return output;
53         }
54     };
55
56     private RoleUtil() {
57         throw new UnsupportedOperationException("RoleUtil is not expected to be instantiated.");
58     }
59
60     /**
61      * @param role openflow role for controller
62      * @return protocol role
63      */
64     public static ControllerRole toOFJavaRole(OfpRole role) {
65         ControllerRole ofJavaRole = null;
66         switch (role) {
67             case BECOMEMASTER:
68                 ofJavaRole = ControllerRole.OFPCRROLEMASTER;
69                 break;
70             case BECOMESLAVE:
71                 ofJavaRole = ControllerRole.OFPCRROLESLAVE;
72                 break;
73             case NOCHANGE:
74                 ofJavaRole = ControllerRole.OFPCRROLENOCHANGE;
75                 break;
76             default:
77                 // no intention
78                 LOG.warn("given role is not supported by protocol roles: {}", role);
79                 break;
80         }
81         return ofJavaRole;
82     }
83
84     /**
85      * @param session switch session context
86      * @param role  controller openflow role
87      * @param generationId generate id for role negotiation
88      * @return input builder
89      */
90     public static RoleRequestInputBuilder createRoleRequestInput(
91             final SessionContext session, OfpRole role, BigInteger generationId) {
92
93         ControllerRole ofJavaRole = RoleUtil.toOFJavaRole(role);
94
95         return new RoleRequestInputBuilder()
96                 .setGenerationId(generationId)
97                 .setRole(ofJavaRole)
98                 .setVersion(session.getFeatures().getVersion())
99                 .setXid(session.getNextXid());
100     }
101
102     /**
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
107      */
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());
112         return roleReply;
113     }
114
115     /**
116      * @param sessionContext switch session context
117      * @return generationId from future RpcResult
118      */
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();
122
123         Futures.addCallback(JdkFutureAdapters.listenInPoolThread(roleReply), new FutureCallback<RpcResult<RoleRequestOutput>>() {
124             @Override
125             public void onSuccess(RpcResult<RoleRequestOutput> input) {
126                 if(input != null && input.getResult() != null) {
127                     result.set(input.getResult().getGenerationId());
128                 }
129             }
130             @Override
131             public void onFailure(Throwable t) {
132                 //TODO
133             }
134         });
135         return result;
136     }
137
138     /**
139      * @param generationId generate id for role negotiation
140      * @return next (incremented value)
141      */
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);
146         } else {
147             nextGenerationId = BigInteger.ZERO;
148         }
149
150         return nextGenerationId;
151     }
152
153     /**
154      * @param rolePushResult result of role push request
155      * @return future which throws {@link RolePushException}
156      */
157     public static CheckedFuture<Boolean, RolePushException> makeCheckedRuleRequestFxResult(
158             ListenableFuture<Boolean> rolePushResult) {
159         return Futures.makeChecked(
160                 rolePushResult, exceptionFunction
161         );
162     }
163 }