2 * Copyright (c) 2016 Hewlett Packard Enterprise, Co. and others. All rights reserved.
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
9 package org.opendaylight.netvirt.vpnmanager;
11 import java.util.ArrayList;
12 import java.util.List;
14 import javax.annotation.PostConstruct;
15 import javax.inject.Inject;
16 import javax.inject.Singleton;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
21 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
22 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
23 import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
24 import org.opendaylight.netvirt.elanmanager.api.IElanService;
25 import org.opendaylight.netvirt.vpnmanager.api.VpnHelper;
26 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
27 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceBuilder;
28 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceKey;
29 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.vpn._interface.VpnInstanceNames;
30 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.vpn._interface.VpnInstanceNames.AssociatedSubnetType;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInterfaces;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.interfaces.ElanInterface;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 public class VpnElanInterfaceChangeListener
40 extends AsyncDataTreeChangeListenerBase<ElanInterface, VpnElanInterfaceChangeListener> {
41 private static final Logger LOG = LoggerFactory.getLogger(VpnElanInterfaceChangeListener.class);
43 private final DataBroker broker;
44 private final ManagedNewTransactionRunner txRunner;
45 private final IElanService elanService;
48 public VpnElanInterfaceChangeListener(final DataBroker broker, final IElanService elanService) {
49 super(ElanInterface.class, VpnElanInterfaceChangeListener.class);
51 this.txRunner = new ManagedNewTransactionRunnerImpl(broker);
52 this.elanService = elanService;
57 LOG.info("{} start", getClass().getSimpleName());
58 registerListener(LogicalDatastoreType.CONFIGURATION, broker);
62 protected InstanceIdentifier<ElanInterface> getWildCardPath() {
63 return InstanceIdentifier.create(ElanInterfaces.class).child(ElanInterface.class);
67 protected void remove(InstanceIdentifier<ElanInterface> key, ElanInterface elanInterface) {
68 String interfaceName = elanInterface.getName();
69 if (!elanService.isExternalInterface(interfaceName)) {
70 LOG.debug("remove: Interface {} is not external. Ignoring interface removal", interfaceName);
74 if (!VpnUtil.isVpnInterfaceConfigured(broker, interfaceName)) {
75 LOG.debug("remove: VpnInterface was never configured for {}. Ignoring interface removal", interfaceName);
79 InstanceIdentifier<VpnInterface> vpnInterfaceIdentifier = VpnUtil.getVpnInterfaceIdentifier(interfaceName);
80 ListenableFutures.addErrorLogging(txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx ->
81 tx.delete(LogicalDatastoreType.CONFIGURATION, vpnInterfaceIdentifier)), LOG,
82 "Error removing VPN interface {}", vpnInterfaceIdentifier);
83 LOG.info("remove: Removed VPN interface {}", interfaceName);
87 protected void update(InstanceIdentifier<ElanInterface> key, ElanInterface origElanInterface,
88 ElanInterface updatedElanInterface) {
93 protected void add(InstanceIdentifier<ElanInterface> key, ElanInterface elanInterface) {
94 String interfaceName = elanInterface.getName();
95 if (!elanService.isExternalInterface(interfaceName)) {
96 LOG.debug("add: Interface {} is not external. Ignoring", interfaceName);
102 networkId = new Uuid(elanInterface.getElanInstanceName());
103 } catch (IllegalArgumentException e) {
104 LOG.debug("add: ELAN instance {} for interface {} is not Uuid", elanInterface.getElanInstanceName(),
105 elanInterface.getName());
109 Uuid vpnId = VpnUtil.getExternalNetworkVpnId(broker, networkId);
111 LOG.debug("add: Network {} is not external or vpn-id missing. Ignoring interface {} on elan {}",
112 networkId.getValue(), elanInterface.getName(), elanInterface.getElanInstanceName());
115 VpnInstanceNames vpnInstance = VpnHelper
116 .getVpnInterfaceVpnInstanceNames(vpnId.getValue(), AssociatedSubnetType.V4AndV6Subnets);
117 List<VpnInstanceNames> listVpn = new ArrayList<>();
118 listVpn.add(vpnInstance);
119 VpnInterface vpnInterface = new VpnInterfaceBuilder().withKey(new VpnInterfaceKey(interfaceName))
120 .setVpnInstanceNames(listVpn)
121 .setScheduledForRemove(Boolean.FALSE)
123 InstanceIdentifier<VpnInterface> vpnInterfaceIdentifier = VpnUtil.getVpnInterfaceIdentifier(interfaceName);
124 VpnUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnInterfaceIdentifier, vpnInterface);
125 LOG.info("add: Added VPN interface {} with VPN-id {} elanInstance {}", interfaceName, vpnId.getValue(),
126 elanInterface.getElanInstanceName());
130 protected VpnElanInterfaceChangeListener getDataTreeChangeListener() {