From 40795958f8ae484ab18f9fadba15ffd49a194f04 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Thu, 4 Feb 2016 17:22:23 +0530 Subject: [PATCH] Bug 5229 - SnIP attr add to SubnetMaps yang; handle neutron subnet deletion 1. Addition of subnetIP attr to SubnetMaps DS for SNAT use case 2. VPN interface update on internal/external VPN assc/dissc handling 3. Fixed issue with neutron subnet deletion 4. Logging changes to catch error exception completely 5. Refactoring in usage of locks 6. Additional null checks for array OoBEs Change-Id: Iec01e8e4e8036ebff37408ecf59e6c8899889dd8 Signed-off-by: Abhinav Gupta --- .../src/main/yang/neutronvpn.yang | 5 + .../neutronvpn/NeutronPortChangeListener.java | 13 +- .../NeutronRouterChangeListener.java | 4 +- .../NeutronSubnetChangeListener.java | 9 +- .../neutronvpn/NeutronvpnManager.java | 142 ++++++++++++------ .../neutronvpn/NeutronvpnUtils.java | 42 +++--- .../vpnservice/VpnInterfaceManager.java | 73 +++------ 7 files changed, 159 insertions(+), 129 deletions(-) diff --git a/neutronvpn/neutronvpn-api/src/main/yang/neutronvpn.yang b/neutronvpn/neutronvpn-api/src/main/yang/neutronvpn.yang index bc306bbf..470e9f0a 100644 --- a/neutronvpn/neutronvpn-api/src/main/yang/neutronvpn.yang +++ b/neutronvpn/neutronvpn-api/src/main/yang/neutronvpn.yang @@ -18,6 +18,11 @@ module neutronvpn { description "UUID representing the subnet "; } + leaf subnet-ip { + type string; + description "Specifies the subnet IP in CIDR format"; + } + leaf tenant-id { type yang:uuid; description "The UUID of the tenant that will own the subnet."; diff --git a/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronPortChangeListener.java b/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronPortChangeListener.java index 821035ed..e88b1be0 100644 --- a/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronPortChangeListener.java +++ b/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronPortChangeListener.java @@ -133,7 +133,7 @@ public class NeutronPortChangeListener extends AbstractDataChangeListener // Create of-port interface for this neutron port createOfPortInterface(port, portVlanId); LOG.debug("Creating ELAN Interface"); - createElanInterface(port, portVlanId); + createElanInterface(port); LOG.debug("Add port to subnet"); // add port to local Subnets DS Uuid vpnId = addPortToSubnets(port); @@ -195,8 +195,8 @@ public class NeutronPortChangeListener extends AbstractDataChangeListener // handle these for trunkport extensions : portVlanId, isVlanTransparent IfL2vlan l2vlan = new IfL2vlanBuilder().setL2vlanMode(IfL2vlan.L2vlanMode.Trunk).build(); ParentRefs parentRefs = new ParentRefsBuilder().setParentInterface(name).build(); - Interface inf = new InterfaceBuilder().setEnabled(true).setName(name).setType(L2vlan.class). - addAugmentation(IfL2vlan.class, l2vlan).addAugmentation(ParentRefs.class, parentRefs).build(); + Interface inf = new InterfaceBuilder().setEnabled(true).setName(name).setType(L2vlan.class) + .addAugmentation(IfL2vlan.class, l2vlan).addAugmentation(ParentRefs.class, parentRefs).build(); MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, interfaceIdentifier, inf); } else { LOG.error("Interface {} is already present", name); @@ -234,7 +234,7 @@ public class NeutronPortChangeListener extends AbstractDataChangeListener .getUuid()); } - private void createElanInterface(Port port, int portVlanId) { + private void createElanInterface(Port port) { String name = NeutronvpnUtils.uuidToTapPortName(port.getUuid()); String elanInstanceName = port.getNetworkId().getValue(); List physAddresses = new ArrayList<>(); @@ -243,8 +243,7 @@ public class NeutronPortChangeListener extends AbstractDataChangeListener InstanceIdentifier id = InstanceIdentifier.builder(ElanInterfaces.class).child(ElanInterface .class, new ElanInterfaceKey(name)).build(); ElanInterface elanInterface = new ElanInterfaceBuilder().setElanInstanceName(elanInstanceName) - .setName(name).setStaticMacEntries(physAddresses). - setKey(new ElanInterfaceKey(name)).build(); + .setName(name).setStaticMacEntries(physAddresses).setKey(new ElanInterfaceKey(name)).build(); MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, id, elanInterface); LOG.debug("Creating new ELan Interface {}", elanInterface); } @@ -268,7 +267,7 @@ public class NeutronPortChangeListener extends AbstractDataChangeListener ipValue, name); subnetId = ip.getSubnetId(); - Subnetmap subnetmap = nvpnManager.updateSubnetNode(subnetId, null, null, null, null, port.getUuid()); + Subnetmap subnetmap = nvpnManager.updateSubnetNode(subnetId, null, null, null, null, null, port.getUuid()); if (vpnId == null && subnetmap != null) { vpnId = subnetmap.getVpnId(); } diff --git a/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronRouterChangeListener.java b/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronRouterChangeListener.java index 9da9f7d4..b2b46a94 100644 --- a/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronRouterChangeListener.java +++ b/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronRouterChangeListener.java @@ -7,15 +7,13 @@ */ package org.opendaylight.vpnservice.neutronvpn; - -import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.l3.attributes.Routes; - 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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid; +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; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router; import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.Interfaces; diff --git a/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronSubnetChangeListener.java b/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronSubnetChangeListener.java index bea68b9a..9a6fa4e1 100644 --- a/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronSubnetChangeListener.java +++ b/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronSubnetChangeListener.java @@ -76,7 +76,7 @@ public class NeutronSubnetChangeListener extends AbstractDataChangeListener 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; @@ -423,8 +442,8 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { } String portname = NeutronvpnUtils.uuidToTapPortName(port.getUuid()); List adjList = new ArrayList(); - InstanceIdentifier vpnIfIdentifier = InstanceIdentifier.builder(VpnInterfaces.class). - child(VpnInterface.class, new VpnInterfaceKey(portname)).build(); + InstanceIdentifier vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(portname); + // find router associated to vpn Uuid routerId = NeutronvpnUtils.getRouterforVpn(broker, vpnId); Router rtr = null; @@ -474,8 +493,8 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { if (port != null) { boolean isLockAcquired = false; String pname = NeutronvpnUtils.uuidToTapPortName(port.getUuid()); - InstanceIdentifier vpnIfIdentifier = InstanceIdentifier.builder(VpnInterfaces.class). - child(VpnInterface.class, new VpnInterfaceKey(pname)).build(); + InstanceIdentifier vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(pname); + try { isLockAcquired = NeutronvpnUtils.lock(lockManager, pname); logger.debug("Deleting vpn interface {}", pname); @@ -490,6 +509,35 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { } } + protected void updateVpnInterface(Uuid vpnId, Port port) { + if (vpnId == null || port == null) { + return; + } + boolean isLockAcquired = false; + String portname = NeutronvpnUtils.uuidToTapPortName(port.getUuid()); + String ifname = new StringBuilder(portname).append(":0").toString(); + InstanceIdentifier vpnIfIdentifier = NeutronvpnUtils.buildVpnInterfaceIdentifier(ifname); + 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, ifname); + logger.debug("Updating vpn interface {}", vpnIf); + MDSALUtil.syncUpdate(broker, LogicalDatastoreType.CONFIGURATION, vpnIfIdentifier, vpnIf); + } else { + logger.error("VPN Interface {} not found", ifname); + } + } catch (Exception ex) { + logger.error("Updation of vpninterface {} failed due to {}", ifname, ex); + } finally { + if (isLockAcquired) { + NeutronvpnUtils.unlock(lockManager, ifname); + } + } + } + public void createL3Vpn(Uuid vpn, String name, Uuid tenant, List rd, List irt, List ert, Uuid router, List networks) { @@ -543,7 +591,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { 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++; @@ -585,7 +633,7 @@ 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); } @@ -616,9 +664,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(",")); @@ -656,10 +703,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; } @@ -693,7 +739,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++; @@ -722,7 +768,7 @@ 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); // Check if there are ports on this subnet and add corresponding vpn-interfaces List portList = sn.getPortList(); if (portList != null) { @@ -733,6 +779,19 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { } } + protected void updateVpnForSubnet(Uuid vpnId, Uuid subnet) { + logger.debug("Updating VPN {} for subnet {}", vpnId.getValue(), subnet.getValue()); + Subnetmap sn = updateSubnetNode(subnet, null, null, null, null, vpnId, null); + // 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, getNeutronPort(port)); + } + } + } + protected List addAdjacencyforExtraRoute(List routeList, boolean rtrUp, String vpnifname) { List adjList = new ArrayList(); for (Routes route : routeList) { @@ -861,36 +920,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); } } + } 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); } } clearFromVpnMaps(vpnId, routerId, null); @@ -977,7 +1030,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()); } @@ -1022,7 +1075,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.."); @@ -1052,7 +1105,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: " + @@ -1065,7 +1118,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()); } @@ -1096,7 +1149,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: " + @@ -1109,7 +1162,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.."); @@ -1189,9 +1242,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()); @@ -1209,7 +1261,7 @@ public class NeutronvpnManager implements NeutronvpnService, AutoCloseable { 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 diff --git a/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronvpnUtils.java b/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronvpnUtils.java index 71d11541..d7adab8b 100644 --- a/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronvpnUtils.java +++ b/neutronvpn/neutronvpn-impl/src/main/java/org/opendaylight/vpnservice/neutronvpn/NeutronvpnUtils.java @@ -13,6 +13,9 @@ import com.google.common.base.Optional; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.VpnInterfaces; +import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterface; +import org.opendaylight.yang.gen.v1.urn.huawei.params.xml.ns.yang.l3vpn.rev140815.vpn.interfaces.VpnInterfaceKey; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey; @@ -95,9 +98,8 @@ public class NeutronvpnUtils { protected static Uuid getVpnForNetwork(DataBroker broker, Uuid network) { InstanceIdentifier vpnMapsIdentifier = InstanceIdentifier.builder(VpnMaps.class).build(); Optional optionalVpnMaps = read(broker, LogicalDatastoreType.CONFIGURATION, vpnMapsIdentifier); - if (optionalVpnMaps.isPresent()) { - VpnMaps vpnMaps = optionalVpnMaps.get(); - List allMaps = vpnMaps.getVpnMap(); + if (optionalVpnMaps.isPresent() && optionalVpnMaps.get().getVpnMap() != null) { + List allMaps = optionalVpnMaps.get().getVpnMap(); for (VpnMap vpnMap : allMaps) { if (vpnMap.getNetworkIds().contains(network)) { return vpnMap.getVpnId(); @@ -112,9 +114,8 @@ public class NeutronvpnUtils { InstanceIdentifier vpnMapsIdentifier = InstanceIdentifier.builder(VpnMaps.class).build(); Optional optionalVpnMaps = read(broker, LogicalDatastoreType.CONFIGURATION, vpnMapsIdentifier); - if (optionalVpnMaps.isPresent()) { - VpnMaps vpnNets = optionalVpnMaps.get(); - List allMaps = vpnNets.getVpnMap(); + if (optionalVpnMaps.isPresent() && optionalVpnMaps.get().getVpnMap() != null) { + List allMaps = optionalVpnMaps.get().getVpnMap(); if (routerId != null) { for (VpnMap vpnMap : allMaps) { if (routerId.equals(vpnMap.getRouterId())) { @@ -238,37 +239,38 @@ public class NeutronvpnUtils { protected static boolean lock(LockManagerService lockManager, String lockName) { TryLockInput input = new TryLockInputBuilder().setLockName(lockName).setTime(5L).setTimeUnit (TimeUnits.Milliseconds).build(); - boolean islockAquired = false; + boolean islockAcquired = false; try { Future> result = lockManager.tryLock(input); if ((result != null) && (result.get().isSuccessful())) { logger.debug("Acquired lock for {}", lockName); - islockAquired = true; + islockAcquired = true; } else { - throw new RuntimeException(String.format("Unable to acquire lock for %s", lockName)); + logger.error("Unable to acquire lock for {}", lockName); } } catch (InterruptedException | ExecutionException e) { logger.error("Unable to acquire lock for {}", lockName); - throw new RuntimeException(String.format("Unable to acquire lock for %s", lockName), e - .getCause()); + throw new RuntimeException(String.format("Unable to acquire lock for %s", lockName), e.getCause()); } - return islockAquired; + return islockAcquired; } - protected static void unlock(LockManagerService lockManager, String lockName) { + protected static boolean unlock(LockManagerService lockManager, String lockName) { UnlockInput input = new UnlockInputBuilder().setLockName(lockName).build(); + boolean islockAcquired = false; try { Future> result = lockManager.unlock(input); if ((result != null) && (result.get().isSuccessful())) { logger.debug("Unlocked {}", lockName); + islockAcquired = true; } else { - logger.debug("Unable to unlock {}", lockName); + logger.error("Unable to unlock {}", lockName); } } catch (InterruptedException | ExecutionException e) { logger.error("Unable to unlock {}", lockName); - throw new RuntimeException(String.format("Unable to unlock %s", lockName), e - .getCause()); + throw new RuntimeException(String.format("Unable to unlock %s", lockName), e.getCause()); } + return islockAcquired; } protected static Short getIPPrefixFromPort(DataBroker broker, Port port) { @@ -296,7 +298,7 @@ public class NeutronvpnUtils { logger.trace("Unable to read on subnet datastore"); } } catch (Exception e) { - logger.trace("Failed to retrieve IP prefix from port : ", e); + logger.error("Failed to retrieve IP prefix from port : ", e); System.out.println("Failed to retrieve IP prefix from port : " + e.getMessage()); } return null; @@ -320,6 +322,12 @@ public class NeutronvpnUtils { return id; } + static InstanceIdentifier buildVpnInterfaceIdentifier(String ifName) { + InstanceIdentifier id = InstanceIdentifier.builder(VpnInterfaces.class). + child(VpnInterface.class, new VpnInterfaceKey(ifName)).build(); + return id; + } + static InstanceIdentifier buildSubnetMapIdentifier(Uuid subnetId) { InstanceIdentifier id = InstanceIdentifier.builder(Subnetmaps.class).child(Subnetmap.class, new SubnetmapKey(subnetId)).build(); diff --git a/vpnmanager/vpnmanager-impl/src/main/java/org/opendaylight/vpnservice/VpnInterfaceManager.java b/vpnmanager/vpnmanager-impl/src/main/java/org/opendaylight/vpnservice/VpnInterfaceManager.java index 66e24e13..e7e47174 100644 --- a/vpnmanager/vpnmanager-impl/src/main/java/org/opendaylight/vpnservice/VpnInterfaceManager.java +++ b/vpnmanager/vpnmanager-impl/src/main/java/org/opendaylight/vpnservice/VpnInterfaceManager.java @@ -209,7 +209,6 @@ public class VpnInterfaceManager extends AbstractDataChangeListener identifier, - VpnInterface original, VpnInterface update) { - LOG.trace("Update VPN Interface {} , original {}, update {}", - identifier, original, update); - String vpnName = original.getVpnInstanceName(); - - boolean vpnNameChanged = false; - String rd = getRouteDistinguisher(vpnName); - String newRd = rd; - String newVpnName = update.getVpnInstanceName(); - if(!vpnName.equals(newVpnName)) { - //VPN for this interface got changed. - //Remove the interface from old VPN and add it to new VPN - newRd = getRouteDistinguisher(newVpnName); - if(newRd.equals("")) { - LOG.warn("VPN Instance {} not found. Update operation aborted", newVpnName); - return; - } - vpnNameChanged = true; - LOG.debug("New VPN Name for the interface {} is {}", newVpnName, original.getName()); + protected void update(InstanceIdentifier identifier, VpnInterface original, VpnInterface update) { + if (LOG.isTraceEnabled()) { + LOG.trace("Updating VPN Interface : key " + identifier + ", original value=" + original + ", update " + + "value=" + update); } - + String oldVpnName = original.getVpnInstanceName(); + String newVpnName = update.getVpnInstanceName(); List oldAdjs = original.getAugmentation(Adjacencies.class).getAdjacency(); List newAdjs = update.getAugmentation(Adjacencies.class).getAdjacency(); - if(vpnNameChanged && newAdjs != null && !newAdjs.isEmpty()) { - long label = VpnConstants.INVALID_ID; - InstanceIdentifier path = identifier.augmentation(Adjacencies.class); - Optional adjacencies = VpnUtil.read(broker, LogicalDatastoreType.OPERATIONAL, path); - if (adjacencies.isPresent()) { - List nextHops = adjacencies.get().getAdjacency(); - for(Adjacency nextHop : nextHops) { - label = nextHop.getLabel(); - if(label == VpnConstants.INVALID_ID) { - //Generate label using ID Manager - label = VpnUtil.getUniqueId(idManager, VpnConstants.VPN_IDPOOL_NAME, - VpnUtil.getNextHopLabelKey(newRd, nextHop.getIpAddress())); - } - if (rd != null) { - removePrefixFromBGP(rd, nextHop.getIpAddress()); - } else { - removeFibEntryFromDS(vpnName, nextHop.getIpAddress()); - } - //updatePrefixToBGP(newRd, nextHop, nextHopIp, label); - } - processVpnInterfaceAdjacencies(identifier, update, true); - VpnUtil.syncUpdate(broker, LogicalDatastoreType.OPERATIONAL, identifier, update); - } - } else if (oldAdjs != newAdjs) { - //handle both addition and removal of adjacencies - //currently, new adjacency may be an extra route + if (oldAdjs == null) { + oldAdjs = new ArrayList<>(); + } + if (newAdjs == null) { + newAdjs = new ArrayList<>(); + } + //handles switching between + if (!oldVpnName.equals(newVpnName)) { + remove(identifier, original); + add(identifier, update); + } + //handle both addition and removal of adjacencies + //currently, new adjacency may be an extra route + if (!oldAdjs.equals(newAdjs)) { for (Adjacency adj : newAdjs) { if (oldAdjs.contains(adj)) { oldAdjs.remove(adj); @@ -601,15 +573,10 @@ public class VpnInterfaceManager extends AbstractDataChangeListener