a314eaf14dc2eb25f0ed6f6157331c201d7afc56
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / ip / subnet / validation / SubnetValidator.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package io.fd.honeycomb.translate.v3po.interfaces.ip.subnet.validation;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
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;
31
32 /**
33  * Validator for detecting if there is an attempt to assign multiple addresses from same subnet
34  */
35 public class SubnetValidator implements Ipv4Writer {
36
37     /**
38      * Checks whether data provided for writing are not in collision with already existing data
39      */
40     public void checkNotAddingToSameSubnet(@Nonnull final List<Address> addresses)
41             throws SubnetValidationException {
42
43         final Multimap<Short, Address> prefixLengthRegister = Multimaps.index(addresses, toPrefixLength());
44         final int keySetSize = prefixLengthRegister.keySet().size();
45
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
48             return;
49         }
50
51         //finds conflicting prefix
52         final Short conflictingPrefix = prefixLengthRegister.keySet()
53                 .stream()
54                 .filter(a -> prefixLengthRegister.get(a).size() > 1)
55                 .findFirst()
56                 .get();
57
58         //and reports it with affected addresses
59         throw SubnetValidationException
60                 .forConflictingData(conflictingPrefix, prefixLengthRegister.get(conflictingPrefix));
61     }
62
63     private Function<Address, Short> toPrefixLength() {
64         return (final Address address) -> {
65             final Subnet subnet = address.getSubnet();
66
67             if (subnet instanceof PrefixLength) {
68                 return ((PrefixLength) subnet).getPrefixLength();
69             }
70
71             if (address.getSubnet() instanceof Netmask) {
72                 return (short) getSubnetMaskLength(
73                         checkNotNull(((Netmask) subnet).getNetmask(), "No netmask defined for %s", subnet)
74                                 .getValue());
75             }
76
77             throw new IllegalArgumentException("Unsupported subnet : " + subnet);
78         };
79     }
80 }