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.checkNotNull;
21 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
22 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
23 import io.fd.honeycomb.translate.vpp.util.NamingContext;
24 import io.fd.honeycomb.translate.vpp.util.SubInterfaceUtils;
25 import io.fd.honeycomb.translate.write.WriteContext;
26 import io.fd.honeycomb.translate.write.WriteFailedException;
27 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
28 import javax.annotation.Nonnull;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DottedQuad;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.Address;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.AddressKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.address.Subnet;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.address.subnet.Netmask;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLength;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * Write customizer for sub-interface {@link Address}
46 public class SubInterfaceIpv4AddressCustomizer extends FutureJVppCustomizer
47 implements ListWriterCustomizer<Address, AddressKey>, Ipv4Writer {
49 private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceIpv4AddressCustomizer.class);
50 private final NamingContext interfaceContext;
52 public SubInterfaceIpv4AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
53 @Nonnull final NamingContext interfaceContext) {
54 super(futureJVppCore);
55 this.interfaceContext = checkNotNull(interfaceContext, "interface context should not be null");
59 public void writeCurrentAttributes(InstanceIdentifier<Address> id, Address dataAfter, WriteContext writeContext)
60 throws WriteFailedException {
61 setAddress(true, id, dataAfter, writeContext);
65 public void updateCurrentAttributes(InstanceIdentifier<Address> id, Address dataBefore, Address dataAfter,
66 WriteContext writeContext) throws WriteFailedException {
67 throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
68 new UnsupportedOperationException("Operation not supported"));
72 public void deleteCurrentAttributes(InstanceIdentifier<Address> id, Address dataBefore, WriteContext writeContext)
73 throws WriteFailedException {
74 setAddress(false, id, dataBefore, writeContext);
77 private void setAddress(boolean add, final InstanceIdentifier<Address> id, final Address address,
78 final WriteContext writeContext) throws WriteFailedException {
80 final String interfaceName = id.firstKeyOf(Interface.class).getName();
81 final String subInterfaceName = getSubInterfaceName(id);
82 final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
84 Subnet subnet = address.getSubnet();
86 if (subnet instanceof PrefixLength) {
87 setPrefixLengthSubnet(add, id, interfaceName, subInterfaceIndex, address, (PrefixLength) subnet);
88 } else if (subnet instanceof Netmask) {
89 setNetmaskSubnet(add, id, interfaceName, subInterfaceIndex, address, (Netmask) subnet);
91 LOG.error("Unable to handle subnet of type {}", subnet.getClass());
92 throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass());
96 private String getSubInterfaceName(@Nonnull final InstanceIdentifier<Address> id) {
97 final InterfaceKey parentInterfacekey = id.firstKeyOf(Interface.class);
98 final SubInterfaceKey subInterfacekey = id.firstKeyOf(SubInterface.class);
99 return SubInterfaceUtils
100 .getSubInterfaceName(parentInterfacekey.getName(), subInterfacekey.getIdentifier().intValue());
103 private void setNetmaskSubnet(final boolean add, @Nonnull final InstanceIdentifier<Address> id,
104 @Nonnull final String subInterfaceName, final int subInterfaceIndex,
105 @Nonnull final Address address, @Nonnull final Netmask subnet)
106 throws WriteFailedException {
108 LOG.debug("Setting Subnet(subnet-mask) for sub-interface: {}(id={}). Subnet: {}, address: {}",
109 subInterfaceName, subInterfaceIndex, subnet, address);
111 final DottedQuad netmask = subnet.getNetmask();
112 checkNotNull(netmask, "netmask value should not be null");
114 final byte subnetLength = getSubnetMaskLength(netmask.getValue());
115 addDelAddress(getFutureJVpp(), add, id, subInterfaceIndex, address.getIp(), subnetLength);
118 private void setPrefixLengthSubnet(final boolean add, @Nonnull final InstanceIdentifier<Address> id,
119 @Nonnull final String subInterfaceName, final int subInterfaceIndex,
120 @Nonnull final Address address, @Nonnull final PrefixLength subnet)
121 throws WriteFailedException {
122 LOG.debug("Setting Subnet(prefix-length) for sub-interface: {}(id={}). Subnet: {}, address: {}",
123 subInterfaceName, subInterfaceIndex, subnet, address);
125 addDelAddress(getFutureJVpp(), add, id, subInterfaceIndex, address.getIp(),
126 subnet.getPrefixLength().byteValue());
128 LOG.debug("Subnet(prefix-length) set successfully for sub-interface: {}(id={}). Subnet: {}, address: {}",
129 subInterfaceName, subInterfaceIndex, subnet, address);