summaryrefslogtreecommitdiffstats
path: root/vpp-common/vpp-translate-utils
diff options
context:
space:
mode:
Diffstat (limited to 'vpp-common/vpp-translate-utils')
-rw-r--r--vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/NamingContext.java21
-rw-r--r--vpp-common/vpp-translate-utils/src/test/java/io/fd/hc2vpp/common/translate/util/NamingContextTest.java8
2 files changed, 29 insertions, 0 deletions
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 5e0eea71b..8e5c31a14 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
@@ -22,6 +22,7 @@ import static com.google.common.base.Preconditions.checkState;
import com.google.common.base.Optional;
import io.fd.honeycomb.translate.MappingContext;
import io.fd.honeycomb.translate.util.RWUtils;
+import java.util.function.Supplier;
import java.util.stream.Collector;
import javax.annotation.Nonnull;
import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.Contexts;
@@ -169,6 +170,26 @@ public final class NamingContext implements AutoCloseable {
}
/**
+ * Returns index value associated with the given name.
+ *
+ * @param name the name whose associated index value is to be returned
+ * @param mappingContext mapping context providing context data for current transaction
+ * @param throwIfNonExisting if mapping was not found, supplied exception will be thrown
+ * @return integer index value matching supplied name
+ * @throws T if name was not found
+ */
+ public synchronized <T extends Throwable> int getIndex(final String name,
+ final MappingContext mappingContext,
+ final Supplier<T> throwIfNonExisting) throws T {
+ // supplier is used to not instantiate exception if not needed
+ final Optional<Mapping> read = mappingContext.read(getMappingIid(name));
+ if (!read.isPresent()) {
+ throw throwIfNonExisting.get();
+ }
+ return read.get().getIndex();
+ }
+
+ /**
* Check whether mapping is present for name.
*
* @param name name of a mapped item
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 4e66315e1..c85a874c3 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
@@ -17,6 +17,7 @@
package io.fd.hc2vpp.common.translate.util;
import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -104,6 +105,13 @@ public class NamingContextTest implements InjectablesProcessor {
.build());
}
+ @Test(expected = IllegalArgumentException.class)
+ public void getAndThrow() {
+ when(mappingContext.read(any())).thenReturn(Optional.absent());
+ namingContext
+ .getIndex("non-existing", mappingContext, () -> new IllegalArgumentException("Non existing index"));
+ }
+
private Mapping filterForParent(final String parent) {
return mappings.getMapping().stream()
.filter(mapping -> mapping.getName().equals(parent))