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.hc2vpp.l3.write.ipv4.subinterface;
19 import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer;
20 import io.fd.hc2vpp.common.translate.util.NamingContext;
21 import io.fd.hc2vpp.l3.utils.ip.write.IpWriter;
22 import io.fd.hc2vpp.v3po.util.SubInterfaceUtils;
23 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
24 import io.fd.honeycomb.translate.write.WriteContext;
25 import io.fd.honeycomb.translate.write.WriteFailedException;
26 import io.fd.jvpp.core.future.FutureJVppCore;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterface;
29 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterfaceKey;
30 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.ip4.attributes.ipv4.Address;
31 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.ip4.attributes.ipv4.AddressKey;
32 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.ip4.attributes.ipv4.address.Subnet;
33 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.ip4.attributes.ipv4.address.subnet.Netmask;
34 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLength;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DottedQuad;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Write customizer for sub-interface {@link Address}
45 public class SubInterfaceIpv4AddressCustomizer extends FutureJVppCustomizer
46 implements ListWriterCustomizer<Address, AddressKey>, IpWriter {
48 private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceIpv4AddressCustomizer.class);
49 private final NamingContext interfaceContext;
51 public SubInterfaceIpv4AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
52 @Nonnull final NamingContext interfaceContext) {
53 super(futureJVppCore);
54 this.interfaceContext = interfaceContext;
58 public void writeCurrentAttributes(InstanceIdentifier<Address> id, Address dataAfter, WriteContext writeContext)
59 throws WriteFailedException {
60 setAddress(true, id, dataAfter, writeContext);
64 public void deleteCurrentAttributes(InstanceIdentifier<Address> id, Address dataBefore, WriteContext writeContext)
65 throws WriteFailedException {
66 setAddress(false, id, dataBefore, writeContext);
69 private void setAddress(boolean add, final InstanceIdentifier<Address> id, final Address address,
70 final WriteContext writeContext) throws WriteFailedException {
72 final String interfaceName = id.firstKeyOf(Interface.class).getName();
73 final String subInterfaceName = getSubInterfaceName(id);
74 final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
76 Subnet subnet = address.getSubnet();
78 if (subnet instanceof PrefixLength) {
79 setPrefixLengthSubnet(add, id, interfaceName, subInterfaceIndex, address, (PrefixLength) subnet);
81 setNetmaskSubnet(add, id, interfaceName, subInterfaceIndex, address, (Netmask) subnet);
85 private String getSubInterfaceName(@Nonnull final InstanceIdentifier<Address> id) {
86 final InterfaceKey parentInterfacekey = id.firstKeyOf(Interface.class);
87 final SubInterfaceKey subInterfacekey = id.firstKeyOf(SubInterface.class);
88 return SubInterfaceUtils
89 .getSubInterfaceName(parentInterfacekey.getName(), subInterfacekey.getIdentifier().intValue());
92 private void setNetmaskSubnet(final boolean add, @Nonnull final InstanceIdentifier<Address> id,
93 @Nonnull final String subInterfaceName, final int subInterfaceIndex,
94 @Nonnull final Address address, @Nonnull final Netmask subnet)
95 throws WriteFailedException {
97 LOG.debug("Setting Subnet(subnet-mask) for sub-interface: {}(id={}). Subnet: {}, address: {}",
98 subInterfaceName, subInterfaceIndex, subnet, address);
100 final DottedQuad netmask = subnet.getNetmask();
102 final byte subnetLength = getSubnetMaskLength(netmask.getValue());
103 addDelAddress(getFutureJVpp(), add, id, subInterfaceIndex, address.getIp(), subnetLength);
106 private void setPrefixLengthSubnet(final boolean add, @Nonnull final InstanceIdentifier<Address> id,
107 @Nonnull final String subInterfaceName, final int subInterfaceIndex,
108 @Nonnull final Address address, @Nonnull final PrefixLength subnet)
109 throws WriteFailedException {
110 LOG.debug("Setting Subnet(prefix-length) for sub-interface: {}(id={}). Subnet: {}, address: {}",
111 subInterfaceName, subInterfaceIndex, subnet, address);
113 addDelAddress(getFutureJVpp(), add, id, subInterfaceIndex, address.getIp(),
114 subnet.getPrefixLength().byteValue());
116 LOG.debug("Subnet(prefix-length) set successfully for sub-interface: {}(id={}). Subnet: {}, address: {}",
117 subInterfaceName, subInterfaceIndex, subnet, address);