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.interfacesstate.ip;
19 import static com.google.common.base.Preconditions.checkNotNull;
21 import com.google.common.base.Optional;
22 import io.fd.honeycomb.translate.read.ReadContext;
23 import io.fd.honeycomb.translate.read.ReadFailedException;
24 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
25 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
26 import io.fd.honeycomb.translate.v3po.interfacesstate.ip.dump.AddressDumpExecutor;
27 import io.fd.honeycomb.translate.v3po.interfacesstate.ip.dump.params.AddressDumpParams;
28 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
29 import io.fd.honeycomb.translate.vpp.util.NamingContext;
30 import io.fd.vpp.jvpp.core.dto.IpAddressDetails;
31 import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump;
32 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
33 import java.util.List;
34 import javax.annotation.Nonnull;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4Builder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.Address;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressBuilder;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressKey;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.address.subnet.PrefixLengthBuilder;
41 import org.opendaylight.yangtools.concepts.Builder;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * Read customizer for interface Ipv4 addresses.
50 public class Ipv4AddressCustomizer extends FutureJVppCustomizer
51 implements ListReaderCustomizer<Address, AddressKey, AddressBuilder>, Ipv4Reader {
53 private static final Logger LOG = LoggerFactory.getLogger(Ipv4AddressCustomizer.class);
54 private static final String CACHE_KEY = Ipv4AddressCustomizer.class.getName();
56 private final NamingContext interfaceContext;
57 private final DumpCacheManager<IpAddressDetailsReplyDump, AddressDumpParams> dumpManager;
59 public Ipv4AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
60 @Nonnull final NamingContext interfaceContext) {
61 super(futureJVppCore);
62 this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
63 this.dumpManager = new DumpCacheManager.DumpCacheManagerBuilder<IpAddressDetailsReplyDump, AddressDumpParams>()
64 .withExecutor(new AddressDumpExecutor(futureJVppCore))
70 public AddressBuilder getBuilder(@Nonnull InstanceIdentifier<Address> id) {
71 return new AddressBuilder();
75 public void readCurrentAttributes(@Nonnull InstanceIdentifier<Address> id, @Nonnull AddressBuilder builder,
76 @Nonnull ReadContext ctx)
77 throws ReadFailedException {
78 LOG.debug("Reading attributes for interface address: {}", id);
80 final String interfaceName = id.firstKeyOf(Interface.class).getName();
81 final int interfaceIndex = interfaceContext.getIndex(interfaceName, ctx.getMappingContext());
82 final Optional<IpAddressDetailsReplyDump> dumpOptional = dumpManager
83 .getDump(id, CACHE_KEY, ctx.getModificationCache(), new AddressDumpParams(interfaceIndex, false));
85 if (!dumpOptional.isPresent() || dumpOptional.get().ipAddressDetails.isEmpty()) {
88 final Optional<IpAddressDetails> ipAddressDetails =
89 findIpAddressDetailsByIp(dumpOptional, id.firstKeyOf(Address.class).getIp());
91 if (ipAddressDetails.isPresent()) {
92 final IpAddressDetails detail = ipAddressDetails.get();
93 builder.setIp(arrayToIpv4AddressNoZone(detail.ip))
95 new PrefixLengthBuilder().setPrefixLength(Short.valueOf(detail.prefixLength)).build());
97 if (LOG.isDebugEnabled()) {
98 LOG.debug("Attributes for {} interface (id={}) address {} successfully read: {}",
99 interfaceName, interfaceIndex, id, builder.build());
105 public List<AddressKey> getAllIds(@Nonnull InstanceIdentifier<Address> id, @Nonnull ReadContext ctx)
106 throws ReadFailedException {
107 LOG.debug("Reading list of keys for interface addresses: {}", id);
109 final String interfaceName = id.firstKeyOf(Interface.class).getName();
110 final int interfaceIndex = interfaceContext.getIndex(interfaceName, ctx.getMappingContext());
111 final Optional<IpAddressDetailsReplyDump> dumpOptional = dumpManager
112 .getDump(id, CACHE_KEY, ctx.getModificationCache(), new AddressDumpParams(interfaceIndex, false));
114 return getAllIpv4AddressIds(dumpOptional, AddressKey::new);
118 public void merge(@Nonnull Builder<? extends DataObject> builder, @Nonnull List<Address> readData) {
119 ((Ipv4Builder) builder).setAddress(readData);