Merge "L2 Gw create changes related to ITM Tunnels creation in neutronvpn module"
[vpnservice.git] / interfacemgr / interfacemgr-impl / src / main / java / org / opendaylight / vpnservice / interfacemgr / rpcservice / InterfaceManagerRpcService.java
1 /*
2  * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.vpnservice.interfacemgr.rpcservice;
10
11 import com.google.common.base.Optional;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.SettableFuture;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.vpnservice.interfacemgr.IfmConstants;
20 import org.opendaylight.vpnservice.interfacemgr.IfmUtil;
21 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceManagerCommonUtils;
22 import org.opendaylight.vpnservice.interfacemgr.commons.InterfaceMetaUtils;
23 import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
24 import org.opendaylight.vpnservice.mdsalutil.ActionType;
25 import org.opendaylight.vpnservice.mdsalutil.InstructionInfo;
26 import org.opendaylight.vpnservice.mdsalutil.InstructionType;
27 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
28 import org.opendaylight.vpnservice.mdsalutil.MatchFieldType;
29 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
30 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
31 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.L2vlan;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana._if.type.rev140508.Tunnel;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfaceType;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfL2vlan;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.IfIndexesInterfaceMap;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._if.indexes._interface.map.IfIndexInterface;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007._if.indexes._interface.map.IfIndexInterfaceKey;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntry;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.BridgeEntryKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.meta.rev151007.bridge._interface.info.bridge.entry.BridgeInterfaceEntry;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.IfTunnel;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.ParentRefs;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeBase;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeMplsOverGre;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.*;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54 import org.opendaylight.yangtools.yang.common.RpcError;
55 import org.opendaylight.yangtools.yang.common.RpcResult;
56 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 import java.math.BigInteger;
61 import java.util.ArrayList;
62 import java.util.Arrays;
63 import java.util.List;
64 import java.util.concurrent.Future;
65
66 public class InterfaceManagerRpcService implements OdlInterfaceRpcService {
67     private static final Logger LOG = LoggerFactory.getLogger(InterfaceManagerRpcService.class);
68     DataBroker dataBroker;
69     IMdsalApiManager mdsalMgr;
70
71
72     public InterfaceManagerRpcService(DataBroker dataBroker, IMdsalApiManager mdsalMgr) {
73         this.dataBroker = dataBroker;
74         this.mdsalMgr = mdsalMgr;
75     }
76
77     @Override
78     public Future<RpcResult<GetDpidFromInterfaceOutput>> getDpidFromInterface(GetDpidFromInterfaceInput input) {
79         String interfaceName = input.getIntfName();
80         RpcResultBuilder<GetDpidFromInterfaceOutput> rpcResultBuilder;
81         try {
82             BigInteger dpId = null;
83             InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
84             Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
85             if (Tunnel.class.equals(interfaceInfo.getType())) {
86                 ParentRefs parentRefs = interfaceInfo.getAugmentation(ParentRefs.class);
87                 dpId = parentRefs.getDatapathNodeIdentifier();
88             } else {
89                 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
90                         InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
91                 String lowerLayerIf = ifState.getLowerLayerIf().get(0);
92                 NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
93                 dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
94             }
95             GetDpidFromInterfaceOutputBuilder output = new GetDpidFromInterfaceOutputBuilder().setDpid(
96                     (dpId));
97             rpcResultBuilder = RpcResultBuilder.success();
98             rpcResultBuilder.withResult(output.build());
99         } catch (Exception e) {
100             LOG.error("Retrieval of datapath id for the key {} failed due to {}", interfaceName, e);
101             rpcResultBuilder = RpcResultBuilder.failed();
102         }
103         return Futures.immediateFuture(rpcResultBuilder.build());
104     }
105
106     @Override
107     public Future<RpcResult<Void>> createTerminatingServiceActions(final CreateTerminatingServiceActionsInput input) {
108         final SettableFuture<RpcResult<Void>> result = SettableFuture.create();
109         try{
110             LOG.info("create terminatingServiceAction on DpnId = {} for tunnel-key {}", input.getDpid() , input.getTunnelKey());
111             Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(new InterfaceKey(input.getInterfaceName()),dataBroker);
112             IfTunnel tunnelInfo = interfaceInfo.getAugmentation(IfTunnel.class);
113             if(tunnelInfo != null) {
114                 ListenableFuture<Void> installFlowResult = (tunnelInfo.getTunnelInterfaceType().isAssignableFrom(TunnelTypeMplsOverGre.class)) ?
115                         makeLFIBFlow(input.getDpid(),input.getTunnelKey(), input.getInstruction(), NwConstants.ADD_FLOW) :
116                         makeTerminatingServiceFlow(tunnelInfo, input.getDpid(), input.getTunnelKey(), input.getInstruction(), NwConstants.ADD_FLOW);
117                 Futures.addCallback(installFlowResult, new FutureCallback<Void>(){
118
119                     @Override
120                     public void onSuccess(Void aVoid) {
121                         result.set(RpcResultBuilder.<Void>success().build());
122                     }
123
124                     @Override
125                     public void onFailure(Throwable error) {
126                         String msg = String.format("Unable to install terminating service flow for %s", input.getInterfaceName());
127                         LOG.error("create terminating service actions failed. {}. {}", msg, error);
128                         result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, msg, error).build());
129                     }
130                 });
131                 result.set(RpcResultBuilder.<Void>success().build());
132             } else {
133                 String msg = String.format("Terminating Service Actions cannot be created for a non-tunnel interface %s",input.getInterfaceName());
134                 LOG.error(msg);
135                 result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, msg).build());
136             }
137         }catch(Exception e){
138             String msg = String.format("create Terminating Service Actions for %s failed",input.getInterfaceName());
139             LOG.error("create Terminating Service Actions for {} failed due to {}" ,input.getDpid(), e);
140             result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, msg, e.getMessage()).build());
141         }
142         return result;
143     }
144
145     @Override
146     public Future<RpcResult<Void>> removeTerminatingServiceActions(final RemoveTerminatingServiceActionsInput input) {
147         final SettableFuture<RpcResult<Void>> result = SettableFuture.create();
148         try{
149             WriteTransaction t = dataBroker.newWriteOnlyTransaction();
150             LOG.info("remove terminatingServiceAction on DpnId = {} for tunnel-key {}", input.getDpid() , input.getTunnelKey());
151
152             Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(new InterfaceKey(input.getInterfaceName()),dataBroker);
153             IfTunnel tunnelInfo = interfaceInfo.getAugmentation(IfTunnel.class);
154             if(tunnelInfo != null) {
155                 ListenableFuture<Void> removeFlowResult = (tunnelInfo.getTunnelInterfaceType().isAssignableFrom(TunnelTypeMplsOverGre.class)) ?
156                         makeLFIBFlow(input.getDpid(),input.getTunnelKey(), null, NwConstants.DEL_FLOW) :
157                         makeTerminatingServiceFlow(tunnelInfo, input.getDpid(), input.getTunnelKey(), null, NwConstants.DEL_FLOW);
158                 Futures.addCallback(removeFlowResult, new FutureCallback<Void>(){
159
160                     @Override
161                     public void onSuccess(Void aVoid) {
162                         result.set(RpcResultBuilder.<Void>success().build());
163                     }
164
165                     @Override
166                     public void onFailure(Throwable error) {
167                         String msg = String.format("Unable to install terminating service flow %s", input.getInterfaceName());
168                         LOG.error("create terminating service actions failed. {}. {}", msg, error);
169                         result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, msg, error).build());
170                     }
171                 });
172                 result.set(RpcResultBuilder.<Void>success().build());
173             } else {
174                 String msg = String.format("Terminating Service Actions cannot be removed for a non-tunnel interface %s",
175                         input.getInterfaceName());
176                 LOG.error(msg);
177                 result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, msg).build());
178             }
179         }catch(Exception e){
180             LOG.error("Remove Terminating Service Actions for {} failed due to {}" ,input.getDpid(), e);
181             String msg = String.format("Remove Terminating Service Actions for %d failed.", input.getDpid());
182             result.set(RpcResultBuilder.<Void>failed().withError(RpcError.ErrorType.APPLICATION, msg, e.getMessage()).build());
183         }
184         return result;
185     }
186
187     @Override
188     public Future<RpcResult<GetEndpointIpForDpnOutput>> getEndpointIpForDpn(GetEndpointIpForDpnInput input) {
189         RpcResultBuilder<GetEndpointIpForDpnOutput> rpcResultBuilder;
190         try {
191             BridgeEntryKey bridgeEntryKey = new BridgeEntryKey(input.getDpid());
192             InstanceIdentifier<BridgeEntry> bridgeEntryInstanceIdentifier =
193                     InterfaceMetaUtils.getBridgeEntryIdentifier(bridgeEntryKey);
194             BridgeEntry bridgeEntry =
195                     InterfaceMetaUtils.getBridgeEntryFromConfigDS(bridgeEntryInstanceIdentifier,
196                             dataBroker);
197             // local ip of any of the bridge interface entry will be the dpn end point ip
198             BridgeInterfaceEntry bridgeInterfaceEntry = bridgeEntry.getBridgeInterfaceEntry().get(0);
199             InterfaceKey interfaceKey = new InterfaceKey(bridgeInterfaceEntry.getInterfaceName());
200             Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
201             IfTunnel tunnel = interfaceInfo.getAugmentation(IfTunnel.class);
202             GetEndpointIpForDpnOutputBuilder endpointIpForDpnOutput = new GetEndpointIpForDpnOutputBuilder().setLocalIps(Arrays.asList(tunnel.getTunnelSource()));
203             rpcResultBuilder = RpcResultBuilder.success();
204             rpcResultBuilder.withResult(endpointIpForDpnOutput.build());
205         }catch(Exception e){
206             LOG.error("Retrieval of endpoint of for dpn {} failed due to {}" ,input.getDpid(), e);
207             rpcResultBuilder = RpcResultBuilder.failed();
208         }
209         return Futures.immediateFuture(rpcResultBuilder.build());
210     }
211
212     @Override
213     public Future<RpcResult<GetEgressInstructionsForInterfaceOutput>> getEgressInstructionsForInterface(GetEgressInstructionsForInterfaceInput input) {
214         RpcResultBuilder<GetEgressInstructionsForInterfaceOutput> rpcResultBuilder;
215         try {
216             List<InstructionInfo> instructionInfo = new ArrayList<InstructionInfo>();
217             List<ActionInfo> actionInfo = IfmUtil.getEgressActionInfosForInterface(input.getIntfName(),
218                                                                                    input.getTunnelKey(),
219                                                                                    0,  /* ActionKey starting value */
220                                                                                    dataBroker);
221             instructionInfo.add(new InstructionInfo(InstructionType.write_actions, actionInfo));
222                     GetEgressInstructionsForInterfaceOutputBuilder output = new GetEgressInstructionsForInterfaceOutputBuilder().
223                     setInstruction(buildInstructions(instructionInfo));
224             rpcResultBuilder = RpcResultBuilder.success();
225             rpcResultBuilder.withResult(output.build());
226         }catch(Exception e){
227             LOG.error("Retrieval of egress actions for the key {} failed due to {}" ,input.getIntfName(), e);
228             rpcResultBuilder = RpcResultBuilder.failed();
229         }
230         return Futures.immediateFuture(rpcResultBuilder.build());
231     }
232
233     @Override
234     public Future<RpcResult<GetInterfaceTypeOutput>> getInterfaceType(GetInterfaceTypeInput input) {
235         String interfaceName = input.getIntfName();
236         RpcResultBuilder<GetInterfaceTypeOutput> rpcResultBuilder;
237         try {
238             InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
239             Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
240
241             GetInterfaceTypeOutputBuilder output = new GetInterfaceTypeOutputBuilder().setInterfaceType(interfaceInfo.getType());
242             rpcResultBuilder = RpcResultBuilder.success();
243             rpcResultBuilder.withResult(output.build());
244         } catch (Exception e) {
245             LOG.error("Retrieval of interface type for the key {} failed due to {}", interfaceName, e);
246             rpcResultBuilder = RpcResultBuilder.failed();
247         }
248         return Futures.immediateFuture(rpcResultBuilder.build());
249     }
250
251     @Override
252     public Future<RpcResult<GetTunnelTypeOutput>> getTunnelType(GetTunnelTypeInput input) {
253         String interfaceName = input.getIntfName();
254         RpcResultBuilder<GetTunnelTypeOutput> rpcResultBuilder;
255         try {
256             InterfaceKey interfaceKey = new InterfaceKey(interfaceName);
257             Interface interfaceInfo = InterfaceManagerCommonUtils.getInterfaceFromConfigDS(interfaceKey, dataBroker);
258
259             if (Tunnel.class.equals(interfaceInfo.getType())) {
260                 IfTunnel tnl = interfaceInfo.getAugmentation(IfTunnel.class);
261                 Class <? extends TunnelTypeBase> tun_type = tnl.getTunnelInterfaceType();
262                 GetTunnelTypeOutputBuilder output = new GetTunnelTypeOutputBuilder().setTunnelType(tun_type);
263                 rpcResultBuilder = RpcResultBuilder.success();
264                 rpcResultBuilder.withResult(output.build());
265             } else {
266                 LOG.error("Retrieval of interface type for the key {} failed", interfaceName);
267                 rpcResultBuilder = RpcResultBuilder.failed();
268             }
269         } catch (Exception e) {
270             LOG.error("Retrieval of interface type for the key {} failed due to {}", interfaceName, e);
271             rpcResultBuilder = RpcResultBuilder.failed();
272         }
273         return Futures.immediateFuture(rpcResultBuilder.build());
274     }
275
276     @Override
277     public Future<RpcResult<GetEgressActionsForInterfaceOutput>> getEgressActionsForInterface(GetEgressActionsForInterfaceInput input) {
278         RpcResultBuilder<GetEgressActionsForInterfaceOutput> rpcResultBuilder;
279         try {
280             LOG.debug("Get Egress Action for interface {} with key {}", input.getIntfName(), input.getTunnelKey());
281             List<Action> actionsList = IfmUtil.getEgressActionsForInterface(input.getIntfName(),
282                                                                             input.getTunnelKey(),
283                                                                             dataBroker);
284             GetEgressActionsForInterfaceOutputBuilder output = new GetEgressActionsForInterfaceOutputBuilder().
285                     setAction(actionsList);
286             rpcResultBuilder = RpcResultBuilder.success();
287             rpcResultBuilder.withResult(output.build());
288         }catch(Exception e){
289             LOG.error("Retrieval of egress actions for the key {} failed due to {}" ,input.getIntfName(), e);
290             rpcResultBuilder = RpcResultBuilder.failed();
291         }
292         return Futures.immediateFuture(rpcResultBuilder.build());
293     }
294
295
296     @Override
297     public Future<RpcResult<GetPortFromInterfaceOutput>> getPortFromInterface(GetPortFromInterfaceInput input) {
298         RpcResultBuilder<GetPortFromInterfaceOutput> rpcResultBuilder;
299         String interfaceName = input.getIntfName();
300         try {
301             BigInteger dpId = null;
302             long portNo = 0;
303             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
304                         InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
305             String lowerLayerIf = ifState.getLowerLayerIf().get(0);
306             NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
307             dpId = new BigInteger(IfmUtil.getDpnFromNodeConnectorId(nodeConnectorId));
308             portNo = Long.valueOf(IfmUtil.getPortNoFromNodeConnectorId(nodeConnectorId));
309             // FIXME Assuming portName and interfaceName are same
310             GetPortFromInterfaceOutputBuilder output = new GetPortFromInterfaceOutputBuilder().setDpid(dpId).
311                     setPortname(interfaceName).setPortno(Long.valueOf(portNo));
312             rpcResultBuilder = RpcResultBuilder.success();
313             rpcResultBuilder.withResult(output.build());
314         }catch(Exception e){
315             LOG.error("Retrieval of lport tag for the key {} failed due to {}" ,input.getIntfName(), e);
316             rpcResultBuilder = RpcResultBuilder.failed();
317         }
318         return Futures.immediateFuture(rpcResultBuilder.build());
319     }
320
321     @Override
322     public Future<RpcResult<GetNodeconnectorIdFromInterfaceOutput>> getNodeconnectorIdFromInterface(GetNodeconnectorIdFromInterfaceInput input) {
323         String interfaceName = input.getIntfName();
324         RpcResultBuilder<GetNodeconnectorIdFromInterfaceOutput> rpcResultBuilder;
325         try {
326             org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface ifState =
327                     InterfaceManagerCommonUtils.getInterfaceStateFromOperDS(interfaceName, dataBroker);
328             String lowerLayerIf = ifState.getLowerLayerIf().get(0);
329             NodeConnectorId nodeConnectorId = new NodeConnectorId(lowerLayerIf);
330
331             GetNodeconnectorIdFromInterfaceOutputBuilder output = new GetNodeconnectorIdFromInterfaceOutputBuilder().setNodeconnectorId(nodeConnectorId);
332             rpcResultBuilder = RpcResultBuilder.success();
333             rpcResultBuilder.withResult(output.build());
334         } catch (Exception e) {
335             LOG.error("Retrieval of nodeconnector id for the key {} failed due to {}", interfaceName, e);
336             rpcResultBuilder = RpcResultBuilder.failed();
337         }
338         return Futures.immediateFuture(rpcResultBuilder.build());
339     }
340
341     @Override
342     public Future<RpcResult<GetInterfaceFromIfIndexOutput>> getInterfaceFromIfIndex(GetInterfaceFromIfIndexInput input) {
343         Integer ifIndex = input.getIfIndex();
344         RpcResultBuilder<GetInterfaceFromIfIndexOutput> rpcResultBuilder = null;
345         try {
346             InstanceIdentifier<IfIndexInterface> id = InstanceIdentifier.builder(IfIndexesInterfaceMap.class).child(IfIndexInterface.class, new IfIndexInterfaceKey(ifIndex)).build();
347             Optional<IfIndexInterface> ifIndexesInterface = IfmUtil.read(LogicalDatastoreType.OPERATIONAL, id, dataBroker);
348             if(ifIndexesInterface.isPresent()) {
349                 String interfaceName = ifIndexesInterface.get().getInterfaceName();
350                 GetInterfaceFromIfIndexOutputBuilder output = new GetInterfaceFromIfIndexOutputBuilder().setInterfaceName(interfaceName);
351                 rpcResultBuilder = RpcResultBuilder.success();
352                 rpcResultBuilder.withResult(output.build());
353             }
354         } catch (Exception e) {
355             LOG.error("Retrieval of interfaceName for the key {} failed due to {}", ifIndex, e);
356             rpcResultBuilder = RpcResultBuilder.failed();
357         }
358         return Futures.immediateFuture(rpcResultBuilder.build());
359     }
360
361     protected static List<Instruction> buildInstructions(List<InstructionInfo> listInstructionInfo) {
362         if (listInstructionInfo != null) {
363             List<Instruction> instructions = new ArrayList<Instruction>();
364             int instructionKey = 0;
365
366             for (InstructionInfo instructionInfo : listInstructionInfo) {
367                 instructions.add(instructionInfo.buildInstruction(instructionKey));
368                 instructionKey++;
369             }
370             return instructions;
371         }
372
373         return null;
374     }
375
376     private ListenableFuture<Void> makeTerminatingServiceFlow(IfTunnel tunnelInfo, BigInteger dpnId, BigInteger tunnelKey, List<Instruction> instruction, int addOrRemove) {
377         List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
378         mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] {tunnelKey}));
379         short tableId = tunnelInfo.isInternal() ? NwConstants.INTERNAL_TUNNEL_TABLE :
380                 NwConstants.EXTERNAL_TUNNEL_TABLE;
381         final String flowRef = getFlowRef(dpnId,tableId, tunnelKey);
382         Flow terminatingSerFlow = MDSALUtil.buildFlowNew(tableId, flowRef,
383                 5, "TST Flow Entry", 0, 0,
384                 IfmConstants.TUNNEL_TABLE_COOKIE.add(tunnelKey), mkMatches, instruction);
385         if (addOrRemove == NwConstants.ADD_FLOW) {
386             return mdsalMgr.installFlow(dpnId, terminatingSerFlow);
387         }
388
389         return mdsalMgr.removeFlow(dpnId, terminatingSerFlow);
390     }
391
392     private ListenableFuture<Void> makeLFIBFlow(BigInteger dpnId, BigInteger tunnelKey, List<Instruction> instruction, int addOrRemove) {
393         List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
394         mkMatches.add(new MatchInfo(MatchFieldType.eth_type,
395                 new long[]{0x8847L}));
396         mkMatches.add(new MatchInfo(MatchFieldType.mpls_label, new String[]{Long.toString(tunnelKey.longValue())}));
397         // Install the flow entry in L3_LFIB_TABLE
398         String flowRef = getFlowRef(dpnId, NwConstants.L3_LFIB_TABLE, tunnelKey);
399
400         Flow lfibFlow = MDSALUtil.buildFlowNew(NwConstants.L3_LFIB_TABLE, flowRef,
401                 IfmConstants.DEFAULT_FLOW_PRIORITY, "LFIB Entry", 0, 0,
402                 IfmConstants.COOKIE_VM_LFIB_TABLE, mkMatches, instruction);
403         if (addOrRemove == NwConstants.ADD_FLOW) {
404             return mdsalMgr.installFlow(dpnId, lfibFlow);
405         }
406         return mdsalMgr.removeFlow(dpnId, lfibFlow);
407     }
408
409     private String getFlowRef(BigInteger dpnId, short tableId, BigInteger tunnelKey) {
410         return new StringBuffer().append(IfmConstants.TUNNEL_TABLE_FLOWID_PREFIX).append(dpnId).append(NwConstants.FLOWID_SEPARATOR)
411                 .append(tableId).append(NwConstants.FLOWID_SEPARATOR).append(tunnelKey).toString();
412     }
413 }