2 * Copyright (c) 2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package io.fd.honeycomb.translate.v3po.interfaces.ip.subnet.validation;
19 import static com.google.common.base.Preconditions.checkNotNull;
21 import com.google.common.base.Function;
22 import com.google.common.collect.Multimap;
23 import com.google.common.collect.Multimaps;
24 import io.fd.honeycomb.translate.v3po.interfaces.ip.Ipv4Writer;
25 import java.util.List;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Address;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.Subnet;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.subnet.Netmask;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.subnet.PrefixLength;
33 * Validator for detecting if there is an attempt to assign multiple addresses from same subnet
35 public class SubnetValidator implements Ipv4Writer {
38 * Checks whether data provided for writing are not in collision with already existing data
40 public void checkNotAddingToSameSubnet(@Nonnull final List<Address> addresses)
41 throws SubnetValidationException {
43 final Multimap<Short, Address> prefixLengthRegister = Multimaps.index(addresses, toPrefixLength());
44 final int keySetSize = prefixLengthRegister.keySet().size();
46 if (keySetSize == 0 || keySetSize == addresses.size()) {
47 //this means that every key is unique(has only one value) or no addresses were prefix-length based ,so there is no conflict
51 //finds conflicting prefix
52 final Short conflictingPrefix = prefixLengthRegister.keySet()
54 .filter(a -> prefixLengthRegister.get(a).size() > 1)
58 //and reports it with affected addresses
59 throw SubnetValidationException
60 .forConflictingData(conflictingPrefix, prefixLengthRegister.get(conflictingPrefix));
63 private Function<Address, Short> toPrefixLength() {
64 return (final Address address) -> {
65 final Subnet subnet = address.getSubnet();
67 if (subnet instanceof PrefixLength) {
68 return ((PrefixLength) subnet).getPrefixLength();
71 if (address.getSubnet() instanceof Netmask) {
72 return (short) getSubnetMaskLength(
73 checkNotNull(((Netmask) subnet).getNetmask(), "No netmask defined for %s", subnet)
77 throw new IllegalArgumentException("Unsupported subnet : " + subnet);