2 * Copyright © 2017 Ericsson India Global Services Pvt Ltd. 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
8 package org.opendaylight.netvirt.cloudservicechain.listeners;
10 import java.util.Optional;
11 import javax.annotation.PostConstruct;
12 import javax.inject.Inject;
13 import javax.inject.Singleton;
14 import org.opendaylight.mdsal.binding.api.DataBroker;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.mdsal.common.api.ReadFailedException;
17 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
18 import org.opendaylight.netvirt.cloudservicechain.VPNServiceChainHandler;
19 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces;
20 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface;
21 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.vpn._interface.VpnInstanceNames;
22 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.vpn._interface.VpnInstanceNames.AssociatedSubnetType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.cloud.servicechain.state.rev160711.vpn.to.pseudo.port.list.VpnToPseudoPortData;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 public class CloudScVpnInterfaceListener
30 extends AsyncDataTreeChangeListenerBase<VpnInterface, CloudScVpnInterfaceListener> {
32 private static final Logger LOG = LoggerFactory.getLogger(CloudScVpnInterfaceListener.class);
33 private final DataBroker dataBroker;
34 private final VPNServiceChainHandler vpnScHandler;
37 public CloudScVpnInterfaceListener(final DataBroker dataBroker, final VPNServiceChainHandler vpnScHandler) {
38 super(VpnInterface.class, CloudScVpnInterfaceListener.class);
40 this.dataBroker = dataBroker;
41 this.vpnScHandler = vpnScHandler;
47 LOG.info("{} init", getClass().getSimpleName());
48 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
52 protected CloudScVpnInterfaceListener getDataTreeChangeListener() {
53 return CloudScVpnInterfaceListener.this;
57 protected InstanceIdentifier<VpnInterface> getWildCardPath() {
58 return InstanceIdentifier.create(VpnInterfaces.class).child(VpnInterface.class);
62 protected void remove(InstanceIdentifier<VpnInterface> key, VpnInterface vpnIfaceRemoved) {
63 for (VpnInstanceNames vpnInstance : vpnIfaceRemoved.getVpnInstanceNames()) {
64 String vpnName = vpnInstance.getVpnName();
65 if (!vpnInstance.getAssociatedSubnetType().equals(AssociatedSubnetType.V4AndV6Subnets)
66 && !vpnInstance.getAssociatedSubnetType().equals(AssociatedSubnetType.V4Subnet)) {
70 Optional<VpnToPseudoPortData> optScfInfoForVpn = vpnScHandler.getScfInfoForVpn(vpnName);
71 if (!optScfInfoForVpn.isPresent()) {
72 LOG.trace("Vpn {} is not related to ServiceChaining. No further action", vpnName);
75 } catch (InterruptedException | ExecutionException e) {
76 LOG.error("Error reading the ServiceChaining information for VPN {}", vpnName, e);
80 vpnScHandler.unbindScfOnVpnInterface(vpnIfaceRemoved.key().getName());
84 protected void update(InstanceIdentifier<VpnInterface> key, VpnInterface dataObjectModificationBefore,
85 VpnInterface dataObjectModificationAfter) {
89 protected void add(InstanceIdentifier<VpnInterface> key, VpnInterface vpnIfaceAdded) {
90 for (VpnInstanceNames vpnInstance : vpnIfaceAdded.getVpnInstanceNames()) {
91 String vpnName = vpnInstance.getVpnName();
92 if (!vpnInstance.getAssociatedSubnetType().equals(AssociatedSubnetType.V4AndV6Subnets)
93 && !vpnInstance.getAssociatedSubnetType().equals(AssociatedSubnetType.V4Subnet)) {
97 Optional<VpnToPseudoPortData> optScfInfoForVpn = vpnScHandler.getScfInfoForVpn(vpnName);
98 if (!optScfInfoForVpn.isPresent()) {
99 LOG.trace("Vpn {} is not related to ServiceChaining. No further action", vpnName);
102 vpnScHandler.bindScfOnVpnInterface(vpnIfaceAdded.key().getName(),
103 optScfInfoForVpn.get().getScfTag());
104 } catch (InterruptedException | ExecutionException e) {
105 LOG.error("Error reading the ServiceChaining information for VPN {}", vpnName, e);