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.v3po.translate.v3po.interfaces.ip;
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
22 import com.google.common.base.Optional;
23 import io.fd.honeycomb.v3po.translate.spi.write.ListWriterCustomizer;
24 import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer;
25 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
26 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
27 import io.fd.honeycomb.v3po.translate.write.WriteContext;
28 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
29 import java.util.List;
30 import java.util.concurrent.CompletionStage;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv4;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Address;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.AddressKey;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.Subnet;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.subnet.Netmask;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.subnet.PrefixLength;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.subnet.PrefixLengthBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DottedQuad;
40 import org.opendaylight.yangtools.yang.binding.DataObject;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.openvpp.jvpp.VppBaseCallException;
43 import org.openvpp.jvpp.dto.SwInterfaceAddDelAddress;
44 import org.openvpp.jvpp.dto.SwInterfaceAddDelAddressReply;
45 import org.openvpp.jvpp.future.FutureJVpp;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * Customizer for writing {@link Address}
52 public class AddressCustomizer extends FutureJVppCustomizer implements ListWriterCustomizer<Address, AddressKey> {
54 private static final Logger LOG = LoggerFactory.getLogger(AddressCustomizer.class);
55 private final NamingContext interfaceContext;
57 public AddressCustomizer(FutureJVpp futureJvpp, NamingContext interfaceContext) {
59 this.interfaceContext = interfaceContext;
63 public void writeCurrentAttributes(InstanceIdentifier<Address> id, Address dataAfter, WriteContext writeContext)
64 throws WriteFailedException {
65 setAddress(true, id, dataAfter, writeContext);
69 public void updateCurrentAttributes(InstanceIdentifier<Address> id, Address dataBefore, Address dataAfter,
70 WriteContext writeContext) throws WriteFailedException {
71 throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
72 new UnsupportedOperationException("Operation not supported"));
76 public void deleteCurrentAttributes(InstanceIdentifier<Address> id, Address dataBefore, WriteContext writeContext)
77 throws WriteFailedException {
78 setAddress(false, id, dataBefore, writeContext);
82 public Optional<List<Address>> extract(InstanceIdentifier<Address> currentId, DataObject parentData) {
83 return Optional.fromNullable((((Ipv4) parentData).getAddress()));
86 private void setAddress(boolean add, final InstanceIdentifier<Address> id, final Address address,
87 final WriteContext writeContext) throws WriteFailedException {
89 final String interfaceName = id.firstKeyOf(Interface.class).getName();
90 final int swIfc = interfaceContext.getIndex(interfaceName, writeContext.getMappingContext());
92 Subnet subnet = address.getSubnet();
94 if (subnet instanceof PrefixLength) {
95 setPrefixLengthSubnet(add, id, interfaceName, swIfc, address, (PrefixLength) subnet);
96 } else if (subnet instanceof Netmask) {
97 setNetmaskSubnet(add, id, interfaceName, swIfc, address, (Netmask) subnet);
99 // FIXME how does choice extensibility work
100 // FIXME it is not even possible to create a dedicated
101 // customizer for Interconnection, since it's not a DataObject
102 // FIXME we might need a choice customizer
103 // THis choice is already from augment, so its probably not
104 // possible to augment augmented choice
105 LOG.error("Unable to handle subnet of type {}", subnet.getClass());
106 throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass());
110 private void setNetmaskSubnet(final boolean add, final InstanceIdentifier<Address> id, final String name,
112 final Address ipv4Addr, final Netmask subnet) throws WriteFailedException {
113 LOG.debug("Setting Subnet(subnet-mask) for interface: {}, {}. Subnet: {}, Ipv4: {}", name, swIfc, subnet,
116 byte[] addr = TranslateUtils.ipv4AddressNoZoneToArray(ipv4Addr.getIp());
117 final DottedQuad netmask = subnet.getNetmask();
119 checkNotNull(addr, "Null address");
120 checkNotNull(netmask, "Null netmask");
122 // find netmask bit-length
123 final short subnetLength = getSubnetMaskLength(netmask.getValue());
124 PrefixLengthBuilder lengthBuilder = new PrefixLengthBuilder().setPrefixLength(subnetLength);
126 setPrefixLengthSubnet(add, id, name, swIfc, ipv4Addr, lengthBuilder.build());
130 * Returns the prefix size in bits of the specified subnet mask. Example: For the subnet mask 255.255.255.128 it
131 * returns 25 while for 255.0.0.0 it returns 8. If the passed subnetMask array is not complete or contains not only
132 * leading ones, IllegalArgumentExpression is thrown
134 * @param mask the subnet mask in dot notation 255.255.255.255
135 * @return the prefix length as number of bits
137 private static short getSubnetMaskLength(final String mask) {
138 String[] maskParts = mask.split("\\.");
140 final int DOTTED_QUAD_MASK_LENGHT = 4;
141 final int IPV4_ADDRESS_PART_BITS_COUNT = 8;
142 final int NETMASK_PART_LIMIT = 256; // 2 power to 8
144 checkArgument(maskParts.length == DOTTED_QUAD_MASK_LENGHT,
145 "Network mask %s is not in Quad Dotted Decimal notation!", mask);
147 long maskAsNumber = 0;
148 for (int i = 0; i < DOTTED_QUAD_MASK_LENGHT; i++) {
149 maskAsNumber <<= IPV4_ADDRESS_PART_BITS_COUNT;
150 int value = Integer.parseInt(maskParts[i]);
151 checkArgument(value < NETMASK_PART_LIMIT, "Network mask %s contains invalid number(s) over 255!", mask);
152 checkArgument(value >= 0, "Network mask %s contains invalid negative number(s)!", mask);
153 maskAsNumber += value;
156 String bits = Long.toBinaryString(maskAsNumber);
157 checkArgument(bits.length() == IPV4_ADDRESS_PART_BITS_COUNT * DOTTED_QUAD_MASK_LENGHT,
158 "Incorrect network mask %s", mask);
159 final int leadingOnes = bits.indexOf('0');
160 checkArgument(leadingOnes != -1, "Broadcast address %s is not allowed!", mask);
161 checkArgument(bits.substring(leadingOnes).indexOf('1') == -1,
162 "Non-contiguous network mask %s is not allowed!", mask);
163 return (short) leadingOnes;
166 private void setPrefixLengthSubnet(boolean add, final InstanceIdentifier<Address> id, final String name,
168 final Address address, final PrefixLength subnet) throws WriteFailedException {
170 LOG.debug("Setting Subnet(prefix-length) for interface: {}, {}. Subnet: {}, Address: {}", name, swIfc,
173 final Short plen = subnet.getPrefixLength();
174 checkArgument(plen > 0, "Invalid length");
176 final byte[] addr = TranslateUtils.ipv4AddressNoZoneToArray(address.getIp());
177 checkNotNull(addr, "Null address");
179 final CompletionStage<SwInterfaceAddDelAddressReply> swInterfaceAddDelAddressReplyCompletionStage =
181 .swInterfaceAddDelAddress(
182 getSwInterfaceAddDelAddressRequest(swIfc, TranslateUtils.booleanToByte(add) /* isAdd */,
183 (byte) 0 /* isIpv6 */, (byte) 0 /* delAll */, plen.byteValue(), addr));
185 TranslateUtils.getReply(swInterfaceAddDelAddressReplyCompletionStage.toCompletableFuture());
187 LOG.debug("Subnet(prefix-length) set successfully for interface: {}, {}, Subnet: {}, Address: {}", name,
188 swIfc, subnet, address);
189 } catch (VppBaseCallException e) {
190 LOG.warn("Failed to set Subnet(prefix-length) for interface: {}, {}, Subnet: {}, Address: {}", name, swIfc,
192 throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass(), e);
196 private SwInterfaceAddDelAddress getSwInterfaceAddDelAddressRequest(final int swIfc, final byte isAdd,
197 final byte ipv6, final byte deleteAll,
198 final byte length, final byte[] addr) {
199 final SwInterfaceAddDelAddress swInterfaceAddDelAddress = new SwInterfaceAddDelAddress();
200 swInterfaceAddDelAddress.swIfIndex = swIfc;
201 swInterfaceAddDelAddress.isAdd = isAdd;
202 swInterfaceAddDelAddress.isIpv6 = ipv6;
203 swInterfaceAddDelAddress.delAll = deleteAll;
204 swInterfaceAddDelAddress.address = addr;
205 swInterfaceAddDelAddress.addressLength = length;
206 return swInterfaceAddDelAddress;