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