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.
16 package io.fd.honeycomb.v3po.translate.v3po.interfacesstate.ip;
18 import io.fd.honeycomb.v3po.translate.read.ReadContext;
19 import io.fd.honeycomb.v3po.translate.read.ReadFailedException;
20 import io.fd.honeycomb.v3po.translate.spi.read.ChildReaderCustomizer;
21 import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer;
22 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
23 import io.fd.honeycomb.v3po.translate.v3po.util.TranslateUtils;
24 import java.util.concurrent.CompletionStage;
25 import java.util.stream.Collectors;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.Interface2Builder;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.Ipv4Builder;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.Address;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.AddressBuilder;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces.state._interface.ipv4.address.subnet.PrefixLengthBuilder;
34 import org.opendaylight.yangtools.concepts.Builder;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.openvpp.jvpp.VppBaseCallException;
38 import org.openvpp.jvpp.dto.IpAddressDetails;
39 import org.openvpp.jvpp.dto.IpAddressDetailsReplyDump;
40 import org.openvpp.jvpp.dto.IpAddressDump;
41 import org.openvpp.jvpp.future.FutureJVpp;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class Ipv4Customizer extends FutureJVppCustomizer implements ChildReaderCustomizer<Ipv4, Ipv4Builder> {
47 private static final Logger LOG = LoggerFactory.getLogger(Ipv4Customizer.class);
49 private final NamingContext interfaceContext;
51 public Ipv4Customizer(@Nonnull final FutureJVpp futureJvpp, final NamingContext interfaceContext) {
53 this.interfaceContext = interfaceContext;
57 public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final Ipv4 readValue) {
58 ((Interface2Builder) parentBuilder).setIpv4(readValue);
63 public Ipv4Builder getBuilder(@Nonnull final InstanceIdentifier<Ipv4> id) {
64 return new Ipv4Builder();
68 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Ipv4> id, @Nonnull final Ipv4Builder builder,
69 @Nonnull final ReadContext ctx) throws ReadFailedException {
70 LOG.debug("Reading attributes for IPv4: {}", id);
71 final IpAddressDump dumpRequest = new IpAddressDump();
72 dumpRequest.isIpv6 = 0;
73 dumpRequest.swIfIndex = interfaceContext.getIndex(id.firstKeyOf(Interface.class).getName(), ctx.getMappingContext());
74 final CompletionStage<IpAddressDetailsReplyDump> addressDumpFuture = getFutureJVpp().ipAddressDump(dumpRequest);
76 // TODO consider extracting customizer for address
77 final IpAddressDetailsReplyDump reply;
79 reply = TranslateUtils.getReply(addressDumpFuture.toCompletableFuture());
80 } catch (VppBaseCallException e) {
81 LOG.warn("Unable to read IPv4 attributes for {}", id, e);
82 throw new ReadFailedException(id, e);
84 if(reply != null && reply.ipAddressDetails != null) {
86 reply.ipAddressDetails.stream()
87 .map(Ipv4Customizer::addressDetailsToIpv4)
88 .collect(Collectors.toList()));
91 // TODO what about other children ?
94 private static Address addressDetailsToIpv4(IpAddressDetails details) {
95 return new AddressBuilder()
96 .setIp(TranslateUtils.arrayToIpv4AddressNoZone(details.ip))
97 .setSubnet(new PrefixLengthBuilder().setPrefixLength((short) details.prefixLength).build())