summaryrefslogtreecommitdiffstats
path: root/v3po/v3po2vpp/src/main
diff options
context:
space:
mode:
authorJan Srnicek <jsrnicek@cisco.com>2016-09-12 13:12:14 +0200
committerMaros Marsalek <mmarsale@cisco.com>2016-09-12 15:41:32 +0000
commitf5d2adbba6f44f22c7cf0d5a34330ce3450428a0 (patch)
tree7017ced973290ae05c0ca8d6d990a4db9d41f674 /v3po/v3po2vpp/src/main
parent19306b9f0c436302d2c77b9bb39b69699526d9be (diff)
HONEYCOMB-142 - Subnet validation
Added validation of address to not be from same subnet Refactored to detect conflicts with mixed types of subnets and report them better Change-Id: Ib815a79c9f61e88b5097884e346320028bbfe914 Signed-off-by: Jan Srnicek <jsrnicek@cisco.com>
Diffstat (limited to 'v3po/v3po2vpp/src/main')
-rw-r--r--v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/Ipv4AddressCustomizer.java71
-rw-r--r--v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/Ipv4Customizer.java23
-rw-r--r--v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/Ipv4WriteUtils.java4
-rw-r--r--v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/subnet/validation/SubnetValidationException.java50
-rw-r--r--v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/subnet/validation/SubnetValidator.java80
5 files changed, 189 insertions, 39 deletions
diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/Ipv4AddressCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/Ipv4AddressCustomizer.java
index e7b932ae0..574a42eb5 100644
--- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/Ipv4AddressCustomizer.java
+++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/Ipv4AddressCustomizer.java
@@ -18,13 +18,18 @@ package io.fd.honeycomb.translate.v3po.interfaces.ip;
import static com.google.common.base.Preconditions.checkNotNull;
+import com.google.common.base.Optional;
import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
+import io.fd.honeycomb.translate.util.RWUtils;
+import io.fd.honeycomb.translate.v3po.interfaces.ip.subnet.validation.SubnetValidationException;
+import io.fd.honeycomb.translate.v3po.interfaces.ip.subnet.validation.SubnetValidator;
import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
import io.fd.honeycomb.translate.v3po.util.NamingContext;
import io.fd.honeycomb.translate.write.WriteContext;
import io.fd.honeycomb.translate.write.WriteFailedException;
import javax.annotation.Nonnull;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv4;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Address;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.AddressKey;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.Subnet;
@@ -44,37 +49,65 @@ public class Ipv4AddressCustomizer extends FutureJVppCustomizer implements ListW
private static final Logger LOG = LoggerFactory.getLogger(Ipv4AddressCustomizer.class);
private final NamingContext interfaceContext;
+ private final SubnetValidator subnetValidator;
- public Ipv4AddressCustomizer(FutureJVppCore futureJVppCore, NamingContext interfaceContext) {
+ Ipv4AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore, @Nonnull final NamingContext interfaceContext,
+ @Nonnull final SubnetValidator subnetValidator) {
super(futureJVppCore);
- this.interfaceContext = interfaceContext;
+ this.interfaceContext = checkNotNull(interfaceContext, "Interface context cannot be null");
+ this.subnetValidator = checkNotNull(subnetValidator, "Subnet validator cannot be null");
+ }
+
+ public Ipv4AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
+ @Nonnull final NamingContext interfaceContext) {
+ this(futureJVppCore, interfaceContext, new SubnetValidator());
}
@Override
public void writeCurrentAttributes(InstanceIdentifier<Address> id, Address dataAfter, WriteContext writeContext)
- throws WriteFailedException {
- setAddress(true, id, dataAfter, writeContext);
+ throws WriteFailedException {
+
+ final String interfaceName = id.firstKeyOf(Interface.class).getName();
+ final int interfaceIndex = interfaceContext.getIndex(interfaceName, writeContext.getMappingContext());
+
+ // checks whether address is not from same subnet of some address already defined on this interface
+ try {
+
+ final InstanceIdentifier<Ipv4> parentId = RWUtils.cutId(id, InstanceIdentifier.create(Ipv4.class));
+ final Optional<Ipv4> ipv4Optional = writeContext.readAfter(parentId);
+
+ //no need to check isPresent() - we are inside of address customizer, therefore there must be Address data
+ //that is being processed by infrastructure
+
+ subnetValidator.checkNotAddingToSameSubnet(ipv4Optional.get().getAddress());
+ } catch (SubnetValidationException e) {
+ throw new WriteFailedException(id, e);
+ }
+
+ setAddress(true, id, interfaceName, interfaceIndex, dataAfter, writeContext);
}
@Override
public void updateCurrentAttributes(InstanceIdentifier<Address> id, Address dataBefore, Address dataAfter,
WriteContext writeContext) throws WriteFailedException {
throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
- new UnsupportedOperationException("Operation not supported"));
+ new UnsupportedOperationException("Operation not supported"));
}
@Override
public void deleteCurrentAttributes(InstanceIdentifier<Address> id, Address dataBefore, WriteContext writeContext)
- throws WriteFailedException {
- setAddress(false, id, dataBefore, writeContext);
- }
-
- private void setAddress(boolean add, final InstanceIdentifier<Address> id, final Address address,
- final WriteContext writeContext) throws WriteFailedException {
+ throws WriteFailedException {
final String interfaceName = id.firstKeyOf(Interface.class).getName();
final int interfaceIndex = interfaceContext.getIndex(interfaceName, writeContext.getMappingContext());
+ setAddress(false, id, interfaceName, interfaceIndex, dataBefore, writeContext);
+ }
+
+ private void setAddress(boolean add, final InstanceIdentifier<Address> id, final String interfaceName,
+ final int interfaceIndex, final Address address,
+ final WriteContext writeContext) throws WriteFailedException {
+
Subnet subnet = address.getSubnet();
if (subnet instanceof PrefixLength) {
@@ -90,10 +123,10 @@ public class Ipv4AddressCustomizer extends FutureJVppCustomizer implements ListW
private void setNetmaskSubnet(final boolean add, @Nonnull final InstanceIdentifier<Address> id,
@Nonnull final String interfaceName, final int interfaceIndex,
@Nonnull final Address address, @Nonnull final Netmask subnet)
- throws WriteFailedException {
+ throws WriteFailedException {
try {
LOG.debug("Setting Subnet(subnet-mask) for interface: {}(id={}). Subnet: {}, address: {}",
- interfaceName, interfaceIndex, subnet, address);
+ interfaceName, interfaceIndex, subnet, address);
final DottedQuad netmask = subnet.getNetmask();
checkNotNull(netmask, "netmask value should not be null");
@@ -102,7 +135,7 @@ public class Ipv4AddressCustomizer extends FutureJVppCustomizer implements ListW
Ipv4WriteUtils.addDelAddress(getFutureJVpp(), add, id, interfaceIndex, address.getIp(), subnetLength);
} catch (VppBaseCallException e) {
LOG.warn("Failed to set Subnet(subnet-mask) for interface: {}(id={}). Subnet: {}, address: {}",
- interfaceName, interfaceIndex, subnet, address);
+ interfaceName, interfaceIndex, subnet, address);
throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass(), e);
}
}
@@ -110,19 +143,19 @@ public class Ipv4AddressCustomizer extends FutureJVppCustomizer implements ListW
private void setPrefixLengthSubnet(final boolean add, @Nonnull final InstanceIdentifier<Address> id,
@Nonnull final String interfaceName, final int interfaceIndex,
@Nonnull final Address address, @Nonnull final PrefixLength subnet)
- throws WriteFailedException {
+ throws WriteFailedException {
try {
LOG.debug("Setting Subnet(prefix-length) for interface: {}(id={}). Subnet: {}, address: {}",
- interfaceName, interfaceIndex, subnet, address);
+ interfaceName, interfaceIndex, subnet, address);
Ipv4WriteUtils.addDelAddress(getFutureJVpp(), add, id, interfaceIndex, address.getIp(),
- subnet.getPrefixLength().byteValue());
+ subnet.getPrefixLength().byteValue());
LOG.debug("Subnet(prefix-length) set successfully for interface: {}(id={}). Subnet: {}, address: {}",
- interfaceName, interfaceIndex, subnet, address);
+ interfaceName, interfaceIndex, subnet, address);
} catch (VppBaseCallException e) {
LOG.warn("Failed to set Subnet(prefix-length) for interface: {}(id={}). Subnet: {}, address: {}",
- interfaceName, interfaceIndex, subnet, address);
+ interfaceName, interfaceIndex, subnet, address);
throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass(), e);
}
}
diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/Ipv4Customizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/Ipv4Customizer.java
index 0e0bba3b0..2c5230cb5 100644
--- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/Ipv4Customizer.java
+++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/Ipv4Customizer.java
@@ -42,20 +42,19 @@ public class Ipv4Customizer extends FutureJVppCustomizer implements WriterCustom
@Override
public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Ipv4> id,
@Nonnull final Ipv4 dataAfter, @Nonnull final WriteContext writeContext)
- throws WriteFailedException {
- final String ifcName = id.firstKeyOf(Interface.class).getName();
- setIpv4(id, ifcName, dataAfter, writeContext);
+ throws WriteFailedException {
+
+ //TODO - add subnet validation after HONEYCOMB-201
}
@Override
public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Ipv4> id,
@Nonnull final Ipv4 dataBefore, @Nonnull final Ipv4 dataAfter,
@Nonnull final WriteContext writeContext)
- throws WriteFailedException {
+ throws WriteFailedException {
final String ifcName = id.firstKeyOf(Interface.class).getName();
- // TODO HONEYCOMB-180 handle update in a better way
- setIpv4(id, ifcName, dataAfter, writeContext);
+ // TODO handle update in a better way
}
@Override
@@ -64,16 +63,4 @@ public class Ipv4Customizer extends FutureJVppCustomizer implements WriterCustom
// TODO HONEYCOMB-180 implement delete
}
- private void setIpv4(final InstanceIdentifier<Ipv4> id, final String name, final Ipv4 ipv4,
- final WriteContext writeContext)
- throws WriteFailedException {
- final int swIfc = interfaceContext.getIndex(name, writeContext.getMappingContext());
-
- LOG.warn("Ignoring Ipv4 leaf nodes (create/update is not supported)");
- // TODO HONEYCOMB-180 add support for:
- // enabled leaf
- // forwarding leaf
- // mtu leaf
- }
-
}
diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/Ipv4WriteUtils.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/Ipv4WriteUtils.java
index ab31debc9..412030200 100644
--- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/Ipv4WriteUtils.java
+++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/Ipv4WriteUtils.java
@@ -35,7 +35,7 @@ import org.openvpp.jvpp.core.future.FutureJVppCore;
* Utility class providing Ipv4 CUD support.
*/
// TODO HONEYCOMB-175 replace with interface with default methods or abstract class
-final class Ipv4WriteUtils {
+public final class Ipv4WriteUtils {
private static final int DOTTED_QUAD_MASK_LENGTH = 4;
private static final int IPV4_ADDRESS_PART_BITS_COUNT = 8;
@@ -83,7 +83,7 @@ final class Ipv4WriteUtils {
* @param mask the subnet mask in dot notation 255.255.255.255
* @return the prefix length as number of bits
*/
- static byte getSubnetMaskLength(final String mask) {
+ public static byte getSubnetMaskLength(final String mask) {
String[] maskParts = mask.split("\\.");
checkArgument(maskParts.length == DOTTED_QUAD_MASK_LENGTH,
diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/subnet/validation/SubnetValidationException.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/subnet/validation/SubnetValidationException.java
new file mode 100644
index 000000000..ae8291027
--- /dev/null
+++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/subnet/validation/SubnetValidationException.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2016 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.honeycomb.translate.v3po.interfaces.ip.subnet.validation;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.Collection;
+import javax.annotation.Nonnull;
+import org.apache.commons.lang3.StringUtils;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Address;
+
+/**
+ * Thrown as negative result of subnet validation
+ */
+public class SubnetValidationException extends Exception {
+
+ private SubnetValidationException(@Nonnull final String message) {
+ super(message);
+ }
+
+ public static SubnetValidationException forConflictingData(@Nonnull final Short prefix, @Nonnull Collection<Address> addresses) {
+ return new SubnetValidationException(
+ "Attempt to define multiple addresses for same subnet[prefixLen = " + prefixToString(prefix) + "], "
+ + "addresses : " + addressesToString(addresses));
+ }
+
+ private static String prefixToString(final Short prefix) {
+ return checkNotNull(prefix, "Cannot create " + SubnetValidationException.class.getName() + " for null prefix")
+ .toString();
+ }
+
+ private static String addressesToString(final Collection<Address> addresses) {
+ return StringUtils.join(checkNotNull(addresses,
+ "Cannot create " + SubnetValidationException.class.getName() + " for null address list"), " | ");
+ }
+}
diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/subnet/validation/SubnetValidator.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/subnet/validation/SubnetValidator.java
new file mode 100644
index 000000000..361113858
--- /dev/null
+++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/interfaces/ip/subnet/validation/SubnetValidator.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2016 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.honeycomb.translate.v3po.interfaces.ip.subnet.validation;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Multimaps;
+import io.fd.honeycomb.translate.v3po.interfaces.ip.Ipv4WriteUtils;
+import java.util.List;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.Subnet;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.subnet.Netmask;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.subnet.PrefixLength;
+
+/**
+ * Validator for detecting if there is an attempt to assign multiple addresses from same subnet
+ */
+public class SubnetValidator {
+
+ /**
+ * Checks whether data provided for writing are not in collision with already existing data
+ */
+ public void checkNotAddingToSameSubnet(@Nonnull final List<Address> addresses)
+ throws SubnetValidationException {
+
+ final Multimap<Short, Address> prefixLengthRegister = Multimaps.index(addresses, toPrefixLength());
+ final int keySetSize = prefixLengthRegister.keySet().size();
+
+ if (keySetSize == 0 || keySetSize == addresses.size()) {
+ //this means that every key is unique(has only one value) or no addresses were prefix-length based ,so there is no conflict
+ return;
+ }
+
+ //finds conflicting prefix
+ final Short conflictingPrefix = prefixLengthRegister.keySet()
+ .stream()
+ .filter(a -> prefixLengthRegister.get(a).size() > 1)
+ .findFirst()
+ .get();
+
+ //and reports it with affected addresses
+ throw SubnetValidationException
+ .forConflictingData(conflictingPrefix, prefixLengthRegister.get(conflictingPrefix));
+ }
+
+ private static Function<Address, Short> toPrefixLength() {
+ return (final Address address) -> {
+ final Subnet subnet = address.getSubnet();
+
+ if (subnet instanceof PrefixLength) {
+ return ((PrefixLength) subnet).getPrefixLength();
+ }
+
+ if (address.getSubnet() instanceof Netmask) {
+ return (short) Ipv4WriteUtils.getSubnetMaskLength(
+ checkNotNull(((Netmask) subnet).getNetmask(), "No netmask defined for %s", subnet)
+ .getValue());
+ }
+
+ throw new IllegalArgumentException("Unsupported subnet : " + subnet);
+ };
+ }
+}