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;
import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.networks.rev150712.networks.attributes.networks.NetworkKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
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.lockmanager.rev150819.LockManagerService;
import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.lockmanager.rev150819.TimeUnits;
import org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.lockmanager.rev150819.TryLockInput;
protected static Uuid getVpnForNetwork(DataBroker broker, Uuid network) {
InstanceIdentifier<VpnMaps> vpnMapsIdentifier = InstanceIdentifier.builder(VpnMaps.class).build();
Optional<VpnMaps> optionalVpnMaps = read(broker, LogicalDatastoreType.CONFIGURATION, vpnMapsIdentifier);
- if (optionalVpnMaps.isPresent()) {
- VpnMaps vpnMaps = optionalVpnMaps.get();
- List<VpnMap> allMaps = vpnMaps.getVpnMap();
+ if (optionalVpnMaps.isPresent() && optionalVpnMaps.get().getVpnMap() != null) {
+ List<VpnMap> allMaps = optionalVpnMaps.get().getVpnMap();
for (VpnMap vpnMap : allMaps) {
if (vpnMap.getNetworkIds().contains(network)) {
return vpnMap.getVpnId();
return null;
}
- protected static Uuid getVpnForRouter(DataBroker broker, Uuid router) {
+ // true for external vpn, false for internal vpn
+ protected static Uuid getVpnForRouter(DataBroker broker, Uuid routerId, Boolean externalVpn) {
InstanceIdentifier<VpnMaps> vpnMapsIdentifier = InstanceIdentifier.builder(VpnMaps.class).build();
- Optional<VpnMaps> optionalVpnMaps = read(broker, LogicalDatastoreType.CONFIGURATION, vpnMapsIdentifier);
- if (optionalVpnMaps.isPresent()) {
- VpnMaps vpnNets = optionalVpnMaps.get();
- List<VpnMap> allMaps = vpnNets.getVpnMap();
- if (router != null) {
+ Optional<VpnMaps> optionalVpnMaps = read(broker, LogicalDatastoreType.CONFIGURATION,
+ vpnMapsIdentifier);
+ if (optionalVpnMaps.isPresent() && optionalVpnMaps.get().getVpnMap() != null) {
+ List<VpnMap> allMaps = optionalVpnMaps.get().getVpnMap();
+ if (routerId != null) {
for (VpnMap vpnMap : allMaps) {
- if (router.equals(vpnMap.getRouterId())) {
- return vpnMap.getVpnId();
+ if (routerId.equals(vpnMap.getRouterId())) {
+ if (externalVpn == true) {
+ if (!routerId.equals(vpnMap.getVpnId())) {
+ return vpnMap.getVpnId();
+ }
+ } else {
+ if (routerId.equals(vpnMap.getVpnId())) {
+ return vpnMap.getVpnId();
+ }
+ }
}
}
}
protected static List<Uuid> getNeutronRouterSubnetIds(DataBroker broker, Uuid routerId) {
logger.info("getNeutronRouterSubnetIds for {}", routerId.getValue());
- List<Uuid> subnetNames = new ArrayList<Uuid>();
+ List<Uuid> subnetIdList = new ArrayList<Uuid>();
Router router = getNeutronRouter(broker, routerId);
if (router != null) {
List<org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router
- .Interfaces> ifs = router.getInterfaces();
- if (!ifs.isEmpty()) {
+ .Interfaces> interfacesList = router.getInterfaces();
+ if (interfacesList != null) {
for (org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers
- .router.Interfaces iff : ifs) {
- subnetNames.add(iff.getSubnetId());
+ .router.Interfaces interfaces : interfacesList) {
+ subnetIdList.add(interfaces.getSubnetId());
}
}
}
logger.info("returning from getNeutronRouterSubnetIds for {}", routerId.getValue());
- return subnetNames;
+ return subnetIdList;
}
protected static String uuidToTapPortName(Uuid id) {
return new StringBuilder().append("tap").append(tapId).toString();
}
- protected static void lockVpnInterface(LockManagerService lockManager, String vpnInterfaceName) {
- TryLockInput input = new TryLockInputBuilder().setLockName(vpnInterfaceName).setTime(5L).setTimeUnit
+ protected static boolean lock(LockManagerService lockManager, String lockName) {
+ TryLockInput input = new TryLockInputBuilder().setLockName(lockName).setTime(5L).setTimeUnit
(TimeUnits.Milliseconds).build();
- Future<RpcResult<Void>> result = lockManager.tryLock(input);
+ boolean islockAcquired = false;
try {
+ Future<RpcResult<Void>> result = lockManager.tryLock(input);
if ((result != null) && (result.get().isSuccessful())) {
- logger.debug("Acquired lock for vpninterface {}", vpnInterfaceName);
+ logger.debug("Acquired lock for {}", lockName);
+ islockAcquired = true;
} else {
- throw new RuntimeException(String.format("Unable to getLock for vpninterface %s", vpnInterfaceName));
+ logger.error("Unable to acquire lock for {}", lockName);
}
} catch (InterruptedException | ExecutionException e) {
- logger.error("Unable to getLock for vpninterface {}", vpnInterfaceName);
- throw new RuntimeException(String.format("Unable to getLock for vpninterface %s", vpnInterfaceName), e
- .getCause());
+ logger.error("Unable to acquire lock for {}", lockName);
+ throw new RuntimeException(String.format("Unable to acquire lock for %s", lockName), e.getCause());
}
+ return islockAcquired;
}
- protected static void unlockVpnInterface(LockManagerService lockManager, String vpnInterfaceName) {
- UnlockInput input = new UnlockInputBuilder().setLockName(vpnInterfaceName).build();
- Future<RpcResult<Void>> result = lockManager.unlock(input);
+ protected static boolean unlock(LockManagerService lockManager, String lockName) {
+ UnlockInput input = new UnlockInputBuilder().setLockName(lockName).build();
+ boolean islockAcquired = false;
try {
+ Future<RpcResult<Void>> result = lockManager.unlock(input);
if ((result != null) && (result.get().isSuccessful())) {
- logger.debug("Unlocked vpninterface{}", vpnInterfaceName);
+ logger.debug("Unlocked {}", lockName);
+ islockAcquired = true;
} else {
- logger.debug("Unable to unlock vpninterface {}", vpnInterfaceName);
+ logger.error("Unable to unlock {}", lockName);
}
} catch (InterruptedException | ExecutionException e) {
- logger.error("Unable to unlock vpninterface {}", vpnInterfaceName);
- throw new RuntimeException(String.format("Unable to unlock vpninterface %s", vpnInterfaceName), e
- .getCause());
+ logger.error("Unable to unlock {}", lockName);
+ throw new RuntimeException(String.format("Unable to unlock %s", lockName), e.getCause());
}
+ return islockAcquired;
}
protected static Short getIPPrefixFromPort(DataBroker broker, Port port) {
try {
Uuid subnetUUID = port.getFixedIps().get(0).getSubnetId();
- org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets
- .SubnetKey subnetkey = new org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets
- .rev150712.subnets.attributes.subnets.SubnetKey(subnetUUID);
- InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets
- .attributes.subnets.Subnet> subnetidentifier = InstanceIdentifier.create(Neutron.class).child(org
- .opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets
- .class).child(org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets
- .attributes.subnets.Subnet.class, subnetkey);
- Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes
- .subnets.Subnet> subnet = read(broker, LogicalDatastoreType.CONFIGURATION, subnetidentifier);
-
+ SubnetKey subnetkey = new SubnetKey(subnetUUID);
+ InstanceIdentifier<Subnet> subnetidentifier = InstanceIdentifier.create(Neutron.class).child(Subnets
+ .class).child(Subnet.class, subnetkey);
+ Optional<Subnet> subnet = read(broker, LogicalDatastoreType.CONFIGURATION,subnetidentifier);
if (subnet.isPresent()) {
cidr = subnet.get().getCidr();
// Extract the prefix length from cidr
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;
return id;
}
+ static InstanceIdentifier<VpnInterface> buildVpnInterfaceIdentifier(String ifName) {
+ InstanceIdentifier<VpnInterface> id = InstanceIdentifier.builder(VpnInterfaces.class).
+ child(VpnInterface.class, new VpnInterfaceKey(ifName)).build();
+ return id;
+ }
+
static InstanceIdentifier<Subnetmap> buildSubnetMapIdentifier(Uuid subnetId) {
InstanceIdentifier<Subnetmap> id = InstanceIdentifier.builder(Subnetmaps.class).child(Subnetmap.class, new
SubnetmapKey(subnetId)).build();