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 static com.google.common.base.Preconditions.checkNotNull;
21 import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer;
22 import io.fd.hc2vpp.common.translate.util.NamingContext;
23 import io.fd.hc2vpp.l3.utils.ip.write.IpWriter;
24 import io.fd.hc2vpp.v3po.util.SubInterfaceUtils;
25 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
26 import io.fd.honeycomb.translate.write.WriteContext;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
29 import javax.annotation.Nonnull;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DottedQuad;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterface;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterfaceKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev180319.sub._interface.ip4.attributes.ipv4.Address;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev180319.sub._interface.ip4.attributes.ipv4.AddressKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev180319.sub._interface.ip4.attributes.ipv4.address.Subnet;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev180319.sub._interface.ip4.attributes.ipv4.address.subnet.Netmask;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev180319.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLength;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * Write customizer for sub-interface {@link Address}
47 public class SubInterfaceIpv4AddressCustomizer extends FutureJVppCustomizer
48 implements ListWriterCustomizer<Address, AddressKey>, IpWriter {
50 private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceIpv4AddressCustomizer.class);
51 private final NamingContext interfaceContext;
53 public SubInterfaceIpv4AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
54 @Nonnull final NamingContext interfaceContext) {
55 super(futureJVppCore);
56 this.interfaceContext = checkNotNull(interfaceContext, "interface context should not be null");
60 public void writeCurrentAttributes(InstanceIdentifier<Address> id, Address dataAfter, WriteContext writeContext)
61 throws WriteFailedException {
62 setAddress(true, id, dataAfter, writeContext);
66 public void deleteCurrentAttributes(InstanceIdentifier<Address> id, Address dataBefore, WriteContext writeContext)
67 throws WriteFailedException {
68 setAddress(false, id, dataBefore, writeContext);
71 private void setAddress(boolean add, final InstanceIdentifier<Address> id, final Address address,
72 final WriteContext writeContext) throws WriteFailedException {
74 final String interfaceName = id.firstKeyOf(Interface.class).getName();
75 final String subInterfaceName = getSubInterfaceName(id);
76 final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
78 Subnet subnet = address.getSubnet();
80 if (subnet instanceof PrefixLength) {
81 setPrefixLengthSubnet(add, id, interfaceName, subInterfaceIndex, address, (PrefixLength) subnet);
82 } else if (subnet instanceof Netmask) {
83 setNetmaskSubnet(add, id, interfaceName, subInterfaceIndex, address, (Netmask) subnet);
85 LOG.error("Unable to handle subnet of type {}", subnet.getClass());
86 throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass());
90 private String getSubInterfaceName(@Nonnull final InstanceIdentifier<Address> id) {
91 final InterfaceKey parentInterfacekey = id.firstKeyOf(Interface.class);
92 final SubInterfaceKey subInterfacekey = id.firstKeyOf(SubInterface.class);
93 return SubInterfaceUtils
94 .getSubInterfaceName(parentInterfacekey.getName(), subInterfacekey.getIdentifier().intValue());
97 private void setNetmaskSubnet(final boolean add, @Nonnull final InstanceIdentifier<Address> id,
98 @Nonnull final String subInterfaceName, final int subInterfaceIndex,
99 @Nonnull final Address address, @Nonnull final Netmask subnet)
100 throws WriteFailedException {
102 LOG.debug("Setting Subnet(subnet-mask) for sub-interface: {}(id={}). Subnet: {}, address: {}",
103 subInterfaceName, subInterfaceIndex, subnet, address);
105 final DottedQuad netmask = subnet.getNetmask();
106 checkNotNull(netmask, "netmask value should not be null");
108 final byte subnetLength = getSubnetMaskLength(netmask.getValue());
109 addDelAddress(getFutureJVpp(), add, id, subInterfaceIndex, address.getIp(), subnetLength);
112 private void setPrefixLengthSubnet(final boolean add, @Nonnull final InstanceIdentifier<Address> id,
113 @Nonnull final String subInterfaceName, final int subInterfaceIndex,
114 @Nonnull final Address address, @Nonnull final PrefixLength subnet)
115 throws WriteFailedException {
116 LOG.debug("Setting Subnet(prefix-length) for sub-interface: {}(id={}). Subnet: {}, address: {}",
117 subInterfaceName, subInterfaceIndex, subnet, address);
119 addDelAddress(getFutureJVpp(), add, id, subInterfaceIndex, address.getIp(),
120 subnet.getPrefixLength().byteValue());
122 LOG.debug("Subnet(prefix-length) set successfully for sub-interface: {}(id={}). Subnet: {}, address: {}",
123 subInterfaceName, subInterfaceIndex, subnet, address);