From 48f7364d64b0c3095f4b5823c74374ac7d45d0ae Mon Sep 17 00:00:00 2001 From: Gilles Thouenon Date: Thu, 10 Feb 2022 14:33:12 +0100 Subject: [PATCH] Rewrite the whole DowngradeConstraintsTest class - rewrite DowngradeConstraintsTest class to take into acount new constraints appreared with service-models 10.1 - complement and fix implementation issues in DowngradeConstraints class JIRA: TRNSPRTPCE-611 Signed-off-by: Gilles Thouenon Change-Id: Iac95bcdf39efcb1d7e5fba00b7888f54dc6b6b21 --- .../servicehandler/DowngradeConstraints.java | 347 ++++++++++--- .../DowngradeConstraintsTest.java | 481 +++++++++++++++++- .../utils/ConstraintsUtils.java | 277 +++++++++- 3 files changed, 1002 insertions(+), 103 deletions(-) diff --git a/servicehandler/src/main/java/org/opendaylight/transportpce/servicehandler/DowngradeConstraints.java b/servicehandler/src/main/java/org/opendaylight/transportpce/servicehandler/DowngradeConstraints.java index 83402578a..c6e9cbb8c 100644 --- a/servicehandler/src/main/java/org/opendaylight/transportpce/servicehandler/DowngradeConstraints.java +++ b/servicehandler/src/main/java/org/opendaylight/transportpce/servicehandler/DowngradeConstraints.java @@ -7,32 +7,46 @@ */ package org.opendaylight.transportpce.servicehandler; +import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; +import java.util.List; import java.util.Map; +import java.util.Set; +import org.opendaylight.yang.gen.v1.http.org.openroadm.common.node.types.rev210528.NodeIdType; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.common.constraints.LinkIdentifier; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.common.constraints.LinkIdentifierKey; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.CoRouting; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.CoRoutingBuilder; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Distance; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.DistanceBuilder; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Diversity; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.DiversityBuilder; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Exclude; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.ExcludeBuilder; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.HopCount; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.HopCountBuilder; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Include; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.IncludeBuilder; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Latency; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.LatencyBuilder; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.TEMetric; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.TEMetricBuilder; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co.routing.ServiceIdentifierList; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co.routing.ServiceIdentifierListKey; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraints; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraintsBuilder; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.SoftConstraints; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.SoftConstraintsBuilder; +import org.opendaylight.yangtools.yang.common.Uint32; +import org.opendaylight.yangtools.yang.common.Uint8; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * Class to Map Hard Constraints to Soft Constraints. - * * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange - * + * @author gilles Thouenon (gilles.thouenon@orange.com) */ public final class DowngradeConstraints { @@ -43,112 +57,309 @@ public final class DowngradeConstraints { /** * Add Hard Constraints to Soft Constraints. - * - * - * @param hardConstraints to be added - * @param softConstraints to be modified - * @return SoftConstraints modified + * @param hardConstraints to be added + * @param softConstraints to be modified + * @return SoftConstraints modified */ public static SoftConstraints updateSoftConstraints(HardConstraints hardConstraints, - SoftConstraints softConstraints) { + SoftConstraints softConstraints) { SoftConstraintsBuilder softConstraintsBuilder = new SoftConstraintsBuilder(softConstraints); - if (hardConstraints.getCustomerCode() != null) { - if (!hardConstraints.getCustomerCode().isEmpty()) { - softConstraintsBuilder.getCustomerCode().addAll(hardConstraints.getCustomerCode()); + if (hardConstraints.getCustomerCode() != null && !hardConstraints.getCustomerCode().isEmpty()) { + if (softConstraintsBuilder.getCustomerCode() == null + || softConstraintsBuilder.getCustomerCode().isEmpty()) { + softConstraintsBuilder.setCustomerCode(hardConstraints.getCustomerCode()); + } else { + List updatedCustomerCode = new ArrayList<>(softConstraintsBuilder.getCustomerCode()); + updatedCustomerCode.addAll(hardConstraints.getCustomerCode()); + softConstraintsBuilder.setCustomerCode(updatedCustomerCode); } } - if (hardConstraints.getOperationalMode() != null) { - if (!hardConstraints.getOperationalMode().isEmpty()) { - softConstraintsBuilder.getOperationalMode().addAll(hardConstraints.getOperationalMode()); + if (hardConstraints.getOperationalMode() != null && !hardConstraints.getOperationalMode().isEmpty()) { + if (softConstraintsBuilder.getOperationalMode() == null + || softConstraintsBuilder.getOperationalMode().isEmpty()) { + softConstraintsBuilder.setOperationalMode(hardConstraints.getOperationalMode()); + } else { + List updatedOperationalMode = new ArrayList<>(softConstraintsBuilder.getOperationalMode()); + updatedOperationalMode.addAll(hardConstraints.getOperationalMode()); + softConstraintsBuilder.setOperationalMode(updatedOperationalMode); } } if (hardConstraints.getDiversity() != null) { - if (softConstraints.getDiversity() != null) { - softConstraintsBuilder - .setDiversity(updateDiveristy(hardConstraints.getDiversity(), softConstraints.getDiversity())); - } + softConstraintsBuilder + .setDiversity(updateDiveristy(hardConstraints.getDiversity(), softConstraints.getDiversity())); } if (hardConstraints.getExclude() != null) { - if (softConstraints.getExclude() != null) { - softConstraintsBuilder - .setExclude(updateExclude(hardConstraints.getExclude(), softConstraints.getExclude())); - } + softConstraintsBuilder + .setExclude(updateExclude(hardConstraints.getExclude(), softConstraints.getExclude())); } if (hardConstraints.getInclude() != null) { - if (softConstraints.getInclude() != null) { - softConstraintsBuilder - .setInclude(updateInclude(hardConstraints.getInclude(), softConstraints.getInclude())); - } + softConstraintsBuilder + .setInclude(updateInclude(hardConstraints.getInclude(), softConstraints.getInclude())); } if (hardConstraints.getLatency() != null) { - if (softConstraints.getLatency() != null) { - softConstraintsBuilder - .setLatency(updateLatency(hardConstraints.getLatency(), softConstraints.getLatency())); - } + softConstraintsBuilder + .setLatency(updateLatency(hardConstraints.getLatency(), softConstraints.getLatency())); + } + if (hardConstraints.getDistance() != null) { + softConstraintsBuilder + .setDistance(updateDistance(hardConstraints.getDistance(), softConstraints.getDistance())); + } + if (hardConstraints.getHopCount() != null) { + softConstraintsBuilder + .setHopCount(updateHopCount(hardConstraints.getHopCount(), softConstraints.getHopCount())); } + if (hardConstraints.getTEMetric() != null) { + softConstraintsBuilder + .setTEMetric(updateTEMetric(hardConstraints.getTEMetric(), softConstraints.getTEMetric())); + } + if (hardConstraints.getCoRouting() != null) { - if (softConstraints.getCoRouting() != null) { - softConstraintsBuilder - .setCoRouting(updateCoRouting(hardConstraints.getCoRouting(), softConstraints.getCoRouting())); - } + softConstraintsBuilder + .setCoRouting(updateCoRouting(hardConstraints.getCoRouting(), softConstraints.getCoRouting())); } return softConstraintsBuilder.build(); } private static Include updateInclude(Include hard, Include soft) { - IncludeBuilder includeBldr = new IncludeBuilder(soft); - includeBldr.getFiberBundle().addAll(hard.getFiberBundle()); - includeBldr.getNodeId().addAll(hard.getNodeId()); - includeBldr.getSite().addAll(hard.getSite()); - includeBldr.getSupportingServiceName().addAll(hard.getSupportingServiceName()); + IncludeBuilder includeBldr = soft == null ? new IncludeBuilder() : new IncludeBuilder(soft); + + if (hard.getFiberBundle() != null && !hard.getFiberBundle().isEmpty()) { + if (includeBldr.getFiberBundle() == null) { + includeBldr.setFiberBundle(hard.getFiberBundle()); + } else { + Set fiberList = new HashSet<>(includeBldr.getFiberBundle()); + fiberList.addAll(hard.getFiberBundle()); + includeBldr.setFiberBundle(new ArrayList<>(fiberList)); + } + } + if (hard.getNodeId() != null && !hard.getNodeId().isEmpty()) { + if (includeBldr.getNodeId() == null) { + includeBldr.setNodeId(hard.getNodeId()); + } else { + Set nodeIdList = new HashSet<>(includeBldr.getNodeId()); + nodeIdList.addAll(hard.getNodeId()); + includeBldr.setNodeId(new ArrayList<>(nodeIdList)); + } + } + if (hard.getSite() != null && !hard.getSite().isEmpty()) { + if (includeBldr.getSite() == null) { + includeBldr.setSite(hard.getSite()); + } else { + Set siteList = new HashSet<>(includeBldr.getSite()); + siteList.addAll(hard.getSite()); + includeBldr.setSite(new ArrayList<>(siteList)); + } + } + if (hard.getSrlgId() != null && !hard.getSrlgId().isEmpty()) { + if (includeBldr.getSrlgId() == null) { + includeBldr.setSrlgId(hard.getSrlgId()); + } else { + Set srlgList = new HashSet<>(includeBldr.getSrlgId()); + srlgList.addAll(hard.getSrlgId()); + includeBldr.setSrlgId(new ArrayList<>(srlgList)); + } + } + if (hard.getSupportingServiceName() != null && !hard.getSupportingServiceName().isEmpty()) { + if (includeBldr.getSupportingServiceName() == null) { + includeBldr.setSupportingServiceName(hard.getSupportingServiceName()); + } else { + Set serviceList = new HashSet<>(includeBldr.getSupportingServiceName()); + serviceList.addAll(hard.getSupportingServiceName()); + includeBldr.setSupportingServiceName(new ArrayList<>(serviceList)); + } + } + if (hard.getLinkIdentifier() != null && !hard.getLinkIdentifier().isEmpty()) { + if (includeBldr.getLinkIdentifier() == null) { + includeBldr.setLinkIdentifier(hard.getLinkIdentifier()); + } else { + Map linkList = new HashMap<>(includeBldr.getLinkIdentifier()); + linkList.putAll(hard.getLinkIdentifier()); + includeBldr.setLinkIdentifier(linkList); + } + } return includeBldr.build(); } private static Exclude updateExclude(Exclude hard, Exclude soft) { - ExcludeBuilder excludeBldr = new ExcludeBuilder(soft); - excludeBldr.getFiberBundle().addAll(hard.getFiberBundle()); - excludeBldr.getNodeId().addAll(hard.getNodeId()); - excludeBldr.getSite().addAll(hard.getSite()); - excludeBldr.getSupportingServiceName().addAll(hard.getSupportingServiceName()); + ExcludeBuilder excludeBldr = soft == null ? new ExcludeBuilder() : new ExcludeBuilder(soft); + + if (hard.getFiberBundle() != null && !hard.getFiberBundle().isEmpty()) { + if (excludeBldr.getFiberBundle() == null) { + excludeBldr.setFiberBundle(hard.getFiberBundle()); + } else { + Set fiberList = new HashSet<>(excludeBldr.getFiberBundle()); + fiberList.addAll(hard.getFiberBundle()); + excludeBldr.setFiberBundle(new ArrayList<>(fiberList)); + } + } + if (hard.getNodeId() != null && !hard.getNodeId().isEmpty()) { + if (excludeBldr.getNodeId() == null) { + excludeBldr.setNodeId(hard.getNodeId()); + } else { + Set nodeIdList = new HashSet<>(excludeBldr.getNodeId()); + nodeIdList.addAll(hard.getNodeId()); + excludeBldr.setNodeId(new ArrayList<>(nodeIdList)); + } + } + if (hard.getSite() != null && !hard.getSite().isEmpty()) { + if (excludeBldr.getSite() == null) { + excludeBldr.setSite(hard.getSite()); + } else { + Set siteList = new HashSet<>(excludeBldr.getSite()); + siteList.addAll(hard.getSite()); + excludeBldr.setSite(new ArrayList<>(siteList)); + } + } + if (hard.getSrlgId() != null && !hard.getSrlgId().isEmpty()) { + if (excludeBldr.getSrlgId() == null) { + excludeBldr.setSrlgId(hard.getSrlgId()); + } else { + Set srlgList = new HashSet<>(excludeBldr.getSrlgId()); + srlgList.addAll(hard.getSrlgId()); + excludeBldr.setSrlgId(new ArrayList<>(srlgList)); + } + } + if (hard.getSupportingServiceName() != null && !hard.getSupportingServiceName().isEmpty()) { + if (excludeBldr.getSupportingServiceName() == null) { + excludeBldr.setSupportingServiceName(hard.getSupportingServiceName()); + } else { + Set serviceList = new HashSet<>(excludeBldr.getSupportingServiceName()); + serviceList.addAll(hard.getSupportingServiceName()); + excludeBldr.setSupportingServiceName(new ArrayList<>(serviceList)); + } + } + if (hard.getLinkIdentifier() != null && !hard.getLinkIdentifier().isEmpty()) { + if (excludeBldr.getLinkIdentifier() == null) { + excludeBldr.setLinkIdentifier(hard.getLinkIdentifier()); + } else { + Map linkList = new HashMap<>(excludeBldr.getLinkIdentifier()); + linkList.putAll(hard.getLinkIdentifier()); + excludeBldr.setLinkIdentifier(linkList); + } + } return excludeBldr.build(); } private static Diversity updateDiveristy(Diversity hard, Diversity soft) { - DiversityBuilder diversityBldr = new DiversityBuilder(soft); - if (!hard.getServiceIdentifierList().isEmpty()) { - Map sil = new HashMap<>(diversityBldr.getServiceIdentifierList()); + org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service + .constraints.ServiceIdentifierList> sil = + diversityBldr.getServiceIdentifierList() == null + ? new HashMap<>() + : new HashMap<>(diversityBldr.getServiceIdentifierList()); + if (!hard.getServiceIdentifierList().isEmpty()) { sil.putAll(hard.getServiceIdentifierList()); diversityBldr.setServiceIdentifierList(sil); } + if (hard.getDiversityType() != null) { + diversityBldr.setDiversityType(hard.getDiversityType()); + } return diversityBldr.build(); } private static Latency updateLatency(Latency hard, Latency soft) { - LatencyBuilder latencyBldr = new LatencyBuilder(soft); - if (hard.getMaxLatency() != null) { - latencyBldr.setMaxLatency(hard.getMaxLatency()); + return soft == null || hard.getMaxLatency().longValue() <= soft.getMaxLatency().longValue() + ? new LatencyBuilder(hard).build() + : new LatencyBuilder(soft).build(); + } + + private static Distance updateDistance(Distance hard, Distance soft) { + return soft == null || hard.getMaxDistance().longValue() <= soft.getMaxDistance().longValue() + ? new DistanceBuilder(hard).build() + : new DistanceBuilder(soft).build(); + } + + private static HopCount updateHopCount(HopCount hard, HopCount soft) { + if (soft == null) { + return new HopCountBuilder(hard).build(); } - return latencyBldr.build(); + String maxWdmHc = null; + if (soft.getMaxWdmHopCount() != null && hard.getMaxWdmHopCount() != null) { + maxWdmHc = soft.getMaxWdmHopCount().intValue() <= hard.getMaxWdmHopCount().intValue() + ? soft.getMaxWdmHopCount().toString() + : hard.getMaxWdmHopCount().toString(); + } else if (hard.getMaxWdmHopCount() != null) { + maxWdmHc = hard.getMaxWdmHopCount().toString(); + } else if (soft.getMaxWdmHopCount() != null) { + maxWdmHc = soft.getMaxWdmHopCount().toString(); + } + String maxOtnHc = null; + if (soft.getMaxOtnHopCount() != null && hard.getMaxOtnHopCount() != null) { + maxOtnHc = soft.getMaxOtnHopCount().intValue() <= hard.getMaxOtnHopCount().intValue() + ? soft.getMaxOtnHopCount().toString() + : hard.getMaxOtnHopCount().toString(); + } else if (hard.getMaxOtnHopCount() != null) { + maxOtnHc = hard.getMaxOtnHopCount().toString(); + } else if (soft.getMaxOtnHopCount() != null) { + maxOtnHc = soft.getMaxOtnHopCount().toString(); + } + HopCountBuilder hcBldr = new HopCountBuilder(); + if (maxWdmHc != null) { + hcBldr.setMaxWdmHopCount(Uint8.valueOf(maxWdmHc)); + } + if (maxOtnHc != null) { + hcBldr.setMaxWdmHopCount(Uint8.valueOf(maxOtnHc)); + } + return hcBldr.build(); + } + + private static TEMetric updateTEMetric(TEMetric hard, TEMetric soft) { + if (soft == null) { + return new TEMetricBuilder(hard).build(); + } + String maxWdmTem = null; + if (soft.getMaxWdmTEMetric() != null && hard.getMaxWdmTEMetric() != null) { + maxWdmTem = soft.getMaxWdmTEMetric().intValue() <= hard.getMaxWdmTEMetric().intValue() + ? soft.getMaxWdmTEMetric().toString() + : hard.getMaxWdmTEMetric().toString(); + } else if (hard.getMaxWdmTEMetric() != null) { + maxWdmTem = hard.getMaxWdmTEMetric().toString(); + } else if (soft.getMaxWdmTEMetric() != null) { + maxWdmTem = soft.getMaxWdmTEMetric().toString(); + } + String maxOtnTem = null; + if (soft.getMaxOtnTEMetric() != null && hard.getMaxOtnTEMetric() != null) { + maxOtnTem = soft.getMaxOtnTEMetric().intValue() <= hard.getMaxOtnTEMetric().intValue() + ? soft.getMaxOtnTEMetric().toString() + : hard.getMaxOtnTEMetric().toString(); + } else if (hard.getMaxOtnTEMetric() != null) { + maxOtnTem = hard.getMaxOtnTEMetric().toString(); + } else if (soft.getMaxOtnTEMetric() != null) { + maxOtnTem = soft.getMaxOtnTEMetric().toString(); + } + TEMetricBuilder temBldr = new TEMetricBuilder(); + if (maxWdmTem != null) { + temBldr.setMaxWdmTEMetric(Uint32.valueOf(maxWdmTem)); + } + if (maxOtnTem != null) { + temBldr.setMaxOtnTEMetric(Uint32.valueOf(maxOtnTem)); + } + return temBldr.build(); } private static CoRouting updateCoRouting(CoRouting hard, CoRouting soft) { - CoRoutingBuilder coRoutingBldr = new CoRoutingBuilder(soft); - Map serviceIdentifierList - = new HashMap(coRoutingBldr.getServiceIdentifierList()); - serviceIdentifierList.putAll(hard.getServiceIdentifierList()); - return coRoutingBldr - .setServiceIdentifierList(serviceIdentifierList) - .build(); + CoRoutingBuilder coRoutingBldr = soft == null ? new CoRoutingBuilder() : new CoRoutingBuilder(soft); + + Map< + ServiceIdentifierListKey, + ServiceIdentifierList> serviceIdentifierList = coRoutingBldr.getServiceIdentifierList() == null + ? new HashMap<>() + : new HashMap<>(coRoutingBldr.getServiceIdentifierList()); + if (!hard.getServiceIdentifierList().isEmpty()) { + serviceIdentifierList.putAll(hard.getServiceIdentifierList()); + coRoutingBldr.setServiceIdentifierList(serviceIdentifierList); + } + return coRoutingBldr.build(); } /** * Remove all hard constraints except latency. - * - * @param hardConstraints HardConstarints to be downgraded - * @return HardConstraints downgraded + * @param hardConstraints HardConstarints to be downgraded + * @return HardConstraints downgraded */ public static HardConstraints downgradeHardConstraints(HardConstraints hardConstraints) { HardConstraintsBuilder hardConstraintsBuilder = new HardConstraintsBuilder(); @@ -162,12 +373,12 @@ public final class DowngradeConstraints { /** * Convert HardConstraints to SoftConstraints. - * - * @param hardConstraints to be converted. - * @return SoftConstraints converted. + * @param hardConstraints to be converted. + * @return SoftConstraints converted. */ public static SoftConstraints convertToSoftConstraints(HardConstraints hardConstraints) { - SoftConstraintsBuilder softConstraintsBuilder = new SoftConstraintsBuilder(hardConstraints); - return softConstraintsBuilder.build(); + return hardConstraints == null + ? new SoftConstraintsBuilder().build() + : new SoftConstraintsBuilder(hardConstraints).build(); } } diff --git a/servicehandler/src/test/java/org/opendaylight/transportpce/servicehandler/DowngradeConstraintsTest.java b/servicehandler/src/test/java/org/opendaylight/transportpce/servicehandler/DowngradeConstraintsTest.java index 6b5acf1a8..3a4ebd787 100644 --- a/servicehandler/src/test/java/org/opendaylight/transportpce/servicehandler/DowngradeConstraintsTest.java +++ b/servicehandler/src/test/java/org/opendaylight/transportpce/servicehandler/DowngradeConstraintsTest.java @@ -1,5 +1,5 @@ /* - * Copyright © 2018 Orange, Inc. and others. All rights reserved. + * Copyright © 2018 2022 Orange, Inc. 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, @@ -7,32 +7,489 @@ */ package org.opendaylight.transportpce.servicehandler; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; import static org.junit.Assert.assertEquals; -import static org.opendaylight.transportpce.servicehandler.utils.ConstraintsUtils.buildHardConstraintWithCoRouting; -import static org.opendaylight.transportpce.servicehandler.utils.ConstraintsUtils.buildSoftConstraintWithCoRouting; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.opendaylight.transportpce.servicehandler.utils.ConstraintsUtils.buildHardConstraint; +import static org.opendaylight.transportpce.servicehandler.utils.ConstraintsUtils.buildSoftConstraint; +import java.util.Arrays; +import java.util.List; +import org.hamcrest.collection.IsMapContaining; import org.junit.Test; +import org.opendaylight.yang.gen.v1.http.org.openroadm.common.node.types.rev210528.NodeIdType; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.DiversityConstraints.DiversityType; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.common.constraints.LinkIdentifierKey; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service.constraints.ServiceIdentifierListKey; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraints; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraintsBuilder; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.SoftConstraints; /** * Class to test downgrading and updating Constraints . - * * @author Ahmed Helmy ( ahmad.helmy@orange.com ) - * + * @author Gilles Thouenon (gilles.thouenon@orange.com) */ public class DowngradeConstraintsTest { + @Test + public void testUpdateSoftConstraintsForCustomerCode() { + // test no addition when hard customer-code is null or empty + HardConstraints initialHardConstraints = + buildHardConstraint(null, false, null, null, null, null, false, false, null, null); + SoftConstraints initialSoftConstraints = + buildSoftConstraint(null, false, null, null, null, null, false, false, null, null); + SoftConstraints generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertNull("updated soft constraints should contain no customer code", + generatedSoftConstraints.getCustomerCode()); + List softCustomerCode = Arrays.asList("soft-customer-code 3", "soft-customer-code 4"); + initialSoftConstraints = + buildSoftConstraint(softCustomerCode, false, null, null, null, null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals(initialSoftConstraints.getCustomerCode(), generatedSoftConstraints.getCustomerCode()); + + // test addition of hard customer-code when no soft customer-code + List hardCustomerCode = Arrays.asList("hard-customer-code 1", "hard-customer-code 2"); + initialHardConstraints = + buildHardConstraint(hardCustomerCode, false, null, null, null, null, false, false, null, null); + initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints should contain the customer code of hard constraint", + generatedSoftConstraints.getCustomerCode(), + initialHardConstraints.getCustomerCode()); + // test addition of hard customer-code when existing soft customer-code + initialSoftConstraints = + buildSoftConstraint(softCustomerCode, false, null, null, null, null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertThat("updated soft constraints should contain 4 customer code", + generatedSoftConstraints.getCustomerCode(), hasSize(4)); + assertThat(generatedSoftConstraints.getCustomerCode(), + containsInAnyOrder("hard-customer-code 1", "hard-customer-code 2", "soft-customer-code 3", + "soft-customer-code 4")); + } + + @Test + public void testUpdateSoftConstraintsForDiversity() { + HardConstraints initialHardConstraints = + buildHardConstraint(null, false, null, null, null, null, false, false, null, null); + SoftConstraints initialSoftConstraints = + buildSoftConstraint(null, false, null, null, null, null, false, false, null, null); + SoftConstraints generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertNull("updated soft constraints should contain no diversity constraint", + generatedSoftConstraints.getDiversity()); + List softDiversityServiceid = Arrays.asList("soft-service 3"); + initialSoftConstraints = + buildSoftConstraint(null, false, softDiversityServiceid, null, null, null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals(initialSoftConstraints.getDiversity(), generatedSoftConstraints.getDiversity()); + + // test addition of hard diversity when no soft diversity + List hardDiversityServiceid = Arrays.asList("hard-service 1", "hard-service 2"); + initialHardConstraints = + buildHardConstraint(null, false, hardDiversityServiceid, null, null, null, false, false, null, null); + initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints should contain the diversity of hard constraint", + generatedSoftConstraints.getDiversity(), + initialHardConstraints.getDiversity()); + + // test addition of hard diversity when existing soft diversity + initialSoftConstraints = + buildSoftConstraint(null, false, softDiversityServiceid, null, null, null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertThat("updated soft constraints should contain diversity with 3 services", + generatedSoftConstraints.getDiversity().getServiceIdentifierList().size(), is(3)); + assertEquals("updated soft constraints should have diversity type of serial", + DiversityType.Serial, + generatedSoftConstraints.getDiversity().getDiversityType()); + assertThat(generatedSoftConstraints.getDiversity().getServiceIdentifierList(), + IsMapContaining.hasKey(new ServiceIdentifierListKey("hard-service 1"))); + assertThat(generatedSoftConstraints.getDiversity().getServiceIdentifierList(), + IsMapContaining.hasKey(new ServiceIdentifierListKey("hard-service 2"))); + assertThat(generatedSoftConstraints.getDiversity().getServiceIdentifierList(), + IsMapContaining.hasKey(new ServiceIdentifierListKey("soft-service 3"))); + } + + @Test + public void testUpdateSoftConstraintsForExclude() { + HardConstraints initialHardConstraints = + buildHardConstraint(null, false, null, null, null, null, false, false, null, null); + SoftConstraints initialSoftConstraints = + buildSoftConstraint(null, false, null, null, null, null, false, false, null, null); + SoftConstraints generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertNull("updated soft constraints should contain no exclude constraint", + generatedSoftConstraints.getExclude()); + + initialSoftConstraints = buildSoftConstraint(null, false, null, "link", null, null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints should not be changed", + initialSoftConstraints.getExclude(), generatedSoftConstraints.getExclude()); + + // test addition of hard exclude with fiber list when no soft exclude + initialHardConstraints = + buildHardConstraint(null, false, null, "fiber1", null, null, false, false, null, null); + initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints should contain the exclude constraint of hard constraint", + generatedSoftConstraints.getExclude(), initialHardConstraints.getExclude()); + + // test addition of hard exclude with fiber list when existing soft + // exclude with fiber list + initialSoftConstraints = + buildSoftConstraint(null, false, null, "fiber2", null, null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertThat("updated soft constraints should contain exclude with 3 fiber bundles", + generatedSoftConstraints.getExclude().getFiberBundle().size(), is(3)); + assertThat(generatedSoftConstraints.getExclude().getFiberBundle(), + containsInAnyOrder("fiber-1", "fiber-2", "fiber-3")); + + // test addition of hard exclude with link list when no soft exclude + initialHardConstraints = buildHardConstraint(null, false, null, "link1", null, null, false, false, null, null); + initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints should contain the exclude constraint of hard constraint", + generatedSoftConstraints.getExclude(), initialHardConstraints.getExclude()); + + // test addition of hard exclude with link list when existing soft + // exclude with link list + initialSoftConstraints = buildSoftConstraint(null, false, null, "link2", null, null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertThat("updated soft constraints should contain exclude with 3 links", + generatedSoftConstraints.getExclude().getLinkIdentifier().size(), is(3)); + assertThat(generatedSoftConstraints.getExclude().getLinkIdentifier(), + IsMapContaining.hasKey(new LinkIdentifierKey("link-id 1", "openroadm-topology"))); + assertThat(generatedSoftConstraints.getExclude().getLinkIdentifier(), + IsMapContaining.hasKey(new LinkIdentifierKey("link-id 2", "openroadm-topology"))); + assertThat(generatedSoftConstraints.getExclude().getLinkIdentifier(), + IsMapContaining.hasKey(new LinkIdentifierKey("link-id 3", "openroadm-topology"))); + } + + @Test + public void testUpdateSoftConstraintsForInclude() { + HardConstraints initialHardConstraints = + buildHardConstraint(null, false, null, null, null, null, false, false, null, null); + SoftConstraints initialSoftConstraints = + buildSoftConstraint(null, false, null, null, null, null, false, false, null, null); + SoftConstraints generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertNull("updated soft constraints should contain no include constraint", + generatedSoftConstraints.getInclude()); + + initialSoftConstraints = buildSoftConstraint(null, false, null, null, "link", null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints should not be changed", + initialSoftConstraints.getInclude(), generatedSoftConstraints.getInclude()); + + // test addition of hard include with fiber list when no soft include + initialHardConstraints = + buildHardConstraint(null, false, null, null, "fiber1", null, false, false, null, null); + initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints should contain the include constraint of hard constraint", + generatedSoftConstraints.getInclude(), initialHardConstraints.getInclude()); + + // test addition of hard include with fiber list when existing soft + // include with fiber list + initialSoftConstraints = + buildSoftConstraint(null, false, null, null, "fiber2", null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertThat("updated soft constraints should contain exclude with 3 fiber bundles", + generatedSoftConstraints.getInclude().getFiberBundle().size(), is(3)); + assertThat(generatedSoftConstraints.getInclude().getFiberBundle(), + containsInAnyOrder("fiber-1", "fiber-2", "fiber-3")); + + // test addition of hard include with link list when no soft include + initialHardConstraints = buildHardConstraint(null, false, null, null, "link1", null, false, false, null, null); + initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints should contain the include constraint of hard constraint", + generatedSoftConstraints.getInclude(), initialHardConstraints.getInclude()); + + // test addition of hard include with link list when existing soft + // include with link list + initialSoftConstraints = buildSoftConstraint(null, false, null, null, "link2", null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertThat("updated soft constraints should contain include with 3 links", + generatedSoftConstraints.getInclude().getLinkIdentifier().size(), is(3)); + assertThat(generatedSoftConstraints.getInclude().getLinkIdentifier(), + IsMapContaining.hasKey(new LinkIdentifierKey("link-id 1", "openroadm-topology"))); + assertThat(generatedSoftConstraints.getInclude().getLinkIdentifier(), + IsMapContaining.hasKey(new LinkIdentifierKey("link-id 2", "openroadm-topology"))); + assertThat(generatedSoftConstraints.getInclude().getLinkIdentifier(), + IsMapContaining.hasKey(new LinkIdentifierKey("link-id 3", "openroadm-topology"))); + } + + @Test + public void testUpdateSoftConstraintsForLatency() { + HardConstraints initialHardConstraints = + buildHardConstraint(null, false, null, null, null, null, false, false, null, null); + SoftConstraints initialSoftConstraints = + buildSoftConstraint(null, false, null, null, null, null, false, false, null, null); + SoftConstraints generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertNull("updated soft constraints should contain no latency constraint", + generatedSoftConstraints.getLatency()); + + initialSoftConstraints = + buildSoftConstraint(null, false, null, null, null, Double.valueOf(12.2), false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints should not be changed", + initialSoftConstraints.getLatency(), generatedSoftConstraints.getLatency()); + assertEquals("updated soft constraints value should be '12.2'", + (float) 12.2, generatedSoftConstraints.getLatency().getMaxLatency().floatValue(), 0.0f); + + // test addition of hard latency when no soft latency + initialHardConstraints = + buildHardConstraint(null, false, null, null, null, Double.valueOf(16.59), false, false, null, null); + initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints value should be '16.59'", + (float) 16.59, generatedSoftConstraints.getLatency().getMaxLatency().floatValue(), 0.0f); + + // test addition of hard latency when existing different soft latency + initialSoftConstraints = + buildSoftConstraint(null, false, null, null, null, Double.valueOf(12.2), false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints value should be '12.2'", + (float) 12.2, generatedSoftConstraints.getLatency().getMaxLatency().floatValue(), 0.0f); + } + + @Test + public void testUpdateSoftConstraintsForDistance() { + HardConstraints initialHardConstraints = + buildHardConstraint(null, false, null, null, null, null, false, false, null, null); + SoftConstraints initialSoftConstraints = + buildSoftConstraint(null, false, null, null, null, null, false, false, null, null); + SoftConstraints generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertNull("updated soft constraints should contain no distance constraint", + generatedSoftConstraints.getDistance()); + + initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, "750.2", null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints should not be changed", + initialSoftConstraints.getDistance(), generatedSoftConstraints.getDistance()); + assertEquals("updated soft constraints value should be '750.2'", + (float) 750.2, generatedSoftConstraints.getDistance().getMaxDistance().floatValue(), 0.0f); + + // test addition of hard distance when no soft distance + initialHardConstraints = buildHardConstraint(null, false, null, null, null, null, false, false, "555.5", null); + initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints value should be '555.5'", + (float) 555.5, generatedSoftConstraints.getDistance().getMaxDistance().floatValue(), 0.0f); + + // test addition of hard distance when existing different soft distance + initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, "750.2", null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints value should be '555.5'", + (float) 555.5, generatedSoftConstraints.getDistance().getMaxDistance().floatValue(), 0.0f); + } + + @Test + public void testUpdateSoftConstraintsForHopCount() { + HardConstraints initialHardConstraints = + buildHardConstraint(null, false, null, null, null, null, false, false, null, null); + SoftConstraints initialSoftConstraints = + buildSoftConstraint(null, false, null, null, null, null, false, false, null, null); + SoftConstraints generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertNull("updated soft constraints should contain no hop-count constraint", + generatedSoftConstraints.getHopCount()); + + initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, true, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints should not be changed", + initialSoftConstraints.getHopCount(), generatedSoftConstraints.getHopCount()); + assertEquals("updated soft constraints max-wdm-hop-count should be '3'", + 3, generatedSoftConstraints.getHopCount().getMaxWdmHopCount().intValue()); + assertNull("updated soft constraints max-otn-hop-count should be null", + generatedSoftConstraints.getHopCount().getMaxOtnHopCount()); + + // test addition of hard hop-count when no soft hop-count + initialHardConstraints = buildHardConstraint(null, false, null, null, null, null, true, false, null, null); + initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints should contain hard constraint", + initialHardConstraints.getHopCount(), generatedSoftConstraints.getHopCount()); + assertEquals("updated soft constraints max-wdm-hop-count should be '3'", + 3, generatedSoftConstraints.getHopCount().getMaxWdmHopCount().intValue()); + assertNull("updated soft constraints max-otn-hop-count should be null", + generatedSoftConstraints.getHopCount().getMaxOtnHopCount()); + // test addition of hard hop-count when existing soft hop-count + initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, true, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints max-wdm-hop-count should be '3'", + 3, generatedSoftConstraints.getHopCount().getMaxWdmHopCount().intValue()); + assertNull("updated soft constraints max-otn-hop-count should be null", + generatedSoftConstraints.getHopCount().getMaxOtnHopCount()); + } @Test - public void testUpdateSoftConstraintsBothCoRouting() { - HardConstraints initialHardConstraints = buildHardConstraintWithCoRouting(); - SoftConstraints initialSoftConstraints = buildSoftConstraintWithCoRouting(); + public void testUpdateSoftConstraintsForCoRouting() { + HardConstraints initialHardConstraints = + buildHardConstraint(null, false, null, null, null, null, false, false, null, null); + SoftConstraints initialSoftConstraints = + buildSoftConstraint(null, false, null, null, null, null, false, false, null, null); SoftConstraints generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( - initialHardConstraints, initialSoftConstraints); - assertEquals( - generatedSoftConstraints.getCustomerCode().get(0), - initialHardConstraints.getCustomerCode().get(0)); + initialHardConstraints, initialSoftConstraints); + assertNull("updated soft constraints should contain no co-routing constraint", + generatedSoftConstraints.getCoRouting()); + + initialSoftConstraints = + buildSoftConstraint(null, false, null, null, null, null, true, false, null, "coRouting1"); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints should not be changed", + initialSoftConstraints.getCoRouting(), generatedSoftConstraints.getCoRouting()); + assertEquals("updated soft constraints should contain 2 co-routed services", + 2, generatedSoftConstraints.getCoRouting().getServiceIdentifierList().size()); + assertTrue(generatedSoftConstraints.getCoRouting().getServiceIdentifierList() + .get(new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co + .routing.ServiceIdentifierListKey("service 1")) + .getServiceApplicability().getEquipment().getRoadmSrg()); + assertNull(generatedSoftConstraints.getCoRouting().getServiceIdentifierList() + .get(new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co + .routing.ServiceIdentifierListKey("service 1")) + .getServiceApplicability().getLink()); + assertNull(generatedSoftConstraints.getCoRouting().getServiceIdentifierList() + .get(new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co + .routing.ServiceIdentifierListKey("service 2")) + .getServiceApplicability().getEquipment()); + assertTrue(generatedSoftConstraints.getCoRouting().getServiceIdentifierList() + .get(new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co + .routing.ServiceIdentifierListKey("service 2")) + .getServiceApplicability().getLink()); + + // test addition of hard co-routing when no soft co-routing + initialHardConstraints = + buildHardConstraint(null, false, null, null, null, null, true, false, null, "coRouting2"); + initialSoftConstraints = buildSoftConstraint(null, false, null, null, null, null, false, false, null, null); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints should contain hard constraint", + initialHardConstraints.getCoRouting(), generatedSoftConstraints.getCoRouting()); + assertEquals("updated soft constraints should contain 1 co-routed service", + 1, generatedSoftConstraints.getCoRouting().getServiceIdentifierList().size()); + assertTrue(generatedSoftConstraints.getCoRouting().getServiceIdentifierList() + .get(new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co + .routing.ServiceIdentifierListKey("service 3")) + .getServiceApplicability().getSite()); + assertNull(generatedSoftConstraints.getCoRouting().getServiceIdentifierList() + .get(new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co + .routing.ServiceIdentifierListKey("service 3")) + .getServiceApplicability().getLink()); + + // test addition of hard hop-count when existing soft hop-count + initialSoftConstraints = + buildSoftConstraint(null, false, null, null, null, null, true, false, null, "coRouting1"); + generatedSoftConstraints = DowngradeConstraints.updateSoftConstraints( + initialHardConstraints, initialSoftConstraints); + assertEquals("updated soft constraints should contain 3 co-routed service", + 3, generatedSoftConstraints.getCoRouting().getServiceIdentifierList().size()); + assertThat(generatedSoftConstraints.getCoRouting().getServiceIdentifierList(), + IsMapContaining.hasKey( + new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co + .routing.ServiceIdentifierListKey("service 1"))); + assertThat(generatedSoftConstraints.getCoRouting().getServiceIdentifierList(), + IsMapContaining.hasKey( + new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co + .routing.ServiceIdentifierListKey("service 2"))); + assertThat(generatedSoftConstraints.getCoRouting().getServiceIdentifierList(), + IsMapContaining.hasKey( + new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co + .routing.ServiceIdentifierListKey("service 3"))); + } + + @Test + public void testDowngradeHardConstraints() { + HardConstraints initialHardConstraints = null; + HardConstraints genHardConstraints = DowngradeConstraints.downgradeHardConstraints(initialHardConstraints); + assertNotNull("generated hard-constraints should be empty, not null", genHardConstraints); + initialHardConstraints = new HardConstraintsBuilder().build(); + genHardConstraints = DowngradeConstraints.downgradeHardConstraints(initialHardConstraints); + assertNotNull("generated hard-constraints should be empty, not null", genHardConstraints); + initialHardConstraints = buildHardConstraint(null, false, null, "link1", null, Double.valueOf(12.8), false, + false, null, null); + genHardConstraints = DowngradeConstraints.downgradeHardConstraints(initialHardConstraints); + assertEquals("Latency value should be 12.8", + (long) 12.8, genHardConstraints.getLatency().getMaxLatency().longValue()); + assertNull("generated hard constraints should only contain max-latency value", + genHardConstraints.getCoRouting()); + assertNull("generated hard constraints should only contain max-latency value", + genHardConstraints.getExclude()); + assertNull("generated hard constraints should only contain max-latency value", + genHardConstraints.getInclude()); + } + + @Test + public void testConvertToSoftConstraints() { + HardConstraints initialHardConstraints = null; + SoftConstraints genSoftConstraints = DowngradeConstraints.convertToSoftConstraints(initialHardConstraints); + assertNotNull("generated soft constraints should never be null", genSoftConstraints); + assertNull("generated soft constraints should be empty", genSoftConstraints.getExclude()); + assertNull("generated soft constraints should be empty", genSoftConstraints.getCoRouting()); + assertNull("generated soft constraints should be empty", genSoftConstraints.getLatency()); + + List hardCustomerCode = Arrays.asList("customer-code 1", "customer-code 2"); + initialHardConstraints = + buildHardConstraint(hardCustomerCode, false, null, "link1", "node", null, false, false, null, null); + genSoftConstraints = DowngradeConstraints.convertToSoftConstraints(initialHardConstraints); + assertEquals("generated soft constraints should contain customer-code items", 2, + genSoftConstraints.getCustomerCode().size()); + assertTrue(genSoftConstraints.getCustomerCode().contains("customer-code 1")); + assertTrue(genSoftConstraints.getCustomerCode().contains("customer-code 2")); + assertNotNull("generated soft constraints should contain exclude constraint", genSoftConstraints.getExclude()); + assertEquals("generated soft constraints should contain exclude constraint with one link-id", + 1, genSoftConstraints.getExclude().getLinkIdentifier().values().size()); + assertEquals("link-id 1", + genSoftConstraints.getExclude().getLinkIdentifier().values().stream().findAny().get().getLinkId()); + assertEquals("openroadm-topology", + genSoftConstraints.getExclude().getLinkIdentifier().values().stream().findAny().get().getLinkNetworkId()); + assertNotNull("generated soft constraints should contain include constraint", genSoftConstraints.getInclude()); + assertEquals("generated soft constraints should contain include constraint with two node-id", + 2, genSoftConstraints.getInclude().getNodeId().size()); + assertTrue(genSoftConstraints.getInclude().getNodeId().contains(new NodeIdType("node-id-1"))); + assertTrue(genSoftConstraints.getInclude().getNodeId().contains(new NodeIdType("node-id-3"))); + assertNull("generated soft constraints should not contain any latency constraint", + genSoftConstraints.getLatency()); + assertNull("generated soft constraints should not contain any max-distance constraint", + genSoftConstraints.getDistance()); + assertNull("generated soft constraints should not contain any co-routing constraint", + genSoftConstraints.getCoRouting()); } } diff --git a/servicehandler/src/test/java/org/opendaylight/transportpce/servicehandler/utils/ConstraintsUtils.java b/servicehandler/src/test/java/org/opendaylight/transportpce/servicehandler/utils/ConstraintsUtils.java index 93d622cd8..d4758dc3b 100644 --- a/servicehandler/src/test/java/org/opendaylight/transportpce/servicehandler/utils/ConstraintsUtils.java +++ b/servicehandler/src/test/java/org/opendaylight/transportpce/servicehandler/utils/ConstraintsUtils.java @@ -1,5 +1,5 @@ /* - * Copyright © 2018 Orange, Inc. and others. All rights reserved. + * Copyright © 2018 2022 Orange, Inc. 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, @@ -7,62 +7,293 @@ */ package org.opendaylight.transportpce.servicehandler.utils; -import java.util.ArrayList; -import java.util.Collections; +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; import java.util.Map; +import org.opendaylight.yang.gen.v1.http.org.openroadm.common.node.types.rev210528.NodeIdType; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.DiversityConstraints.DiversityType; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.common.constraints.LinkIdentifier; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.common.constraints.LinkIdentifierBuilder; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.CoRouting; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.CoRoutingBuilder; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Distance; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.DistanceBuilder; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Diversity; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.DiversityBuilder; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Exclude; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.ExcludeBuilder; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.HopCount; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.HopCountBuilder; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Include; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.IncludeBuilder; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.Latency; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.LatencyBuilder; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.TEMetric; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.TEMetricBuilder; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co.routing.ServiceIdentifierList; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.constraints.co.routing.ServiceIdentifierListBuilder; +import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service.constraints.ServiceIdentifierListKey; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.equipment.EquipmentBuilder; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraints; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.HardConstraintsBuilder; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.SoftConstraints; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.routing.constraints.SoftConstraintsBuilder; import org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.service.applicability.g.ServiceApplicabilityBuilder; +import org.opendaylight.yangtools.yang.common.Uint32; +import org.opendaylight.yangtools.yang.common.Uint8; /** * Utility Class to Build Hard Constraints and Soft Constraints. - * * @author Ahmed Helmy ( ahmad.helmy@orange.com ) - * + * @author Gilles Thouenon (gilles.thouenon@orange.com) */ public final class ConstraintsUtils { private ConstraintsUtils() { + } + + /** + * Build a rather configurable soft-constraint. + * @param customerCode set the list of customer-code provided + * @param operationalMode set list of operational-mode + * @param diversityServiceList set diversity constraints from serviceListId + * provided + * @param exclude set exclude constraints + * @param include set include constraints + * @param maxLatency set a maxLatency value, null otherwise + * @param hopCount set a max-wdm-hop-count value + * @param teMetric set a max-wdm-TE-metric + * @param maxDistance set a max-distance value, null otherwise + * @param coRoutingServiceId set co-routing constraints + * @return the hard-constraints + */ + public static SoftConstraints buildSoftConstraint(List customerCode, boolean operationalMode, + List diversityServiceList, String exclude, String include, + Double maxLatency, boolean hopCount, boolean teMetric, + String maxDistance, String coRoutingServiceId) { + + HardConstraints baseConstraint = buildHardConstraint(customerCode, operationalMode, diversityServiceList, + exclude, include, maxLatency, hopCount, teMetric, maxDistance, coRoutingServiceId); + return new SoftConstraintsBuilder() + .setCustomerCode(baseConstraint.getCustomerCode()) + .setOperationalMode(baseConstraint.getOperationalMode()) + .setDiversity(baseConstraint.getDiversity()) + .setExclude(baseConstraint.getExclude()) + .setInclude(baseConstraint.getInclude()) + .setLatency(baseConstraint.getLatency()) + .setHopCount(baseConstraint.getHopCount()) + .setTEMetric(baseConstraint.getTEMetric()) + .setDistance(baseConstraint.getDistance()) + .setCoRouting(baseConstraint.getCoRouting()) + .build(); + } + + /** + * Build a rather configurable hard-constraint. + * @param customerCode set the list of customer-code provided + * @param operationalMode set list of operational-mode + * @param diversityServiceList set diversity constraints from serviceListId + * provided + * @param exclude set exclude constraints + * @param include set include constraints + * @param maxLatency set a maxLatency value, null otherwise + * @param hopCount set a max-wdm-hop-count value + * @param teMetric set a max-wdm-TE-metric + * @param maxDistance set a max-distance value, null otherwise + * @param coRoutingServiceId set co-routing constraints + * @return the hard-constraints + */ + public static HardConstraints buildHardConstraint(List customerCode, boolean operationalMode, + List diversityServiceList, String exclude, String include, + Double maxLatency, boolean hopCount, boolean teMetric, + String maxDistance, String coRoutingServiceId) { + + List operationalModeList = null; + if (operationalMode) { + operationalModeList = Arrays.asList("operational-mode 1", "operational-mode 2"); + } + Diversity diversity = null; + if (diversityServiceList != null && !diversityServiceList.isEmpty()) { + Map< + ServiceIdentifierListKey, + org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service + .constraints.ServiceIdentifierList> serviceIdList = new HashMap<>(); + for (String serviceId : diversityServiceList) { + org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing.service + .constraints.ServiceIdentifierList sil = createServiceIdentifierListForDiversity(serviceId); + serviceIdList.put(sil.key(), sil); + } + diversity = new DiversityBuilder() + .setDiversityType(DiversityType.Serial) + .setServiceIdentifierList(serviceIdList) + .build(); + } + + Latency latency = null; + if (maxLatency != null) { + latency = new LatencyBuilder() + .setMaxLatency(new BigDecimal(maxLatency)) + .build(); + } + HopCount hc = null; + if (hopCount) { + hc = new HopCountBuilder() + .setMaxWdmHopCount(Uint8.valueOf(3)) + .build(); + } + Map excludeMap = initialiseExcludeMap(); + Exclude exclu = exclude == null || !excludeMap.containsKey(exclude) + ? null + : excludeMap.get(exclude); + + Map includeMap = initialiseIncludeMap(); + Include inclu = include == null || !includeMap.containsKey(include) + ? null + : includeMap.get(include); + TEMetric tem = null; + if (teMetric) { + tem = new TEMetricBuilder() + .setMaxWdmTEMetric(Uint32.valueOf(8)) + .build(); + } + Distance distance = null; + if (maxDistance != null) { + distance = new DistanceBuilder() + .setMaxDistance(new BigDecimal(maxDistance)) + .build(); + } + Map coRoutingMap = initialiseCoRoutingMap(); + CoRouting coRouting = coRoutingServiceId == null || !coRoutingMap.containsKey(coRoutingServiceId) + ? null + : coRoutingMap.get(coRoutingServiceId); + return new HardConstraintsBuilder() + .setCustomerCode(customerCode) + .setOperationalMode(operationalModeList) + .setDiversity(diversity) + .setExclude(exclu) + .setInclude(inclu) + .setLatency(latency) + .setHopCount(hc) + .setTEMetric(tem) + .setDistance(distance) + .setCoRouting(coRouting) + .build(); } - public static SoftConstraints buildSoftConstraintWithCoRouting() { - ServiceIdentifierList sil = new ServiceIdentifierListBuilder() - .setServiceIdentifier("service-id-soft") + private static org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing + .service.constraints.ServiceIdentifierList createServiceIdentifierListForDiversity(String serviceId) { + return new org.opendaylight.yang.gen.v1.http.org.openroadm.routing.constraints.rev211210.diversity.existing + .service.constraints.ServiceIdentifierListBuilder() + .setServiceIndentifier(serviceId) .setServiceApplicability(new ServiceApplicabilityBuilder() - .setEquipment(new EquipmentBuilder() - .setRoadmSrg(true) - .build()) + .setLink(true) + .setNode(true) + .setSite(false) + .setSrlg(false) .build()) .build(); - return new SoftConstraintsBuilder() - .setCustomerCode(new ArrayList<>(Collections.singletonList("customer-code 1"))) - .setCoRouting(new CoRoutingBuilder() - .setServiceIdentifierList(Map.of(sil.key(), sil)) - .build()) + } + + private static Map initialiseExcludeMap() { + LinkIdentifier linkId1 = new LinkIdentifierBuilder() + .setLinkId("link-id 1") + .setLinkNetworkId("openroadm-topology") + .build(); + Exclude exclude1 = new ExcludeBuilder() + .setLinkIdentifier(Map.of(linkId1.key(), linkId1)) + .build(); + Exclude exclude2 = new ExcludeBuilder() + .setNodeId(List.of(new NodeIdType("node-id-2"))) + .build(); + Exclude exclude3 = new ExcludeBuilder() + .setSupportingServiceName(List.of("supported-service-1", "supported-service-5")) .build(); + Exclude exclude4 = new ExcludeBuilder() + .setFiberBundle(List.of("fiber-1", "fiber-2")) + .build(); + Exclude exclude5 = new ExcludeBuilder() + .setFiberBundle(List.of("fiber-2", "fiber-3")) + .build(); + LinkIdentifier linkId2 = new LinkIdentifierBuilder() + .setLinkId("link-id 2") + .setLinkNetworkId("openroadm-topology") + .build(); + LinkIdentifier linkId3 = new LinkIdentifierBuilder() + .setLinkId("link-id 3") + .setLinkNetworkId("openroadm-topology") + .build(); + Exclude exclude6 = new ExcludeBuilder() + .setLinkIdentifier(Map.of(linkId2.key(), linkId2, linkId3.key(), linkId3)) + .build(); + return Map.of("link1", exclude1, "node", exclude2, "service", exclude3, "fiber1", exclude4, "fiber2", exclude5, + "link2", exclude6); } - public static HardConstraints buildHardConstraintWithCoRouting() { - ServiceIdentifierList sil = new ServiceIdentifierListBuilder() - .setServiceIdentifier("service-id-hard") + private static Map initialiseIncludeMap() { + LinkIdentifier linkId1 = new LinkIdentifierBuilder() + .setLinkId("link-id 1") + .setLinkNetworkId("openroadm-topology") + .build(); + Include exclude1 = new IncludeBuilder() + .setLinkIdentifier(Map.of(linkId1.key(), linkId1)) + .build(); + Include exclude2 = new IncludeBuilder() + .setNodeId(List.of(new NodeIdType("node-id-1"), new NodeIdType("node-id-3"))) + .build(); + Include exclude3 = new IncludeBuilder() + .setSupportingServiceName(List.of("supported-service-1", "supported-service-5")) + .build(); + Include exclude4 = new IncludeBuilder() + .setFiberBundle(List.of("fiber-1", "fiber-2")) + .build(); + Include exclude5 = new IncludeBuilder() + .setFiberBundle(List.of("fiber-2", "fiber-3")) + .build(); + LinkIdentifier linkId2 = new LinkIdentifierBuilder() + .setLinkId("link-id 2") + .setLinkNetworkId("openroadm-topology") + .build(); + LinkIdentifier linkId3 = new LinkIdentifierBuilder() + .setLinkId("link-id 3") + .setLinkNetworkId("openroadm-topology") + .build(); + Include exclude6 = new IncludeBuilder() + .setLinkIdentifier(Map.of(linkId2.key(), linkId2, linkId3.key(), linkId3)) + .build(); + return Map.of("link1", exclude1, "node", exclude2, "service", exclude3, "fiber1", exclude4, "fiber2", exclude5, + "link2", exclude6); + } + + private static Map initialiseCoRoutingMap() { + ServiceIdentifierList sil1 = new ServiceIdentifierListBuilder() + .setServiceIdentifier("service 1") .setServiceApplicability(new ServiceApplicabilityBuilder() .setEquipment(new EquipmentBuilder() .setRoadmSrg(true) .build()) .build()) .build(); - return new HardConstraintsBuilder() - .setCustomerCode(new ArrayList<>(Collections.singletonList("customer-code 1"))) - .setCoRouting(new CoRoutingBuilder() - .setServiceIdentifierList(Map.of(sil.key(), sil)) + ServiceIdentifierList sil2 = new ServiceIdentifierListBuilder() + .setServiceIdentifier("service 2") + .setServiceApplicability(new ServiceApplicabilityBuilder() + .setLink(true) .build()) .build(); + ServiceIdentifierList sil3 = new ServiceIdentifierListBuilder() + .setServiceIdentifier("service 3") + .setServiceApplicability(new ServiceApplicabilityBuilder() + .setSite(true) + .build()) + .build(); + CoRouting coRouting1 = new CoRoutingBuilder() + .setServiceIdentifierList(Map.of(sil1.key(), sil1, sil2.key(), sil2)) + .build(); + CoRouting coRouting2 = new CoRoutingBuilder() + .setServiceIdentifierList(Map.of(sil3.key(), sil3)) + .build(); + return Map.of("coRouting1", coRouting1, "coRouting2", coRouting2); } } -- 2.36.6