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
8 package org.opendaylight.netvirt.neutronvpn;
10 import com.google.common.base.Optional;
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;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
20 import org.opendaylight.genius.datastoreutils.AsyncDataTreeChangeListenerBase;
21 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
22 import org.opendaylight.genius.mdsalutil.MDSALUtil;
23 import org.opendaylight.netvirt.vpnmanager.api.IVpnManager;
24 import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.af.config.vpntargets.VpnTarget;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.natservice.rev160111.ProviderTypes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.NetworkAttributes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.networkmaps.NetworkMap;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.networkmaps.NetworkMapBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.networkmaps.NetworkMapKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.neutronvpn.rev150602.subnetmaps.Subnetmap;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 public class NeutronSubnetChangeListener extends AsyncDataTreeChangeListenerBase<Subnet, NeutronSubnetChangeListener> {
42 private static final Logger LOG = LoggerFactory.getLogger(NeutronSubnetChangeListener.class);
43 private final DataBroker dataBroker;
44 private final NeutronvpnManager nvpnManager;
45 private final NeutronExternalSubnetHandler externalSubnetHandler;
46 private final NeutronvpnUtils neutronvpnUtils;
47 private final IVpnManager vpnManager;
50 public NeutronSubnetChangeListener(final DataBroker dataBroker, final NeutronvpnManager neutronvpnManager,
51 final NeutronExternalSubnetHandler externalSubnetHandler, final NeutronvpnUtils neutronvpnUtils,
52 final IVpnManager vpnManager) {
53 super(Subnet.class, NeutronSubnetChangeListener.class);
54 this.dataBroker = dataBroker;
55 this.nvpnManager = neutronvpnManager;
56 this.externalSubnetHandler = externalSubnetHandler;
57 this.neutronvpnUtils = neutronvpnUtils;
58 this.vpnManager = vpnManager;
64 LOG.info("{} init", getClass().getSimpleName());
65 registerListener(LogicalDatastoreType.CONFIGURATION, dataBroker);
69 protected InstanceIdentifier<Subnet> getWildCardPath() {
70 return InstanceIdentifier.create(Neutron.class).child(Subnets.class).child(Subnet.class);
74 protected NeutronSubnetChangeListener getDataTreeChangeListener() {
75 return NeutronSubnetChangeListener.this;
80 protected void add(InstanceIdentifier<Subnet> identifier, Subnet input) {
81 LOG.trace("Adding Subnet : key: {}, value={}", identifier, input);
82 Uuid networkId = input.getNetworkId();
83 Uuid subnetId = input.getUuid();
84 Network network = neutronvpnUtils.getNeutronNetwork(networkId);
85 if (network == null || !NeutronvpnUtils.isNetworkTypeSupported(network)) {
86 LOG.warn("neutron vpn received a subnet add() for a network without a provider extension augmentation "
87 + "or with an unsupported network type for the subnet {} which is part of network {}",
88 subnetId.getValue(), network);
91 neutronvpnUtils.addToSubnetCache(input);
92 handleNeutronSubnetCreated(input, network);
93 externalSubnetHandler.handleExternalSubnetAdded(network, subnetId, null);
97 protected void remove(InstanceIdentifier<Subnet> identifier, Subnet input) {
98 LOG.trace("Removing subnet : key: {}, value={}", identifier, input);
99 Uuid networkId = input.getNetworkId();
100 Uuid subnetId = input.getUuid();
101 Network network = neutronvpnUtils.getNeutronNetwork(networkId);
102 if (network == null || !NeutronvpnUtils.isNetworkTypeSupported(network)) {
103 LOG.warn("neutron vpn received a subnet remove() for a network without a provider extension augmentation "
104 + "or with an unsupported network type for the subnet {} which is part of network {}",
105 subnetId.getValue(), network);
108 handleNeutronSubnetDeleted(subnetId, networkId, input.getCidr().stringValue());
109 externalSubnetHandler.handleExternalSubnetRemoved(network, subnetId);
110 neutronvpnUtils.removeFromSubnetCache(input);
114 protected void update(InstanceIdentifier<Subnet> identifier, Subnet original, Subnet update) {
115 LOG.trace("Updating Subnet : key: {}, original value={}, update value={}", identifier, original, update);
116 neutronvpnUtils.addToSubnetCache(update);
119 private void handleNeutronSubnetCreated(Subnet subnet, Network network) {
120 Uuid networkId = network.getUuid();
121 Uuid subnetId = subnet.getUuid();
122 ProviderTypes providerType = NeutronvpnUtils.getProviderNetworkType(network);
123 String segmentationId = NeutronvpnUtils.getSegmentationIdFromNeutronNetwork(network);
124 nvpnManager.createSubnetmapNode(subnetId, subnet.getCidr().stringValue(), subnet.getTenantId(), networkId,
125 providerType != null ? NetworkAttributes.NetworkType.valueOf(providerType.getName()) : null,
126 segmentationId != null ? Long.parseLong(segmentationId) : 0L);
127 createSubnetToNetworkMapping(subnetId, networkId);
130 private void handleNeutronSubnetDeleted(Uuid subnetId, Uuid networkId, String subnetCidr) {
131 Uuid vpnId = neutronvpnUtils.getVpnForNetwork(networkId);
133 LOG.warn("Subnet {} deleted without disassociating network {} from VPN {}. Ideally, please disassociate "
134 + "network from VPN before deleting neutron subnet.", subnetId.getValue(), networkId.getValue(),
136 Subnetmap subnetmap = neutronvpnUtils.getSubnetmap(subnetId);
137 if (subnetmap != null) {
138 nvpnManager.removeSubnetFromVpn(vpnId, subnetId, null /* internet-vpn-id */);
140 LOG.error("Subnetmap for subnet {} not found", subnetId.getValue());
142 Set<VpnTarget> routeTargets = vpnManager.getRtListForVpn(vpnId.getValue());
143 if (!routeTargets.isEmpty()) {
144 vpnManager.removeRouteTargetsToSubnetAssociation(routeTargets, subnetCidr, subnetId.getValue());
147 if (networkId != null) {
148 deleteSubnetToNetworkMapping(subnetId, networkId);
150 nvpnManager.deleteSubnetMapNode(subnetId);
153 // TODO Clean up the exception handling
154 @SuppressWarnings("checkstyle:IllegalCatch")
155 private void createSubnetToNetworkMapping(Uuid subnetId, Uuid networkId) {
157 InstanceIdentifier<NetworkMap> networkMapIdentifier = NeutronvpnUtils.buildNetworkMapIdentifier(networkId);
158 Optional<NetworkMap> optionalNetworkMap = SingleTransactionDataBroker.syncReadOptional(dataBroker,
159 LogicalDatastoreType.CONFIGURATION, networkMapIdentifier);
160 NetworkMapBuilder nwMapBuilder = null;
161 if (optionalNetworkMap.isPresent()) {
162 nwMapBuilder = new NetworkMapBuilder(optionalNetworkMap.get());
164 nwMapBuilder = new NetworkMapBuilder().withKey(new NetworkMapKey(networkId)).setNetworkId(networkId);
165 LOG.debug("Adding a new network node in NetworkMaps DS for network {}", networkId.getValue());
167 List<Uuid> subnetIdList = nwMapBuilder.getSubnetIdList();
168 if (subnetIdList == null) {
169 subnetIdList = new ArrayList<>();
171 subnetIdList.add(subnetId);
172 nwMapBuilder.setSubnetIdList(subnetIdList);
173 MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, networkMapIdentifier, nwMapBuilder
175 LOG.debug("Created subnet-network mapping for subnet {} network {}", subnetId.getValue(),
176 networkId.getValue());
177 } catch (ReadFailedException | RuntimeException e) {
178 LOG.error("Create subnet-network mapping failed for subnet {} network {}", subnetId.getValue(),
179 networkId.getValue());
183 // TODO Clean up the exception handling
184 @SuppressWarnings("checkstyle:IllegalCatch")
185 private void deleteSubnetToNetworkMapping(Uuid subnetId, Uuid networkId) {
187 InstanceIdentifier<NetworkMap> networkMapIdentifier = NeutronvpnUtils.buildNetworkMapIdentifier(networkId);
188 Optional<NetworkMap> optionalNetworkMap = SingleTransactionDataBroker.syncReadOptional(dataBroker,
189 LogicalDatastoreType.CONFIGURATION, networkMapIdentifier);
190 if (optionalNetworkMap.isPresent()) {
191 NetworkMapBuilder nwMapBuilder = new NetworkMapBuilder(optionalNetworkMap.get());
192 List<Uuid> subnetIdList = nwMapBuilder.getSubnetIdList();
193 if (subnetIdList.remove(subnetId)) {
194 if (subnetIdList.isEmpty()) {
195 MDSALUtil.syncDelete(dataBroker, LogicalDatastoreType.CONFIGURATION, networkMapIdentifier);
196 LOG.debug("Deleted network node in NetworkMaps DS for subnet {} network {}",
197 subnetId.getValue(), networkId.getValue());
199 nwMapBuilder.setSubnetIdList(subnetIdList);
200 MDSALUtil.syncWrite(dataBroker, LogicalDatastoreType.CONFIGURATION, networkMapIdentifier,
201 nwMapBuilder.build());
202 LOG.debug("Deleted subnet-network mapping for subnet {} network {}", subnetId.getValue(),
203 networkId.getValue());
206 LOG.error("Subnet {} is not mapped to network {}", subnetId.getValue(), networkId.getValue());
209 LOG.error("network {} not present for subnet {} ", networkId, subnetId);
211 } catch (ReadFailedException | RuntimeException e) {
212 LOG.error("Delete subnet-network mapping failed for subnet {} network {}", subnetId.getValue(),
213 networkId.getValue());