755627f125b2bb50081ae62706a963304b5cadcc
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / read / VniTableCustomizer.java
1 /*
2  * Copyright (c) 2015 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.honeycomb.lisp.translate.read;
18
19 import static com.google.common.base.Preconditions.checkState;
20 import static io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor.NO_PARAMS;
21
22 import com.google.common.base.Optional;
23 import io.fd.honeycomb.lisp.translate.read.dump.executor.VniTableDumpExecutor;
24 import io.fd.honeycomb.translate.read.ReadContext;
25 import io.fd.honeycomb.translate.read.ReadFailedException;
26 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
27 import io.fd.honeycomb.translate.util.RWUtils;
28 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
29 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
30 import io.fd.vpp.jvpp.core.dto.LispEidTableVniDetails;
31 import io.fd.vpp.jvpp.core.dto.LispEidTableVniDetailsReplyDump;
32 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.stream.Collectors;
36 import javax.annotation.Nonnull;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.EidTableBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.VniTable;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.VniTableBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.VniTableKey;
41 import org.opendaylight.yangtools.concepts.Builder;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * Handles the reads of {@link VniTable} nodes
49  */
50 public class VniTableCustomizer extends FutureJVppCustomizer
51         implements ListReaderCustomizer<VniTable, VniTableKey, VniTableBuilder> {
52
53     private static final Logger LOG = LoggerFactory.getLogger(VniTableCustomizer.class);
54
55     private static final String LISP_TABLE_ID_DUMP = VniTableCustomizer.class.getName();
56     private final DumpCacheManager<LispEidTableVniDetailsReplyDump, Void> dumpManager;
57
58     public VniTableCustomizer(@Nonnull final FutureJVppCore futureJvpp) {
59         super(futureJvpp);
60         this.dumpManager = new DumpCacheManager.DumpCacheManagerBuilder<LispEidTableVniDetailsReplyDump, Void>()
61                 .withExecutor(new VniTableDumpExecutor(futureJvpp))
62                 .build();
63     }
64
65     private static VniTableKey detailsToKey(final LispEidTableVniDetails lispEidTableMapDetails) {
66         return new VniTableKey(Integer.valueOf(lispEidTableMapDetails.vni).longValue());
67
68     }
69
70     @Override
71     public void merge(@Nonnull final Builder<? extends DataObject> builder, @Nonnull final List<VniTable> readData) {
72         ((EidTableBuilder) builder).setVniTable(readData);
73     }
74
75     @Nonnull
76     @Override
77     public VniTableBuilder getBuilder(@Nonnull final InstanceIdentifier<VniTable> id) {
78         return new VniTableBuilder();
79     }
80
81     @Nonnull
82     @Override
83     public List<VniTableKey> getAllIds(@Nonnull final InstanceIdentifier<VniTable> id,
84                                        @Nonnull final ReadContext context)
85             throws ReadFailedException {
86         LOG.trace("Reading all IDS...");
87
88         final Optional<LispEidTableVniDetailsReplyDump> optionalReply =
89                 dumpManager.getDump(id, LISP_TABLE_ID_DUMP, context.getModificationCache(), NO_PARAMS);
90
91         if (!optionalReply.isPresent() || optionalReply.get().lispEidTableVniDetails.isEmpty()) {
92             return Collections.emptyList();
93         }
94
95         return optionalReply.get().lispEidTableVniDetails.stream().map(VniTableCustomizer::detailsToKey)
96                 .collect(Collectors.toList());
97     }
98
99     @Override
100     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<VniTable> id,
101                                       @Nonnull final VniTableBuilder builder, @Nonnull final ReadContext ctx)
102             throws ReadFailedException {
103
104         checkState(id.firstKeyOf(VniTable.class) != null, "No VNI present");
105         VniTableKey key = new VniTableKey(id.firstKeyOf(VniTable.class).getVirtualNetworkIdentifier());
106
107         final Optional<LispEidTableVniDetailsReplyDump> optionalReply =
108                 dumpManager.getDump(id, LISP_TABLE_ID_DUMP, ctx.getModificationCache(), NO_PARAMS);
109
110         if (!optionalReply.isPresent() || optionalReply.get().lispEidTableVniDetails.isEmpty()) {
111             return;
112         }
113
114         //transforming right away to single detail(specific request should do the magic)
115         final LispEidTableVniDetails details = optionalReply.get()
116                 .lispEidTableVniDetails
117                 .stream()
118                 .filter(a -> a.vni == key.getVirtualNetworkIdentifier().intValue())
119                 .collect(RWUtils.singleItemCollector());
120
121         builder.setVirtualNetworkIdentifier((long) details.vni);
122         builder.setKey(new VniTableKey(Long.valueOf(details.vni)));
123     }
124 }