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