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.util.read.cache.exceptions.execution.DumpExecutionFailedException;
27 import io.fd.honeycomb.translate.v3po.interfacesstate.ip.dump.AddressDumpExecutor;
28 import io.fd.honeycomb.translate.v3po.interfacesstate.ip.dump.check.AddressDumpCheck;
29 import io.fd.honeycomb.translate.v3po.interfacesstate.ip.dump.params.AddressDumpParams;
30 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
31 import io.fd.honeycomb.translate.vpp.util.NamingContext;
32 import java.util.List;
33 import javax.annotation.Nonnull;
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.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4Builder;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.Address;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressBuilder;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressKey;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.address.subnet.PrefixLengthBuilder;
40 import org.opendaylight.yangtools.concepts.Builder;
41 import org.opendaylight.yangtools.yang.binding.DataObject;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.openvpp.jvpp.core.dto.IpAddressDetails;
44 import org.openvpp.jvpp.core.dto.IpAddressDetailsReplyDump;
45 import org.openvpp.jvpp.core.future.FutureJVppCore;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * Read customizer for interface Ipv4 addresses.
52 public class Ipv4AddressCustomizer extends FutureJVppCustomizer
53 implements ListReaderCustomizer<Address, AddressKey, AddressBuilder>, Ipv4Reader {
55 private static final Logger LOG = LoggerFactory.getLogger(Ipv4AddressCustomizer.class);
56 private static final String CACHE_KEY = Ipv4AddressCustomizer.class.getName();
58 private final NamingContext interfaceContext;
59 private final DumpCacheManager<IpAddressDetailsReplyDump, AddressDumpParams> dumpManager;
61 public Ipv4AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
62 @Nonnull final NamingContext interfaceContext) {
63 super(futureJVppCore);
64 this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
65 this.dumpManager = new DumpCacheManager.DumpCacheManagerBuilder<IpAddressDetailsReplyDump, AddressDumpParams>()
66 .withExecutor(new AddressDumpExecutor(futureJVppCore))
67 .withNonEmptyPredicate(new AddressDumpCheck())
73 public AddressBuilder getBuilder(@Nonnull InstanceIdentifier<Address> id) {
74 return new AddressBuilder();
78 public void readCurrentAttributes(@Nonnull InstanceIdentifier<Address> id, @Nonnull AddressBuilder builder,
79 @Nonnull ReadContext ctx)
80 throws ReadFailedException {
81 LOG.debug("Reading attributes for interface address: {}", id);
83 final String interfaceName = id.firstKeyOf(Interface.class).getName();
84 final int interfaceIndex = interfaceContext.getIndex(interfaceName, ctx.getMappingContext());
85 final Optional<IpAddressDetailsReplyDump> dumpOptional;
88 dumpManager.getDump(CACHE_KEY, ctx.getModificationCache(),
89 new AddressDumpParams(interfaceIndex, false));
90 } catch (DumpExecutionFailedException e) {
91 throw new ReadFailedException(id, e);
94 if (dumpOptional.isPresent()) {
95 final Optional<IpAddressDetails> ipAddressDetails =
96 findIpAddressDetailsByIp(dumpOptional, id.firstKeyOf(Address.class).getIp());
98 if (ipAddressDetails.isPresent()) {
99 final IpAddressDetails detail = ipAddressDetails.get();
100 builder.setIp(arrayToIpv4AddressNoZone(detail.ip))
102 new PrefixLengthBuilder().setPrefixLength(Short.valueOf(detail.prefixLength)).build());
104 if (LOG.isDebugEnabled()) {
105 LOG.debug("Attributes for {} interface (id={}) address {} successfully read: {}",
106 interfaceName, interfaceIndex, id, builder.build());
115 public List<AddressKey> getAllIds(@Nonnull InstanceIdentifier<Address> id, @Nonnull ReadContext ctx)
116 throws ReadFailedException {
117 LOG.debug("Reading list of keys for interface addresses: {}", id);
119 final String interfaceName = id.firstKeyOf(Interface.class).getName();
120 final int interfaceIndex = interfaceContext.getIndex(interfaceName, ctx.getMappingContext());
121 final Optional<IpAddressDetailsReplyDump> dumpOptional;
124 dumpManager.getDump(CACHE_KEY, ctx.getModificationCache(),
125 new AddressDumpParams(interfaceIndex, false));
126 } catch (DumpExecutionFailedException e) {
127 throw new ReadFailedException(id, e);
130 return getAllIpv4AddressIds(dumpOptional, AddressKey::new);
134 public void merge(@Nonnull Builder<? extends DataObject> builder, @Nonnull List<Address> readData) {
135 ((Ipv4Builder) builder).setAddress(readData);