5072c37c210f8b549d5031cbcd9e7ffa839cef35
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / read / VrfSubtableCustomizer.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.checkNotNull;
20
21 import com.google.common.base.Optional;
22 import io.fd.honeycomb.lisp.translate.read.dump.executor.SubtableDumpExecutor;
23 import io.fd.honeycomb.lisp.translate.read.dump.executor.params.SubtableDumpParams;
24 import io.fd.honeycomb.lisp.translate.read.trait.SubtableReader;
25 import io.fd.honeycomb.translate.read.ReadContext;
26 import io.fd.honeycomb.translate.read.ReadFailedException;
27 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
28 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
29 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
30 import java.util.List;
31 import java.util.stream.Collectors;
32 import javax.annotation.Nonnull;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.VniTable;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.VniTableBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.VrfSubtable;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev160520.eid.table.grouping.eid.table.vni.table.VrfSubtableBuilder;
37 import org.opendaylight.yangtools.concepts.Builder;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import io.fd.vpp.jvpp.core.dto.LispEidTableMapDetails;
41 import io.fd.vpp.jvpp.core.dto.LispEidTableMapDetailsReplyDump;
42 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 public class VrfSubtableCustomizer extends FutureJVppCustomizer
47         implements ReaderCustomizer<VrfSubtable, VrfSubtableBuilder>, SubtableReader {
48
49     private static final Logger LOG = LoggerFactory.getLogger(VrfSubtableCustomizer.class);
50     private static final String CACHE_KEY = VrfSubtableCustomizer.class.getName();
51
52     private final DumpCacheManager<LispEidTableMapDetailsReplyDump, SubtableDumpParams> dumpManager;
53     private final SubtableDumpExecutor dumpExecutor;
54
55     public VrfSubtableCustomizer(@Nonnull final FutureJVppCore futureJvpp) {
56         super(futureJvpp);
57         dumpExecutor = new SubtableDumpExecutor(futureJvpp);
58         dumpManager =
59                 new DumpCacheManager.DumpCacheManagerBuilder<LispEidTableMapDetailsReplyDump, SubtableDumpParams>()
60                         .withExecutor(dumpExecutor)
61                         .build();
62     }
63
64     @Nonnull
65     @Override
66     public VrfSubtableBuilder getBuilder(@Nonnull final InstanceIdentifier<VrfSubtable> id) {
67         return new VrfSubtableBuilder();
68     }
69
70     @Override
71     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<VrfSubtable> id,
72                                       @Nonnull final VrfSubtableBuilder builder, @Nonnull final ReadContext ctx)
73             throws ReadFailedException {
74         LOG.debug("Read attributes for {}", id);
75         final int vni = checkNotNull(id.firstKeyOf(VniTable.class), "Cannot find parent VNI Table")
76                 .getVirtualNetworkIdentifier().intValue();
77
78         final Optional<LispEidTableMapDetailsReplyDump> reply =
79                 readSubtable(dumpManager, CACHE_KEY, ctx.getModificationCache(), id, L3_PARAMS);
80
81         if (!reply.isPresent() || reply.get().lispEidTableMapDetails.isEmpty()) {
82             return;
83         }
84
85         // Single item collector cant be used in this case,because vrf-subtable is container
86         // so read is invoked every time parent is defined
87         final List<LispEidTableMapDetails> details =
88                 reply.get().lispEidTableMapDetails.stream().filter(a -> a.vni == vni)
89                         .collect(Collectors.toList());
90         if (details.size() == 1) {
91             final LispEidTableMapDetails detail = details.get(0);
92             builder.setTableId(Integer.valueOf(detail.dpTable).longValue());
93
94             LOG.debug("Attributes for {} successfully loaded", id);
95         }
96     }
97
98     @Override
99     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder,
100                       @Nonnull final VrfSubtable readValue) {
101         ((VniTableBuilder) parentBuilder).setVrfSubtable(readValue);
102     }
103 }