2 * Copyright (c) 2015 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.honeycomb.lisp.translate.read;
19 import static com.google.common.base.Preconditions.checkState;
20 import static io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor.NO_PARAMS;
22 import com.google.common.base.Optional;
23 import io.fd.honeycomb.lisp.translate.read.dump.check.VniTableDumpCheck;
24 import io.fd.honeycomb.lisp.translate.read.dump.executor.VniTableDumpExecutor;
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.RWUtils;
29 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
30 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
31 import io.fd.honeycomb.translate.util.read.cache.exceptions.execution.DumpExecutionFailedException;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.stream.Collectors;
35 import javax.annotation.Nonnull;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.EidTableBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.VniTable;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.VniTableBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.VniTableKey;
40 import org.opendaylight.yangtools.concepts.Builder;
41 import org.opendaylight.yangtools.yang.binding.DataObject;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.openvpp.jvpp.core.dto.LispEidTableMapDetails;
44 import org.openvpp.jvpp.core.dto.LispEidTableMapDetailsReplyDump;
45 import org.openvpp.jvpp.core.future.FutureJVppCore;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * Handles the reads of {@link VniTable} nodes
52 public class VniTableCustomizer extends FutureJVppCustomizer
53 implements ListReaderCustomizer<VniTable, VniTableKey, VniTableBuilder> {
55 private static final Logger LOG = LoggerFactory.getLogger(VniTableCustomizer.class);
57 private static final String LISP_TABLE_ID_DUMP = VniTableCustomizer.class.getName();
58 private final DumpCacheManager<LispEidTableMapDetailsReplyDump, Void> dumpManager;
60 public VniTableCustomizer(@Nonnull final FutureJVppCore futureJvpp) {
62 this.dumpManager = new DumpCacheManager.DumpCacheManagerBuilder<LispEidTableMapDetailsReplyDump, Void>()
63 .withExecutor(new VniTableDumpExecutor(futureJvpp))
64 .withNonEmptyPredicate(new VniTableDumpCheck())
68 private static VniTableKey detailsToKey(final LispEidTableMapDetails lispEidTableMapDetails) {
69 return new VniTableKey(Integer.valueOf(lispEidTableMapDetails.vni).longValue());
74 public void merge(@Nonnull final Builder<? extends DataObject> builder, @Nonnull final List<VniTable> readData) {
75 ((EidTableBuilder) builder).setVniTable(readData);
80 public VniTableBuilder getBuilder(@Nonnull final InstanceIdentifier<VniTable> id) {
81 return new VniTableBuilder();
86 public List<VniTableKey> getAllIds(@Nonnull final InstanceIdentifier<VniTable> id,
87 @Nonnull final ReadContext context)
88 throws ReadFailedException {
89 LOG.trace("Reading all IDS...");
91 Optional<LispEidTableMapDetailsReplyDump> optionalReply;
93 optionalReply = dumpManager.getDump(LISP_TABLE_ID_DUMP, context.getModificationCache(), NO_PARAMS);
94 } catch (DumpExecutionFailedException e) {
95 throw new ReadFailedException(id, e);
98 if (!optionalReply.isPresent()) {
99 return Collections.emptyList();
102 LispEidTableMapDetailsReplyDump reply = optionalReply.get();
103 LOG.debug("Dumped ...");
105 // Just transform received details into a list of keys
106 final List<VniTableKey> collect = reply.lispEidTableMapDetails.stream().map(VniTableCustomizer::detailsToKey)
107 .collect(Collectors.toList());
108 LOG.debug("All IDs found: {} ...", collect);
113 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<VniTable> id,
114 @Nonnull final VniTableBuilder builder, @Nonnull final ReadContext ctx)
115 throws ReadFailedException {
117 checkState(id.firstKeyOf(VniTable.class) != null, "No VNI present");
118 VniTableKey key = new VniTableKey(id.firstKeyOf(VniTable.class).getVirtualNetworkIdentifier());
120 Optional<LispEidTableMapDetailsReplyDump> optionalReply;
122 optionalReply = dumpManager.getDump(LISP_TABLE_ID_DUMP, ctx.getModificationCache(), NO_PARAMS);
123 } catch (DumpExecutionFailedException e) {
124 throw new ReadFailedException(id, e);
127 if (!optionalReply.isPresent()) {
131 //transforming right away to single detail(specific request should do the magic)
132 LispEidTableMapDetails details =
133 optionalReply.get().lispEidTableMapDetails.stream().filter(a -> detailsToKey(a).equals(key))
134 .collect(RWUtils.singleItemCollector());
136 builder.setVirtualNetworkIdentifier((long) details.vni);
137 builder.setTableId((long) details.dpTable);