summaryrefslogtreecommitdiffstats
path: root/nat/nat2vpp/src/main
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/nat2vpp/src/main
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/nat2vpp/src/main')
-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
2 files changed, 76 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);
+ }
+}