summaryrefslogtreecommitdiffstats
path: root/nat
diff options
context:
space:
mode:
authorMarek Gradzki <mgradzki@cisco.com>2018-05-16 12:42:01 +0200
committerMarek Gradzki <mgradzki@cisco.com>2018-05-17 05:20:09 +0000
commit6971c996c4056952c6f4ec48d5985e1a0333d97a (patch)
tree9c975199ef6482148263ce5c320377d4f2a104d1 /nat
parentf031dcade0a8a9b8b7d6af4f4afe0d690e9246bc (diff)
HC2VPP-320: ensure at most one policy is configured per NAT instance
ReflexiveListReaderCustomizer is not used due to HONEYCOMB-437 Change-Id: I62755aaedd6e179b349ec47e4a304399c4fa18cd Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
Diffstat (limited to 'nat')
-rw-r--r--nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/read/NatReaderFactory.java3
-rw-r--r--nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/read/PolicyCustomizer.java75
-rw-r--r--nat/nat2vpp/src/test/java/io/fd/hc2vpp/nat/read/PolicyCustomizerTest.java76
3 files changed, 152 insertions, 2 deletions
diff --git a/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/read/NatReaderFactory.java b/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/read/NatReaderFactory.java
index c89b3030f..bf1517646 100644
--- a/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/read/NatReaderFactory.java
+++ b/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/read/NatReaderFactory.java
@@ -86,8 +86,7 @@ public class NatReaderFactory implements ReaderFactory {
new GenericInitListReader<>(MAPPING_ENTRY_ID,
new MappingEntryCustomizer(mapEntryNat44DumpMgr, mapEntryNat64DumpMgr, mappingEntryContext)));
- // Ony single policy is supported
- registry.addStructuralListReader(POLICY_ID, PolicyBuilder.class, Collections.singletonList(new PolicyKey(0L)));
+ registry.add(new GenericInitListReader<>(POLICY_ID, new PolicyCustomizer()));
registry.add(new GenericInitListReader<>(ADDRESS_POOL_ID, new ExternalIpPoolCustomizer(jvppNat)));
// nat64-prefixes
diff --git a/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/read/PolicyCustomizer.java b/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/read/PolicyCustomizer.java
new file mode 100644
index 000000000..4b23451dc
--- /dev/null
+++ b/nat/nat2vpp/src/main/java/io/fd/hc2vpp/nat/read/PolicyCustomizer.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2018 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.hc2vpp.nat.read;
+
+import io.fd.honeycomb.translate.read.ReadContext;
+import io.fd.honeycomb.translate.read.ReadFailedException;
+import io.fd.honeycomb.translate.spi.read.Initialized;
+import io.fd.honeycomb.translate.spi.read.InitializingListReaderCustomizer;
+import java.util.Collections;
+import java.util.List;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.nat.instances.Instance;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.nat.instances.InstanceBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.nat.instances.instance.Policy;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.nat.instances.instance.PolicyBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.nat.instances.instance.PolicyKey;
+import org.opendaylight.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+final class PolicyCustomizer implements InitializingListReaderCustomizer<Policy, PolicyKey, PolicyBuilder> {
+
+ static final long DEFAULT_POLICY_ID = 0;
+ private static final List<PolicyKey> IDS = Collections.singletonList(new PolicyKey(DEFAULT_POLICY_ID));
+
+ @Nonnull
+ @Override
+ public PolicyBuilder getBuilder(@Nonnull final InstanceIdentifier<Policy> id) {
+ return new PolicyBuilder();
+ }
+
+ @Override
+ public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Policy> id,
+ @Nonnull final PolicyBuilder builder, @Nonnull final ReadContext ctx)
+ throws ReadFailedException {
+ final Long policyId = id.firstKeyOf(Policy.class).getId();
+ if (policyId == 0) {
+ builder.setId(policyId);
+ }
+ }
+
+ @Nonnull
+ @Override
+ public List<PolicyKey> getAllIds(@Nonnull final InstanceIdentifier<Policy> id,
+ @Nonnull final ReadContext context) throws ReadFailedException {
+ return IDS;
+ }
+
+ @Override
+ public void merge(@Nonnull final Builder<? extends DataObject> builder, @Nonnull final List<Policy> readData) {
+ ((InstanceBuilder) builder).setPolicy(readData);
+ }
+
+ @Override
+ public Initialized<Policy> init(
+ @Nonnull final InstanceIdentifier<Policy> id,
+ @Nonnull final Policy readValue,
+ @Nonnull final ReadContext ctx) {
+ return Initialized.create(id, readValue);
+ }
+}
diff --git a/nat/nat2vpp/src/test/java/io/fd/hc2vpp/nat/read/PolicyCustomizerTest.java b/nat/nat2vpp/src/test/java/io/fd/hc2vpp/nat/read/PolicyCustomizerTest.java
new file mode 100644
index 000000000..f1fe82ce1
--- /dev/null
+++ b/nat/nat2vpp/src/test/java/io/fd/hc2vpp/nat/read/PolicyCustomizerTest.java
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2018 Cisco and/or its affiliates.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package io.fd.hc2vpp.nat.read;
+
+import static io.fd.hc2vpp.nat.read.PolicyCustomizer.DEFAULT_POLICY_ID;
+import static org.hamcrest.core.IsCollectionContaining.hasItems;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
+
+import io.fd.hc2vpp.common.test.read.ListReaderCustomizerTest;
+import io.fd.hc2vpp.nat.NatIds;
+import io.fd.honeycomb.translate.read.ReadFailedException;
+import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.nat.Instances;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.nat.instances.Instance;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.nat.instances.InstanceBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.nat.instances.InstanceKey;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.nat.instances.instance.Policy;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.nat.instances.instance.PolicyBuilder;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.nat.rev180223.nat.instances.instance.PolicyKey;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+public class PolicyCustomizerTest extends ListReaderCustomizerTest<Policy, PolicyKey, PolicyBuilder> {
+
+ private static final InstanceIdentifier<Instance> INSTANCE_ID = InstanceIdentifier.create(Instances.class)
+ .child(Instance.class, new InstanceKey(NatInstanceCustomizer.DEFAULT_VRF_ID));
+
+ public PolicyCustomizerTest() {
+ super(Policy.class, InstanceBuilder.class);
+ }
+
+ @Override
+ protected ReaderCustomizer<Policy, PolicyBuilder> initCustomizer() {
+ return new PolicyCustomizer();
+ }
+
+ @Test
+ public void testGetAllIds() throws ReadFailedException {
+ assertThat(getCustomizer().getAllIds(NatIds.POLICY_ID, ctx), hasItems(new PolicyKey(0L)));
+ }
+
+ @Test
+ public void testReadDefault() throws ReadFailedException {
+ final PolicyBuilder builder = mock(PolicyBuilder.class);
+ getCustomizer().readCurrentAttributes(getId(DEFAULT_POLICY_ID), builder, ctx);
+ verify(builder).setId(DEFAULT_POLICY_ID);
+ }
+
+ @Test
+ public void testReadNonDefault() throws ReadFailedException {
+ final PolicyBuilder builder = mock(PolicyBuilder.class);
+ getCustomizer().readCurrentAttributes(getId(1L), builder, ctx);
+ verifyZeroInteractions(builder);
+ }
+
+ private static InstanceIdentifier<Policy> getId(final long id) {
+ return INSTANCE_ID.child(Policy.class, new PolicyKey(id));
+ }
+} \ No newline at end of file