2 * Copyright (c) 2017 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.
17 package io.fd.hc2vpp.lisp.gpe.translate.read;
19 import com.google.common.primitives.UnsignedInts;
20 import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer;
21 import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer;
22 import io.fd.honeycomb.translate.read.ReadContext;
23 import io.fd.honeycomb.translate.read.ReadFailedException;
24 import io.fd.honeycomb.translate.spi.read.Initialized;
25 import io.fd.honeycomb.translate.spi.read.InitializingListReaderCustomizer;
26 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
27 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager.DumpCacheManagerBuilder;
28 import io.fd.jvpp.core.dto.Ip6FibDetailsReplyDump;
29 import io.fd.jvpp.core.dto.Ip6FibDump;
30 import io.fd.jvpp.core.dto.IpFibDetailsReplyDump;
31 import io.fd.jvpp.core.dto.IpFibDump;
32 import io.fd.jvpp.core.future.FutureJVppCore;
33 import java.util.List;
34 import java.util.OptionalLong;
35 import java.util.stream.Collectors;
36 import java.util.stream.Stream;
37 import javax.annotation.Nonnull;
38 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801.NativeForwardPathsTables;
39 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801.NativeForwardPathsTablesStateBuilder;
40 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801._native.forward.paths.tables.state.NativeForwardPathsTable;
41 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801._native.forward.paths.tables.state.NativeForwardPathsTableBuilder;
42 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801._native.forward.paths.tables.state.NativeForwardPathsTableKey;
43 import org.opendaylight.yangtools.concepts.Builder;
44 import org.opendaylight.yangtools.yang.binding.DataObject;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
48 public class NativeForwardPathsTableCustomizer extends FutureJVppCustomizer implements
49 InitializingListReaderCustomizer<NativeForwardPathsTable, NativeForwardPathsTableKey, NativeForwardPathsTableBuilder>,
52 // no need to recreate, has no params
53 private static final IpFibDump REQUEST_V4 = new IpFibDump();
54 private static final Ip6FibDump REQUEST_V6 = new Ip6FibDump();
55 private static final IpFibDetailsReplyDump DEFAULT_REPLY_V4 = new IpFibDetailsReplyDump();
56 private static final Ip6FibDetailsReplyDump DEFAULT_REPLY_V6 = new Ip6FibDetailsReplyDump();
57 private DumpCacheManager<IpFibDetailsReplyDump, Void> dumpCacheManagerV4;
58 private DumpCacheManager<Ip6FibDetailsReplyDump, Void> dumpCacheManagerV6;
60 public NativeForwardPathsTableCustomizer(@Nonnull final FutureJVppCore futureJVppCore) {
61 super(futureJVppCore);
63 // there's no lisp specific dump for tables created by gpe_add_del_iface,
64 // so have to use standard fib dump
65 dumpCacheManagerV4 = new DumpCacheManagerBuilder<IpFibDetailsReplyDump, Void>()
66 .acceptOnly(IpFibDetailsReplyDump.class)
67 .withExecutor((identifier, params) -> getReplyForRead(
68 getFutureJVpp().ipFibDump(REQUEST_V4).toCompletableFuture(),
71 dumpCacheManagerV6 = new DumpCacheManagerBuilder<Ip6FibDetailsReplyDump, Void>()
72 .acceptOnly(Ip6FibDetailsReplyDump.class)
73 .withExecutor((identifier, params) -> getReplyForRead(
74 getFutureJVpp().ip6FibDump(REQUEST_V6).toCompletableFuture(),
80 public Initialized<? extends DataObject> init(@Nonnull final InstanceIdentifier<NativeForwardPathsTable> id,
81 @Nonnull final NativeForwardPathsTable readValue,
82 @Nonnull final ReadContext ctx) {
83 final org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801._native.forward.paths.tables.NativeForwardPathsTable
85 new org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801._native.forward.paths.tables.NativeForwardPathsTableBuilder()
86 .setTableId(readValue.getTableId())
89 final KeyedInstanceIdentifier<org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801._native.forward.paths.tables.NativeForwardPathsTable, org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801._native.forward.paths.tables.NativeForwardPathsTableKey>
90 cfgKey = InstanceIdentifier.create(NativeForwardPathsTables.class)
91 .child(org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801._native.forward.paths.tables.NativeForwardPathsTable.class,
92 new org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.gpe.rev170801._native.forward.paths.tables.NativeForwardPathsTableKey(
95 return Initialized.create(cfgKey, cfgValue);
100 public List<NativeForwardPathsTableKey> getAllIds(@Nonnull final InstanceIdentifier<NativeForwardPathsTable> id,
101 @Nonnull final ReadContext context) throws ReadFailedException {
103 return Stream.concat(v4FibsStream(id, context), v6FibsStream(id, context))
104 .mapToLong(UnsignedInts::toLong)
106 .mapToObj(NativeForwardPathsTableKey::new)
107 .collect(Collectors.toList());
112 public void merge(@Nonnull final Builder<? extends DataObject> builder,
113 @Nonnull final List<NativeForwardPathsTable> readData) {
114 ((NativeForwardPathsTablesStateBuilder) builder).setNativeForwardPathsTable(readData);
119 public NativeForwardPathsTableBuilder getBuilder(@Nonnull final InstanceIdentifier<NativeForwardPathsTable> id) {
120 return new NativeForwardPathsTableBuilder();
124 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<NativeForwardPathsTable> id,
125 @Nonnull final NativeForwardPathsTableBuilder builder,
126 @Nonnull final ReadContext ctx)
127 throws ReadFailedException {
128 final Long tableId = id.firstKeyOf(NativeForwardPathsTable.class).getTableId();
130 final OptionalLong optionalTable = Stream.concat(v4FibsStream(id, ctx), v6FibsStream(id, ctx))
131 .mapToLong(UnsignedInts::toLong)
133 .filter(tblId -> tblId == tableId)
136 if (optionalTable.isPresent()) {
137 final long existingTableId = optionalTable.getAsLong();
138 builder.setTableId(existingTableId);
139 builder.withKey(new NativeForwardPathsTableKey(existingTableId));
143 private Stream<Integer> v4FibsStream(final InstanceIdentifier<NativeForwardPathsTable> id,
144 final ReadContext ctx) throws ReadFailedException {
146 return dumpCacheManagerV4.getDump(id, ctx.getModificationCache()).or(DEFAULT_REPLY_V4)
147 .ipFibDetails.stream()
148 .map(ipFibDetails -> ipFibDetails.tableId);
151 private Stream<Integer> v6FibsStream(final InstanceIdentifier<NativeForwardPathsTable> id,
152 final ReadContext ctx) throws ReadFailedException {
154 return dumpCacheManagerV6.getDump(id, ctx.getModificationCache()).or(DEFAULT_REPLY_V6)
155 .ip6FibDetails.stream()
156 .map(ip6FibDetails -> ip6FibDetails.tableId);