2 * Copyright (c) 2016, 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
9 package org.opendaylight.netvirt.vpnmanager;
11 import com.google.common.base.Optional;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.ElanInstances;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstance;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.elan.rev150602.elan.instances.ElanInstanceKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.Subnetmaps;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 public class SubnetmapChangeListener extends AsyncDataTreeChangeListenerBase<Subnetmap, SubnetmapChangeListener> {
30 private static final Logger LOG = LoggerFactory.getLogger(SubnetmapChangeListener.class);
31 private final DataBroker dataBroker;
32 private final VpnSubnetRouteHandler vpnSubnetRouteHandler;
34 public SubnetmapChangeListener(final DataBroker dataBroker, final VpnSubnetRouteHandler vpnSubnetRouteHandler) {
35 super(Subnetmap.class, SubnetmapChangeListener.class);
36 this.dataBroker = dataBroker;
37 this.vpnSubnetRouteHandler = vpnSubnetRouteHandler;
41 LOG.info("{} start", getClass().getSimpleName());
42 registerListener(dataBroker);
46 protected InstanceIdentifier<Subnetmap> getWildCardPath() {
47 return InstanceIdentifier.create(Subnetmaps.class).child(Subnetmap.class);
50 // TODO Clean up the exception handling
51 @SuppressWarnings("checkstyle:IllegalCatch")
52 private void registerListener(final DataBroker db) {
54 registerListener(LogicalDatastoreType.CONFIGURATION, db);
55 } catch (final Exception e) {
56 LOG.error("VPNManager subnetMap config DataChange listener registration fail!", e);
57 throw new IllegalStateException("VPNManager subnetMap config DataChange listener registration failed.", e);
62 protected void add(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmap) {
63 LOG.trace("add:SubnetmapChangeListener add subnetmap method - key: {}, value: {}", identifier, subnetmap);
64 Uuid subnetId = subnetmap.getId();
65 Uuid vpnId = subnetmap.getVpnId();
66 if (subnetmap.getVpnId() != null) {
67 // SubnetRoute for ExternalSubnets is handled in ExternalSubnetVpnInstanceListener.
68 // Here we must handle only InternalVpnSubnetRoute and BGPVPNBasedSubnetRoute
69 Network network = VpnUtil.getNeutronNetwork(dataBroker, subnetmap.getNetworkId());
70 if (network == null) {
71 LOG.info("update: vpnId {}, networkId: {}, subnetId: {}: network was not found",
72 vpnId.getValue(), subnetmap.getNetworkId().getValue(), subnetId.getValue());
74 if (VpnUtil.getIsExternal(network)) {
77 boolean isBgpVpn = !vpnId.equals(subnetmap.getRouterId());
78 String elanInstanceName = subnetmap.getNetworkId().getValue();
79 Long elanTag = getElanTag(elanInstanceName);
80 if (elanTag.equals(0L)) {
81 LOG.error("add:Unable to fetch elantag from ElanInstance {} and hence not proceeding with "
82 + "subnetmapListener add for subnet {}", elanInstanceName, subnetId.getValue());
85 // subnet added to VPN case upon config DS replay after reboot
86 // ports added to subnet upon config DS replay after reboot are handled implicitly by subnetAddedToVpn
87 // in SubnetRouteHandler
88 vpnSubnetRouteHandler.onSubnetAddedToVpn(subnetmap, isBgpVpn , elanTag);
93 protected void remove(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmap) {
94 LOG.trace("remove:SubnetmapListener remove subnetmap method - key: {}, value: {}", identifier, subnetmap);
98 // TODO Clean up the exception handling
99 @SuppressWarnings("checkstyle:IllegalCatch")
100 protected void update(InstanceIdentifier<Subnetmap> identifier, Subnetmap subnetmapOriginal, Subnetmap
102 LOG.trace("update:SubnetmapListener update subnetmap method - key {}, original {}, update {}", identifier,
103 subnetmapOriginal, subnetmapUpdate);
104 Uuid vpnIdNew = subnetmapUpdate.getVpnId();
105 Uuid vpnIdOld = subnetmapOriginal.getVpnId();
106 Uuid subnetId = subnetmapUpdate.getId();
107 String elanInstanceName = subnetmapUpdate.getNetworkId().getValue();
108 // SubnetRoute for ExternalSubnets is handled in ExternalSubnetVpnInstanceListener.
109 // Here we must handle only InternalVpnSubnetRoute and BGPVPNBasedSubnetRoute
110 Network network = VpnUtil.getNeutronNetwork(dataBroker, subnetmapUpdate.getNetworkId());
111 if (network == null) {
112 LOG.info("update: vpnIdNew: {}, vpnIdOld: {}, networkId: {}, subnetId: {}: network was not found",
113 vpnIdNew.getValue(), vpnIdOld.getValue(), elanInstanceName, subnetId.getValue());
115 if (VpnUtil.getIsExternal(network)) {
118 Long elanTag = getElanTag(elanInstanceName);
119 if (elanTag.equals(0L)) {
120 LOG.error("update:Unable to fetch elantag from ElanInstance {} and hence not proceeding with "
121 + "subnetmapListener update for subnet {}", elanInstanceName, subnetId.getValue());
124 // subnet added to VPN case
125 if (vpnIdNew != null && vpnIdOld == null) {
126 boolean isBgpVpn = !vpnIdNew.equals(subnetmapUpdate.getRouterId());
130 vpnSubnetRouteHandler.onSubnetAddedToVpn(subnetmapUpdate, true, elanTag);
133 // subnet removed from VPN case
134 if (vpnIdOld != null && vpnIdNew == null) {
135 Boolean isBgpVpn = vpnIdOld.equals(subnetmapOriginal.getRouterId()) ? false : true;
139 vpnSubnetRouteHandler.onSubnetDeletedFromVpn(subnetmapOriginal, true);
142 // subnet updated in VPN case
143 if (vpnIdOld != null && vpnIdNew != null && (!vpnIdNew.equals(vpnIdOld))) {
144 vpnSubnetRouteHandler.onSubnetUpdatedInVpn(subnetmapUpdate, elanTag);
147 // port added/removed to/from subnet case
148 List<Uuid> oldPortList;
149 List<Uuid> newPortList;
150 newPortList = subnetmapUpdate.getPortList() != null ? subnetmapUpdate.getPortList() : new ArrayList<>();
151 oldPortList = subnetmapOriginal.getPortList() != null ? subnetmapOriginal.getPortList() : new ArrayList<>();
152 if (newPortList.size() == oldPortList.size()) {
155 if (newPortList.size() > oldPortList.size()) {
156 for (Uuid portId : newPortList) {
157 if (! oldPortList.contains(portId)) {
158 vpnSubnetRouteHandler.onPortAddedToSubnet(subnetmapUpdate, portId);
163 for (Uuid portId : oldPortList) {
164 if (! newPortList.contains(portId)) {
165 vpnSubnetRouteHandler.onPortRemovedFromSubnet(subnetmapUpdate, portId);
173 protected SubnetmapChangeListener getDataTreeChangeListener() {
177 // TODO Clean up the exception handling
178 @SuppressWarnings("checkstyle:IllegalCatch")
179 protected long getElanTag(String elanInstanceName) {
180 InstanceIdentifier<ElanInstance> elanIdentifierId = InstanceIdentifier.builder(ElanInstances.class)
181 .child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build();
184 Optional<ElanInstance> elanInstance = VpnUtil.read(dataBroker, LogicalDatastoreType
185 .CONFIGURATION, elanIdentifierId);
186 if (elanInstance.isPresent()) {
187 if (elanInstance.get().getElanTag() != null) {
188 elanTag = elanInstance.get().getElanTag();
190 LOG.error("Notification failed because of failure in fetching elanTag for ElanInstance {}",
194 LOG.error("Notification failed because of failure in reading ELANInstance {}", elanInstanceName);
196 } catch (Exception e) {
197 LOG.error("Notification failed because of failure in fetching elanTag for ElanInstance {}",
198 elanInstanceName, e);