X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=neutronvpn%2Fneutronvpn-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fvpnservice%2Fneutronvpn%2FNeutronvpnManager.java;h=201a2f4d35e6cf95f8463e475afbf70ddb683e92;hb=4110307879cad1aa54943aedbf937b2f21575b4c;hp=505d128a215506837909732f8dee23a0e2b488e3;hpb=c103ce9e5b1e4acfb3320f245503e46332593e43;p=vpnservice.git diff --git a/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronvpnManager.java b/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronvpnManager.java index 505d128a..201a2f4d 100644 --- a/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronvpnManager.java +++ b/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronvpnManager.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved. + * Copyright (c) 2015 - 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, @@ -9,8 +9,9 @@ package org.opendaylight.vpnservice.neutronvpn; import com.google.common.base.Optional; import com.google.common.util.concurrent.SettableFuture; - import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService; +import org.opendaylight.controller.md.sal.binding.api.NotificationService; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.vpnservice.mdsalutil.MDSALUtil; import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager; @@ -37,38 +38,21 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.Adjacencies import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.Adjacency; import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.AdjacencyBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.l3vpn.rev130911.adjacency.list.AdjacencyKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.l3.attributes.Routes; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router; +import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.Network; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.port.attributes.FixedIps; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port; -import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.PortKey; 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.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.ElanInstances; +import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.instances.ElanInstance; +import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.elan.rev150602.elan.instances.ElanInstanceKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.lockmanager.rev150819.LockManagerService; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.AssociateNetworksInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.AssociateNetworksOutput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.AssociateNetworksOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.AssociateRouterInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.CreateL3VPNInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.CreateL3VPNOutput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.CreateL3VPNOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DeleteL3VPNInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DeleteL3VPNOutput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DeleteL3VPNOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DissociateNetworksInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DissociateNetworksOutput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DissociateNetworksOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.DissociateRouterInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.GetL3VPNInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.GetL3VPNInputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.GetL3VPNOutput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.GetL3VPNOutputBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.L3vpnInstance; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.NeutronvpnService; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.Subnetmaps; -import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.VpnMaps; +import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.*; import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.createl3vpn.input.L3vpn; import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.getl3vpn.output.L3vpnInstances; import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.getl3vpn.output @@ -80,33 +64,40 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev15 import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.vpnmaps.VpnMapBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.neutronvpn.rev150602.vpnmaps.VpnMapKey; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.common.RpcError; import org.opendaylight.yangtools.yang.common.RpcError.ErrorType; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; +import java.util.EventListener; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; -public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { +public class NeutronvpnManager implements NeutronvpnService, AutoCloseable , EventListener{ private static final Logger logger = LoggerFactory.getLogger(NeutronvpnManager.class); private final DataBroker broker; private LockManagerService lockManager; IMdsalApiManager mdsalUtil; + private NotificationPublishService notificationPublishService; + private NotificationService notificationService; + Boolean isExternalVpn; /** * @param db - dataBroker reference * @param mdsalManager - MDSAL Util API access */ - public NeutronvpnManager(final DataBroker db, IMdsalApiManager mdsalManager) { + public NeutronvpnManager(final DataBroker db, IMdsalApiManager mdsalManager,NotificationPublishService notiPublishService, + NotificationService notiService) { broker = db; mdsalUtil = mdsalManager; + notificationPublishService = notiPublishService; + notificationService = notiService; } public void setLockManager(LockManagerService lockManager) { @@ -118,14 +109,14 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { logger.info("Neutron VPN Manager Closed"); } - protected Subnetmap updateSubnetNode(Uuid subnetId, Uuid tenantId, Uuid networkId, Uuid routerId, Uuid vpnId, - Uuid portId) { - + protected Subnetmap updateSubnetNode(Uuid subnetId, String subnetIp, Uuid tenantId, Uuid networkId, Uuid routerId, + Uuid vpnId, Uuid portId) { + Subnetmap subnetmap = null; + SubnetmapBuilder builder = null; + boolean isLockAcquired = false; + InstanceIdentifier id = InstanceIdentifier.builder(Subnetmaps.class). + child(Subnetmap.class, new SubnetmapKey(subnetId)).build(); try { - SubnetmapBuilder builder = null; - - InstanceIdentifier id = InstanceIdentifier.builder(Subnetmaps.class). - child(Subnetmap.class, new SubnetmapKey(subnetId)).build(); Optional sn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, id); logger.debug("updating Subnet :read: "); if (sn.isPresent()) { @@ -136,6 +127,9 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { logger.debug("updating Subnet :new: "); } + if (subnetIp != null) { + builder.setSubnetIp(subnetIp); + } if (routerId != null) { builder.setRouterId(routerId); } @@ -158,30 +152,29 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { builder.setPortList(portList); } - Subnetmap subnetmap = builder.build(); + subnetmap = builder.build(); + isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue()); + logger.debug("Creating/Updating subnetMap node: {} ", subnetId.getValue()); MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, subnetmap); - logger.debug("Created/Updated subnetmap node: {} ", subnetId.getValue()); - - return subnetmap; } catch (Exception e) { - logger.error("Update local subnetmap failed for node: {} {} {} {} {} {} ", - subnetId.getValue(), tenantId.getValue(), networkId.getValue(), routerId.getValue(), vpnId - .getValue(), portId.getValue()); - throw new RuntimeException(e); + logger.error("Updation of subnetMap failed for node: {}", subnetId.getValue()); + } finally { + if (isLockAcquired) { + NeutronvpnUtils.unlock(lockManager, subnetId.getValue()); + } } - + return subnetmap; } protected Subnetmap removeFromSubnetNode(Uuid subnetId, Uuid networkId, Uuid routerId, Uuid vpnId, Uuid portId) { Subnetmap subnetmap = null; + boolean isLockAcquired = false; + InstanceIdentifier id = InstanceIdentifier.builder(Subnetmaps.class). + child(Subnetmap.class, new SubnetmapKey(subnetId)).build(); try { - InstanceIdentifier id = InstanceIdentifier.builder(Subnetmaps.class). - child(Subnetmap.class, new SubnetmapKey(subnetId)).build(); Optional sn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, id); - if (sn.isPresent()) { SubnetmapBuilder builder = new SubnetmapBuilder(sn.get()); - if (routerId != null) { builder.setRouterId(null); } @@ -198,37 +191,56 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { } subnetmap = builder.build(); + isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue()); logger.debug("Removing from existing subnetmap node: {} ", subnetId.getValue()); MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, subnetmap); } else { - logger.warn("remove from non-existing subnetmap node: {} ", subnetId.getValue()); + logger.warn("removing from non-existing subnetmap node: {} ", subnetId.getValue()); } } catch (Exception e) { - logger.error("Remove from subnetmap failed for node: {} {} {} {} {} {} ", subnetId.getValue(), networkId - .getValue(), routerId.getValue(), vpnId.getValue(), portId.getValue()); - throw new RuntimeException(e); + logger.error("Removal from subnetmap failed for node: {}", subnetId.getValue()); + } finally { + if (isLockAcquired) { + NeutronvpnUtils.unlock(lockManager, subnetId.getValue()); + } } - return subnetmap; } - private void updateVpnInstanceNode(String name, List rd, List irt, List ert) { + protected void deleteSubnetMapNode(Uuid subnetId) { + boolean isLockAcquired = false; + InstanceIdentifier subnetMapIdentifier = InstanceIdentifier.builder(Subnetmaps.class) + .child(Subnetmap.class, new SubnetmapKey(subnetId)).build(); + logger.debug("removing subnetMap node: {} ", subnetId.getValue()); + try { + isLockAcquired = NeutronvpnUtils.lock(lockManager, subnetId.getValue()); + MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, subnetMapIdentifier); + } catch (Exception e) { + logger.error("Delete subnetMap node failed for subnet : {} ", subnetId.getValue()); + } finally { + if (isLockAcquired) { + NeutronvpnUtils.unlock(lockManager, subnetId.getValue()); + } + } + } + + private void updateVpnInstanceNode(String vpnName, List rd, List irt, List ert) { + VpnInstanceBuilder builder = null; + List vpnTargetList = new ArrayList(); + boolean isLockAcquired = false; + InstanceIdentifier vpnIdentifier = InstanceIdentifier.builder(VpnInstances.class). + child(VpnInstance.class, new VpnInstanceKey(vpnName)).build(); try { - VpnInstanceBuilder builder = null; - List vpnTargetList = new ArrayList(); - InstanceIdentifier vpnIdentifier = InstanceIdentifier.builder(VpnInstances.class). - child(VpnInstance.class, new VpnInstanceKey(name)).build(); Optional optionalVpn = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, vpnIdentifier); - logger.debug("Creating/Updating a new vpn-instance node: {} ", name); + logger.debug("Creating/Updating a new vpn-instance node: {} ", vpnName); if (optionalVpn.isPresent()) { builder = new VpnInstanceBuilder(optionalVpn.get()); logger.debug("updating existing vpninstance node"); } else { - builder = new VpnInstanceBuilder().setKey(new VpnInstanceKey(name)).setVpnInstanceName(name); + builder = new VpnInstanceBuilder().setKey(new VpnInstanceKey(vpnName)).setVpnInstanceName(vpnName); } - if (irt != null && !irt.isEmpty()) { if (ert != null && !ert.isEmpty()) { List commonRT = new ArrayList(irt); @@ -266,113 +278,164 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { } VpnInstance newVpn = builder.setIpv4Family(ipv4vpnBuilder.build()).build(); + isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnName); + logger.debug("Creating/Updating vpn-instance for {} ", vpnName); MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnIdentifier, newVpn); - logger.debug("Created/Updated vpn-instance for {} ", name); } catch (Exception e) { - logger.error("Update VPN Instance node failed for node: {} {} {} {}", name, rd, irt, ert); - throw new RuntimeException(e); + logger.error("Update VPN Instance node failed for node: {} {} {} {}", vpnName, rd, irt, ert); + } finally { + if (isLockAcquired) { + NeutronvpnUtils.unlock(lockManager, vpnName); + } } } private void deleteVpnMapsNode(Uuid vpnid) { + boolean isLockAcquired = false; InstanceIdentifier vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class) .child(VpnMap.class, new VpnMapKey(vpnid)).build(); logger.debug("removing vpnMaps node: {} ", vpnid.getValue()); - MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier); + try { + isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnid.getValue()); + MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier); + } catch (Exception e) { + logger.error("Delete vpnMaps node failed for vpn : {} ", vpnid.getValue()); + } finally { + if (isLockAcquired) { + NeutronvpnUtils.unlock(lockManager, vpnid.getValue()); + } + } } private void updateVpnMaps(Uuid vpnId, String name, Uuid router, Uuid tenantId, List networks) { VpnMapBuilder builder; + boolean isLockAcquired = false; InstanceIdentifier vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class) .child(VpnMap.class, new VpnMapKey(vpnId)).build(); - Optional optionalVpnMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, - vpnMapIdentifier); - if (optionalVpnMap.isPresent()) { - builder = new VpnMapBuilder(optionalVpnMap.get()); - } else { - builder = new VpnMapBuilder().setKey(new VpnMapKey(vpnId)).setVpnId(vpnId); - } + try { + Optional optionalVpnMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, + vpnMapIdentifier); + if (optionalVpnMap.isPresent()) { + builder = new VpnMapBuilder(optionalVpnMap.get()); + } else { + builder = new VpnMapBuilder().setKey(new VpnMapKey(vpnId)).setVpnId(vpnId); + } - if (name != null) { - builder.setName(name); - } - if (tenantId != null) { - builder.setTenantId(tenantId); - } - if (router != null) { - builder.setRouterId(router); - } - if (networks != null) { - List nwList = builder.getNetworkIds(); - if (nwList == null) { - nwList = new ArrayList(); + if (name != null) { + builder.setName(name); + } + if (tenantId != null) { + builder.setTenantId(tenantId); + } + if (router != null) { + builder.setRouterId(router); + } + if (networks != null) { + List nwList = builder.getNetworkIds(); + if (nwList == null) { + nwList = new ArrayList(); + } + nwList.addAll(networks); + builder.setNetworkIds(nwList); } - nwList.addAll(networks); - builder.setNetworkIds(nwList); - } - logger.debug("Creating/Updating vpnMaps node: {} ", vpnId.getValue()); - MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier, builder.build()); - logger.debug("VPNMaps DS updated for VPN {} ", vpnId.getValue()); + isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue()); + logger.debug("Creating/Updating vpnMaps node: {} ", vpnId.getValue()); + MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier, builder.build()); + logger.debug("VPNMaps DS updated for VPN {} ", vpnId.getValue()); + } catch (Exception e) { + logger.error("UpdateVpnMaps failed for node: {} ", vpnId.getValue()); + } finally { + if (isLockAcquired) { + NeutronvpnUtils.unlock(lockManager, vpnId.getValue()); + } + } } - private void clearFromVpnMaps(Uuid id, Uuid router, List networks) { + private void clearFromVpnMaps(Uuid vpnId, Uuid routerId, List networkIds) { + boolean isLockAcquired = false; InstanceIdentifier vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class) - .child(VpnMap.class, new VpnMapKey(id)).build(); + .child(VpnMap.class, new VpnMapKey(vpnId)).build(); Optional optionalVpnMap = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier); if (optionalVpnMap.isPresent()) { VpnMap vpnMap = optionalVpnMap.get(); VpnMapBuilder vpnMapBuilder = new VpnMapBuilder(vpnMap); - if (router != null) { - if (vpnMap.getNetworkIds() == null && router.equals(vpnMap.getVpnId())) { - // remove entire node in case of internal VPN - logger.debug("removing vpnMaps node: {} ", id); - MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier); + if (routerId != null) { + if (vpnMap.getNetworkIds() == null && routerId.equals(vpnMap.getVpnId())) { + try { + // remove entire node in case of internal VPN + isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue()); + logger.debug("removing vpnMaps node: {} ", vpnId); + MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier); + } catch (Exception e) { + logger.error("Deletion of vpnMaps node failed for vpn {}", vpnId.getValue()); + } finally { + if (isLockAcquired) { + NeutronvpnUtils.unlock(lockManager, vpnId.getValue()); + } + } return; } vpnMapBuilder.setRouterId(null); } - if (networks != null) { + if (networkIds != null) { List vpnNw = vpnMap.getNetworkIds(); - for (Uuid nw : networks) { + for (Uuid nw : networkIds) { vpnNw.remove(nw); } if (vpnNw.isEmpty()) { - logger.debug("setting networks null in vpnMaps node: {} ", id.getValue()); + logger.debug("setting networks null in vpnMaps node: {} ", vpnId.getValue()); vpnMapBuilder.setNetworkIds(null); } else { vpnMapBuilder.setNetworkIds(vpnNw); } } - logger.debug("clearing from vpnMaps node: {} ", id.getValue()); - MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier, vpnMapBuilder.build()); + try { + isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue()); + logger.debug("clearing from vpnMaps node: {} ", vpnId.getValue()); + MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnMapIdentifier, vpnMapBuilder.build + ()); + } catch (Exception e) { + logger.error("Clearing from vpnMaps node failed for vpn {}", vpnId.getValue()); + } finally { + if (isLockAcquired) { + NeutronvpnUtils.unlock(lockManager, vpnId.getValue()); + } + } } else { - logger.error("VPN : {} not found", id.getValue()); + logger.error("VPN : {} not found", vpnId.getValue()); } - logger.debug("VPNMaps DS clear success for VPN {} ", id.getValue()); + logger.debug("Clear from VPNMaps DS successful for VPN {} ", vpnId.getValue()); } private void deleteVpnInstance(Uuid vpnId) { - + boolean isLockAcquired = false; InstanceIdentifier vpnIdentifier = InstanceIdentifier.builder(VpnInstances.class). child(VpnInstance.class, new VpnInstanceKey(vpnId.getValue())).build(); - logger.debug("removing vpn Instance {}", vpnId.getValue()); - MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnIdentifier); + try { + isLockAcquired = NeutronvpnUtils.lock(lockManager, vpnId.getValue()); + logger.debug("Deleting vpnInstance {}", vpnId.getValue()); + MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnIdentifier); + } catch (Exception e) { + logger.error("Deletion of VPNInstance node failed for VPN {}", vpnId.getValue()); + } finally { + if (isLockAcquired) { + NeutronvpnUtils.unlock(lockManager, vpnId.getValue()); + } + } } - protected void createVpnInterface(Uuid vpnId, Port port) { - + boolean isLockAcquired = false; if (vpnId == null || port == null) { return; } - String portname = NeutronvpnUtils.uuidToTapPortName(port.getUuid()); - String name = new StringBuilder(portname).append(":0").toString(); + String infName = port.getUuid().getValue(); List adjList = new ArrayList(); - InstanceIdentifier vpnIfIdentifier = InstanceIdentifier.builder(VpnInterfaces.class). - child(VpnInterface.class, new VpnInterfaceKey(name)).build(); + InstanceIdentifier vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(infName); + // find router associated to vpn Uuid routerId = NeutronvpnUtils.getRouterforVpn(broker, vpnId); Router rtr = null; @@ -391,46 +454,77 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { adjList.add(vmAdj); // create extra route adjacency if (rtr != null && rtr.getRoutes() != null) { - List routeList = rtr.getRoutes(); - List erAdjList = addAdjacencyforExtraRoute(routeList, false, name); - if (erAdjList != null) { + List routeList = rtr.getRoutes(); + List erAdjList = addAdjacencyforExtraRoute(routeList, false, infName); + if (erAdjList != null && !erAdjList.isEmpty()) { adjList.addAll(erAdjList); } } } // create vpn-interface on this neutron port Adjacencies adjs = new AdjacenciesBuilder().setAdjacency(adjList).build(); - VpnInterfaceBuilder vpnb = new VpnInterfaceBuilder().setKey(new VpnInterfaceKey(name)). - setName(name).setVpnInstanceName(vpnId.getValue()).addAugmentation(Adjacencies.class, adjs); + VpnInterfaceBuilder vpnb = new VpnInterfaceBuilder().setKey(new VpnInterfaceKey(infName)). + setName(infName).setVpnInstanceName(vpnId.getValue()).addAugmentation(Adjacencies.class, adjs); VpnInterface vpnIf = vpnb.build(); - NeutronvpnUtils.lockVpnInterface(lockManager, name); try { + isLockAcquired = NeutronvpnUtils.lock(lockManager, infName); logger.debug("Creating vpn interface {}", vpnIf); MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf); } catch (Exception ex) { - logger.error("Creation of vpninterface {} failed due to {}", name, ex); + logger.error("Creation of vpninterface {} failed due to {}", infName, ex); } finally { - NeutronvpnUtils.unlockVpnInterface(lockManager, name); + if (isLockAcquired) { + NeutronvpnUtils.unlock(lockManager, infName); + } } } protected void deleteVpnInterface(Port port) { if (port != null) { - String pname = NeutronvpnUtils.uuidToTapPortName(port.getUuid()); - String name = new StringBuilder(pname).append(":0").toString(); - InstanceIdentifier vpnIfIdentifier = InstanceIdentifier.builder(VpnInterfaces.class). - child(VpnInterface.class, new VpnInterfaceKey(name)).build(); + boolean isLockAcquired = false; + String infName = port.getUuid().getValue(); + InstanceIdentifier vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(infName); - NeutronvpnUtils.lockVpnInterface(lockManager, name); try { - logger.debug("Deleting vpn interface {}", name); + isLockAcquired = NeutronvpnUtils.lock(lockManager, infName); + logger.debug("Deleting vpn interface {}", infName); MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier); } catch (Exception ex) { - logger.error("Deletion of vpninterface {} failed due to {}", name, ex); + logger.error("Deletion of vpninterface {} failed due to {}", infName, ex); } finally { - NeutronvpnUtils.unlockVpnInterface(lockManager, name); + if (isLockAcquired) { + NeutronvpnUtils.unlock(lockManager, infName); + } + } + } + } + + protected void updateVpnInterface(Uuid vpnId, Port port) { + if (vpnId == null || port == null) { + return; + } + boolean isLockAcquired = false; + String infName = port.getUuid().getValue(); + InstanceIdentifier vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(infName); + try { + Optional optionalVpnInterface = NeutronvpnUtils.read(broker, LogicalDatastoreType + .CONFIGURATION, vpnIfIdentifier); + if (optionalVpnInterface.isPresent()) { + VpnInterfaceBuilder vpnIfBuilder = new VpnInterfaceBuilder(optionalVpnInterface.get()); + VpnInterface vpnIf = vpnIfBuilder.setVpnInstanceName(vpnId.getValue()).build(); + isLockAcquired = NeutronvpnUtils.lock(lockManager, infName); + logger.debug("Updating vpn interface {}", vpnIf); + MDSALUtil.syncUpdate(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf); + } else { + logger.error("VPN Interface {} not found", infName); + } + } catch (Exception ex) { + logger.error("Updation of vpninterface {} failed due to {}", infName, ex); + } finally { + if (isLockAcquired) { + NeutronvpnUtils.unlock(lockManager, infName); } } } @@ -463,7 +557,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { List vpns = input.getL3vpn(); for (L3vpn vpn : vpns) { - RpcError error; + RpcError error = null; String msg; if (vpn.getRouteDistinguisher() == null || vpn.getImportRT() == null || vpn.getExportRT() == null) { msg = String.format("Creation of L3VPN failed for VPN %s due to absence of RD/iRT/eRT input", @@ -483,12 +577,57 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { warningcount++; continue; } + if (vpn.getRouterId() != null) { + if (NeutronvpnUtils.getNeutronRouter(broker, vpn.getRouterId()) == null) { + msg = String.format("Creation of L3VPN failed for VPN %s due to router not found %s", + vpn.getId().getValue(), vpn.getRouterId().getValue()); + logger.warn(msg); + error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg); + errorList.add(error); + warningcount++; + continue; + } + Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, vpn.getRouterId(), true); + if (vpnId != null) { + msg = String.format("Creation of L3VPN failed for VPN %s due to router %s already associated to " + + "another VPN %s", vpn.getId().getValue(), vpn.getRouterId().getValue(), vpnId.getValue()); + logger.warn(msg); + error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg); + errorList.add(error); + warningcount++; + continue; + } + } + if (vpn.getNetworkIds() != null) { + for (Uuid nw : vpn.getNetworkIds()) { + Network network = NeutronvpnUtils.getNeutronNetwork(broker, nw); + Uuid vpnId = NeutronvpnUtils.getVpnForNetwork(broker, nw); + if (network == null) { + msg = String.format("Creation of L3VPN failed for VPN %s due to network not found %s", + vpn.getId().getValue(), nw.getValue()); + logger.warn(msg); + error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg); + errorList.add(error); + warningcount++; + } else if (vpnId != null) { + msg = String.format("Creation of L3VPN failed for VPN %s due to network %s already associated" + + " to another VPN %s", vpn.getId().getValue(), nw.getValue(), vpnId.getValue()); + logger.warn(msg); + error = RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "invalid-input", msg); + errorList.add(error); + warningcount++; + } + } + if (error != null) { + continue; + } + } try { createL3Vpn(vpn.getId(), vpn.getName(), vpn.getTenantId(), vpn.getRouteDistinguisher(), vpn.getImportRT(), vpn.getExportRT(), vpn.getRouterId(), vpn.getNetworkIds()); } catch (Exception ex) { msg = String.format("Creation of L3VPN failed for VPN %s", vpn.getId().getValue()); - logger.error(msg, ex.getMessage()); + logger.error(msg, ex); error = RpcResultBuilder.newError(ErrorType.APPLICATION, msg, ex.getMessage()); errorList.add(error); failurecount++; @@ -530,9 +669,12 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { InstanceIdentifier.builder(VpnInstances.class).build(); Optional optionalVpns = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, vpnsIdentifier); - if (optionalVpns.isPresent()) { + if (optionalVpns.isPresent() && optionalVpns.get().getVpnInstance() != null) { for (VpnInstance vpn : optionalVpns.get().getVpnInstance()) { - vpns.add(vpn); + // eliminating internal VPNs from getL3VPN output + if (vpn.getIpv4Family().getRouteDistinguisher() != null) { + vpns.add(vpn); + } } } else { // No VPN present @@ -561,9 +703,8 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { for (VpnInstance vpnInstance : vpns) { Uuid vpnId = new Uuid(vpnInstance.getVpnInstanceName()); // create VpnMaps id - InstanceIdentifier vpnMapIdentifier = - InstanceIdentifier.builder(VpnMaps.class) - .child(VpnMap.class, new VpnMapKey(vpnId)).build(); + InstanceIdentifier vpnMapIdentifier = InstanceIdentifier.builder(VpnMaps.class).child(VpnMap + .class, new VpnMapKey(vpnId)).build(); L3vpnInstancesBuilder l3vpn = new L3vpnInstancesBuilder(); List rd = Arrays.asList(vpnInstance.getIpv4Family().getRouteDistinguisher().split(",")); @@ -601,10 +742,9 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { } catch (Exception ex) { String message = String.format("GetL3VPN failed due to %s", ex.getMessage()); - logger.error(message); + logger.error(message, ex); result.set(RpcResultBuilder.failed().withError(ErrorType.APPLICATION, message).build()); } - return result; } @@ -638,7 +778,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { } } catch (Exception ex) { msg = String.format("Deletion of L3VPN failed when deleting for uuid %s", vpn.getValue()); - logger.error(msg, ex.getMessage()); + logger.error(msg, ex); error = RpcResultBuilder.newError(ErrorType.APPLICATION, msg, ex.getMessage()); errorList.add(error); failurecount++; @@ -667,96 +807,145 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { protected void addSubnetToVpn(Uuid vpnId, Uuid subnet) { logger.debug("Adding subnet {} to vpn {}", subnet.getValue(), vpnId.getValue()); - Subnetmap sn = updateSubnetNode(subnet, null, null, null, vpnId, null); + Subnetmap sn = updateSubnetNode(subnet, null, null, null, null, vpnId, null); + boolean isLockAcquired = false; + String lockName = vpnId.getValue() + subnet.getValue(); + String elanInstanceName = sn.getNetworkId().getValue(); + InstanceIdentifierelanIdentifierId = InstanceIdentifier.builder(ElanInstances.class).child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build(); + Optional elanInstance = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, elanIdentifierId); + long elanTag = elanInstance.get().getElanTag(); + if(vpnId.equals(NeutronvpnUtils.getVpnMap(broker,vpnId).getRouterId())){isExternalVpn = false;} + else {isExternalVpn = true;} + try { + isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName); + checkAndPublishSubnetAddNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isExternalVpn, elanTag); + logger.debug("Subnet added to Vpn notification sent"); + }catch (Exception e){ + logger.error("Subnet added to Vpn notification failed",e); + }finally { + if (isLockAcquired) { + NeutronvpnUtils.unlock(lockManager, lockName); + } + } // Check if there are ports on this subnet and add corresponding vpn-interfaces List portList = sn.getPortList(); if (portList != null) { for (Uuid port : sn.getPortList()) { logger.debug("adding vpn-interface for port {}", port.getValue()); - createVpnInterface(vpnId, getNeutronPort(port)); + createVpnInterface(vpnId, NeutronvpnUtils.getNeutronPort(broker, port)); } } } - protected List addAdjacencyforExtraRoute(List routeList, boolean rtrUp, String vpnifname) { + protected void updateVpnForSubnet(Uuid vpnId, Uuid subnet, boolean isBeingAssociated) { + logger.debug("Updating VPN {} for subnet {}", vpnId.getValue(), subnet.getValue()); + Subnetmap sn = updateSubnetNode(subnet, null, null, null, null, vpnId, null); + boolean isLockAcquired = false; + String lockName = vpnId.getValue() + subnet.getValue(); + String elanInstanceName = sn.getNetworkId().getValue(); + InstanceIdentifierelanIdentifierId = InstanceIdentifier.builder(ElanInstances.class).child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build(); + Optional elanInstance = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, elanIdentifierId); + long elanTag = elanInstance.get().getElanTag(); try { - List adjList = new ArrayList(); - for (String route : routeList) { - // assuming extra route is strictly in the format "nexthop destination" > "10.1.1.10 40.0.1.0/24" - String[] parts = route.split(" "); - if (parts.length == 2) { - String nextHop = parts[0]; - String destination = parts[1]; - - String tapPortName = NeutronvpnUtils.getNeutronPortNamefromPortFixedIp(broker, nextHop); - String ifname = new StringBuilder(tapPortName).append(":0").toString(); - logger.trace("Adding extra route with nexthop {}, destination {}, ifName {}", nextHop, - destination, ifname); - Adjacency erAdj = new AdjacencyBuilder().setIpAddress(destination).setNextHopIp(nextHop).setKey - (new AdjacencyKey(destination)).build(); - if (rtrUp == false) { - if (ifname.equals(vpnifname)) { - adjList.add(erAdj); - } - continue; + isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName); + checkAndPublishSubnetUpdNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isBeingAssociated, elanTag); + logger.debug("Subnet updated in Vpn notification sent"); + }catch (Exception e){ + logger.error("Subnet updated in Vpn notification failed",e); + }finally { + if (isLockAcquired) { + NeutronvpnUtils.unlock(lockManager, lockName); + } + } + // Check for ports on this subnet and update association of corresponding vpn-interfaces to external vpn + List portList = sn.getPortList(); + if (portList != null) { + for (Uuid port : sn.getPortList()) { + logger.debug("Updating vpn-interface for port {}", port.getValue()); + updateVpnInterface(vpnId, NeutronvpnUtils.getNeutronPort(broker, port)); + } + } + } + + protected List addAdjacencyforExtraRoute(List routeList, boolean rtrUp, String vpnifname) { + List adjList = new ArrayList(); + for (Routes route : routeList) { + if (route != null && route.getNexthop() != null && route.getDestination() != null) { + boolean isLockAcquired = false; + String nextHop = String.valueOf(route.getNexthop().getValue()); + String destination = String.valueOf(route.getDestination().getValue()); + + String infName = NeutronvpnUtils.getNeutronPortNamefromPortFixedIp(broker, nextHop); + logger.trace("Adding extra route with nexthop {}, destination {}, infName {}", nextHop, + destination, infName); + Adjacency erAdj = new AdjacencyBuilder().setIpAddress(destination).setNextHopIp(nextHop).setKey + (new AdjacencyKey(destination)).build(); + if (rtrUp == false) { + if (infName.equals(vpnifname)) { + adjList.add(erAdj); } - InstanceIdentifier vpnIfIdentifier = InstanceIdentifier.builder(VpnInterfaces.class). - child(VpnInterface.class, new VpnInterfaceKey(ifname)).build(); + continue; + } + InstanceIdentifier vpnIfIdentifier = InstanceIdentifier.builder(VpnInterfaces.class). + child(VpnInterface.class, new VpnInterfaceKey(infName)).build(); + try { Optional optionalVpnInterface = NeutronvpnUtils.read(broker, LogicalDatastoreType .CONFIGURATION, vpnIfIdentifier); if (optionalVpnInterface.isPresent()) { Adjacencies erAdjs = new AdjacenciesBuilder().setAdjacency(Arrays.asList(erAdj)).build(); - VpnInterface vpnIf = new VpnInterfaceBuilder().setKey(new VpnInterfaceKey(ifname)) + VpnInterface vpnIf = new VpnInterfaceBuilder().setKey(new VpnInterfaceKey(infName)) .addAugmentation(Adjacencies.class, erAdjs).build(); + isLockAcquired = NeutronvpnUtils.lock(lockManager, infName); + logger.debug("Adding extra route {}", route); MDSALUtil.syncUpdate(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf); - logger.trace("extra route {} added successfully", route); } else { logger.error("VM adjacency for interface {} not present ; cannot add extra route adjacency", - ifname); + infName); + } + } catch (Exception e) { + logger.error("exception in adding extra route: {}" + e); + } finally { + if (isLockAcquired) { + NeutronvpnUtils.unlock(lockManager, infName); } - } else { - logger.error("Incorrect input received for extra route. {}", parts); } + } else { + logger.error("Incorrect input received for extra route. {}", route); } - return adjList; - } catch (Exception e) { - logger.error("exception in adding extra route: {}" + e); } - return null; + return adjList; } - protected void removeAdjacencyforExtraRoute(List routeList) { - try { - for (String route : routeList) { - // assuming extra route is strictly in the format "nexthop destination" > "10.1.1.10 40.0.1.0/24" - String[] parts = route.split(" "); - if (parts.length == 2) { - String nextHop = parts[0]; - String destination = parts[1]; - - String tapPortName = NeutronvpnUtils.getNeutronPortNamefromPortFixedIp(broker, nextHop); - String ifname = new StringBuilder(tapPortName).append(":0").toString(); - logger.trace("Removing extra route with nexthop {}, destination {}, ifName {}", nextHop, - destination, ifname); - InstanceIdentifier adjacencyIdentifier = InstanceIdentifier.builder(VpnInterfaces.class). - child(VpnInterface.class, new VpnInterfaceKey(ifname)).augmentation(Adjacencies.class) - .child(Adjacency.class, new AdjacencyKey(destination)).build(); + protected void removeAdjacencyforExtraRoute(List routeList) { + for (Routes route : routeList) { + if (route != null && route.getNexthop() != null && route.getDestination() != null) { + boolean isLockAcquired = false; + String nextHop = String.valueOf(route.getNexthop().getValue()); + String destination = String.valueOf(route.getDestination().getValue()); + + String infName = NeutronvpnUtils.getNeutronPortNamefromPortFixedIp(broker, nextHop); + logger.trace("Removing extra route with nexthop {}, destination {}, infName {}", nextHop, + destination, infName); + InstanceIdentifier adjacencyIdentifier = InstanceIdentifier.builder(VpnInterfaces.class). + child(VpnInterface.class, new VpnInterfaceKey(infName)).augmentation(Adjacencies.class) + .child(Adjacency.class, new AdjacencyKey(destination)).build(); + try { + isLockAcquired = NeutronvpnUtils.lock(lockManager, infName); MDSALUtil.syncDelete(broker, LogicalDatastoreType.CONFIGURATION, adjacencyIdentifier); logger.trace("extra route {} deleted successfully", route); - } else { - logger.error("Incorrect input received for extra route. {}", parts); + } catch (Exception e) { + logger.error("exception in deleting extra route: {}" + e); + } finally { + if (isLockAcquired) { + NeutronvpnUtils.unlock(lockManager, infName); + } } + } else { + logger.error("Incorrect input received for extra route. {}", route); } - } catch (Exception e) { - logger.error("exception in deleting extra route: {}" + e); } } - protected void addPortToVpn(Uuid vpnId, Uuid port) { - logger.debug("Adding Port to vpn node..."); - createVpnInterface(vpnId, getNeutronPort(port)); - } - protected void removeL3Vpn(Uuid id) { // read VPNMaps VpnMap vpnMap = NeutronvpnUtils.getVpnMap(broker, id); @@ -776,21 +965,35 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { deleteVpnInstance(id); } - protected void removePortFromVpn(Uuid vpnId, Uuid port) { - logger.debug("Removing Port from vpn node..."); - deleteVpnInterface(getNeutronPort(port)); - } - protected void removeSubnetFromVpn(Uuid vpnId, Uuid subnet) { logger.debug("Removing subnet {} from vpn {}", subnet.getValue(), vpnId.getValue()); Subnetmap sn = NeutronvpnUtils.getSubnetmap(broker, subnet); + boolean isLockAcquired = false; + String lockName = vpnId.getValue() + subnet.getValue(); + String elanInstanceName = sn.getNetworkId().getValue(); + InstanceIdentifierelanIdentifierId = InstanceIdentifier.builder(ElanInstances.class).child(ElanInstance.class, new ElanInstanceKey(elanInstanceName)).build(); + Optional elanInstance = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, elanIdentifierId); + long elanTag = elanInstance.get().getElanTag(); + if(vpnId.equals(NeutronvpnUtils.getVpnMap(broker,vpnId).getRouterId())){isExternalVpn = false;} + else {isExternalVpn = true;} + try { + isLockAcquired = NeutronvpnUtils.lock(lockManager, lockName); + checkAndPublishSubnetDelNotification(subnet, sn.getSubnetIp(), vpnId.getValue(), isExternalVpn, elanTag); + logger.debug("Subnet removed from Vpn notification sent"); + }catch (Exception e){ + logger.error("Subnet removed from Vpn notification failed",e); + }finally { + if (isLockAcquired) { + NeutronvpnUtils.unlock(lockManager, lockName); + } + } if (sn != null) { // Check if there are ports on this subnet; remove corresponding vpn-interfaces List portList = sn.getPortList(); if (portList != null) { for (Uuid port : sn.getPortList()) { logger.debug("removing vpn-interface for port {}", port.getValue()); - deleteVpnInterface(getNeutronPort(port)); + deleteVpnInterface(NeutronvpnUtils.getNeutronPort(broker, port)); } } // update subnet-vpn association @@ -801,36 +1004,30 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { } protected void associateRouterToVpn(Uuid vpnId, Uuid routerId) { - + updateVpnMaps(vpnId, null, routerId, null, null); List routerSubnets = NeutronvpnUtils.getNeutronRouterSubnetIds(broker, routerId); - if (!vpnId.equals(routerId)) { - logger.debug("Removing subnets from internal vpn {}", routerId.getValue()); + logger.debug("Updating association of subnets to external vpn {}", vpnId.getValue()); if (routerSubnets != null) { - for (Uuid subnet : routerSubnets) { - removeSubnetFromVpn(routerId, subnet); + for (Uuid subnetId : routerSubnets) { + updateVpnForSubnet(vpnId, subnetId,true); } } + } else { + logger.debug("Adding subnets to internal vpn {}", vpnId.getValue()); + for (Uuid subnet : routerSubnets) { + addSubnetToVpn(vpnId, subnet); + } } - logger.debug("Adding subnets to vpn {}", vpnId.getValue()); - for (Uuid subnet : routerSubnets) { - addSubnetToVpn(vpnId, subnet); - } - - updateVpnMaps(vpnId, null, routerId, null, null); } protected void dissociateRouterFromVpn(Uuid vpnId, Uuid routerId) { - // remove existing external vpn interfaces List routerSubnets = NeutronvpnUtils.getNeutronRouterSubnetIds(broker, routerId); - if (routerSubnets != null) { - for (Uuid subnet : routerSubnets) { - logger.debug("Removing subnets from external vpn {}", vpnId.getValue()); - removeSubnetFromVpn(vpnId, subnet); - logger.debug("Adding subnets to internal vpn {}", routerId.getValue()); - addSubnetToVpn(routerId, subnet); + for (Uuid subnetId : routerSubnets) { + logger.debug("Updating association of subnets to internal vpn {}", routerId.getValue()); + updateVpnForSubnet(routerId, subnetId,false); } } clearFromVpnMaps(vpnId, routerId, null); @@ -917,7 +1114,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { } catch (Exception ex) { String message = String.format("associate Networks to vpn %s failed due to %s", input.getVpnId().getValue(), ex.getMessage()); - logger.error(message); + logger.error(message, ex); result.set(RpcResultBuilder.failed().withError(ErrorType.APPLICATION, message) .build()); } @@ -962,7 +1159,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { } catch (Exception ex) { String message = String.format("associate router %s to vpn %s failed due to %s", routerId.getValue(), vpnId.getValue(), ex.getMessage()); - logger.error(message); + logger.error(message, ex); result.set(RpcResultBuilder.failed().withError(ErrorType.APPLICATION, message).build()); } logger.debug("associateRouter returns.."); @@ -992,7 +1189,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { returnMsg.append("VPN not found : ").append(vpnId.getValue()); } if (returnMsg.length() != 0) { - String message = String.format("disssociate Networks to vpn %s failed due to %s", vpnId.getValue(), + String message = String.format("dissociate Networks to vpn %s failed due to %s", vpnId.getValue(), returnMsg); logger.error(message); String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " + @@ -1005,7 +1202,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { } catch (Exception ex) { String message = String.format("dissociate Networks to vpn %s failed due to %s", input.getVpnId(). getValue(), ex.getMessage()); - logger.error(message); + logger.error(message, ex); result.set(RpcResultBuilder.failed().withError(ErrorType.APPLICATION, message) .build()); } @@ -1036,7 +1233,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { returnMsg.append("VPN not found : ").append(vpnId.getValue()); } if (returnMsg.length() != 0) { - String message = String.format("disssociate router %s to vpn %s failed due to %s", routerId.getValue(), + String message = String.format("dissociate router %s to vpn %s failed due to %s", routerId.getValue(), vpnId.getValue(), returnMsg); logger.error(message); String errorResponse = String.format("ErrorType: PROTOCOL, ErrorTag: invalid-value, ErrorMessage: " + @@ -1049,7 +1246,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { } catch (Exception ex) { String message = String.format("disssociate router %s to vpn %s failed due to %s", routerId.getValue(), vpnId.getValue(), ex.getMessage()); - logger.error(message); + logger.error(message, ex); result.set(RpcResultBuilder.failed().withError(ErrorType.APPLICATION, message).build()); } logger.debug("dissociateRouter returns.."); @@ -1057,6 +1254,45 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { return result; } + @Override + public Future> getFixedIPsForNeutronPort(GetFixedIPsForNeutronPortInput + input) { + GetFixedIPsForNeutronPortOutputBuilder opBuilder = new GetFixedIPsForNeutronPortOutputBuilder(); + SettableFuture> result = SettableFuture.create(); + Uuid portId = input.getPortId(); + StringBuilder returnMsg = new StringBuilder(); + try { + List fixedIPList = new ArrayList<>(); + Port port = NeutronvpnUtils.getNeutronPort(broker, portId); + if (port != null) { + List fixedIPs = port.getFixedIps(); + for (FixedIps ip : fixedIPs) { + fixedIPList.add(ip.getIpAddress().getIpv4Address().getValue()); + } + } else { + returnMsg.append("neutron port: ").append(portId.getValue()).append(" not found"); + } + if (returnMsg.length() != 0) { + String message = String.format("Retrieval of FixedIPList for neutron port failed due to %s", returnMsg); + logger.error(message); + result.set(RpcResultBuilder.failed().withWarning(ErrorType.PROTOCOL, + "invalid-value", message).build()); + } else { + opBuilder.setFixedIPs(fixedIPList); + result.set(RpcResultBuilder.success().withResult(opBuilder.build()) + .build()); + result.set(RpcResultBuilder.success().build()); + } + } catch (Exception ex) { + String message = String.format("Retrieval of FixedIPList for neutron port %s failed due to %s", portId + .getValue(), ex.getMessage()); + logger.error(message, ex); + result.set(RpcResultBuilder.failed().withError(ErrorType.APPLICATION, + message).build()); + } + return result; + } + protected void handleNeutronRouterDeleted(Uuid routerId, List routerSubnetIds) { // check if the router is associated to some VPN Uuid vpnId = NeutronvpnUtils.getVpnForRouter(broker, routerId, true); @@ -1099,28 +1335,11 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { } protected Port getNeutronPort(String name) { - Uuid portId = NeutronvpnUtils.getNeutronPortIdfromPortName(broker, name); - if (portId != null) { - InstanceIdentifier pid = InstanceIdentifier.create(Neutron.class). - child(Ports.class).child(Port.class, new PortKey(portId)); - Optional optPort = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, pid); - if (optPort.isPresent()) { - return optPort.get(); - } - } else { - logger.error("Port {} not Found!!", name); - } - return null; + return NeutronvpnUtils.getNeutronPort(broker, new Uuid(name)); } protected Port getNeutronPort(Uuid portId) { - InstanceIdentifier pid = InstanceIdentifier.create(Neutron.class). - child(Ports.class).child(Port.class, new PortKey(portId)); - Optional optPort = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, pid); - if (optPort.isPresent()) { - return optPort.get(); - } - return null; + return NeutronvpnUtils.getNeutronPort(broker, portId); } protected List getSubnetsforVpn(Uuid vpnid) { @@ -1129,9 +1348,8 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { InstanceIdentifier subnetmapsid = InstanceIdentifier.builder(Subnetmaps.class).build(); Optional subnetmaps = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, subnetmapsid); - if (subnetmaps.isPresent()) { - Subnetmaps smaps = subnetmaps.get(); - List subnetMapList = smaps.getSubnetmap(); + if (subnetmaps.isPresent() && subnetmaps.get().getSubnetmap() != null) { + List subnetMapList = subnetmaps.get().getSubnetmap(); for (Subnetmap subnetMap : subnetMapList) { if (subnetMap.getVpnId() != null && subnetMap.getVpnId().equals(vpnid)) { subnets.add(subnetMap.getId()); @@ -1143,22 +1361,22 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { public List showNeutronPortsCLI() { List result = new ArrayList(); - result.add(String.format(" %-22s %-22s %-22s %-6s ", "PortName", "Mac Address", "IP Address", + result.add(String.format(" %-34s %-22s %-22s %-6s ", "PortName", "Mac Address", "IP Address", "Prefix Length")); result.add("---------------------------------------------------------------------------------------"); InstanceIdentifier portidentifier = InstanceIdentifier.create(Neutron.class).child(Ports.class); try { Optional ports = NeutronvpnUtils.read(broker, LogicalDatastoreType.CONFIGURATION, portidentifier); - if (ports.isPresent()) { + if (ports.isPresent() && ports.get().getPort() != null) { List portList = ports.get().getPort(); for (Port port : portList) { - result.add(String.format(" %-22s %-22s %-22s %-6s ", NeutronvpnUtils.uuidToTapPortName(port - .getUuid()), port.getMacAddress(), port.getFixedIps().get(0).getIpAddress().getIpv4Address() - .getValue(), NeutronvpnUtils.getIPPrefixFromPort(broker, port))); + result.add(String.format(" %-34s %-22s %-22s %-6s ", port.getUuid().getValue(), port + .getMacAddress(), port.getFixedIps().get(0).getIpAddress().getIpv4Address().getValue(), + NeutronvpnUtils.getIPPrefixFromPort(broker, port))); } } } catch (Exception e) { - logger.trace("Failed to retrieve neutronPorts info : ", e); + logger.error("Failed to retrieve neutronPorts info : ", e); System.out.println("Failed to retrieve neutronPorts info : " + e.getMessage()); } return result; @@ -1225,7 +1443,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { } } } catch (InterruptedException | ExecutionException e) { - logger.trace("error getting VPN info : ", e); + logger.error("error getting VPN info : ", e); System.out.println("error getting VPN info : " + e.getMessage()); } return result; @@ -1237,4 +1455,46 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { return help.toString(); } + private void checkAndPublishSubnetAddNotification(Uuid subnetId, String subnetIp, String vpnName, Boolean isExternalvpn, Long elanTag)throws InterruptedException{ + SubnetAddedToVpnBuilder builder = new SubnetAddedToVpnBuilder(); + + logger.info("publish notification called"); + + builder.setSubnetId(subnetId); + builder.setSubnetIp(subnetIp); + builder.setVpnName(vpnName); + builder.setExternalVpn(isExternalvpn); + builder.setElanTag(elanTag); + + notificationPublishService.putNotification(builder.build()); + } + + private void checkAndPublishSubnetDelNotification(Uuid subnetId, String subnetIp, String vpnName, Boolean isExternalvpn, Long elanTag)throws InterruptedException{ + SubnetDeletedFromVpnBuilder builder = new SubnetDeletedFromVpnBuilder(); + + logger.info("publish notification called"); + + builder.setSubnetId(subnetId); + builder.setSubnetIp(subnetIp); + builder.setVpnName(vpnName); + builder.setExternalVpn(isExternalvpn); + builder.setElanTag(elanTag); + + notificationPublishService.putNotification(builder.build()); + } + + private void checkAndPublishSubnetUpdNotification(Uuid subnetId, String subnetIp, String vpnName, Boolean isExternalvpn, Long elanTag)throws InterruptedException{ + SubnetUpdatedInVpnBuilder builder = new SubnetUpdatedInVpnBuilder(); + + logger.info("publish notification called"); + + builder.setSubnetId(subnetId); + builder.setSubnetIp(subnetIp); + builder.setVpnName(vpnName); + builder.setExternalVpn(isExternalvpn); + builder.setElanTag(elanTag); + + notificationPublishService.putNotification(builder.build()); + } + }