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.v3po.interfacesstate.ip.v4.subinterface;
19 import com.google.common.base.Optional;
20 import io.fd.hc2vpp.common.translate.util.NamingContext;
21 import io.fd.hc2vpp.v3po.interfacesstate.SubInterfaceCustomizer;
22 import io.fd.hc2vpp.v3po.interfacesstate.ip.dump.params.IfaceDumpFilter;
23 import io.fd.hc2vpp.v3po.interfacesstate.ip.readers.IpAddressReader;
24 import io.fd.hc2vpp.v3po.util.SubInterfaceUtils;
25 import io.fd.honeycomb.translate.read.ReadContext;
26 import io.fd.honeycomb.translate.read.ReadFailedException;
27 import io.fd.honeycomb.translate.spi.read.Initialized;
28 import io.fd.honeycomb.translate.spi.read.InitializingListReaderCustomizer;
29 import io.fd.honeycomb.translate.util.RWUtils;
30 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
31 import io.fd.vpp.jvpp.core.dto.IpAddressDetails;
32 import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump;
33 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces.state._interface.sub.interfaces.SubInterface;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4Builder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.Address;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.AddressBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.AddressKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLengthBuilder;
42 import org.opendaylight.yangtools.concepts.Builder;
43 import org.opendaylight.yangtools.yang.binding.DataObject;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 import javax.annotation.Nonnull;
49 import java.util.List;
52 * Read customizer for sub-interface Ipv4 addresses.
54 public class SubInterfaceIpv4AddressCustomizer extends IpAddressReader
55 implements InitializingListReaderCustomizer<Address, AddressKey, AddressBuilder> {
57 private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceIpv4AddressCustomizer.class);
59 public SubInterfaceIpv4AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
60 @Nonnull final NamingContext interfaceContext) {
61 super(interfaceContext, false, new DumpCacheManager.DumpCacheManagerBuilder<IpAddressDetailsReplyDump, IfaceDumpFilter>()
62 .withExecutor(createAddressDumpExecutor(futureJVppCore))
63 //same as with ipv4 addresses for interfaces, these must have cache scope of their parent sub-interface
64 .withCacheKeyFactory(subInterfaceScopedCacheKeyFactory(IpAddressDetailsReplyDump.class))
68 private static String getSubInterfaceName(@Nonnull final InstanceIdentifier<Address> id) {
69 return SubInterfaceUtils.getSubInterfaceName(id.firstKeyOf(Interface.class).getName(),
70 Math.toIntExact(id.firstKeyOf(SubInterface.class).getIdentifier()));
75 public AddressBuilder getBuilder(@Nonnull InstanceIdentifier<Address> id) {
76 return new AddressBuilder();
80 public void readCurrentAttributes(@Nonnull InstanceIdentifier<Address> id, @Nonnull AddressBuilder builder,
81 @Nonnull ReadContext ctx)
82 throws ReadFailedException {
83 LOG.debug("Reading attributes for sub-interface address: {}", id);
85 final Optional<IpAddressDetails> ipAddressDetails =
86 findIpv4AddressDetailsByIp(subInterfaceAddressDumpSupplier(id, ctx), id.firstKeyOf(Address.class).getIp());
88 if (ipAddressDetails.isPresent()) {
89 final IpAddressDetails detail = ipAddressDetails.get();
90 builder.setIp(arrayToIpv4AddressNoZone(detail.ip));
91 builder.setSubnet(new PrefixLengthBuilder().setPrefixLength((short) detail.prefixLength).build());
93 if (LOG.isDebugEnabled()) {
94 final String subInterfaceName = getSubInterfaceName(id);
95 final int subInterfaceIndex = getInterfaceContext().getIndex(subInterfaceName, ctx.getMappingContext());
96 LOG.debug("Attributes for {} sub-interface (id={}) address {} successfully read: {}",
97 subInterfaceName, subInterfaceIndex, id, builder.build());
104 public List<AddressKey> getAllIds(@Nonnull InstanceIdentifier<Address> id, @Nonnull ReadContext ctx)
105 throws ReadFailedException {
106 LOG.debug("Reading list of keys for sub-interface addresses: {}", id);
107 return getAllIpv4AddressIds(subInterfaceAddressDumpSupplier(id, ctx), AddressKey::new);
111 public void merge(@Nonnull Builder<? extends DataObject> builder, @Nonnull List<Address> readData) {
112 ((Ipv4Builder) builder).setAddress(readData);
116 public Initialized<Address> init(
117 @Nonnull final InstanceIdentifier<Address> id, @Nonnull final Address readValue,
118 @Nonnull final ReadContext ctx) {
119 return Initialized.create(getCfgId(id), readValue);
122 private InstanceIdentifier<Address> getCfgId(final InstanceIdentifier<Address> id) {
123 return SubInterfaceCustomizer.getCfgId(RWUtils.cutId(id, SubInterface.class))
125 .child(Address.class, new AddressKey(id.firstKeyOf(Address.class)));