BUG-6858: adapt to ise api, fix sgt-generator 07/46607/1
authorMichal Rehak <mirehak@cisco.com>
Thu, 6 Oct 2016 10:30:00 +0000 (12:30 +0200)
committerMichal Rehak <mirehak@cisco.com>
Thu, 6 Oct 2016 10:39:39 +0000 (12:39 +0200)
    - if no templates available then use range lower end point

Change-Id: I54b3f1f4edd96b800b9b4d22d5bb48ebc918faf0
Signed-off-by: Michal Rehak <mirehak@cisco.com>
sxp-integration/sxp-ep-provider/src/main/java/org/opendaylight/groupbasedpolicy/sxp/ep/provider/impl/SgtGeneratorImpl.java
sxp-integration/sxp-ep-provider/src/test/java/org/opendaylight/groupbasedpolicy/sxp/ep/provider/impl/SgtGeneratorImplTest.java

index 1c4ec781254c8b7faeaac9447de7062f29abe141..ea8ee04739975b3fe59545d6cfecbe17dd00caba 100644 (file)
@@ -11,13 +11,12 @@ package org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl;
 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 java.util.Optional;
-
 /**
  * Purpose: generate {@link Sgt} value on demand
  */
@@ -39,15 +38,17 @@ public class SgtGeneratorImpl {
      */
     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))
         );
     }
 
-    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) {
index 2d5b46ca8248233ee5bb13e0c96d87dbf7223cf6..1b5900cebcaba0eb4067e2acf9a581fcab487e55 100644 (file)
@@ -51,7 +51,8 @@ public class SgtGeneratorImplTest {
     @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());
     }
 
     @Test