/*
- * Copyright (c) 2015 - 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
+ * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
*/
package org.opendaylight.vpnservice.neutronvpn;
+import com.google.common.base.Optional;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.vpnservice.mdsalutil.AbstractDataChangeListener;
+import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.networkmaps.NetworkMap;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.networkmaps.NetworkMapBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.networkmaps.NetworkMapKey;
import org.opendaylight.yangtools.concepts.ListenerRegistration;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.util.ArrayList;
+import java.util.List;
+
public class NeutronSubnetChangeListener extends AbstractDataChangeListener<Subnet> implements AutoCloseable {
private static final Logger LOG = LoggerFactory.getLogger(NeutronSubnetChangeListener.class);
@Override
protected void add(InstanceIdentifier<Subnet> identifier, Subnet input) {
- LOG.trace("Adding Subnet : key: " + identifier + ", value=" + input);
- nvpnManager.updateSubnetNode(input.getUuid(), input.getTenantId(), input.getNetworkId(), null, null, null);
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Adding Subnet : key: " + identifier + ", value=" + input);
+ }
+ handleNeutronSubnetCreated(input.getUuid(), String.valueOf(input.getCidr().getValue()), input.getNetworkId(), input.getTenantId());
}
@Override
protected void remove(InstanceIdentifier<Subnet> identifier, Subnet input) {
- LOG.trace("Removing subnet : key: " + identifier + ", value=" + input);
- Uuid vpnId = NeutronvpnUtils.getVpnForNetwork(broker, input.getNetworkId());
- if (vpnId != null) {
- nvpnManager.removeSubnetFromVpn(vpnId, input.getUuid());
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("Removing subnet : key: " + identifier + ", value=" + input);
}
+ handleNeutronSubnetDeleted(input.getUuid(), input.getNetworkId(), null);
}
@Override
LOG.trace("Updating Subnet : key: " + identifier + ", original value=" + original + ", update value=" +
update);
}
- nvpnManager.updateSubnetNode(update.getUuid(), update.getTenantId(), update.getNetworkId(), null, null, null);
+ handleNeutronSubnetUpdated(update.getUuid(), update.getNetworkId(), update.getTenantId());
+ }
+
+ private void handleNeutronSubnetCreated(Uuid subnetId, String subnetIp, Uuid networkId, Uuid tenantId) {
+ nvpnManager.updateSubnetNode(subnetId, subnetIp, tenantId, networkId, null, null, null);
+ if (networkId != null && NeutronvpnUtils.getNeutronNetwork(broker, networkId) != null) {
+ createSubnetToNetworkMapping(subnetId, networkId);
+ }
+ }
+
+ private void handleNeutronSubnetDeleted(Uuid subnetId, Uuid networkId, Uuid tenantId) {
+ Uuid vpnId = NeutronvpnUtils.getVpnForNetwork(broker, networkId);
+ if (vpnId != null) {
+ nvpnManager.removeSubnetFromVpn(vpnId, subnetId);
+ }
+ if (networkId != null) {
+ deleteSubnetToNetworkMapping(subnetId, networkId);
+ }
+ nvpnManager.deleteSubnetMapNode(subnetId);
+ }
+
+ private void handleNeutronSubnetUpdated(Uuid subnetId, Uuid networkId, Uuid tenantId) {
+ Uuid oldNetworkId = NeutronvpnUtils.getSubnetmap(broker, subnetId).getNetworkId();
+ if (oldNetworkId != null && !oldNetworkId.equals(networkId)) {
+ deleteSubnetToNetworkMapping(subnetId, oldNetworkId);
+ }
+ if (networkId != null && !networkId.equals(oldNetworkId)) {
+ createSubnetToNetworkMapping(subnetId, networkId);
+ }
+ nvpnManager.updateSubnetNode(subnetId, null, tenantId, networkId, null, null, null);
+ }
+
+ private void createSubnetToNetworkMapping(Uuid subnetId, Uuid networkId) {
+ try {
+ InstanceIdentifier networkMapIdentifier = NeutronvpnUtils.buildNetworkMapIdentifier(networkId);
+ Optional<NetworkMap> optionalNetworkMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
+ networkMapIdentifier);
+ NetworkMapBuilder nwMapBuilder = null;
+ if (optionalNetworkMap.isPresent()) {
+ nwMapBuilder = new NetworkMapBuilder(optionalNetworkMap.get());
+ } else {
+ nwMapBuilder = new NetworkMapBuilder().setKey(new NetworkMapKey(networkId)).setNetworkId(networkId);
+ LOG.debug("Adding a new network node in NetworkMaps DS for network {}", networkId.getValue());
+ }
+ List<Uuid> subnetIdList = nwMapBuilder.getSubnetIdList();
+ if (subnetIdList == null) {
+ subnetIdList = new ArrayList<>();
+ }
+ subnetIdList.add(subnetId);
+ nwMapBuilder.setSubnetIdList(subnetIdList);
+ MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, networkMapIdentifier, nwMapBuilder.build());
+ LOG.debug("Created subnet-network mapping for subnet {} network {}", subnetId.getValue(),
+ networkId.getValue());
+ } catch (Exception e) {
+ LOG.error("Create subnet-network mapping failed for subnet {} network {}", subnetId.getValue(),
+ networkId.getValue());
+ }
+ }
+
+ private void deleteSubnetToNetworkMapping(Uuid subnetId, Uuid networkId) {
+ try {
+ InstanceIdentifier networkMapIdentifier = NeutronvpnUtils.buildNetworkMapIdentifier(networkId);
+ Optional<NetworkMap> optionalNetworkMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION,
+ networkMapIdentifier);
+ if (optionalNetworkMap.isPresent()) {
+ NetworkMapBuilder nwMapBuilder = new NetworkMapBuilder(optionalNetworkMap.get());
+ List<Uuid> subnetIdList = nwMapBuilder.getSubnetIdList();
+ if (subnetIdList.remove(subnetId)) {
+ if (subnetIdList.size() == 0) {
+ MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, networkMapIdentifier);
+ LOG.debug("Deleted network node in NetworkMaps DS for network {}", subnetId.getValue(),
+ networkId.getValue());
+ } else {
+ nwMapBuilder.setSubnetIdList(subnetIdList);
+ MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, networkMapIdentifier,
+ nwMapBuilder.build());
+ LOG.debug("Deleted subnet-network mapping for subnet {} network {}", subnetId.getValue(),
+ networkId.getValue());
+ }
+ } else {
+ LOG.error("Subnet {} is not mapped to network {}", subnetId.getValue(), networkId.getValue());
+ }
+ } else {
+ LOG.error("network {} not present for subnet {} ", networkId, subnetId);
+ }
+ } catch (Exception e) {
+ LOG.error("Delete subnet-network mapping failed for subnet {} network {}", subnetId.getValue(),
+ networkId.getValue());
+ }
}
}