diff options
Diffstat (limited to 'vpp-common')
3 files changed, 39 insertions, 23 deletions
diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/MultiNamingContext.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/MultiNamingContext.java index 6b10881d6..4d0e7a98f 100644 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/MultiNamingContext.java +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/MultiNamingContext.java @@ -23,6 +23,7 @@ import com.google.common.base.Optional; import io.fd.honeycomb.translate.MappingContext; import io.fd.honeycomb.translate.util.RWUtils; import java.util.Collections; +import java.util.OptionalInt; import java.util.stream.Collectors; import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.multi.naming.context.rev160411.MultiMappingCtxAugmentation; @@ -134,15 +135,17 @@ public class MultiNamingContext { return startIndex; } - return read.get().getMapping() - .stream() - .filter(mapping -> mapping.getName().equals(parentName)) - .flatMap(mapping -> mapping.getValue().stream()) - .mapToInt(Value::getIndex) - // do not use i++(need increase before, not after - .map(i -> ++i) - .max() - .orElse(startIndex); + final OptionalInt max = read.get().getMapping() + .stream() + .filter(mapping -> mapping.getName().equals(parentName)) + .flatMap(mapping -> mapping.getValue().stream()) + .mapToInt(Value::getIndex) + .max(); + if (max.isPresent()) { + return max.getAsInt() + 1; + } else { + return startIndex; + } } private KeyedInstanceIdentifier<Mapping, MappingKey> getMappingIid(final String name) { diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/NamingContext.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/NamingContext.java index 60bd822fb..da7be8109 100644 --- a/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/NamingContext.java +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/NamingContext.java @@ -43,6 +43,7 @@ import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier; public final class NamingContext implements AutoCloseable { private static final Collector<Mapping, ?, Mapping> SINGLE_ITEM_COLLECTOR = RWUtils.singleItemCollector(); + private static final int START_INDEX = 0; private final String artificialNamePrefix; private final KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.NamingContext, NamingContextKey> namingContextIid; @@ -220,17 +221,14 @@ public final class NamingContext implements AutoCloseable { private int getNextAvailableIndex(final MappingContext mappingContext) { final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class)); - if (!read.isPresent()) { - return 0; + if (!read.isPresent() || read.get().getMapping().isEmpty()) { + return START_INDEX; } - return read.get().getMapping() - .stream() - .mapToInt(Mapping::getIndex) - // do not use i++(need increase before, not after - .map(i -> ++i) - .max() - .orElse(0); + return 1 + read.get().getMapping() + .stream() + .mapToInt(Mapping::getIndex) + .max().getAsInt(); } @Override diff --git a/vpp-common/vpp-translate-utils/src/test/java/io/fd/hc2vpp/common/translate/util/NamingContextTest.java b/vpp-common/vpp-translate-utils/src/test/java/io/fd/hc2vpp/common/translate/util/NamingContextTest.java index 1fe4da50a..df561c737 100644 --- a/vpp-common/vpp-translate-utils/src/test/java/io/fd/hc2vpp/common/translate/util/NamingContextTest.java +++ b/vpp-common/vpp-translate-utils/src/test/java/io/fd/hc2vpp/common/translate/util/NamingContextTest.java @@ -96,16 +96,31 @@ public class NamingContextTest implements InjectablesProcessor { } @Test - public void addNameNextIndex() throws Exception { + public void addName() throws Exception { namingContext.addName("name-3", mappingContext); verify(mappingContext, times(1)) - .put(instanceIdentifierArgumentCaptor.capture(), mappingArgumentCaptor.capture()); + .put(instanceIdentifierArgumentCaptor.capture(), mappingArgumentCaptor.capture()); assertEquals(instanceIdentifierArgumentCaptor.getValue(), parentKey("name-3")); assertEquals(mappingArgumentCaptor.getValue(), new MappingBuilder() - .setIndex(3) - .setName("name-3") - .build()); + .setIndex(3) + .setName("name-3") + .build()); + } + + @Test + public void addNameNoMapings() throws Exception { + when(mappingContext.read(namingContextIid.child(Mappings.class))).thenReturn(Optional.absent()); + + namingContext.addName("name-0", mappingContext); + verify(mappingContext, times(1)) + .put(instanceIdentifierArgumentCaptor.capture(), mappingArgumentCaptor.capture()); + + assertEquals(instanceIdentifierArgumentCaptor.getValue(), parentKey("name-0")); + assertEquals(mappingArgumentCaptor.getValue(), new MappingBuilder() + .setIndex(0) + .setName("name-0") + .build()); } @Test(expected = IllegalArgumentException.class) |