Moving TableIds into a single constants file
[vpnservice.git] / fibmanager / fibmanager-impl / src / main / java / org / opendaylight / vpnservice / fibmanager / FibManager.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 package org.opendaylight.vpnservice.fibmanager;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14
15 import java.math.BigInteger;
16 import java.net.InetAddress;
17 import java.net.UnknownHostException;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.Future;
23
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
26 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
27 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
28 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
29 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
30 import org.opendaylight.vpnmanager.api.IVpnManager;
31 import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
32 import org.opendaylight.vpnservice.itm.globals.ITMConstants;
33 import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
34 import org.opendaylight.vpnservice.mdsalutil.ActionType;
35 import org.opendaylight.vpnservice.mdsalutil.FlowEntity;
36 import org.opendaylight.vpnservice.mdsalutil.InstructionInfo;
37 import org.opendaylight.vpnservice.mdsalutil.InstructionType;
38 import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
39 import org.opendaylight.vpnservice.mdsalutil.MatchFieldType;
40 import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
41 import org.opendaylight.vpnservice.mdsalutil.MetaDataUtil;
42 import org.opendaylight.vpnservice.mdsalutil.NwConstants;
43 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.PrefixToInterface;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.VpnInstanceOpData;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.prefix.to._interface.VpnIds;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.prefix.to._interface.VpnIdsKey;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.prefix.to._interface.vpn.ids.Prefixes;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.prefix.to._interface.vpn.ids.PrefixesKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntryKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnToDpnList;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.ItmRpcService;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.FibEntries;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.fibentries.VrfTables;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.fibentries.VrfTablesKey;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.vrfentries.VrfEntry;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeBase;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rev150331.TunnelTypeMplsOverGre;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetEgressActionsForInterfaceInputBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetEgressActionsForInterfaceOutput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetTunnelTypeInputBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.GetTunnelTypeOutput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.OdlInterfaceRpcService;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.l3nexthop.rev150409.l3nexthop.vpnnexthops.VpnNexthop;
67 import org.opendaylight.yangtools.concepts.ListenerRegistration;
68 import org.opendaylight.yangtools.yang.binding.DataObject;
69 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
70 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
71 import org.opendaylight.yangtools.yang.common.RpcResult;
72 import org.slf4j.Logger;
73 import org.slf4j.LoggerFactory;
74
75 public class FibManager extends AbstractDataChangeListener<VrfEntry> implements AutoCloseable{
76   private static final Logger LOG = LoggerFactory.getLogger(FibManager.class);
77   private static final String FLOWID_PREFIX = "L3.";
78   private ListenerRegistration<DataChangeListener> listenerRegistration;
79   private final DataBroker broker;
80   private IMdsalApiManager mdsalManager;
81   private IVpnManager vpnmanager;
82   private NexthopManager nextHopManager;
83   private ItmRpcService itmManager;
84   private OdlInterfaceRpcService interfaceManager;
85   private static final BigInteger COOKIE_VM_LFIB_TABLE = new BigInteger("8000002", 16);
86   private static final BigInteger COOKIE_VM_FIB_TABLE =  new BigInteger("8000003", 16);
87   private static final int DEFAULT_FIB_FLOW_PRIORITY = 10;
88   private static final BigInteger METADATA_MASK_CLEAR = new BigInteger("000000FFFFFFFFFF", 16);
89   private static final BigInteger CLEAR_METADATA = BigInteger.valueOf(0);
90   public static final BigInteger COOKIE_TUNNEL = new BigInteger("9000000", 16);
91
92
93   private static final FutureCallback<Void> DEFAULT_CALLBACK =
94       new FutureCallback<Void>() {
95         public void onSuccess(Void result) {
96           LOG.debug("Success in Datastore write operation");
97         }
98
99         public void onFailure(Throwable error) {
100           LOG.error("Error in Datastore write operation", error);
101         };
102       };
103
104   public FibManager(final DataBroker db) {
105     super(VrfEntry.class);
106     broker = db;
107     registerListener(db);
108   }
109
110   @Override
111   public void close() throws Exception {
112     if (listenerRegistration != null) {
113       try {
114         listenerRegistration.close();
115       } catch (final Exception e) {
116         LOG.error("Error when cleaning up DataChangeListener.", e);
117       }
118       listenerRegistration = null;
119     }
120     LOG.info("Fib Manager Closed");
121   }
122
123   public void setNextHopManager(NexthopManager nextHopManager) {
124     this.nextHopManager = nextHopManager;
125   }
126
127   public void setMdsalManager(IMdsalApiManager mdsalManager) {
128     this.mdsalManager = mdsalManager;
129   }
130
131   public void setVpnmanager(IVpnManager vpnmanager) {
132     this.vpnmanager = vpnmanager;
133   }
134
135   public void setITMRpcService(ItmRpcService itmManager) {
136       this.itmManager = itmManager;
137   }
138   
139   public void setInterfaceManager(OdlInterfaceRpcService ifManager) {
140       this.interfaceManager = ifManager;
141   }
142
143   private void registerListener(final DataBroker db) {
144     try {
145       listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
146                                                            getWildCardPath(), FibManager.this, DataChangeScope.SUBTREE);
147     } catch (final Exception e) {
148       LOG.error("FibManager DataChange listener registration fail!", e);
149       throw new IllegalStateException("FibManager registration Listener failed.", e);
150     }
151   }
152
153   private <T extends DataObject> Optional<T> read(LogicalDatastoreType datastoreType,
154                                                   InstanceIdentifier<T> path) {
155
156     ReadOnlyTransaction tx = broker.newReadOnlyTransaction();
157
158     Optional<T> result = Optional.absent();
159     try {
160       result = tx.read(datastoreType, path).get();
161     } catch (Exception e) {
162       throw new RuntimeException(e);
163     }
164
165     return result;
166   }
167
168   private InstanceIdentifier<VrfEntry> getWildCardPath() {
169     return InstanceIdentifier.create(FibEntries.class).child(VrfTables.class).child(VrfEntry.class);
170   }
171
172   private <T extends DataObject> void asyncWrite(LogicalDatastoreType datastoreType,
173                                                  InstanceIdentifier<T> path, T data, FutureCallback<Void> callback) {
174     WriteTransaction tx = broker.newWriteOnlyTransaction();
175     tx.put(datastoreType, path, data, true);
176     Futures.addCallback(tx.submit(), callback);
177   }
178
179   @Override
180   protected void add(final InstanceIdentifier<VrfEntry> identifier,
181                      final VrfEntry vrfEntry) {
182     LOG.trace("key: " + identifier + ", value=" + vrfEntry );
183     createFibEntries(identifier, vrfEntry);
184   }
185
186   @Override
187   protected void remove(InstanceIdentifier<VrfEntry> identifier, VrfEntry vrfEntry) {
188     LOG.trace("key: " + identifier + ", value=" + vrfEntry);
189     deleteFibEntries(identifier, vrfEntry);
190   }
191
192   @Override
193   protected void update(InstanceIdentifier<VrfEntry> identifier, VrfEntry original, VrfEntry update) {
194     LOG.trace("key: " + identifier + ", original=" + original + ", update=" + update );
195     createFibEntries(identifier, update);
196   }
197
198   private void createFibEntries(final InstanceIdentifier<VrfEntry> identifier,
199                                 final VrfEntry vrfEntry) {
200     final VrfTablesKey vrfTableKey = identifier.firstKeyOf(VrfTables.class, VrfTablesKey.class);
201     Preconditions.checkNotNull(vrfTableKey, "VrfTablesKey cannot be null or empty!");
202     Preconditions.checkNotNull(vrfEntry, "VrfEntry cannot be null or empty!");
203
204     VpnInstanceOpDataEntry vpnInstance = getVpnInstance(vrfTableKey.getRouteDistinguisher());
205     Preconditions.checkNotNull(vpnInstance, "Vpn Instance not available!");
206     Preconditions.checkNotNull(vpnInstance.getVpnId(), "Vpn Instance with rd " + vpnInstance.getVrfId() + "has null vpnId!");
207
208     Collection<VpnToDpnList> vpnToDpnList = vpnInstance.getVpnToDpnList();
209     BigInteger localDpnId = createLocalFibEntry(vpnInstance.getVpnId(),
210               vrfTableKey.getRouteDistinguisher(), vrfEntry);
211     if (vpnToDpnList != null) {
212       for (VpnToDpnList curDpn : vpnToDpnList) {
213         if (!curDpn.getDpnId().equals(localDpnId)) {
214           createRemoteFibEntry(localDpnId, curDpn.getDpnId(), vpnInstance.getVpnId(),
215                                vrfTableKey, vrfEntry);
216         }
217       }
218     }
219   }
220
221   public BigInteger createLocalFibEntry(Long vpnId, String rd, VrfEntry vrfEntry) {
222     BigInteger localDpnId = BigInteger.ZERO;
223     Prefixes localNextHopInfo = getPrefixToInterface(vpnId, vrfEntry.getDestPrefix());
224     boolean staticRoute = false;
225
226     //If the vrf entry is a static/extra route, the nexthop of the entry would be a adjacency in the vpn
227     if(localNextHopInfo == null) {
228       localNextHopInfo = getPrefixToInterface(vpnId, vrfEntry.getNextHopAddress() + "/32");
229       staticRoute = true;
230     }
231
232     if(localNextHopInfo != null) {
233       localDpnId = localNextHopInfo.getDpnId();
234       long groupId = nextHopManager.createLocalNextHop(vpnId, localDpnId, localNextHopInfo.getVpnInterfaceName(),
235                                                         (staticRoute == true) ? vrfEntry.getNextHopAddress() + "/32" : vrfEntry.getDestPrefix());
236       List<ActionInfo> actionInfos = new ArrayList<ActionInfo>();
237
238       actionInfos.add(new ActionInfo(ActionType.group, new String[] { String.valueOf(groupId)}));
239
240       makeConnectedRoute(localDpnId, vpnId, vrfEntry, rd, actionInfos, NwConstants.ADD_FLOW);
241       makeLFibTableEntry(localDpnId, vrfEntry.getLabel(), groupId, vrfEntry.getNextHopAddress(), NwConstants.ADD_FLOW);
242
243       LOG.debug("Installing tunnel table entry on dpn {} for interface {} with label {}", 
244                       localDpnId, localNextHopInfo.getVpnInterfaceName(), vrfEntry.getLabel());
245       makeTunnelTableEntry(localDpnId, vrfEntry.getLabel(), groupId);
246
247     }
248     return localDpnId;
249   }
250
251   private void makeTunnelTableEntry(BigInteger dpId, long label, long groupId/*String egressInterfaceName*/) {
252       List<ActionInfo> actionsInfos = new ArrayList<ActionInfo>();
253       actionsInfos.add(new ActionInfo(ActionType.group, new String[] { String.valueOf(groupId) }));
254
255
256       createTerminatingServiceActions(dpId, (int)label, actionsInfos);
257
258       LOG.debug("Terminating service Entry for dpID {} : label : {} egress : {} installed successfully {}",
259               dpId, label, groupId);
260   }
261
262   public void createTerminatingServiceActions( BigInteger destDpId, int label, List<ActionInfo> actionsInfos) {
263       List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
264
265       LOG.info("create terminatingServiceAction on DpnId = {} and serviceId = {} and actions = {}", destDpId , label,actionsInfos);
266
267       // Matching metadata
268       // FIXME vxlan vni bit set is not working properly with OVS.need to revisit
269       mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] {BigInteger.valueOf(label)}));
270
271       List<InstructionInfo> mkInstructions = new ArrayList<InstructionInfo>();
272       mkInstructions.add(new InstructionInfo(InstructionType.write_actions, actionsInfos));
273
274       FlowEntity terminatingServiceTableFlowEntity = MDSALUtil.buildFlowEntity(destDpId, NwConstants.INTERNAL_TUNNEL_TABLE,
275                       getFlowRef(destDpId, NwConstants.INTERNAL_TUNNEL_TABLE,label), 5, String.format("%s:%d","TST Flow Entry ",label),
276                       0, 0, COOKIE_TUNNEL.add(BigInteger.valueOf(label)),mkMatches, mkInstructions);
277
278       mdsalManager.installFlow(terminatingServiceTableFlowEntity);
279  }
280
281   private void removeTunnelTableEntry(BigInteger dpId, long label) {
282     FlowEntity flowEntity;
283     LOG.info("remove terminatingServiceActions called with DpnId = {} and label = {}", dpId , label);
284     List<MatchInfo> mkMatches = new ArrayList<MatchInfo>();
285     // Matching metadata
286     mkMatches.add(new MatchInfo(MatchFieldType.tunnel_id, new BigInteger[] {
287         MetaDataUtil.getTunnelIdWithValidVniBitAndVniSet((int)label),
288         MetaDataUtil.METADA_MASK_TUNNEL_ID }));
289     flowEntity = MDSALUtil.buildFlowEntity(dpId,
290                                            NwConstants.INTERNAL_TUNNEL_TABLE,
291                                            getFlowRef(dpId, NwConstants.INTERNAL_TUNNEL_TABLE, (int)label),
292                                            5, String.format("%s:%d","TST Flow Entry ",label), 0, 0,
293                                            COOKIE_TUNNEL.add(BigInteger.valueOf(label)), mkMatches, null);
294     mdsalManager.removeFlow(flowEntity);
295     LOG.debug("Terminating service Entry for dpID {} : label : {} removed successfully {}",dpId, label);
296   }
297
298   public BigInteger deleteLocalFibEntry(Long vpnId, String rd, VrfEntry vrfEntry) {
299     BigInteger localDpnId = BigInteger.ZERO;
300     VpnNexthop localNextHopInfo = nextHopManager.getVpnNexthop(vpnId, vrfEntry.getDestPrefix());
301     boolean staticRoute = false;
302
303     //If the vrf entry is a static/extra route, the nexthop of the entry would be a adjacency in the vpn
304     if(localNextHopInfo == null) {
305       localNextHopInfo = nextHopManager.getVpnNexthop(vpnId, vrfEntry.getNextHopAddress() + "/32");
306       staticRoute = true;
307     }
308
309     if(localNextHopInfo != null) {
310       localDpnId = localNextHopInfo.getDpnId();
311       if (getPrefixToInterface(vpnId, (staticRoute == true) ? vrfEntry.getNextHopAddress() + "/32" : vrfEntry.getDestPrefix()) == null) {
312         makeConnectedRoute(localDpnId, vpnId, vrfEntry, rd, null /* invalid */,
313                            NwConstants.DEL_FLOW);
314         makeLFibTableEntry(localDpnId, vrfEntry.getLabel(), 0 /* invalid */,
315                            vrfEntry.getNextHopAddress(), NwConstants.DEL_FLOW);
316         removeTunnelTableEntry(localDpnId, vrfEntry.getLabel());
317         deleteLocalAdjacency(localDpnId, vpnId, (staticRoute == true) ? vrfEntry.getNextHopAddress() + "/32" : vrfEntry.getDestPrefix());
318       }
319     }
320     return localDpnId;
321   }
322
323   private InstanceIdentifier<Prefixes> getPrefixToInterfaceIdentifier(Long vpnId, String ipPrefix) {
324     return InstanceIdentifier.builder(PrefixToInterface.class)
325         .child(VpnIds.class, new VpnIdsKey(vpnId)).child(Prefixes.class, new PrefixesKey(ipPrefix)).build();
326   }
327
328   private Prefixes getPrefixToInterface(Long vpnId, String ipPrefix) {
329     Optional<Prefixes> localNextHopInfoData =
330         read(LogicalDatastoreType.OPERATIONAL, getPrefixToInterfaceIdentifier(vpnId, ipPrefix));
331     return  localNextHopInfoData.isPresent() ? localNextHopInfoData.get() : null;
332   }
333   
334
335   private Class<? extends TunnelTypeBase> getTunnelType(String ifName) {
336         try {
337             Future<RpcResult<GetTunnelTypeOutput>> result = interfaceManager.getTunnelType(
338                           new GetTunnelTypeInputBuilder().setIntfName(ifName).build());
339           RpcResult<GetTunnelTypeOutput> rpcResult = result.get();
340           if(!rpcResult.isSuccessful()) {
341               LOG.warn("RPC Call to getTunnelInterfaceId returned with Errors {}", rpcResult.getErrors());
342           } else {
343               return rpcResult.getResult().getTunnelType();
344           }
345           
346       } catch (InterruptedException | ExecutionException e) {
347           LOG.warn("Exception when getting tunnel interface Id for tunnel type {}", e);
348       }
349   
350   return null;
351
352   }
353   private void createRemoteFibEntry(final BigInteger localDpnId, final BigInteger remoteDpnId,
354                                     final long vpnId, final VrfTablesKey vrfTableKey,
355                                     final VrfEntry vrfEntry) {
356     String rd = vrfTableKey.getRouteDistinguisher();
357     LOG.debug("adding route " + vrfEntry.getDestPrefix() + " " + rd);
358     /********************************************/
359     String tunnelInterface = resolveAdjacency(localDpnId, remoteDpnId, vpnId, vrfEntry);
360     if(tunnelInterface == null) {
361       LOG.error("Could not get interface for nexthop: {} in vpn {}",
362                                    vrfEntry.getNextHopAddress(), rd);
363       LOG.warn("Failed to add Route: {} in vpn: {}",
364                              vrfEntry.getDestPrefix(), rd);
365       return;
366     }
367       List<ActionInfo> actionInfos = new ArrayList<>();
368         Class<? extends TunnelTypeBase> tunnel_type = getTunnelType(tunnelInterface);
369     if (tunnel_type.equals(TunnelTypeMplsOverGre.class)) {
370         LOG.debug("Push label action for prefix {}", vrfEntry.getDestPrefix());
371         actionInfos.add(new ActionInfo(ActionType.push_mpls, new String[] { null }));
372         actionInfos.add(new ActionInfo(ActionType.set_field_mpls_label, new String[] { Long.toString(vrfEntry.getLabel())}));
373     } else {
374         int label = vrfEntry.getLabel().intValue();
375         BigInteger tunnelId;
376         // FIXME vxlan vni bit set is not working properly with OVS.need to revisit
377         if(tunnel_type.equals(TunnelTypeVxlan.class)) {
378                 tunnelId = BigInteger.valueOf(label);
379         } else {
380                 tunnelId = BigInteger.valueOf(label);
381         }
382         LOG.debug("adding set tunnel id action for label {}", label);
383         actionInfos.add(new ActionInfo(ActionType.set_field_tunnel_id, new BigInteger[]{
384                 tunnelId}));
385     }
386     actionInfos.addAll(nextHopManager.getEgressActionsForInterface(tunnelInterface));
387 /*
388     List<ActionInfo> actionInfos = resolveAdjacency(localDpnId, remoteDpnId, vpnId, vrfEntry);
389     if(actionInfos == null) {
390       LOG.error("Could not get nexthop group id for nexthop: {} in vpn {}",
391                                    vrfEntry.getNextHopAddress(), rd);
392       LOG.warn("Failed to add Route: {} in vpn: {}",
393                              vrfEntry.getDestPrefix(), rd);
394       return;
395     }
396     BigInteger dpnId = nextHopManager.getDpnForPrefix(vpnId, vrfEntry.getDestPrefix());
397     if(dpnId == null) {
398         //This route may be extra route... try to query with nexthop Ip
399         LOG.debug("Checking for extra route to install remote fib entry {}", vrfEntry.getDestPrefix());
400         dpnId = nextHopManager.getDpnForPrefix(vpnId, vrfEntry.getNextHopAddress() + "/32");
401     }
402     if(dpnId == null) {
403         LOG.debug("Push label action for prefix {}", vrfEntry.getDestPrefix());
404         actionInfos.add(new ActionInfo(ActionType.push_mpls, new String[] { null }));
405         actionInfos.add(new ActionInfo(ActionType.set_field_mpls_label, new String[] { Long.toString(vrfEntry.getLabel())}));
406     } else {
407         int label = vrfEntry.getLabel().intValue();
408         LOG.debug("adding set tunnel id action for label {}", label);
409         actionInfos.add(new ActionInfo(ActionType.set_field_tunnel_id, new BigInteger[] {
410                 MetaDataUtil.getTunnelIdWithValidVniBitAndVniSet(label),
411                 MetaDataUtil.METADA_MASK_VALID_TUNNEL_ID_BIT_AND_TUNNEL_ID }));
412     }
413 **/
414       makeConnectedRoute(remoteDpnId, vpnId, vrfEntry, rd, actionInfos, NwConstants.ADD_FLOW);
415     LOG.debug(
416         "Successfully added fib entry for " + vrfEntry.getDestPrefix() + " vpnId " + vpnId);
417   }
418
419   private void deleteFibEntries(final InstanceIdentifier<VrfEntry> identifier,
420                                 final VrfEntry vrfEntry) {
421     final VrfTablesKey vrfTableKey = identifier.firstKeyOf(VrfTables.class, VrfTablesKey.class);
422     Preconditions.checkNotNull(vrfTableKey, "VrfTablesKey cannot be null or empty!");
423     Preconditions.checkNotNull(vrfEntry, "VrfEntry cannot be null or empty!");
424
425     VpnInstanceOpDataEntry vpnInstance = getVpnInstance(vrfTableKey.getRouteDistinguisher());
426     Preconditions.checkNotNull(vpnInstance, "Vpn Instance not available!");
427     Collection<VpnToDpnList> vpnToDpnList = vpnInstance.getVpnToDpnList();
428     BigInteger localDpnId = deleteLocalFibEntry(vpnInstance.getVpnId(),
429               vrfTableKey.getRouteDistinguisher(), vrfEntry);
430     if (vpnToDpnList != null) {
431       for (VpnToDpnList curDpn : vpnToDpnList) {
432         if (!curDpn.getDpnId().equals(localDpnId)) {
433           deleteRemoteRoute(localDpnId, curDpn.getDpnId(), vpnInstance.getVpnId(), vrfTableKey, vrfEntry);
434         }
435       }
436
437     }
438   }
439
440   public void deleteRemoteRoute(final BigInteger localDpnId, final BigInteger remoteDpnId,
441                                 final long vpnId, final VrfTablesKey vrfTableKey,
442                                 final VrfEntry vrfEntry) {
443     LOG.debug("deleting route "+ vrfEntry.getDestPrefix() + " "+vpnId);
444     String rd = vrfTableKey.getRouteDistinguisher();
445     String egressInterface = resolveAdjacency(localDpnId, remoteDpnId, vpnId, vrfEntry);
446     if(egressInterface == null) {
447       LOG.error("Could not get nexthop group id for nexthop: {} in vpn {}",
448                 vrfEntry.getNextHopAddress(), rd);
449       LOG.warn("Failed to delete Route: {} in vpn: {}",
450                vrfEntry.getDestPrefix(), rd);
451       return;
452     }
453
454     makeConnectedRoute(remoteDpnId, vpnId, vrfEntry, rd, null, NwConstants.DEL_FLOW);
455     LOG.debug("Successfully delete fib entry for "+ vrfEntry.getDestPrefix() + " vpnId "+vpnId);
456   }
457
458   private long getIpAddress(byte[] rawIpAddress) {
459     return (((rawIpAddress[0] & 0xFF) << (3 * 8)) + ((rawIpAddress[1] & 0xFF) << (2 * 8))
460             + ((rawIpAddress[2] & 0xFF) << (1 * 8)) + (rawIpAddress[3] & 0xFF)) & 0xffffffffL;
461   }
462
463   private void makeConnectedRoute(BigInteger dpId, long vpnId, VrfEntry vrfEntry, String rd,
464                                   List<ActionInfo> actionInfos, int addOrRemove) {
465     LOG.trace("makeConnectedRoute: vrfEntry {}",vrfEntry);
466     String values[] = vrfEntry.getDestPrefix().split("/");
467     String ipAddress = values[0];
468     int prefixLength = (values.length == 1) ? 0 : Integer.parseInt(values[1]);
469     LOG.debug("Adding route to DPN. ip {} masklen {}", ipAddress, prefixLength);
470     InetAddress destPrefix = null;
471     try {
472       destPrefix = InetAddress.getByName(ipAddress);
473     } catch (UnknownHostException e) {
474       LOG.error("UnknowHostException in addRoute. Failed  to add Route for ipPrefix {}", vrfEntry.getDestPrefix());
475       return;
476     }
477
478     List<MatchInfo> matches = new ArrayList<MatchInfo>();
479
480     matches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
481         BigInteger.valueOf(vpnId), MetaDataUtil.METADATA_MASK_VRFID }));
482
483     matches.add(new MatchInfo(MatchFieldType.eth_type,
484                               new long[] { 0x0800L }));
485
486     if(prefixLength != 0) {
487       matches.add(new MatchInfo(MatchFieldType.ipv4_dst, new long[] {
488           getIpAddress(destPrefix.getAddress()), prefixLength }));
489     }
490
491     List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
492     if(addOrRemove == NwConstants.ADD_FLOW) {
493       instructions.add(new InstructionInfo(InstructionType.write_actions, actionInfos));
494     }
495
496     String flowRef = getFlowRef(dpId, NwConstants.L3_FIB_TABLE, rd, destPrefix);
497
498     FlowEntity flowEntity;
499
500     int priority = DEFAULT_FIB_FLOW_PRIORITY + prefixLength;
501     flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.L3_FIB_TABLE, flowRef,
502                                            priority, flowRef, 0, 0,
503                                            COOKIE_VM_FIB_TABLE, matches, instructions);
504
505     if (addOrRemove == NwConstants.ADD_FLOW) {
506       /* We need to call sync API to install flow so that 2 DS operations on the same object do not
507       * happen at same time. However, MDSALManager's syncInstallFlow takes a delay time (or uses a default one) to wait
508       * for or for notification that operational DS write for flows is done. We do not turn on the stats writing for flows,
509       * so that notification never comes, so we do not need that wait. Sending the lowest value of wait "1 ms" since 0 wait means
510       * wait indefinitely. */
511       // FIXME: sync calls.
512       //mdsalManager.syncInstallFlow(flowEntity, 1);
513       mdsalManager.installFlow(flowEntity);
514     } else {
515       // FIXME: sync calls.
516       // mdsalManager.syncRemoveFlow(flowEntity, 1);
517       mdsalManager.removeFlow(flowEntity);
518     }
519   }
520
521   private void makeLFibTableEntry(BigInteger dpId, long label, long groupId,
522                                   String nextHop, int addOrRemove) {
523     List<MatchInfo> matches = new ArrayList<MatchInfo>();
524     matches.add(new MatchInfo(MatchFieldType.eth_type,
525                               new long[] { 0x8847L }));
526     matches.add(new MatchInfo(MatchFieldType.mpls_label, new String[]{Long.toString(label)}));
527
528     List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
529     List<ActionInfo> actionsInfos = new ArrayList<ActionInfo>();
530     actionsInfos.add(new ActionInfo(ActionType.pop_mpls, new String[]{}));
531     actionsInfos.add(new ActionInfo(ActionType.group, new String[] { String.valueOf(groupId) }));
532     instructions.add(new InstructionInfo(InstructionType.write_actions, actionsInfos));
533
534     // Install the flow entry in L3_LFIB_TABLE
535     String flowRef = getFlowRef(dpId, NwConstants.L3_LFIB_TABLE, label, nextHop);
536
537     FlowEntity flowEntity;
538     flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.L3_LFIB_TABLE, flowRef,
539                                            DEFAULT_FIB_FLOW_PRIORITY, flowRef, 0, 0,
540                                            COOKIE_VM_LFIB_TABLE, matches, instructions);
541
542     if (addOrRemove == NwConstants.ADD_FLOW) {
543       /* We need to call sync API to install flow so that 2 DS operations on the same object do not
544       * happen at same time. However, MDSALManager's syncInstallFlow takes a delay time (or uses a default one) to wait
545       * for or for notification that operational DS write for flows is done. We do not turn on the stats writing for flows,
546       * so that notification never comes, so we do not need that wait. Sending the lowest value of wait "1 ms" since 0 wait means
547       * wait indefinitely. */
548
549       // FIXME:
550       // mdsalManager.syncInstallFlow(flowEntity, 1);
551       mdsalManager.installFlow(flowEntity);
552     } else {
553       // FIXME:
554       // mdsalManager.syncRemoveFlow(flowEntity, 1);
555       mdsalManager.removeFlow(flowEntity);
556     }
557     LOG.debug("LFIB Entry for dpID {} : label : {} group {} modified successfully {}",dpId, label, groupId );
558   }
559
560   private void deleteLocalAdjacency(final BigInteger dpId, final long vpnId, final String ipAddress) {
561     LOG.trace("deleteLocalAdjacency called with dpid {}, vpnId{}, ipAddress {}",dpId, vpnId, ipAddress);
562     try {
563       nextHopManager.removeLocalNextHop(dpId, vpnId, ipAddress);
564     } catch (NullPointerException e) {
565       LOG.trace("", e);
566     }
567   }
568
569   public void populateFibOnNewDpn(BigInteger dpnId, long vpnId, String rd) {
570     LOG.trace("New dpn {} for vpn {} : populateFibOnNewDpn", dpnId, rd);
571     InstanceIdentifier<VrfTables> id = buildVrfId(rd);
572     Optional<VrfTables> vrfTable = read(LogicalDatastoreType.OPERATIONAL, id);
573     if(vrfTable.isPresent()) {
574       for(VrfEntry vrfEntry : vrfTable.get().getVrfEntry()) {
575         // Passing null as we don't know the dpn
576         // to which prefix is attached at this point
577         createRemoteFibEntry(null, dpnId, vpnId, vrfTable.get().getKey(), vrfEntry);
578       }
579     }
580   }
581
582   public void cleanUpDpnForVpn(BigInteger dpnId, long vpnId, String rd) {
583     LOG.trace("Remove dpn {} for vpn {} : cleanUpDpnForVpn", dpnId, rd);
584     InstanceIdentifier<VrfTables> id = buildVrfId(rd);
585     Optional<VrfTables> vrfTable = read(LogicalDatastoreType.OPERATIONAL, id);
586     if(vrfTable.isPresent()) {
587       for(VrfEntry vrfEntry : vrfTable.get().getVrfEntry()) {
588         // Passing null as we don't know the dpn
589         // to which prefix is attached at this point
590         deleteRemoteRoute(null, dpnId, vpnId, vrfTable.get().getKey(), vrfEntry);
591       }
592     }
593   }
594
595   public static InstanceIdentifier<VrfTables> buildVrfId(String rd) {
596     InstanceIdentifierBuilder<VrfTables> idBuilder =
597         InstanceIdentifier.builder(FibEntries.class).child(VrfTables.class, new VrfTablesKey(rd));
598     InstanceIdentifier<VrfTables> id = idBuilder.build();
599     return id;
600   }
601
602   private String getFlowRef(BigInteger dpnId, short tableId, long label, String nextHop) {
603     return new StringBuilder(64).append(FLOWID_PREFIX).append(dpnId).append(NwConstants.FLOWID_SEPARATOR)
604         .append(tableId).append(NwConstants.FLOWID_SEPARATOR)
605         .append(label).append(NwConstants.FLOWID_SEPARATOR)
606         .append(nextHop).toString();
607   }
608
609   private String getFlowRef(BigInteger dpnId, short tableId, String rd, InetAddress destPrefix) {
610     return new StringBuilder(64).append(FLOWID_PREFIX).append(dpnId).append(NwConstants.FLOWID_SEPARATOR)
611         .append(tableId).append(NwConstants.FLOWID_SEPARATOR)
612         .append(rd).append(NwConstants.FLOWID_SEPARATOR)
613         .append(destPrefix.getHostAddress()).toString();
614   }
615
616   protected String resolveAdjacency(final BigInteger localDpnId, final BigInteger remoteDpnId,
617                                               final long vpnId, final VrfEntry vrfEntry) {
618     String adjacency = null;
619     LOG.trace("resolveAdjacency called with localdpid{} remotedpid {}, vpnId{}, VrfEntry {}", localDpnId, remoteDpnId, vpnId, vrfEntry);;
620     try {
621       adjacency =
622           nextHopManager.getRemoteNextHopPointer(localDpnId, remoteDpnId, vpnId,
623                                                  vrfEntry.getDestPrefix(),
624                                                  vrfEntry.getNextHopAddress());
625     } catch (NullPointerException e) {
626       LOG.trace("", e);
627     }
628     return adjacency;
629   }
630
631   protected VpnInstanceOpDataEntry getVpnInstance(String rd) {
632     InstanceIdentifier<VpnInstanceOpDataEntry> id = InstanceIdentifier.create(VpnInstanceOpData.class).child(
633         VpnInstanceOpDataEntry.class, new VpnInstanceOpDataEntryKey(rd));
634     Optional<VpnInstanceOpDataEntry> vpnInstanceOpData = read(LogicalDatastoreType.OPERATIONAL, id);
635     if(vpnInstanceOpData.isPresent()) {
636       return vpnInstanceOpData.get();
637     }
638     return null;
639   }
640
641     public void processNodeAdd(BigInteger dpnId) {
642         LOG.debug("Received notification to install TableMiss entries for dpn {} ", dpnId);
643         makeTableMissFlow(dpnId, NwConstants.ADD_FLOW);
644         makeProtocolTableFlow(dpnId, NwConstants.ADD_FLOW);
645         makeL3IntfTblMissFlow(dpnId, NwConstants.ADD_FLOW);
646     }
647
648     private void makeTableMissFlow(BigInteger dpnId, int addOrRemove) {
649         final BigInteger COOKIE_TABLE_MISS = new BigInteger("1030000", 16);
650         // Instruction to goto L3 InterfaceTable
651         List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
652         instructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.L3_INTERFACE_TABLE }));
653         List<MatchInfo> matches = new ArrayList<MatchInfo>();
654         FlowEntity flowEntityLfib = MDSALUtil.buildFlowEntity(dpnId, NwConstants.L3_LFIB_TABLE,
655                 getFlowRef(dpnId, NwConstants.L3_LFIB_TABLE, NwConstants.TABLE_MISS_FLOW),
656                 NwConstants.TABLE_MISS_PRIORITY, "Table Miss", 0, 0, COOKIE_TABLE_MISS, matches, instructions);
657
658         FlowEntity flowEntityFib = MDSALUtil.buildFlowEntity(dpnId,NwConstants.L3_FIB_TABLE, getFlowRef(dpnId, NwConstants.L3_FIB_TABLE, NwConstants.TABLE_MISS_FLOW),
659                 NwConstants.TABLE_MISS_PRIORITY, "FIB Table Miss Flow", 0, 0, COOKIE_VM_FIB_TABLE,
660                 matches, instructions);
661
662         if (addOrRemove == NwConstants.ADD_FLOW) {
663             LOG.debug("Invoking MDSAL to install Table Miss Entries");
664             mdsalManager.installFlow(flowEntityLfib);
665             mdsalManager.installFlow(flowEntityFib);
666         } else {
667             mdsalManager.removeFlow(flowEntityLfib);
668             mdsalManager.removeFlow(flowEntityFib);
669
670         }
671     }
672
673     private String getFlowRef(BigInteger dpnId, short tableId, int tableMiss) {
674         return new StringBuffer().append(FLOWID_PREFIX).append(dpnId).append(NwConstants.FLOWID_SEPARATOR)
675                 .append(tableId).append(NwConstants.FLOWID_SEPARATOR).append(tableMiss)
676                 .append(FLOWID_PREFIX).toString();
677     }
678
679   /*
680    * Install flow entry in protocol table to forward mpls
681    * coming through gre tunnel to LFIB table.
682    */
683   private void makeProtocolTableFlow(BigInteger dpnId, int addOrRemove) {
684     final BigInteger COOKIE_PROTOCOL_TABLE = new BigInteger("1070000", 16);
685     // Instruction to goto L3 InterfaceTable
686     List<InstructionInfo> instructions = new ArrayList<>();
687     instructions.add(new InstructionInfo(InstructionType.goto_table, new long[] {NwConstants.L3_LFIB_TABLE}));
688     List<MatchInfo> matches = new ArrayList<MatchInfo>();
689     matches.add(new MatchInfo(MatchFieldType.eth_type,
690                               new long[] { 0x8847L }));
691     FlowEntity flowEntityToLfib = MDSALUtil.buildFlowEntity(dpnId, NwConstants.L3_PROTOCOL_TABLE,
692                                                           getFlowRef(dpnId, NwConstants.L3_PROTOCOL_TABLE,
693                                                                   NwConstants.L3_LFIB_TABLE),
694                                                           DEFAULT_FIB_FLOW_PRIORITY,
695                                                           "Protocol Table For LFIB",
696                                                           0, 0,
697                                                           COOKIE_PROTOCOL_TABLE,
698                                                           matches, instructions);
699
700     if (addOrRemove == NwConstants.ADD_FLOW) {
701       LOG.debug("Invoking MDSAL to install Protocol Entries for dpn {}", dpnId);
702       mdsalManager.installFlow(flowEntityToLfib);
703     } else {
704       mdsalManager.removeFlow(flowEntityToLfib);
705     }
706   }
707
708   public List<String> printFibEntries() {
709     List<String> result = new ArrayList<String>();
710     result.add(String.format("   %-7s  %-20s  %-20s  %-7s", "RD", "Prefix", "Nexthop", "Label"));
711     result.add("-------------------------------------------------------------------");
712     InstanceIdentifier<FibEntries> id = InstanceIdentifier.create(FibEntries.class);
713     Optional<FibEntries> fibEntries = read(LogicalDatastoreType.OPERATIONAL, id);
714     if (fibEntries.isPresent()) {
715       List<VrfTables> vrfTables = fibEntries.get().getVrfTables();
716       for (VrfTables vrfTable : vrfTables) {
717         for (VrfEntry vrfEntry : vrfTable.getVrfEntry()) {
718           result.add(String.format("   %-7s  %-20s  %-20s  %-7s", vrfTable.getRouteDistinguisher(),
719                   vrfEntry.getDestPrefix(), vrfEntry.getNextHopAddress(), vrfEntry.getLabel()));
720         }
721       }
722     }
723     return result;
724   }
725
726   private void makeL3IntfTblMissFlow(BigInteger dpnId, int addOrRemove) {
727     List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
728     List<MatchInfo> matches = new ArrayList<MatchInfo>();
729     final BigInteger COOKIE_TABLE_MISS = new BigInteger("1030000", 16);
730     // Instruction to clear metadata except SI and LportTag bits
731     instructions.add(new InstructionInfo(InstructionType.write_metadata, new BigInteger[] {
732                     CLEAR_METADATA, METADATA_MASK_CLEAR }));
733     // Instruction to clear action
734     instructions.add(new InstructionInfo(InstructionType.clear_actions));
735     // Instruction to goto L3 InterfaceTable
736
737     instructions.add(new InstructionInfo(InstructionType.goto_table, new long[] { NwConstants.LPORT_DISPATCHER_TABLE }));
738
739     FlowEntity flowEntityL3Intf = MDSALUtil.buildFlowEntity(dpnId, NwConstants.L3_INTERFACE_TABLE,
740             getFlowRef(dpnId, NwConstants.L3_INTERFACE_TABLE, NwConstants.TABLE_MISS_FLOW),
741             NwConstants.TABLE_MISS_PRIORITY, "L3 Interface Table Miss", 0, 0, COOKIE_TABLE_MISS, matches, instructions);
742     if (addOrRemove == NwConstants.ADD_FLOW) {
743       LOG.info("Invoking MDSAL to install L3 interface Table Miss Entries");
744       mdsalManager.installFlow(flowEntityL3Intf);
745     } else {
746       mdsalManager.removeFlow(flowEntityL3Intf);
747     }
748   }
749
750 }