Upgrade to the Neon base platform
[netvirt.git] / vpnmanager / impl / src / main / java / org / opendaylight / netvirt / vpnmanager / TunnelEndPointChangeListener.java
1 /*
2  * Copyright (c) 2016 Hewlett Packard Enterprise, Co. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.netvirt.vpnmanager;
10
11 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
12 import static org.opendaylight.genius.infra.Datastore.OPERATIONAL;
13
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.math.BigInteger;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
19 import javax.annotation.PostConstruct;
20 import javax.inject.Inject;
21 import javax.inject.Singleton;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
25 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
26 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
27 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
28 import org.opendaylight.netvirt.vpnmanager.api.InterfaceUtils;
29 import org.opendaylight.netvirt.vpnmanager.api.VpnHelper;
30 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.instances.VpnInstance;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.DpnEndpoints;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.DPNTEPsInfo;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.itm.op.rev160406.dpn.endpoints.dpn.teps.info.TunnelEndPoints;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.l3vpn.rev130911.vpn.instance.op.data.vpn.instance.op.data.entry.vpn.to.dpn.list.VpnInterfaces;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 @Singleton
40 public class TunnelEndPointChangeListener
41     extends AsyncDataTreeChangeListenerBase<TunnelEndPoints, TunnelEndPointChangeListener> {
42     private static final Logger LOG = LoggerFactory.getLogger(TunnelEndPointChangeListener.class);
43
44     private final DataBroker broker;
45     private final ManagedNewTransactionRunner txRunner;
46     private final VpnInterfaceManager vpnInterfaceManager;
47     private final JobCoordinator jobCoordinator;
48     private final VpnUtil vpnUtil;
49
50     @Inject
51     public TunnelEndPointChangeListener(final DataBroker broker, final VpnInterfaceManager vpnInterfaceManager,
52             final JobCoordinator jobCoordinator, VpnUtil vpnUtil) {
53         super(TunnelEndPoints.class, TunnelEndPointChangeListener.class);
54         this.broker = broker;
55         this.txRunner = new ManagedNewTransactionRunnerImpl(broker);
56         this.vpnInterfaceManager = vpnInterfaceManager;
57         this.jobCoordinator = jobCoordinator;
58         this.vpnUtil = vpnUtil;
59     }
60
61     @PostConstruct
62     public void start() {
63         LOG.info("{} start", getClass().getSimpleName());
64         registerListener(LogicalDatastoreType.CONFIGURATION, broker);
65     }
66
67     @Override
68     protected InstanceIdentifier<TunnelEndPoints> getWildCardPath() {
69         return InstanceIdentifier.builder(DpnEndpoints.class).child(DPNTEPsInfo.class).child(TunnelEndPoints.class)
70             .build();
71     }
72
73     @Override
74     protected void remove(InstanceIdentifier<TunnelEndPoints> key, TunnelEndPoints tep) {
75     }
76
77     @Override
78     protected void update(InstanceIdentifier<TunnelEndPoints> key, TunnelEndPoints origTep,
79         TunnelEndPoints updatedTep) {
80     }
81
82     @Override
83     protected void add(InstanceIdentifier<TunnelEndPoints> key, TunnelEndPoints tep) {
84         BigInteger dpnId = key.firstIdentifierOf(DPNTEPsInfo.class).firstKeyOf(DPNTEPsInfo.class).getDPNID();
85         if (BigInteger.ZERO.equals(dpnId)) {
86             LOG.warn("add: Invalid DPN id for TEP {}", tep.getInterfaceName());
87             return;
88         }
89
90         List<VpnInstance> vpnInstances = VpnHelper.getAllVpnInstances(broker);
91         if (vpnInstances == null || vpnInstances.isEmpty()) {
92             LOG.warn("add: dpnId: {}: tep: {}: No VPN instances defined", dpnId, tep.getInterfaceName());
93             return;
94         }
95
96         for (VpnInstance vpnInstance : vpnInstances) {
97             final String vpnName = vpnInstance.getVpnInstanceName();
98             final long vpnId = vpnUtil.getVpnId(vpnName);
99             LOG.info("add: Handling TEP {} add for VPN instance {}", tep.getInterfaceName(), vpnName);
100             final String primaryRd = vpnUtil.getPrimaryRd(vpnName);
101             if (!vpnUtil.isVpnPendingDelete(primaryRd)) {
102                 List<VpnInterfaces> vpnInterfaces = vpnUtil.getDpnVpnInterfaces(vpnInstance, dpnId);
103                 if (vpnInterfaces != null) {
104                     for (VpnInterfaces vpnInterface : vpnInterfaces) {
105                         String vpnInterfaceName = vpnInterface.getInterfaceName();
106                         jobCoordinator.enqueueJob("VPNINTERFACE-" + vpnInterfaceName,
107                             () -> {
108                                 final org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces
109                                         .rev140508.interfaces.state.Interface
110                                         interfaceState =
111                                         InterfaceUtils.getInterfaceStateFromOperDS(broker, vpnInterfaceName);
112                                 if (interfaceState == null) {
113                                     LOG.debug("add: Cannot retrieve interfaceState for vpnInterfaceName {}, "
114                                             + "cannot generate lPortTag and process adjacencies", vpnInterfaceName);
115                                     return Collections.emptyList();
116                                 }
117                                 final int lPortTag = interfaceState.getIfIndex();
118                                 List<ListenableFuture<Void>> futures = new ArrayList<>();
119                                 futures.add(txRunner.callWithNewWriteOnlyTransactionAndSubmit(CONFIGURATION,
120                                     writeConfigTxn -> futures.add(
121                                         txRunner.callWithNewWriteOnlyTransactionAndSubmit(OPERATIONAL,
122                                             writeOperTxn -> futures.add(
123                                                 txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION,
124                                                     writeInvTxn -> vpnInterfaceManager.processVpnInterfaceAdjacencies(
125                                                         dpnId, lPortTag, vpnName, primaryRd, vpnInterfaceName, vpnId,
126                                                         writeConfigTxn, writeOperTxn, writeInvTxn,
127                                                         interfaceState)))))));
128                                 LOG.trace("add: Handled TEP {} add for VPN instance {} VPN interface {}",
129                                         tep.getInterfaceName(), vpnName, vpnInterfaceName);
130                                 return futures;
131                             });
132                     }
133                 }
134             } else {
135                 LOG.error("add: Ignoring addition of tunnel interface {} dpn {} for vpnInstance {} with primaryRd {},"
136                         + " as the VPN is already marked for deletion", tep.getInterfaceName(), dpnId,
137                         vpnName, primaryRd);
138             }
139         }
140     }
141
142     @Override
143     protected TunnelEndPointChangeListener getDataTreeChangeListener() {
144         return this;
145     }
146 }