- if no templates available then use range lower end point
Change-Id: I54b3f1f4edd96b800b9b4d22d5bb48ebc918faf0
Signed-off-by: Michal Rehak <mirehak@cisco.com>
(cherry picked from commit
dfe97dfd48667a052b36a8a6a81379f172440496)
import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering;
import com.google.common.collect.Range;
import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering;
import com.google.common.collect.Range;
+import java.util.Optional;
import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.util.EPTemplateUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.model.rev160302.sxp.ep.mapper.EndpointPolicyTemplateBySgt;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.rev160722.SgtGeneratorConfig;
import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.util.EPTemplateUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.model.rev160302.sxp.ep.mapper.EndpointPolicyTemplateBySgt;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.rev160722.SgtGeneratorConfig;
import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
-import java.util.Optional;
-
/**
* Purpose: generate {@link Sgt} value on demand
*/
/**
* Purpose: generate {@link Sgt} value on demand
*/
*/
public java.util.Optional<Sgt> generateNextSgt(SimpleCachedDao<Sgt, EndpointPolicyTemplateBySgt> templateCache) {
return sgtRange.flatMap(range ->
*/
public java.util.Optional<Sgt> generateNextSgt(SimpleCachedDao<Sgt, EndpointPolicyTemplateBySgt> templateCache) {
return sgtRange.flatMap(range ->
- findTopUsedSgt(templateCache.keySet())
+ findTopUsedSgt(templateCache.keySet(), range.lowerEndpoint())
.map(topUsedSgt -> incrementSafely(range, topUsedSgt))
);
}
.map(topUsedSgt -> incrementSafely(range, topUsedSgt))
);
}
- private Optional<Sgt> findTopUsedSgt(final Iterable<Sgt> sgts) {
- return java.util.Optional.ofNullable(sgts)
- .filter(sgtBag -> !Iterables.isEmpty(sgtBag))
- .map(sgtOrdering::max);
+ private Optional<Sgt> findTopUsedSgt(final Iterable<Sgt> sgts, final Integer lowerEndPoint) {
+ final Sgt sgt = java.util.Optional.ofNullable(sgts)
+ .filter(sgtBag -> ! Iterables.isEmpty(sgtBag))
+ .map(sgtOrdering::max)
+ .orElseGet(() -> new Sgt(lowerEndPoint - 1));
+ return Optional.of(sgt);
}
private Sgt incrementSafely(final Range<Integer> range, final Sgt topUsedSgt) {
}
private Sgt incrementSafely(final Range<Integer> range, final Sgt topUsedSgt) {
@Test
public void testGenerateNextSgt_noData() throws Exception {
final Optional<Sgt> sgt = generator.generateNextSgt(templateDao);
@Test
public void testGenerateNextSgt_noData() throws Exception {
final Optional<Sgt> sgt = generator.generateNextSgt(templateDao);
- Assert.assertFalse(sgt.isPresent());
+ Assert.assertTrue(sgt.isPresent());
+ Assert.assertEquals(10, sgt.get().getValue().intValue());