2bd8346d13962a0ca05e3e611afbea4a82529b9f
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / OFRpcTaskFactory.java
1 /**
2  * Copyright (c) 2013 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.core.sal;
9
10 import java.math.BigInteger;
11 import java.util.concurrent.Future;
12 import java.util.concurrent.TimeUnit;
13
14 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.FlowConvertor;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAddedBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdatedBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowInput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev131103.TransactionId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23
24 /**
25  *
26  */
27 public abstract class OFRpcTaskFactory {
28
29     /**
30      * @param maxTimeout
31      * @param maxTimeoutUnit
32      * @param helper
33      * @return UpdateFlow task
34      */
35     public static OFRpcTask<AddFlowInput, RpcResult<UpdateFlowOutput>> createAddFlowTask(
36             final long maxTimeout, final TimeUnit maxTimeoutUnit, final OFRpcTaskHelper helper) {
37         OFRpcTask<AddFlowInput, RpcResult<UpdateFlowOutput>> task =
38                 new OFRpcTask<AddFlowInput, RpcResult<UpdateFlowOutput>>() {
39
40             @Override
41             public void run() {
42                 helper.rawBarrierSend(maxTimeout, maxTimeoutUnit, getInput().isBarrier(), getCookie(), getResult());
43                 if (getResult().isDone()) {
44                     return;
45                 }
46
47                 // Convert the AddFlowInput to FlowModInput
48                 FlowModInputBuilder ofFlowModInput = FlowConvertor.toFlowModInput(getInput(),
49                         getVersion(), getSession().getFeatures().getDatapathId());
50                 Long xId = getSession().getNextXid();
51                 ofFlowModInput.setXid(xId);
52
53                 if (null != getRpcNotificationProviderService()) {
54                     FlowAddedBuilder newFlow = new FlowAddedBuilder(
55                             (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow) getInput());
56                     newFlow.setTransactionId(new TransactionId(BigInteger.valueOf(xId.intValue())));
57                     newFlow.setFlowRef(getInput().getFlowRef());
58                     getRpcNotificationProviderService().publish(newFlow.build());
59                 }
60
61                 Future<RpcResult<UpdateFlowOutput>> resultFromOFLib =
62                         getMessageService().flowMod(ofFlowModInput.build(), getCookie());
63                 OFRpcTaskHelper.chainFutures(resultFromOFLib, getResult());
64             }
65         };
66         return task;
67     }
68
69     /**
70      * @param maxTimeout
71      * @param maxTimeoutUnit
72      * @param helper
73      * @return UpdateFlow task
74      */
75     public static OFRpcTask<UpdateFlowInput, RpcResult<UpdateFlowOutput>> createUpdateFlowTask(
76             final long maxTimeout, final TimeUnit maxTimeoutUnit, final OFRpcTaskHelper helper) {
77         OFRpcTask<UpdateFlowInput, RpcResult<UpdateFlowOutput>> task =
78                 new OFRpcTask<UpdateFlowInput, RpcResult<UpdateFlowOutput>>() {
79
80             @Override
81             public void run() {
82                 helper.rawBarrierSend(maxTimeout, maxTimeoutUnit, getInput().getUpdatedFlow().isBarrier(), getCookie(), getResult());
83                 if (getResult().isDone()) {
84                     return;
85                 }
86
87                 // Convert the AddFlowInput to FlowModInput
88                 FlowModInputBuilder ofFlowModInput = FlowConvertor.toFlowModInput(getInput().getUpdatedFlow(),
89                         getVersion(), getSession().getFeatures().getDatapathId());
90                 Long xId = getSession().getNextXid();
91                 ofFlowModInput.setXid(xId);
92
93                 if (null != getRpcNotificationProviderService()) {
94                     FlowUpdatedBuilder updFlow = new FlowUpdatedBuilder(getInput().getUpdatedFlow());
95                     updFlow.setTransactionId(new TransactionId(BigInteger.valueOf(xId.intValue())));
96                     updFlow.setFlowRef(getInput().getFlowRef());
97                     getRpcNotificationProviderService().publish(updFlow.build());
98                 }
99
100                 Future<RpcResult<UpdateFlowOutput>> resultFromOFLib =
101                         getMessageService().flowMod(ofFlowModInput.build(), getCookie());
102                 OFRpcTaskHelper.chainFutures(resultFromOFLib, getResult());
103             }
104         };
105         return task;
106     }
107
108 }