Bug 5054: Fix for switch restart
[vpnservice.git] / vpnmanager / vpnmanager-impl / src / main / java / org / opendaylight / vpnservice / VpnInterfaceManager.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;
9
10 import org.opendaylight.vpnservice.utilities.InterfaceUtils;
11
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.JdkFutureAdapters;
15
16 import org.opendaylight.controller.md.sal.binding.api.*;
17 import org.opendaylight.vpnservice.mdsalutil.*;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.OpState;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.OpStateBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.AdjacencyKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.VpnInstanceOpDataEntry;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnToDpnList;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.VpnToDpnListBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfacesBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfacesKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.arputil.rev151126.OdlArputilService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.arputil.rev151126.SendArpResponseInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.arputil.rev151126.SendArpResponseInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.servicebinding.rev151015.service.bindings.services.info.BoundServices;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.FibEntries;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.fibentries.VrfTables;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.fibentries.VrfTablesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.fibentries.VrfTablesKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.vrfentries.VrfEntry;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.vrfentries.VrfEntryBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.fibmanager.rev150330.vrfentries.VrfEntryKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.idmanager.rev150403.IdManagerService;
41
42 import java.math.BigInteger;
43 import java.util.Collection;
44 import java.util.Iterator;
45 import java.util.List;
46 import java.util.ArrayList;
47 import java.util.concurrent.*;
48
49 import com.google.common.base.Optional;
50
51 import org.opendaylight.bgpmanager.api.IBgpManager;
52 import org.opendaylight.fibmanager.api.IFibManager;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.interfacemgr.rpcs.rev151003.OdlInterfaceRpcService;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.itm.rpcs.rev151217.ItmRpcService;
55 import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
56 import org.opendaylight.yangtools.concepts.ListenerRegistration;
57 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
59 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
60 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
61 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.InterfacesState;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.Adjacency;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.AdjacencyBuilder;
64 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.Adjacencies;
66 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnAfConfig;
67 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInstances;
68 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstance;
69 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstanceKey;
70 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
71 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceKey;
72 import org.opendaylight.yangtools.yang.common.RpcError;
73 import org.opendaylight.yangtools.yang.common.RpcResult;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
76
77 public class VpnInterfaceManager extends AbstractDataChangeListener<VpnInterface> implements AutoCloseable {
78     private static final Logger LOG = LoggerFactory.getLogger(VpnInterfaceManager.class);
79     private ListenerRegistration<DataChangeListener> listenerRegistration, opListenerRegistration;
80     private ConcurrentMap<String, Runnable> vpnIntfMap = new ConcurrentHashMap<String, Runnable>();
81     private ExecutorService executorService = Executors.newSingleThreadExecutor();
82     private final DataBroker broker;
83     private final IBgpManager bgpManager;
84     private IFibManager fibManager;
85     private IMdsalApiManager mdsalManager;
86     private OdlInterfaceRpcService interfaceManager;
87     private ItmRpcService itmProvider;
88     private IdManagerService idManager;
89     private OdlArputilService arpManager;
90     private InterfaceStateChangeListener interfaceListener;
91     private VpnInterfaceOpListener vpnInterfaceOpListener;
92     private ArpNotificationHandler arpNotificationHandler;
93     protected enum UpdateRouteAction {
94         ADVERTISE_ROUTE, WITHDRAW_ROUTE
95     }
96     /**
97      * Responsible for listening to data change related to VPN Interface
98      * Bind VPN Service on the interface and informs the BGP service
99      *
100      * @param db - dataBroker service reference
101      */
102     public VpnInterfaceManager(final DataBroker db, final IBgpManager bgpManager, NotificationService notificationService) {
103         super(VpnInterface.class);
104         broker = db;
105         this.bgpManager = bgpManager;
106         interfaceListener = new InterfaceStateChangeListener(db, this);
107         vpnInterfaceOpListener = new VpnInterfaceOpListener();
108         arpNotificationHandler = new ArpNotificationHandler(this, broker);
109         notificationService.registerNotificationListener(arpNotificationHandler);
110         registerListener(db);
111     }
112
113     public void setMdsalManager(IMdsalApiManager mdsalManager) {
114         this.mdsalManager = mdsalManager;
115     }
116
117     public void setInterfaceManager(OdlInterfaceRpcService interfaceManager) {
118         this.interfaceManager = interfaceManager;
119         interfaceListener.setInterfaceManager(interfaceManager);
120     }
121
122     public void setITMProvider(ItmRpcService itmProvider) {
123         this.itmProvider = itmProvider;
124     }
125
126     public void setFibManager(IFibManager fibManager) {
127         this.fibManager = fibManager;
128     }
129
130     public void setIdManager(IdManagerService idManager) {
131         this.idManager = idManager;
132     }
133
134     public void setArpManager(OdlArputilService arpManager) {
135         this.arpManager = arpManager;
136     }
137
138     @Override
139     public void close() throws Exception {
140         if (listenerRegistration != null) {
141             try {
142                 listenerRegistration.close();
143                 opListenerRegistration.close();
144             } catch (final Exception e) {
145                 LOG.error("Error when cleaning up DataChangeListener.", e);
146             }
147             listenerRegistration = null;
148             opListenerRegistration = null;
149         }
150         LOG.info("VPN Interface Manager Closed");
151     }
152
153     private void registerListener(final DataBroker db) {
154         try {
155             listenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.CONFIGURATION,
156                     getWildCardPath(), VpnInterfaceManager.this, DataChangeScope.SUBTREE);
157             opListenerRegistration = db.registerDataChangeListener(LogicalDatastoreType.OPERATIONAL,
158                                                                    getWildCardPath(), vpnInterfaceOpListener, DataChangeScope.SUBTREE);
159         } catch (final Exception e) {
160             LOG.error("VPN Service DataChange listener registration fail!", e);
161             throw new IllegalStateException("VPN Service registration Listener failed.", e);
162         }
163     }
164
165     private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface> getInterfaceListenerPath() {
166         return InstanceIdentifier.create(InterfacesState.class)
167             .child(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface.class);
168     }
169
170     @Override
171     protected void add(final InstanceIdentifier<VpnInterface> identifier,
172             final VpnInterface vpnInterface) {
173         LOG.trace("VPN Interface key: {} , value: {}", identifier, vpnInterface );
174         addInterface(identifier, vpnInterface);
175     }
176
177     private void addInterface(final InstanceIdentifier<VpnInterface> identifier,
178                               final VpnInterface vpnInterface) {
179         LOG.trace("VPN Interface add event - key: {}, value: {}" ,identifier, vpnInterface );
180         final VpnInterfaceKey key = identifier.firstKeyOf(VpnInterface.class, VpnInterfaceKey.class);
181         String interfaceName = key.getName();
182
183         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState =
184             InterfaceUtils.getInterfaceStateFromOperDS(broker, interfaceName);
185         if (interfaceState != null) {
186             // Interface state is up
187             processVpnInterfaceUp(InterfaceUtils.getDpIdFromInterface(interfaceState), interfaceName, interfaceState.getIfIndex());
188         } else {
189             LOG.trace("VPN interfaces are not yet operational.");
190         }
191     }
192
193     protected void processVpnInterfaceUp(BigInteger dpId, String interfaceName, int lPortTag) {
194
195         VpnInterface vpnInterface = VpnUtil.getConfiguredVpnInterface(broker, interfaceName);
196         if(vpnInterface == null) {
197             LOG.info("Unable to process add/up for interface {} as it is not configured", interfaceName);
198             return;
199         }
200         String vpnName = vpnInterface.getVpnInstanceName();
201         LOG.info("Binding vpn service to interface {} ", interfaceName);
202         long vpnId = VpnUtil.getVpnId(broker, vpnName);
203         if (vpnId == VpnConstants.INVALID_ID) {
204             LOG.trace("VpnInstance to VPNId mapping is not yet available, bailing out now.");
205             return;
206         }
207         synchronized (interfaceName.intern()) {
208             if (VpnUtil.getOperationalVpnInterface(broker, vpnInterface.getName()) != null) {
209                 LOG.trace("VPN Interface already provisioned , bailing out from here.");
210                 return;
211             }
212
213             bindService(dpId, vpnName, interfaceName, lPortTag);
214             updateDpnDbs(vpnName, interfaceName, true);
215             processVpnInterfaceAdjacencies(VpnUtil.getVpnInterfaceIdentifier(vpnInterface.getName()), vpnInterface, true);
216         }
217
218     }
219
220     private void updateDpnDbs(String vpnName, String interfaceName, boolean add) {
221         long vpnId = VpnUtil.getVpnId(broker, vpnName);
222         BigInteger dpId = InterfaceUtils.getDpnForInterface(interfaceManager, interfaceName);
223         if(!dpId.equals(BigInteger.ZERO)) {
224             if(add)
225                 updateMappingDbs(vpnId, dpId, interfaceName, vpnName);
226             else
227                 removeFromMappingDbs(vpnId, dpId, interfaceName, vpnName);
228         }
229
230     }
231
232     private void bindService(BigInteger dpId, String vpnInstanceName, String vpnInterfaceName, int lPortTag) {
233         int priority = VpnConstants.DEFAULT_FLOW_PRIORITY;
234         long vpnId = VpnUtil.getVpnId(broker, vpnInstanceName);
235
236         int instructionKey = 0;
237         List<Instruction> instructions = new ArrayList<Instruction>();
238
239         instructions.add(MDSALUtil.buildAndGetWriteMetadaInstruction(BigInteger.valueOf(vpnId), MetaDataUtil.METADATA_MASK_VRFID, ++instructionKey));
240         instructions.add(MDSALUtil.buildAndGetGotoTableInstruction(NwConstants.L3_FIB_TABLE, ++instructionKey));
241
242         BoundServices
243             serviceInfo =
244             InterfaceUtils.getBoundServices(String.format("%s.%s.%s", "vpn",vpnInstanceName, vpnInterfaceName),
245                                             VpnConstants.L3VPN_SERVICE_IDENTIFIER, priority,
246                                             VpnConstants.COOKIE_VM_INGRESS_TABLE, instructions);
247         VpnUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
248                           InterfaceUtils.buildServiceId(vpnInterfaceName, VpnConstants.L3VPN_SERVICE_IDENTIFIER), serviceInfo);
249         makeArpFlow(dpId, VpnConstants.L3VPN_SERVICE_IDENTIFIER, lPortTag, vpnInterfaceName,
250                     vpnId, ArpReplyOrRequest.REQUEST, NwConstants.ADD_FLOW);
251
252     }
253
254     private void processVpnInterfaceAdjacencies(final InstanceIdentifier<VpnInterface> identifier, VpnInterface intf,
255                                                 boolean vpnInterfaceState) {
256         String intfName = intf.getName();
257
258         synchronized (intfName) {
259             // Read NextHops
260             InstanceIdentifier<Adjacencies> path = identifier.augmentation(Adjacencies.class);
261             Optional<Adjacencies> adjacencies = VpnUtil.read(broker, LogicalDatastoreType.CONFIGURATION, path);
262
263             if (adjacencies.isPresent()) {
264                 List<Adjacency> nextHops = adjacencies.get().getAdjacency();
265                 List<Adjacency> value = new ArrayList<>();
266
267                 // Get the rd of the vpn instance
268                 String rd = getRouteDistinguisher(intf.getVpnInstanceName());
269
270                 BigInteger dpnId = InterfaceUtils.getDpnForInterface(interfaceManager, intfName);
271                 String nextHopIp = InterfaceUtils.getEndpointIpAddressForDPN(broker, dpnId);
272                 if (nextHopIp == null){
273                     LOG.error("NextHop for interface {} is null", intfName);
274                 }
275
276                 LOG.trace("NextHops are {}", nextHops);
277                 for (Adjacency nextHop : nextHops) {
278                     String prefix = VpnUtil.getIpPrefix(nextHop.getIpAddress());
279                     long label = VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME, VpnUtil
280                             .getNextHopLabelKey((rd == null) ? intf.getVpnInstanceName() : rd, prefix));
281                     String adjNextHop = nextHop.getNextHopIp();
282                     value.add(new AdjacencyBuilder(nextHop).setLabel(label).setNextHopIp((adjNextHop != null && !adjNextHop.isEmpty()) ? adjNextHop : nextHopIp)
283                             .setIpAddress(prefix).setKey(new AdjacencyKey(prefix)).build());
284                     if(nextHop.getMacAddress() != null && !nextHop.getMacAddress().isEmpty()) {
285                         VpnUtil.syncUpdate(
286                                 broker,
287                                 LogicalDatastoreType.OPERATIONAL,
288                                 VpnUtil.getPrefixToInterfaceIdentifier(
289                                         VpnUtil.getVpnId(broker, intf.getVpnInstanceName()), prefix),
290                                 VpnUtil.getPrefixToInterface(dpnId, intf.getName(), prefix));
291                     }
292                 }
293
294                 Adjacencies aug = VpnUtil.getVpnInterfaceAugmentation(value);
295                 VpnInterface opInterface = VpnUtil.getVpnInterface(intfName, intf.getVpnInstanceName(),
296                                                                    aug, vpnInterfaceState);
297                 InstanceIdentifier<VpnInterface> interfaceId = VpnUtil.getVpnInterfaceIdentifier(intfName);
298                 VpnUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, interfaceId, opInterface);
299                 for (Adjacency nextHop : aug.getAdjacency()) {
300                     long label = nextHop.getLabel();
301                     String adjNextHop = nextHop.getNextHopIp();
302                     if (rd != null) {
303                         addPrefixToBGP(rd, nextHop.getIpAddress(),
304                                             (adjNextHop != null && !adjNextHop.isEmpty()) ? adjNextHop : nextHopIp, label);
305                     } else {
306                         // ### add FIB route directly
307                         addFibEntryToDS(intf.getVpnInstanceName(), nextHop.getIpAddress(),
308                                             (adjNextHop != null && !adjNextHop.isEmpty()) ? adjNextHop : nextHopIp, (int) label);
309                     }
310                 }
311             }
312         }
313     }
314
315     private void makeArpFlow(BigInteger dpId,short sIndex, int lPortTag, String vpnInterfaceName,
316                              long vpnId, ArpReplyOrRequest replyOrRequest, int addOrRemoveFlow){
317         List<MatchInfo> matches = new ArrayList<MatchInfo>();
318         BigInteger metadata = MetaDataUtil.getMetaDataForLPortDispatcher(lPortTag, ++sIndex, BigInteger.valueOf(vpnId));
319         BigInteger metadataMask = MetaDataUtil.getMetaDataMaskForLPortDispatcher(MetaDataUtil.METADATA_MASK_SERVICE_INDEX,
320                 MetaDataUtil.METADATA_MASK_LPORT_TAG, MetaDataUtil.METADATA_MASK_VRFID);
321
322         // Matching Arp reply flows
323         matches.add(new MatchInfo(MatchFieldType.eth_type, new long[] { NwConstants.ETHTYPE_ARP }));
324         matches.add(new MatchInfo(MatchFieldType.metadata, new BigInteger[] {
325                 metadata, metadataMask }));
326
327         matches.add(new MatchInfo(MatchFieldType.arp_op, new long[] { replyOrRequest.getArpOperation() }));
328
329         // Instruction to punt to controller
330         List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
331         List<ActionInfo> actionsInfos = new ArrayList<ActionInfo>();
332         actionsInfos.add(new ActionInfo(ActionType.punt_to_controller, new String[] {}));
333         instructions.add(new InstructionInfo(InstructionType.write_actions, actionsInfos));
334
335         // Install the flow entry in L3_INTERFACE_TABLE
336         String flowRef = VpnUtil.getFlowRef(dpId, NwConstants.L3_INTERFACE_TABLE,
337                     NwConstants.ETHTYPE_ARP, lPortTag, replyOrRequest.getArpOperation());
338         FlowEntity flowEntity;
339         flowEntity = MDSALUtil.buildFlowEntity(dpId, NwConstants.L3_INTERFACE_TABLE, flowRef,
340                 NwConstants.DEFAULT_ARP_FLOW_PRIORITY, replyOrRequest.getName(), 0, 0,
341                 VpnUtil.getCookieArpFlow(lPortTag), matches, instructions);
342
343         if (addOrRemoveFlow == NwConstants.ADD_FLOW) {
344             LOG.debug("Creating ARP Flow for interface {}",vpnInterfaceName);
345             mdsalManager.installFlow(flowEntity);
346         } else {
347             LOG.debug("Deleting ARP Flow for interface {}",vpnInterfaceName);
348             mdsalManager.removeFlow(flowEntity);
349         }
350     }
351
352     private String getRouteDistinguisher(String vpnName) {
353         InstanceIdentifier<VpnInstance> id = InstanceIdentifier.builder(VpnInstances.class)
354                                       .child(VpnInstance.class, new VpnInstanceKey(vpnName)).build();
355         Optional<VpnInstance> vpnInstance = VpnUtil.read(broker, LogicalDatastoreType.CONFIGURATION, id);
356         String rd = "";
357         if(vpnInstance.isPresent()) {
358             VpnInstance instance = vpnInstance.get();
359             VpnAfConfig config = instance.getIpv4Family();
360             rd = config.getRouteDistinguisher();
361         }
362         return rd;
363     }
364
365     private synchronized void updateMappingDbs(long vpnId, BigInteger dpnId, String intfName, String vpnName) {
366         String routeDistinguisher = getRouteDistinguisher(vpnName);
367         String rd = (routeDistinguisher == null) ? vpnName : routeDistinguisher;
368         InstanceIdentifier<VpnToDpnList> id = VpnUtil.getVpnToDpnListIdentifier(rd, dpnId);
369         Optional<VpnToDpnList> dpnInVpn = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, id);
370         org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces
371             vpnInterface = new VpnInterfacesBuilder().setInterfaceName(intfName).build();
372
373         if (dpnInVpn.isPresent()) {
374             VpnUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, id.child(
375                 org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance
376                     .op.data.vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces.class,
377                     new VpnInterfacesKey(intfName)), vpnInterface);
378         } else {
379             VpnUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL,
380                                     VpnUtil.getVpnInstanceOpDataIdentifier(rd),
381                                     VpnUtil.getVpnInstanceOpDataBuilder(rd, vpnId));
382             VpnToDpnListBuilder vpnToDpnList = new VpnToDpnListBuilder().setDpnId(dpnId);
383             List<org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
384                 .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces> vpnInterfaces =  new ArrayList<>();
385             vpnInterfaces.add(vpnInterface);
386             VpnUtil.syncWrite(broker, LogicalDatastoreType.OPERATIONAL, id,
387                               vpnToDpnList.setVpnInterfaces(vpnInterfaces).build());
388
389             /**
390              * FIXME: DC Gateway tunnel should be built dynamically
391             //this is the first VM in this VPN on the DPN, may be a new DPN has come up,
392             //if tunnel to DC GW does not exist, create it
393             //if(!tunnelExists(dpnID, bgpManager.getDCGWIP()))
394             String dcGW = bgpManager.getDCGwIP();
395             if(dcGW != null && !dcGW.isEmpty())
396             {
397                 LOG.debug("Building tunnel from DPN {} to DC GW {}", dpnId, dcGW);
398                 itmProvider.buildTunnelFromDPNToDCGW(dpnId, new IpAddress(dcGW.toCharArray()));
399             }*/
400             fibManager.populateFibOnNewDpn(dpnId, vpnId, (rd == null) ? vpnName : rd);
401         }
402     }
403
404     private synchronized void removeFromMappingDbs(long vpnId, BigInteger dpnId, String intfName, String vpnName) {
405         //TODO: Delay 'DPN' removal so that other services can cleanup the entries for this dpn
406         String rd = VpnUtil.getVpnRd(broker, vpnName);
407         InstanceIdentifier<VpnToDpnList> id = VpnUtil.getVpnToDpnListIdentifier(rd, dpnId);
408         Optional<VpnToDpnList> dpnInVpn = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, id);
409         if (dpnInVpn.isPresent()) {
410             List<org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
411                 .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces> vpnInterfaces = dpnInVpn.get().getVpnInterfaces();
412             org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces
413                     currVpnInterface = new VpnInterfacesBuilder().setInterfaceName(intfName).build();
414
415             if (vpnInterfaces.remove(currVpnInterface)) {
416                 if (vpnInterfaces.isEmpty()) {
417                     VpnUtil.delete(broker, LogicalDatastoreType.OPERATIONAL, id, VpnUtil.DEFAULT_CALLBACK);
418                     fibManager.cleanUpDpnForVpn(dpnId, vpnId, rd);
419                 } else {
420                     VpnUtil.delete(broker, LogicalDatastoreType.OPERATIONAL, id.child(
421                         org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
422                             .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces.class,
423                             new VpnInterfacesKey(intfName)), VpnUtil.DEFAULT_CALLBACK);
424                 }
425             }
426         }
427     }
428
429     private void addPrefixToBGP(String rd, String prefix, String nextHopIp, long label) {
430         try {
431             bgpManager.addPrefix(rd, prefix, nextHopIp, (int)label);
432         } catch(Exception e) {
433             LOG.error("Add prefix failed", e);
434         }
435     }
436
437
438     private InstanceIdentifier<VpnInterface> getWildCardPath() {
439         return InstanceIdentifier.create(VpnInterfaces.class).child(VpnInterface.class);
440     }
441
442     @Override
443     protected void remove( InstanceIdentifier<VpnInterface> identifier, VpnInterface vpnInterface) {
444         LOG.trace("Remove event - key: {}, value: {}" ,identifier, vpnInterface );
445         final VpnInterfaceKey key = identifier.firstKeyOf(VpnInterface.class, VpnInterfaceKey.class);
446         String interfaceName = key.getName();
447
448         InstanceIdentifier<VpnInterface> interfaceId = VpnUtil.getVpnInterfaceIdentifier(interfaceName);
449         Optional<VpnInterface> existingVpnInterface = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, interfaceId);
450         org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface interfaceState =
451             InterfaceUtils.getInterfaceStateFromOperDS(broker, interfaceName);
452
453         if (existingVpnInterface.isPresent() && interfaceState != null) {
454             processVpnInterfaceDown(InterfaceUtils.getDpIdFromInterface(interfaceState), interfaceName, interfaceState.getIfIndex(), false);
455         } else {
456             LOG.warn("VPN interface {} was unavailable in operational data store to handle remove event", interfaceName);
457         }
458     }
459
460     protected void processVpnInterfaceDown(BigInteger dpId, String interfaceName, int lPortTag, boolean isInterfaceStateDown) {
461         VpnInterface vpnInterface = VpnUtil.getOperationalVpnInterface(broker, interfaceName);
462         if(vpnInterface == null) {
463             LOG.info("Unable to process delete/down for interface {} as it is not available in operational data store", interfaceName);
464             return;
465         }
466         String vpnName = vpnInterface.getVpnInstanceName();
467         InstanceIdentifier<VpnInterface> identifier = VpnUtil.getVpnInterfaceIdentifier(interfaceName);
468
469         synchronized (interfaceName.intern()) {
470             removeAdjacenciesFromVpn(identifier, vpnInterface);
471             LOG.info("Unbinding vpn service from interface {} ", interfaceName);
472             unbindService(dpId, vpnName, interfaceName, lPortTag, isInterfaceStateDown);
473
474             //wait till DCN for removal of vpn interface in operational DS arrives
475             Runnable notifyTask = new VpnNotifyTask();
476             synchronized (interfaceName.intern()) {
477                 vpnIntfMap.put(interfaceName, notifyTask);
478                 synchronized (notifyTask) {
479                     try {
480                         notifyTask.wait(VpnConstants.WAIT_TIME_IN_MILLISECONDS);
481                     } catch (InterruptedException e) {
482                     }
483                 }
484             }
485
486         }
487     }
488
489     private void removeAdjacenciesFromVpn(final InstanceIdentifier<VpnInterface> identifier, VpnInterface intf) {
490         //Read NextHops
491         InstanceIdentifier<Adjacencies> path = identifier.augmentation(Adjacencies.class);
492         Optional<Adjacencies> adjacencies = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, path);
493
494         String rd = VpnUtil.getVpnRd(broker, intf.getVpnInstanceName());
495         if (adjacencies.isPresent()) {
496             VpnUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, identifier.augmentation(OpState.class),
497                                new OpStateBuilder().setStateUp(false).build());
498             List<Adjacency> nextHops = adjacencies.get().getAdjacency();
499
500             if (!nextHops.isEmpty()) {
501                 LOG.trace("NextHops are " + nextHops);
502                 for (Adjacency nextHop : nextHops) {
503                     VpnUtil.releaseId(idManager, VpnConstants.VPN_IDPOOL_NAME,
504                                       VpnUtil.getNextHopLabelKey(rd, nextHop.getIpAddress()));
505                     /*VpnUtil.delete(broker, LogicalDatastoreType.OPERATIONAL,
506                                    VpnUtil.getPrefixToInterfaceIdentifier(
507                                        VpnUtil.getVpnId(broker, intf.getVpnInstanceName()),
508                                        nextHop.getIpAddress()),
509                                    VpnUtil.DEFAULT_CALLBACK);*/
510                     if (rd.equals(intf.getVpnInstanceName())) {
511                         //this is an internal vpn - the rd is assigned to the vpn instance name;
512                         //remove from FIB directly
513                         removeFibEntryFromDS(intf.getVpnInstanceName(), nextHop.getIpAddress());
514                     } else {
515                         removePrefixFromBGP(rd, nextHop.getIpAddress());
516                     }
517                 }
518             }
519         }
520     }
521
522
523     private void unbindService(BigInteger dpId, String vpnInstanceName, String vpnInterfaceName,
524                                int lPortTag, boolean isInterfaceStateDown) {
525         if (!isInterfaceStateDown) {
526             VpnUtil.delete(broker, LogicalDatastoreType.CONFIGURATION,
527                            InterfaceUtils.buildServiceId(vpnInterfaceName,
528                                                          VpnConstants.L3VPN_SERVICE_IDENTIFIER),
529                            VpnUtil.DEFAULT_CALLBACK);
530         }
531         long vpnId = VpnUtil.getVpnId(broker, vpnInstanceName);
532         makeArpFlow(dpId, VpnConstants.L3VPN_SERVICE_IDENTIFIER, lPortTag, vpnInterfaceName,
533                     vpnId, ArpReplyOrRequest.REQUEST, NwConstants.DEL_FLOW);
534     }
535
536
537     private void removePrefixFromBGP(String rd, String prefix) {
538         try {
539             bgpManager.deletePrefix(rd, prefix);
540         } catch(Exception e) {
541             LOG.error("Delete prefix failed", e);
542         }
543     }
544
545     @Override
546     protected void update(InstanceIdentifier<VpnInterface> identifier,
547                                    VpnInterface original, VpnInterface update) {
548         LOG.trace("Update VPN Interface {} , original {}, update {}",
549                                                   identifier, original, update);
550         String vpnName = original.getVpnInstanceName();
551
552         boolean vpnNameChanged = false;
553         String rd = getRouteDistinguisher(vpnName);
554         String newRd = rd;
555         String newVpnName = update.getVpnInstanceName();
556         if(!vpnName.equals(newVpnName)) {
557             //VPN for this interface got changed.
558             //Remove the interface from old VPN and add it to new VPN
559             newRd = getRouteDistinguisher(newVpnName);
560             if(newRd.equals("")) {
561                 LOG.warn("VPN Instance {} not found. Update operation aborted", newVpnName);
562                 return;
563             }
564             vpnNameChanged = true;
565             LOG.debug("New VPN Name for the interface {} is {}", newVpnName, original.getName());
566         }
567
568         List<Adjacency> oldAdjs = original.getAugmentation(Adjacencies.class).getAdjacency();
569         List<Adjacency> newAdjs = update.getAugmentation(Adjacencies.class).getAdjacency();
570         if(vpnNameChanged && newAdjs != null && !newAdjs.isEmpty()) {
571             long label = VpnConstants.INVALID_ID;
572             InstanceIdentifier<Adjacencies> path = identifier.augmentation(Adjacencies.class);
573             Optional<Adjacencies> adjacencies = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, path);
574             if (adjacencies.isPresent()) {
575                 List<Adjacency> nextHops = adjacencies.get().getAdjacency();
576                 for(Adjacency nextHop : nextHops) {
577                     label = nextHop.getLabel();
578                     if(label == VpnConstants.INVALID_ID) {
579                         //Generate label using ID Manager
580                         label = VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME,
581                                                     VpnUtil.getNextHopLabelKey(newRd, nextHop.getIpAddress()));
582                     }
583                     if (rd != null) {
584                         removePrefixFromBGP(rd, nextHop.getIpAddress());
585                     } else {
586                         removeFibEntryFromDS(vpnName, nextHop.getIpAddress());
587                     }
588                     //updatePrefixToBGP(newRd, nextHop, nextHopIp, label);
589                 }
590                 processVpnInterfaceAdjacencies(identifier, update, true);
591                 VpnUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, identifier, update);
592             }
593         } else if (oldAdjs != newAdjs) {
594             //handle both addition and removal of adjacencies
595             //currently, new adjacency may be an extra route
596             for (Adjacency adj : newAdjs) {
597                 if (oldAdjs.contains(adj)) {
598                     oldAdjs.remove(adj);
599                 } else {
600                     // add new adjacency - right now only extra route will hit this path
601                     addNewAdjToVpnInterface(identifier, adj);
602                 }
603             }
604
605             for (Adjacency adj : oldAdjs) {
606                 delAdjFromVpnInterface(identifier, adj);
607             }
608         }
609         else {
610             LOG.debug("No Update information is available for VPN Interface to proceed");
611         }
612
613     }
614
615     public void processArpRequest(IpAddress srcIP, PhysAddress srcMac, IpAddress targetIP, String srcInterface){
616         SendArpResponseInput input = new SendArpResponseInputBuilder().setInterface(srcInterface)
617                                                                     .setIpaddress(srcIP).setSrcIpAddress(targetIP).setMacaddress(srcMac).build();
618         final String msgFormat = String.format("Send ARP Response on interface %s to destination %s", srcInterface, srcIP);
619         Future<RpcResult<Void>> future = arpManager.sendArpResponse(input);
620         Futures.addCallback(JdkFutureAdapters.listenInPoolThread(future), new FutureCallback<RpcResult<Void>>() {
621             @Override
622             public void onFailure(Throwable error) {
623                 LOG.error("Error - {}", msgFormat, error);
624             }
625
626             @Override
627             public void onSuccess(RpcResult<Void> result) {
628                 if(!result.isSuccessful()) {
629                     LOG.warn("Rpc call to {} failed", msgFormat, getErrorText(result.getErrors()));
630                 } else {
631                     LOG.debug("Successful RPC Result - {}", msgFormat);
632                 }
633             }
634         });
635     }
636
637     private String getErrorText(Collection<RpcError> errors) {
638         StringBuilder errorText = new StringBuilder();
639         for(RpcError error : errors) {
640             errorText.append(",").append(error.getErrorType()).append("-")
641                      .append(error.getMessage());
642         }
643         return errorText.toString();
644     }
645
646     private String getTunnelInterfaceFlowRef(BigInteger dpnId, short tableId, String ifName) {
647         return new StringBuilder().append(dpnId).append(tableId).append(ifName).toString();
648     }
649
650
651
652     public synchronized void addFibEntryToDS(String rd, String prefix,
653             String nexthop, int label) {
654
655         VrfEntry vrfEntry = new VrfEntryBuilder().setDestPrefix(prefix).
656                     setNextHopAddress(nexthop).setLabel((long)label).build();
657         LOG.debug("Created vrfEntry for {} nexthop {} label {}", prefix, nexthop, label);
658
659         List<VrfEntry> vrfEntryList = new ArrayList<VrfEntry>();
660         vrfEntryList.add(vrfEntry);
661
662         InstanceIdentifierBuilder<VrfTables> idBuilder =
663                     InstanceIdentifier.builder(FibEntries.class).child(VrfTables.class, new VrfTablesKey(rd));
664         InstanceIdentifier<VrfTables> vrfTableId = idBuilder.build();
665
666         VrfTables vrfTableNew = new VrfTablesBuilder().setRouteDistinguisher(rd).
667                     setVrfEntry(vrfEntryList).build();
668
669         VpnUtil.syncUpdate(broker, LogicalDatastoreType.CONFIGURATION, vrfTableId, vrfTableNew);
670     }
671
672     public synchronized void removeFibEntryFromDS(String rd, String prefix) {
673
674         LOG.debug("Removing fib entry with destination prefix {} from vrf table for rd {}", prefix, rd);
675
676         InstanceIdentifierBuilder<VrfEntry> idBuilder =
677             InstanceIdentifier.builder(FibEntries.class).child(VrfTables.class, new VrfTablesKey(rd)).child(VrfEntry.class, new VrfEntryKey(prefix));
678         InstanceIdentifier<VrfEntry> vrfEntryId = idBuilder.build();
679         VpnUtil.delete(broker, LogicalDatastoreType.CONFIGURATION, vrfEntryId, VpnUtil.DEFAULT_CALLBACK);
680
681     }
682
683     public synchronized void removeVrfFromDS(String rd) {
684         LOG.debug("Removing vrf table for  rd {}", rd);
685
686         InstanceIdentifierBuilder<VrfTables> idBuilder =
687                 InstanceIdentifier.builder(FibEntries.class).child(VrfTables.class, new VrfTablesKey(rd));
688         InstanceIdentifier<VrfTables> vrfTableId = idBuilder.build();
689
690         VpnUtil.delete(broker, LogicalDatastoreType.CONFIGURATION, vrfTableId, VpnUtil.DEFAULT_CALLBACK);
691
692     }
693
694     protected void addNewAdjToVpnInterface(InstanceIdentifier<VpnInterface> identifier, Adjacency adj) {
695
696         Optional<VpnInterface> optVpnInterface = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, identifier);
697
698         if (optVpnInterface.isPresent()) {
699             VpnInterface currVpnIntf = optVpnInterface.get();
700             String prefix = VpnUtil.getIpPrefix(adj.getIpAddress());
701             String rd = getRouteDistinguisher(currVpnIntf.getVpnInstanceName());
702             InstanceIdentifier<Adjacencies> adjPath = identifier.augmentation(Adjacencies.class);
703             Optional<Adjacencies> optAdjacencies = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, adjPath);
704             long label =
705                     VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME,
706                             VpnUtil.getNextHopLabelKey((rd != null) ? rd : currVpnIntf.getVpnInstanceName(), prefix));
707
708             List<Adjacency> adjacencies;
709             if (optAdjacencies.isPresent()) {
710                 adjacencies = optAdjacencies.get().getAdjacency();
711             } else {
712                 //This code will not be hit since VM adjacency will always be there
713                 adjacencies = new ArrayList<>();
714             }
715
716             adjacencies.add(new AdjacencyBuilder(adj).setLabel(label).setNextHopIp(adj.getNextHopIp())
717                     .setIpAddress(prefix).setKey(new AdjacencyKey(prefix)).build());
718
719             Adjacencies aug = VpnUtil.getVpnInterfaceAugmentation(adjacencies);
720             VpnInterface newVpnIntf = VpnUtil.getVpnInterface(currVpnIntf.getName(), currVpnIntf.getVpnInstanceName(),
721                                                               aug, currVpnIntf.getAugmentation(OpState.class).isStateUp());
722
723             VpnUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, identifier, newVpnIntf);
724             addExtraRoute(adj.getIpAddress(), adj.getNextHopIp(), rd, currVpnIntf.getVpnInstanceName(), (int) label, currVpnIntf.getName());
725
726         }
727
728     }
729
730     protected void delAdjFromVpnInterface(InstanceIdentifier<VpnInterface> identifier, Adjacency adj) {
731         Optional<VpnInterface> optVpnInterface = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, identifier);
732
733         if (optVpnInterface.isPresent()) {
734             VpnInterface currVpnIntf = optVpnInterface.get();
735
736             InstanceIdentifier<Adjacencies> path = identifier.augmentation(Adjacencies.class);
737             Optional<Adjacencies> optAdjacencies = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, path);
738             if (optAdjacencies.isPresent()) {
739                 List<Adjacency> adjacencies = optAdjacencies.get().getAdjacency();
740
741                 if (!adjacencies.isEmpty()) {
742                     String rd = getRouteDistinguisher(currVpnIntf.getVpnInstanceName());
743                     LOG.trace("Adjacencies are " + adjacencies);
744                     Iterator<Adjacency> adjIt = adjacencies.iterator();
745                     while (adjIt.hasNext()) {
746                         Adjacency adjElem = adjIt.next();
747                         if (adjElem.getIpAddress().equals(adj.getIpAddress())) {
748                             VpnUtil.releaseId(idManager, VpnConstants.VPN_IDPOOL_NAME,
749                                     VpnUtil.getNextHopLabelKey(rd, adj.getIpAddress()));
750                             adjIt.remove();
751
752                             Adjacencies aug = VpnUtil.getVpnInterfaceAugmentation(adjacencies);
753                             VpnInterface newVpnIntf = VpnUtil.getVpnInterface(currVpnIntf.getName(),
754                                                                               currVpnIntf.getVpnInstanceName(),
755                                                                               aug,
756                                                                               currVpnIntf.getAugmentation(OpState.class).isStateUp());
757
758                             VpnUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, identifier, newVpnIntf);
759
760                             delExtraRoute(adj.getIpAddress(), rd, currVpnIntf.getVpnInstanceName());
761                             break;
762                         }
763
764                     }
765                 }
766             }
767         }
768
769     }
770
771     protected void addExtraRoute(String destination, String nextHop, String rd, String routerID, int label, String intfName) {
772
773         //add extra route to vpn mapping; advertise with nexthop as tunnel ip
774         VpnUtil.syncUpdate(
775             broker,
776             LogicalDatastoreType.OPERATIONAL,
777             VpnUtil.getVpnToExtrarouteIdentifier(
778                 (rd != null) ? rd : routerID, destination),
779             VpnUtil.getVpnToExtraroute(destination, nextHop));
780
781         if(intfName != null && !intfName.isEmpty()) {
782             BigInteger dpnId = InterfaceUtils.getDpnForInterface(interfaceManager, intfName);
783             String nextHopIp = InterfaceUtils.getEndpointIpAddressForDPN(broker, dpnId);
784             if (nextHopIp == null && !nextHopIp.isEmpty()) {
785                 LOG.error("NextHop for interface {} is null. Failed adding extra route for prefix {}", intfName, destination);
786                 return;
787             }
788             nextHop = nextHopIp;
789         }
790         if (rd != null) {
791             addPrefixToBGP(rd, destination, nextHop, label);
792         } else {
793             // ### add FIB route directly
794             addFibEntryToDS(routerID, destination, nextHop, label);
795         }
796     }
797
798     protected void delExtraRoute(String destination, String rd, String routerID) {
799         if (rd != null) {
800             removePrefixFromBGP(rd, destination);
801         } else {
802             // ### add FIB route directly
803             removeFibEntryFromDS(routerID, destination);
804         }
805     }
806
807     class VpnInterfaceOpListener extends org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener<VpnInterface> {
808
809         public VpnInterfaceOpListener() {
810             super(VpnInterface.class);
811         }
812
813         @Override
814         protected void remove(InstanceIdentifier<VpnInterface> identifier, VpnInterface del) {
815             final VpnInterfaceKey key = identifier.firstKeyOf(VpnInterface.class, VpnInterfaceKey.class);
816             String interfaceName = key.getName();
817
818             //increment the vpn interface count in Vpn Instance Op Data
819             Long ifCnt = 0L;
820             String rd = getRouteDistinguisher(del.getVpnInstanceName());
821             if(rd.isEmpty()) rd = del.getVpnInstanceName();
822             VpnInstanceOpDataEntry vpnInstOp = VpnUtil.getVpnInstanceOpData(broker, rd);
823             if(vpnInstOp != null && vpnInstOp.getVpnInterfaceCount() != null) {
824                 ifCnt = vpnInstOp.getVpnInterfaceCount();
825             }
826
827             LOG.trace("VpnInterfaceOpListener remove: interface name {} rd {} interface count in Vpn Op Instance {}", interfaceName, rd, ifCnt);
828
829             VpnUtil.asyncUpdate(broker, LogicalDatastoreType.OPERATIONAL,
830                     VpnUtil.getVpnInstanceOpDataIdentifier(rd),
831                     VpnUtil.updateIntfCntInVpnInstOpData(ifCnt - 1, rd), VpnUtil.DEFAULT_CALLBACK);
832
833             //TODO: Clean up the DPN List in Vpn Instance Op if ifCnt is zero
834
835             notifyTaskIfRequired(interfaceName);
836         }
837
838         private void notifyTaskIfRequired(String intfName) {
839             Runnable notifyTask = vpnIntfMap.remove(intfName);
840             if (notifyTask == null) {
841                 return;
842             }
843             executorService.execute(notifyTask);
844         }
845
846         @Override
847         protected void update(InstanceIdentifier<VpnInterface> identifier, VpnInterface original, VpnInterface update) {
848         }
849
850         @Override
851         protected void add(InstanceIdentifier<VpnInterface> identifier, VpnInterface add) {
852             final VpnInterfaceKey key = identifier.firstKeyOf(VpnInterface.class, VpnInterfaceKey.class);
853             String interfaceName = key.getName();
854
855             //increment the vpn interface count in Vpn Instance Op Data
856             Long ifCnt = 0L;
857             String rd = getRouteDistinguisher(add.getVpnInstanceName());
858             if(rd.isEmpty()) rd = add.getVpnInstanceName();
859             VpnInstanceOpDataEntry vpnInstOp = VpnUtil.getVpnInstanceOpData(broker, rd);
860             if(vpnInstOp != null &&  vpnInstOp.getVpnInterfaceCount() != null) {
861                 ifCnt = vpnInstOp.getVpnInterfaceCount();
862             }
863
864             LOG.trace("VpnInterfaceOpListener add: interface name {} rd {} interface count in Vpn Op Instance {}", interfaceName, rd, ifCnt);
865
866             VpnUtil.asyncUpdate(broker, LogicalDatastoreType.OPERATIONAL,
867                     VpnUtil.getVpnInstanceOpDataIdentifier(rd),
868                     VpnUtil.updateIntfCntInVpnInstOpData(ifCnt + 1, rd), VpnUtil.DEFAULT_CALLBACK);
869
870
871         }
872     }
873
874     protected void updatePrefixesForDPN(BigInteger dpnId, UpdateRouteAction action) {
875
876         LOG.info("Tunnel event triggered {} for Dpn:{} ", action.name(), dpnId);
877         InstanceIdentifierBuilder<VpnInstances> idBuilder = InstanceIdentifier.builder(VpnInstances.class);
878         InstanceIdentifier<VpnInstances> vpnInstancesId = idBuilder.build();
879         Optional<VpnInstances> vpnInstances = VpnUtil.read(broker, LogicalDatastoreType.CONFIGURATION, vpnInstancesId);
880
881         if (vpnInstances.isPresent()) {
882             List<VpnInstance> vpnInstanceList = vpnInstances.get().getVpnInstance();
883             Iterator<VpnInstance> vpnInstIter = vpnInstanceList.iterator();
884             while (vpnInstIter.hasNext()) {
885                 VpnInstance vpnInstance = vpnInstIter.next();
886                 VpnAfConfig vpnConfig = vpnInstance.getIpv4Family();
887                 String rd = vpnConfig.getRouteDistinguisher();
888
889                 InstanceIdentifier<VpnToDpnList> id = VpnUtil.getVpnToDpnListIdentifier(rd, dpnId);
890                 Optional<VpnToDpnList> dpnInVpn = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, id);
891                 if (dpnInVpn.isPresent()) {
892                     if (action == UpdateRouteAction.ADVERTISE_ROUTE) {
893                         fibManager.populateFibOnNewDpn(dpnId, VpnUtil
894                             .getVpnId(broker, vpnInstance.getVpnInstanceName()), rd);
895                     }
896                     List<org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
897                         .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces> vpnInterfaces = dpnInVpn.get().getVpnInterfaces();
898                     for(org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.vpn.instance.op.data
899                         .vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces vpnInterface : vpnInterfaces) {
900                         InstanceIdentifier<VpnInterface> vpnIntfId = VpnUtil.getVpnInterfaceIdentifier(vpnInterface.getInterfaceName());
901                         InstanceIdentifier<Adjacencies> path = vpnIntfId.augmentation(Adjacencies.class);
902                         Optional<Adjacencies> adjacencies = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, path);
903
904                         if (adjacencies.isPresent()) {
905                             List<Adjacency> adjacencyList = adjacencies.get().getAdjacency();
906                             Iterator<Adjacency> adjacencyIterator = adjacencyList.iterator();
907
908                             while (adjacencyIterator.hasNext()) {
909                                 Adjacency adjacency = adjacencyIterator.next();
910                                 try {
911                                     if(action == UpdateRouteAction.ADVERTISE_ROUTE)
912                                         bgpManager.addPrefix(rd, adjacency.getIpAddress(), adjacency.getNextHopIp(), adjacency.getLabel().intValue());
913                                     else if(action == UpdateRouteAction.WITHDRAW_ROUTE)
914                                         bgpManager.deletePrefix(rd, adjacency.getIpAddress());
915                                 } catch (Exception e) {
916                                     LOG.error("Exception when updating prefix {} in vrf {} to BGP", adjacency.getIpAddress(), rd);
917                                 }
918                             }
919                         }
920
921                     }
922                     if (action == UpdateRouteAction.WITHDRAW_ROUTE) {
923                         fibManager.cleanUpDpnForVpn(dpnId, VpnUtil
924                             .getVpnId(broker, vpnInstance.getVpnInstanceName()), rd);
925                     }
926                 }
927             }
928         }
929     }
930
931 }