summaryrefslogtreecommitdiffstats
path: root/vpp-common/vpp-translate-utils/src/main
diff options
context:
space:
mode:
authorJan Srnicek <jsrnicek@cisco.com>2017-08-01 10:11:42 +0200
committerJan Srnicek <jsrnicek@cisco.com>2017-08-01 10:11:42 +0200
commit36f5d08e1e7fb2838d88133140f5dab53f8d9882 (patch)
tree0927e68f451909a5b155dc5d1f859e64bfcd5713 /vpp-common/vpp-translate-utils/src/main
parent5c12a3c798386ad84b76d9794d787e0497aaec4f (diff)
HC2VPP-187 - Naming context improvements
Adds option to throw specific exception on non existing mapping Change-Id: I1903ad8d0d660380680f99f0252e7ec6d9c8862d Signed-off-by: Jan Srnicek <jsrnicek@cisco.com>
Diffstat (limited to 'vpp-common/vpp-translate-utils/src/main')
-rw-r--r--vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/NamingContext.java21
1 files changed, 21 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