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.v3po.util.SubInterfaceUtils;
28 import io.fd.honeycomb.v3po.translate.write.WriteContext;
29 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
30 import java.util.List;
31 import javax.annotation.Nonnull;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DottedQuad;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.Ipv4;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.Address;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.AddressKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.address.Subnet;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.address.subnet.Netmask;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLength;
43 import org.opendaylight.yangtools.yang.binding.DataObject;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.openvpp.jvpp.VppBaseCallException;
46 import org.openvpp.jvpp.future.FutureJVpp;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * Write customizer for sub-interface {@link Address}
53 public class SubInterfaceIpv4AddressCustomizer extends FutureJVppCustomizer
54 implements ListWriterCustomizer<Address, AddressKey> {
56 private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceIpv4AddressCustomizer.class);
57 private final NamingContext interfaceContext;
59 public SubInterfaceIpv4AddressCustomizer(@Nonnull final FutureJVpp futureJvpp,
60 @Nonnull final NamingContext interfaceContext) {
62 this.interfaceContext = checkNotNull(interfaceContext, "interface context should not be null");
66 public void writeCurrentAttributes(InstanceIdentifier<Address> id, Address dataAfter, WriteContext writeContext)
67 throws WriteFailedException {
68 setAddress(true, id, dataAfter, writeContext);
72 public void updateCurrentAttributes(InstanceIdentifier<Address> id, Address dataBefore, Address dataAfter,
73 WriteContext writeContext) throws WriteFailedException {
74 throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
75 new UnsupportedOperationException("Operation not supported"));
79 public void deleteCurrentAttributes(InstanceIdentifier<Address> id, Address dataBefore, WriteContext writeContext)
80 throws WriteFailedException {
81 setAddress(false, id, dataBefore, writeContext);
85 public Optional<List<Address>> extract(InstanceIdentifier<Address> currentId, DataObject parentData) {
86 return Optional.fromNullable((((Ipv4) parentData).getAddress()));
89 private void setAddress(boolean add, final InstanceIdentifier<Address> id, final Address address,
90 final WriteContext writeContext) throws WriteFailedException {
92 final String interfaceName = id.firstKeyOf(Interface.class).getName();
93 final String subInterfaceName = getSubInterfaceName(id);
94 final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
96 Subnet subnet = address.getSubnet();
98 if (subnet instanceof PrefixLength) {
99 setPrefixLengthSubnet(add, id, interfaceName, subInterfaceIndex, address, (PrefixLength) subnet);
100 } else if (subnet instanceof Netmask) {
101 setNetmaskSubnet(add, id, interfaceName, subInterfaceIndex, address, (Netmask) subnet);
103 // FIXME how does choice extensibility work
104 // FIXME it is not even possible to create a dedicated
105 // customizer for Interconnection, since it's not a DataObject
106 // FIXME we might need a choice customizer
107 // THis choice is already from augment, so its probably not
108 // possible to augment augmented choice
109 LOG.error("Unable to handle subnet of type {}", subnet.getClass());
110 throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass());
114 private String getSubInterfaceName(@Nonnull final InstanceIdentifier<Address> id) {
115 final InterfaceKey parentInterfacekey = id.firstKeyOf(Interface.class);
116 final SubInterfaceKey subInterfacekey = id.firstKeyOf(SubInterface.class);
117 return SubInterfaceUtils
118 .getSubInterfaceName(parentInterfacekey.getName(), subInterfacekey.getIdentifier().intValue());
121 private void setNetmaskSubnet(final boolean add, @Nonnull final InstanceIdentifier<Address> id,
122 @Nonnull final String subInterfaceName, final int subInterfaceIndex,
123 @Nonnull final Address address, @Nonnull final Netmask subnet)
124 throws WriteFailedException {
126 LOG.debug("Setting Subnet(subnet-mask) for sub-interface: {}(id={}). Subnet: {}, address: {}",
127 subInterfaceName, subInterfaceIndex, subnet, address);
129 final DottedQuad netmask = subnet.getNetmask();
130 checkNotNull(netmask, "netmask value should not be null");
132 final byte subnetLength = getSubnetMaskLength(netmask.getValue());
133 addDelAddress(getFutureJVpp(), add, id, subInterfaceIndex, address.getIp(), subnetLength);
135 } catch (VppBaseCallException e) {
136 LOG.warn("Failed to set Subnet(subnet-mask) for sub-interface: {}(id={}). Subnet: {}, address: {}",
137 subInterfaceName, subInterfaceIndex, subnet, address);
138 throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass(), e);
142 private void setPrefixLengthSubnet(final boolean add, @Nonnull final InstanceIdentifier<Address> id,
143 @Nonnull final String subInterfaceName, final int subInterfaceIndex,
144 @Nonnull final Address address, @Nonnull final PrefixLength subnet)
145 throws WriteFailedException {
147 LOG.debug("Setting Subnet(prefix-length) for sub-interface: {}(id={}). Subnet: {}, address: {}",
148 subInterfaceName, subInterfaceIndex, subnet, address);
150 addDelAddress(getFutureJVpp(), add, id, subInterfaceIndex, address.getIp(),
151 subnet.getPrefixLength().byteValue());
153 LOG.debug("Subnet(prefix-length) set successfully for sub-interface: {}(id={}). Subnet: {}, address: {}",
154 subInterfaceName, subInterfaceIndex, subnet, address);
155 } catch (VppBaseCallException e) {
156 LOG.warn("Failed to set Subnet(prefix-length) for sub-interface: {}(id={}). Subnet: {}, address: {}",
157 subInterfaceName, subInterfaceIndex, subnet, address);
158 throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass(), e);