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.checkNotNull;
20 import static io.fd.honeycomb.v3po.translate.v3po.interfaces.ip.Ipv4WriteUtils.addDelAddress;
21 import static io.fd.honeycomb.v3po.translate.v3po.interfaces.ip.Ipv4WriteUtils.getSubnetMaskLength;
23 import com.google.common.base.Optional;
24 import io.fd.honeycomb.v3po.translate.spi.write.ListWriterCustomizer;
25 import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer;
26 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
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 javax.annotation.Nonnull;
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.yang.types.rev130715.DottedQuad;
39 import org.opendaylight.yangtools.yang.binding.DataObject;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.openvpp.jvpp.VppBaseCallException;
42 import org.openvpp.jvpp.future.FutureJVpp;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * Customizer for writing {@link Address}
49 public class Ipv4AddressCustomizer extends FutureJVppCustomizer implements ListWriterCustomizer<Address, AddressKey> {
51 private static final Logger LOG = LoggerFactory.getLogger(Ipv4AddressCustomizer.class);
52 private final NamingContext interfaceContext;
54 public Ipv4AddressCustomizer(FutureJVpp futureJvpp, NamingContext interfaceContext) {
56 this.interfaceContext = interfaceContext;
60 public void writeCurrentAttributes(InstanceIdentifier<Address> id, Address dataAfter, WriteContext writeContext)
61 throws WriteFailedException {
62 setAddress(true, id, dataAfter, writeContext);
66 public void updateCurrentAttributes(InstanceIdentifier<Address> id, Address dataBefore, Address dataAfter,
67 WriteContext writeContext) throws WriteFailedException {
68 throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
69 new UnsupportedOperationException("Operation not supported"));
73 public void deleteCurrentAttributes(InstanceIdentifier<Address> id, Address dataBefore, WriteContext writeContext)
74 throws WriteFailedException {
75 setAddress(false, id, dataBefore, writeContext);
79 public Optional<List<Address>> extract(InstanceIdentifier<Address> currentId, DataObject parentData) {
80 return Optional.fromNullable((((Ipv4) parentData).getAddress()));
83 private void setAddress(boolean add, final InstanceIdentifier<Address> id, final Address address,
84 final WriteContext writeContext) throws WriteFailedException {
86 final String interfaceName = id.firstKeyOf(Interface.class).getName();
87 final int interfaceIndex = interfaceContext.getIndex(interfaceName, writeContext.getMappingContext());
89 Subnet subnet = address.getSubnet();
91 if (subnet instanceof PrefixLength) {
92 setPrefixLengthSubnet(add, id, interfaceName, interfaceIndex, address, (PrefixLength) subnet);
93 } else if (subnet instanceof Netmask) {
94 setNetmaskSubnet(add, id, interfaceName, interfaceIndex, address, (Netmask) subnet);
96 // FIXME how does choice extensibility work
97 // FIXME it is not even possible to create a dedicated
98 // customizer for Interconnection, since it's not a DataObject
99 // FIXME we might need a choice customizer
100 // THis choice is already from augment, so its probably not
101 // possible to augment augmented choice
102 LOG.error("Unable to handle subnet of type {}", subnet.getClass());
103 throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass());
107 private void setNetmaskSubnet(final boolean add, @Nonnull final InstanceIdentifier<Address> id,
108 @Nonnull final String interfaceName, final int interfaceIndex,
109 @Nonnull final Address address, @Nonnull final Netmask subnet)
110 throws WriteFailedException {
112 LOG.debug("Setting Subnet(subnet-mask) for interface: {}(id={}). Subnet: {}, address: {}",
113 interfaceName, interfaceIndex, subnet, address);
115 final DottedQuad netmask = subnet.getNetmask();
116 checkNotNull(netmask, "netmask value should not be null");
118 final byte subnetLength = getSubnetMaskLength(netmask.getValue());
119 addDelAddress(getFutureJVpp(), add, id, interfaceIndex, address.getIp(), subnetLength);
120 } catch (VppBaseCallException e) {
121 LOG.warn("Failed to set Subnet(subnet-mask) for interface: {}(id={}). Subnet: {}, address: {}",
122 interfaceName, interfaceIndex, subnet, address);
123 throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass(), e);
127 private void setPrefixLengthSubnet(final boolean add, @Nonnull final InstanceIdentifier<Address> id,
128 @Nonnull final String interfaceName, final int interfaceIndex,
129 @Nonnull final Address address, @Nonnull final PrefixLength subnet)
130 throws WriteFailedException {
132 LOG.debug("Setting Subnet(prefix-length) for interface: {}(id={}). Subnet: {}, address: {}",
133 interfaceName, interfaceIndex, subnet, address);
135 addDelAddress(getFutureJVpp(), add, id, interfaceIndex, address.getIp(),
136 subnet.getPrefixLength().byteValue());
138 LOG.debug("Subnet(prefix-length) set successfully for interface: {}(id={}). Subnet: {}, address: {}",
139 interfaceName, interfaceIndex, subnet, address);
140 } catch (VppBaseCallException e) {
141 LOG.warn("Failed to set Subnet(prefix-length) for interface: {}(id={}). Subnet: {}, address: {}",
142 interfaceName, interfaceIndex, subnet, address);
143 throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass(), e);