ae8291027a8be80cf4d4e29606aca6bf27ff5d3e
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / ip / subnet / validation / SubnetValidationException.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 java.util.Collection;
22 import javax.annotation.Nonnull;
23 import org.apache.commons.lang3.StringUtils;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Address;
25
26 /**
27  * Thrown as negative result of subnet validation
28  */
29 public class SubnetValidationException extends Exception {
30
31     private SubnetValidationException(@Nonnull final String message) {
32         super(message);
33     }
34
35     public static SubnetValidationException forConflictingData(@Nonnull final Short prefix, @Nonnull Collection<Address> addresses) {
36         return new SubnetValidationException(
37                 "Attempt to define multiple addresses for same subnet[prefixLen = " + prefixToString(prefix) + "], "
38                         + "addresses : " + addressesToString(addresses));
39     }
40
41     private static String prefixToString(final Short prefix) {
42         return checkNotNull(prefix, "Cannot create " + SubnetValidationException.class.getName() + " for null prefix")
43                 .toString();
44     }
45
46     private static String addressesToString(final Collection<Address> addresses) {
47         return StringUtils.join(checkNotNull(addresses,
48                 "Cannot create " + SubnetValidationException.class.getName() + " for null address list"), " | ");
49     }
50 }