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