ba34dcf82764b47ad0a1caa3722ee7a471759637
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2017 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
17 package io.fd.hc2vpp.l3.read.ipv6.subinterface;
18
19 import com.google.common.base.Optional;
20 import io.fd.hc2vpp.common.translate.util.NamingContext;
21 import io.fd.hc2vpp.l3.utils.ip.read.IfaceDumpFilter;
22 import io.fd.hc2vpp.l3.utils.ip.read.IpNeighbourReader;
23 import io.fd.honeycomb.translate.read.ReadContext;
24 import io.fd.honeycomb.translate.read.ReadFailedException;
25 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
26 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager.DumpCacheManagerBuilder;
27 import io.fd.vpp.jvpp.core.dto.IpNeighborDetails;
28 import io.fd.vpp.jvpp.core.dto.IpNeighborDetailsReplyDump;
29 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
30 import java.util.List;
31 import java.util.function.Function;
32 import javax.annotation.Nonnull;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev180319.sub._interface.ip6.attributes.Ipv6Builder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev180319.sub._interface.ip6.attributes.ipv6.Neighbor;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev180319.sub._interface.ip6.attributes.ipv6.NeighborBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev180319.sub._interface.ip6.attributes.ipv6.NeighborKey;
38 import org.opendaylight.yangtools.concepts.Builder;
39 import org.opendaylight.yangtools.yang.binding.DataObject;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41
42 public class SubInterfaceIpv6NeighbourCustomizer extends IpNeighbourReader
43         implements ListReaderCustomizer<Neighbor, NeighborKey, NeighborBuilder> {
44
45     public SubInterfaceIpv6NeighbourCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
46                                                @Nonnull final NamingContext interfaceContext) {
47         super(interfaceContext, true, new DumpCacheManagerBuilder<IpNeighborDetailsReplyDump, IfaceDumpFilter>()
48                 .withExecutor(createNeighbourDumpExecutor(futureJVppCore))
49                 // cached with parent interface scope
50                 .withCacheKeyFactory(subInterfaceScopedCacheKeyFactory(IpNeighborDetailsReplyDump.class))
51                 .build());
52     }
53
54     @Override
55     public NeighborBuilder getBuilder(InstanceIdentifier<Neighbor> id) {
56         return new NeighborBuilder();
57     }
58
59     @Override
60     public void readCurrentAttributes(InstanceIdentifier<Neighbor> id, NeighborBuilder builder, ReadContext ctx)
61             throws ReadFailedException {
62
63         final Ipv6AddressNoZone ip = id.firstKeyOf(Neighbor.class).getIp();
64
65         final Optional<IpNeighborDetailsReplyDump> dumpOpt = subInterfaceNeighboursDump(id, ctx);
66
67         if (dumpOpt.isPresent()) {
68             dumpOpt.get().ipNeighborDetails
69                     .stream()
70                     .filter(ipNeighborDetails -> ip.equals(arrayToIpv6AddressNoZone(ipNeighborDetails.ipAddress)))
71                     .findFirst()
72                     .ifPresent(ipNeighborDetails -> builder.setIp(arrayToIpv6AddressNoZone(ipNeighborDetails.ipAddress))
73                             .withKey(keyMapper().apply(ipNeighborDetails))
74                             .setLinkLayerAddress(toPhysAddress(ipNeighborDetails.macAddress)));
75         }
76     }
77
78     @Override
79     public List<NeighborKey> getAllIds(InstanceIdentifier<Neighbor> id, ReadContext context)
80             throws ReadFailedException {
81         return getNeighborKeys(subInterfaceNeighboursDump(id, context), keyMapper());
82     }
83
84     @Override
85     public void merge(Builder<? extends DataObject> builder, List<Neighbor> readData) {
86         ((Ipv6Builder) builder).setNeighbor(readData);
87     }
88
89     private Function<IpNeighborDetails, NeighborKey> keyMapper() {
90         return ipNeighborDetails -> new NeighborKey(arrayToIpv6AddressNoZone(ipNeighborDetails.ipAddress));
91     }
92 }