71833db0d9637dd4310064854a0b11d96b5a3e98
[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 com.google.common.collect.ImmutableSet;
21 import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer;
22 import io.fd.hc2vpp.common.translate.util.NamingContext;
23 import io.fd.hc2vpp.v3po.interfacesstate.ip.IpReader;
24 import io.fd.hc2vpp.v3po.interfacesstate.ip.dump.params.IfaceDumpFilter;
25 import io.fd.honeycomb.translate.read.ReadContext;
26 import io.fd.honeycomb.translate.read.ReadFailedException;
27 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
28 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
29 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager.DumpCacheManagerBuilder;
30 import io.fd.honeycomb.translate.util.read.cache.DumpSupplier;
31 import io.fd.honeycomb.translate.util.read.cache.TypeAwareIdentifierCacheKeyFactory;
32 import io.fd.vpp.jvpp.core.dto.IpNeighborDetails;
33 import io.fd.vpp.jvpp.core.dto.IpNeighborDetailsReplyDump;
34 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
35 import java.util.List;
36 import java.util.function.Function;
37 import javax.annotation.Nonnull;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.Ipv4Builder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.Neighbor;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.NeighborBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.ip4.attributes.ipv4.NeighborKey;
44 import org.opendaylight.yangtools.concepts.Builder;
45 import org.opendaylight.yangtools.yang.binding.DataObject;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47
48 public class SubInterfaceIpv4NeighbourCustomizer extends FutureJVppCustomizer
49         implements ListReaderCustomizer<Neighbor, NeighborKey, NeighborBuilder>, IpReader {
50
51     private final DumpCacheManager<IpNeighborDetailsReplyDump, IfaceDumpFilter> dumpManager;
52     private final NamingContext interfaceContext;
53
54     public SubInterfaceIpv4NeighbourCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
55                                                @Nonnull final NamingContext interfaceContext) {
56         super(futureJVppCore);
57         dumpManager = new DumpCacheManagerBuilder<IpNeighborDetailsReplyDump, IfaceDumpFilter>()
58                 .withExecutor(createNeighbourDumpExecutor(futureJVppCore))
59                 // cached with parent interface scope
60                 .withCacheKeyFactory(new TypeAwareIdentifierCacheKeyFactory(IpNeighborDetailsReplyDump.class,
61                         ImmutableSet.of(Interface.class)))
62                 .build();
63         this.interfaceContext = interfaceContext;
64     }
65
66     @Override
67     public NeighborBuilder getBuilder(InstanceIdentifier<Neighbor> id) {
68         return new NeighborBuilder();
69     }
70
71     @Override
72     public void readCurrentAttributes(InstanceIdentifier<Neighbor> id, NeighborBuilder builder, ReadContext ctx)
73             throws ReadFailedException {
74
75         final Ipv4AddressNoZone ip = id.firstKeyOf(Neighbor.class).getIp();
76
77         final Optional<IpNeighborDetailsReplyDump> dumpOpt = dumpSupplier(id, ctx).get();
78
79         if (dumpOpt.isPresent()) {
80             dumpOpt.get().ipNeighborDetails
81                     .stream()
82                     .filter(ipNeighborDetails -> ip.equals(arrayToIpv4AddressNoZone(ipNeighborDetails.ipAddress)))
83                     .findFirst()
84                     .ifPresent(ipNeighborDetails -> builder.setIp(arrayToIpv4AddressNoZone(ipNeighborDetails.ipAddress))
85                             .setKey(keyMapper().apply(ipNeighborDetails))
86                             .setLinkLayerAddress(toPhysAddress(ipNeighborDetails.macAddress)));
87         }
88     }
89
90     @Override
91     public List<NeighborKey> getAllIds(InstanceIdentifier<Neighbor> id, ReadContext context)
92             throws ReadFailedException {
93         return getNeighborKeys(dumpSupplier(id, context), keyMapper());
94     }
95
96     @Override
97     public void merge(Builder<? extends DataObject> builder, List<Neighbor> readData) {
98         ((Ipv4Builder) builder).setNeighbor(readData);
99     }
100
101     private Function<IpNeighborDetails, NeighborKey> keyMapper() {
102         return ipNeighborDetails -> new NeighborKey(arrayToIpv4AddressNoZone(ipNeighborDetails.ipAddress));
103     }
104
105     private DumpSupplier<Optional<IpNeighborDetailsReplyDump>> dumpSupplier(final InstanceIdentifier<Neighbor> id,
106                                                                             final ReadContext context) {
107         return () -> dumpManager
108                 .getDump(id, context.getModificationCache(), new IfaceDumpFilter(interfaceContext
109                         .getIndex(id.firstKeyOf(Interface.class).getName(), context.getMappingContext()),
110                         false));
111     }
112
113 }