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