summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarek Gradzki <mgradzki@cisco.com>2017-08-24 13:08:49 +0200
committerMarek Gradzki <mgradzki@cisco.com>2017-08-24 13:51:30 +0200
commit31996a6707d75a5f87f7d8c89f563af74e32fe70 (patch)
tree6f615c73e831cf459c0aeb71a8565d7973ae0aa3
parent26e7c2dcd0d1b1e5d9efb475eeb78effb897a731 (diff)
NamingContext.getNameIfPresent should not fail if name is missing
Also makes InterfaceChangeNotificationProducer notification translation code more defensive. The issue was revealed by HC2VPP-216 and HC2VPP-220. Change-Id: I20792a51743ae621d86c1b9066d680bc2303ed82 Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
-rw-r--r--v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/notification/InterfaceChangeNotificationProducer.java8
-rw-r--r--vpp-common/vpp-translate-utils/src/main/java/io/fd/hc2vpp/common/translate/util/NamingContext.java20
-rw-r--r--vpp-common/vpp-translate-utils/src/test/java/io/fd/hc2vpp/common/translate/util/NamingContextTest.java33
3 files changed, 54 insertions, 7 deletions
diff --git a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/notification/InterfaceChangeNotificationProducer.java b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/notification/InterfaceChangeNotificationProducer.java
index e971db2ee..44da8279c 100644
--- a/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/notification/InterfaceChangeNotificationProducer.java
+++ b/v3po/v3po2vpp/src/main/java/io/fd/hc2vpp/v3po/notification/InterfaceChangeNotificationProducer.java
@@ -79,7 +79,13 @@ final class InterfaceChangeNotificationProducer implements ManagedNotificationPr
swInterfaceEventNotification -> {
LOG.trace("Interface notification received: {}", swInterfaceEventNotification);
// TODO HONEYCOMB-166 this should be lazy
- collector.onNotification(transformNotification(swInterfaceEventNotification));
+ try {
+ collector.onNotification(transformNotification(swInterfaceEventNotification));
+ } catch (Exception e) {
+ // There is no need to propagate exception to jvpp rx thread in case of unexpected failures.
+ // We can't do much about it, so lets log the exception.
+ LOG.warn("Failed to process interface notification {}", swInterfaceEventNotification, e);
+ }
}
);
}
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 8e5c31a14..60bd822fb 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,8 +22,10 @@ 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.List;
import java.util.function.Supplier;
import java.util.stream.Collector;
+import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.Contexts;
import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.NamingContextKey;
@@ -93,13 +95,21 @@ public final class NamingContext implements AutoCloseable {
public synchronized Optional<String> getNameIfPresent(final int index,
@Nonnull final MappingContext mappingContext) {
final Optional<Mappings> read = mappingContext.read(namingContextIid.child(Mappings.class));
+ if (!read.isPresent()) {
+ return Optional.absent();
+ }
- return read.isPresent()
- ? Optional.of(read.get().getMapping().stream()
+ final List<Mapping> mappings = read.get().getMapping().stream()
.filter(mapping -> mapping.getIndex().equals(index))
- .collect(SINGLE_ITEM_COLLECTOR)
- .getName())
- : Optional.absent();
+ .collect(Collectors.toList());
+
+ if (mappings.size() > 1) {
+ throw new IllegalStateException("Multiple mappings defined with index=" + index + ": " + mappings);
+ } else if (mappings.size() == 1) {
+ return Optional.of(mappings.get(0).getName());
+ } else {
+ return Optional.absent();
+ }
}
/**
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 c3c753376..1fe4da50a 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
@@ -18,11 +18,13 @@ package io.fd.hc2vpp.common.translate.util;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.common.base.Optional;
+import com.google.common.collect.Lists;
import io.fd.honeycomb.test.tools.HoneycombTestRunner;
import io.fd.honeycomb.test.tools.annotations.InjectTestData;
import io.fd.honeycomb.test.tools.annotations.InjectablesProcessor;
@@ -37,14 +39,15 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
+import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.$YangModuleInfoImpl;
import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.Contexts;
import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.NamingContextKey;
import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.Mappings;
+import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.MappingsBuilder;
import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.Mapping;
import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingBuilder;
import org.opendaylight.yang.gen.v1.urn.honeycomb.params.xml.ns.yang.naming.context.rev160513.contexts.naming.context.mappings.MappingKey;
-import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
@@ -112,6 +115,34 @@ public class NamingContextTest implements InjectablesProcessor {
.getIndex("non-existing", mappingContext, () -> new IllegalArgumentException("Non existing index"));
}
+ @Test(expected = IllegalStateException.class)
+ public void getNameIfPresentFails() {
+ final Mapping mapping1 = mock(Mapping.class);
+ final Mapping mapping2 = mock(Mapping.class);
+ final Mappings mappings = new MappingsBuilder().setMapping(Lists.newArrayList(mapping1, mapping2)).build();
+ when(mappingContext.read(namingContextIid.child(Mappings.class))).thenReturn(Optional.of(mappings));
+
+ namingContext.getNameIfPresent(0, mappingContext);
+ }
+
+ @Test
+ public void getNameIfPresentReturnsAbsent() {
+ final Mapping mapping1 = new MappingBuilder().setIndex(1).setName(NAME_1).build();
+ final Mappings mappings = new MappingsBuilder().setMapping(Lists.newArrayList(mapping1)).build();
+ when(mappingContext.read(namingContextIid.child(Mappings.class))).thenReturn(Optional.of(mappings));
+
+ assertEquals(Optional.absent(), namingContext.getNameIfPresent(0, mappingContext));
+ }
+
+ @Test
+ public void getNameIfPresent() {
+ final Mapping mapping1 = new MappingBuilder().setIndex(1).setName(NAME_1).build();
+ final Mappings mappings = new MappingsBuilder().setMapping(Lists.newArrayList(mapping1)).build();
+ when(mappingContext.read(namingContextIid.child(Mappings.class))).thenReturn(Optional.of(mappings));
+
+ assertEquals(Optional.of(NAME_1), namingContext.getNameIfPresent(1, mappingContext));
+ }
+
private Mapping filterForParent(final String parent) {
return mappings.getMapping().stream()
.filter(mapping -> mapping.getName().equals(parent))