aaedabd9fcb4cc401b41635c5847c87923f99a18
[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.v3po.interfacesstate.ip.v4.subinterface;
18
19 import com.google.common.base.Optional;
20 import io.fd.hc2vpp.common.translate.util.NamingContext;
21 import io.fd.hc2vpp.v3po.interfacesstate.ip.dump.params.IfaceDumpFilter;
22 import io.fd.hc2vpp.v3po.interfacesstate.ip.readers.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 org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4Builder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.Neighbor;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.NeighborBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.NeighborKey;
35 import org.opendaylight.yangtools.concepts.Builder;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38
39 import javax.annotation.Nonnull;
40 import java.util.List;
41 import java.util.function.Function;
42
43 public class SubInterfaceIpv4NeighbourCustomizer extends IpNeighbourReader
44         implements ListReaderCustomizer<Neighbor, NeighborKey, NeighborBuilder> {
45
46     public SubInterfaceIpv4NeighbourCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
47                                                @Nonnull final NamingContext interfaceContext) {
48         super(interfaceContext, false, new DumpCacheManagerBuilder<IpNeighborDetailsReplyDump, IfaceDumpFilter>()
49                 .withExecutor(createNeighbourDumpExecutor(futureJVppCore))
50                 // cached with parent interface scope
51                 .withCacheKeyFactory(subInterfaceScopedCacheKeyFactory(IpNeighborDetailsReplyDump.class))
52                 .build());
53     }
54
55     @Override
56     public NeighborBuilder getBuilder(InstanceIdentifier<Neighbor> id) {
57         return new NeighborBuilder();
58     }
59
60     @Override
61     public void readCurrentAttributes(InstanceIdentifier<Neighbor> id, NeighborBuilder builder, ReadContext ctx)
62             throws ReadFailedException {
63
64         final Ipv4AddressNoZone ip = id.firstKeyOf(Neighbor.class).getIp();
65
66         final Optional<IpNeighborDetailsReplyDump> dumpOpt = subInterfaceNeighboursDump(id, ctx);
67
68         if (dumpOpt.isPresent()) {
69             dumpOpt.get().ipNeighborDetails
70                     .stream()
71                     .filter(ipNeighborDetails -> ip.equals(arrayToIpv4AddressNoZone(ipNeighborDetails.ipAddress)))
72                     .findFirst()
73                     .ifPresent(ipNeighborDetails -> builder.setIp(arrayToIpv4AddressNoZone(ipNeighborDetails.ipAddress))
74                             .setKey(keyMapper().apply(ipNeighborDetails))
75                             .setLinkLayerAddress(toPhysAddress(ipNeighborDetails.macAddress)));
76         }
77     }
78
79     @Override
80     public List<NeighborKey> getAllIds(InstanceIdentifier<Neighbor> id, ReadContext context)
81             throws ReadFailedException {
82         return getNeighborKeys(subInterfaceNeighboursDump(id, context), keyMapper());
83     }
84
85     @Override
86     public void merge(Builder<? extends DataObject> builder, List<Neighbor> readData) {
87         ((Ipv4Builder) builder).setNeighbor(readData);
88     }
89
90     private Function<IpNeighborDetails, NeighborKey> keyMapper() {
91         return ipNeighborDetails -> new NeighborKey(arrayToIpv4AddressNoZone(ipNeighborDetails.ipAddress));
92     }
93 }