582d9c8e3c7d303c4c2028cccedab351d9541a46
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package io.fd.honeycomb.v3po.translate.v3po.interfacesstate.ip;
17
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;
44
45 public class Ipv4Customizer extends FutureJVppCustomizer implements ChildReaderCustomizer<Ipv4, Ipv4Builder> {
46
47     private static final Logger LOG = LoggerFactory.getLogger(Ipv4Customizer.class);
48
49     private final NamingContext interfaceContext;
50
51     public Ipv4Customizer(@Nonnull final FutureJVpp futureJvpp, final NamingContext interfaceContext) {
52         super(futureJvpp);
53         this.interfaceContext = interfaceContext;
54     }
55
56     @Override
57     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final Ipv4 readValue) {
58         ((Interface2Builder) parentBuilder).setIpv4(readValue);
59     }
60
61     @Nonnull
62     @Override
63     public Ipv4Builder getBuilder(@Nonnull final InstanceIdentifier<Ipv4> id) {
64         return new Ipv4Builder();
65     }
66
67     @Override
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);
75
76         // TODO consider extracting customizer for address
77         final IpAddressDetailsReplyDump reply;
78         try {
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);
83         }
84         if(reply != null && reply.ipAddressDetails != null) {
85             builder.setAddress(
86                 reply.ipAddressDetails.stream()
87                     .map(Ipv4Customizer::addressDetailsToIpv4)
88                     .collect(Collectors.toList()));
89         }
90
91         // TODO what about other children ?
92     }
93
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())
98 //            .setOrigin()
99             .build();
100     }
101 }