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;
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
22 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
23 import io.fd.honeycomb.translate.vpp.util.Ipv4Translator;
24 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
25 import io.fd.honeycomb.translate.write.WriteFailedException;
26 import io.fd.vpp.jvpp.core.dto.SwInterfaceAddDelAddress;
27 import io.fd.vpp.jvpp.core.dto.SwInterfaceAddDelAddressReply;
28 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
29 import java.util.concurrent.CompletionStage;
30 import javax.annotation.Nonnegative;
31 import javax.annotation.Nonnull;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 * Utility class providing Ipv4 CUD support.
38 public interface Ipv4Writer extends ByteDataTranslator, Ipv4Translator, JvppReplyConsumer {
40 int DOTTED_QUAD_MASK_LENGTH = 4;
41 int IPV4_ADDRESS_PART_BITS_COUNT = 8;
42 int NETMASK_PART_LIMIT = 256; // 2 power to 8
44 default void addDelAddress(@Nonnull final FutureJVppCore futureJVppCore, final boolean add,
45 final InstanceIdentifier<?> id,
46 @Nonnegative final int ifaceId,
47 @Nonnull final Ipv4AddressNoZone address, @Nonnegative final byte prefixLength)
48 throws WriteFailedException {
49 checkArgument(prefixLength > 0, "Invalid prefix length");
50 checkNotNull(address, "address should not be null");
52 final byte[] addressBytes = ipv4AddressNoZoneToArray(address);
54 final CompletionStage<SwInterfaceAddDelAddressReply> swInterfaceAddDelAddressReplyCompletionStage =
55 futureJVppCore.swInterfaceAddDelAddress(
56 getSwInterfaceAddDelAddressRequest(ifaceId, booleanToByte(add) /* isAdd */,
57 (byte) 0 /* isIpv6 */, (byte) 0 /* delAll */, prefixLength, addressBytes));
59 getReplyForWrite(swInterfaceAddDelAddressReplyCompletionStage.toCompletableFuture(), id);
62 default SwInterfaceAddDelAddress getSwInterfaceAddDelAddressRequest(final int swIfc, final byte isAdd,
63 final byte ipv6, final byte deleteAll,
64 final byte length, final byte[] addr) {
65 final SwInterfaceAddDelAddress swInterfaceAddDelAddress = new SwInterfaceAddDelAddress();
66 swInterfaceAddDelAddress.swIfIndex = swIfc;
67 swInterfaceAddDelAddress.isAdd = isAdd;
68 swInterfaceAddDelAddress.isIpv6 = ipv6;
69 swInterfaceAddDelAddress.delAll = deleteAll;
70 swInterfaceAddDelAddress.address = addr;
71 swInterfaceAddDelAddress.addressLength = length;
72 return swInterfaceAddDelAddress;
76 * Returns the prefix size in bits of the specified subnet mask. Example: For the subnet mask 255.255.255.128 it
77 * returns 25 while for 255.0.0.0 it returns 8. If the passed subnetMask array is not complete or contains not only
78 * leading ones, IllegalArgumentExpression is thrown
80 * @param mask the subnet mask in dot notation 255.255.255.255
81 * @return the prefix length as number of bits
83 default byte getSubnetMaskLength(final String mask) {
84 String[] maskParts = mask.split("\\.");
86 checkArgument(maskParts.length == DOTTED_QUAD_MASK_LENGTH,
87 "Network mask %s is not in Quad Dotted Decimal notation!", mask);
89 long maskAsNumber = 0;
90 for (int i = 0; i < DOTTED_QUAD_MASK_LENGTH; i++) {
91 maskAsNumber <<= IPV4_ADDRESS_PART_BITS_COUNT;
92 int value = Integer.parseInt(maskParts[i]);
93 checkArgument(value < NETMASK_PART_LIMIT, "Network mask %s contains invalid number(s) over 255!", mask);
94 checkArgument(value >= 0, "Network mask %s contains invalid negative number(s)!", mask);
95 maskAsNumber += value;
98 String bits = Long.toBinaryString(maskAsNumber);
99 checkArgument(bits.length() == IPV4_ADDRESS_PART_BITS_COUNT * DOTTED_QUAD_MASK_LENGTH,
100 "Incorrect network mask %s", mask);
101 final int leadingOnes = bits.indexOf('0');
102 checkArgument(leadingOnes != -1, "Broadcast address %s is not allowed!", mask);
103 checkArgument(bits.substring(leadingOnes).indexOf('1') == -1,
104 "Non-contiguous network mask %s is not allowed!", mask);
105 return (byte) leadingOnes;