HONEYCOMB-206: change package name to match groupId
[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.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;
48
49 /**
50  * Handles the reads of {@link VniTable} nodes
51  */
52 public class VniTableCustomizer extends FutureJVppCustomizer
53         implements ListReaderCustomizer<VniTable, VniTableKey, VniTableBuilder> {
54
55     private static final Logger LOG = LoggerFactory.getLogger(VniTableCustomizer.class);
56
57     private static final String LISP_TABLE_ID_DUMP = VniTableCustomizer.class.getName();
58     private final DumpCacheManager<LispEidTableMapDetailsReplyDump, Void> dumpManager;
59
60     public VniTableCustomizer(@Nonnull final FutureJVppCore futureJvpp) {
61         super(futureJvpp);
62         this.dumpManager = new DumpCacheManager.DumpCacheManagerBuilder<LispEidTableMapDetailsReplyDump, Void>()
63                 .withExecutor(new VniTableDumpExecutor(futureJvpp))
64                 .withNonEmptyPredicate(new VniTableDumpCheck())
65                 .build();
66     }
67
68     private static VniTableKey detailsToKey(final LispEidTableMapDetails lispEidTableMapDetails) {
69         return new VniTableKey(Integer.valueOf(lispEidTableMapDetails.vni).longValue());
70
71     }
72
73     @Override
74     public void merge(@Nonnull final Builder<? extends DataObject> builder, @Nonnull final List<VniTable> readData) {
75         ((EidTableBuilder) builder).setVniTable(readData);
76     }
77
78     @Nonnull
79     @Override
80     public VniTableBuilder getBuilder(@Nonnull final InstanceIdentifier<VniTable> id) {
81         return new VniTableBuilder();
82     }
83
84     @Nonnull
85     @Override
86     public List<VniTableKey> getAllIds(@Nonnull final InstanceIdentifier<VniTable> id,
87                                        @Nonnull final ReadContext context)
88             throws ReadFailedException {
89         LOG.trace("Reading all IDS...");
90
91         Optional<LispEidTableMapDetailsReplyDump> optionalReply;
92         try {
93             optionalReply = dumpManager.getDump(LISP_TABLE_ID_DUMP, context.getModificationCache(), NO_PARAMS);
94         } catch (DumpExecutionFailedException e) {
95             throw new ReadFailedException(id, e);
96         }
97
98         if (!optionalReply.isPresent()) {
99             return Collections.emptyList();
100         }
101
102         LispEidTableMapDetailsReplyDump reply = optionalReply.get();
103         LOG.debug("Dumped ...");
104
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);
109         return collect;
110     }
111
112     @Override
113     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<VniTable> id,
114                                       @Nonnull final VniTableBuilder builder, @Nonnull final ReadContext ctx)
115             throws ReadFailedException {
116
117         checkState(id.firstKeyOf(VniTable.class) != null, "No VNI present");
118         VniTableKey key = new VniTableKey(id.firstKeyOf(VniTable.class).getVirtualNetworkIdentifier());
119
120         Optional<LispEidTableMapDetailsReplyDump> optionalReply;
121         try {
122             optionalReply = dumpManager.getDump(LISP_TABLE_ID_DUMP, ctx.getModificationCache(), NO_PARAMS);
123         } catch (DumpExecutionFailedException e) {
124             throw new ReadFailedException(id, e);
125         }
126
127         if (!optionalReply.isPresent()) {
128             return;
129         }
130
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());
135
136         builder.setVirtualNetworkIdentifier((long) details.vni);
137         builder.setTableId((long) details.dpTable);
138     }
139 }