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