HONEYCOMB-58 - Routing Api
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / read / VrfSubtableCustomizer.java
1 /*
2  * Copyright (c) 2016 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.params.SubtableDumpParams;
23 import io.fd.honeycomb.lisp.translate.read.trait.SubtableReader;
24 import io.fd.honeycomb.translate.read.ReadContext;
25 import io.fd.honeycomb.translate.read.ReadFailedException;
26 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
27 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
28 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager.DumpCacheManagerBuilder;
29 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
30 import io.fd.vpp.jvpp.core.dto.LispEidTableMapDetails;
31 import io.fd.vpp.jvpp.core.dto.LispEidTableMapDetailsReplyDump;
32 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
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.rev161214.eid.table.grouping.eid.table.VniTable;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.eid.table.VniTableBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.eid.table.vni.table.VrfSubtable;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.eid.table.grouping.eid.table.vni.table.VrfSubtableBuilder;
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.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
51     private final DumpCacheManager<LispEidTableMapDetailsReplyDump, SubtableDumpParams> dumpManager;
52
53     public VrfSubtableCustomizer(@Nonnull final FutureJVppCore futureJvpp) {
54         super(futureJvpp);
55         dumpManager = new DumpCacheManagerBuilder<LispEidTableMapDetailsReplyDump, SubtableDumpParams>()
56                 .withExecutor(createExecutor(futureJvpp))
57                 .build();
58     }
59
60     @Nonnull
61     @Override
62     public VrfSubtableBuilder getBuilder(@Nonnull final InstanceIdentifier<VrfSubtable> id) {
63         return new VrfSubtableBuilder();
64     }
65
66     @Override
67     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<VrfSubtable> id,
68                                       @Nonnull final VrfSubtableBuilder builder, @Nonnull final ReadContext ctx)
69             throws ReadFailedException {
70         LOG.debug("Read attributes for {}", id);
71         final int vni = checkNotNull(id.firstKeyOf(VniTable.class), "Cannot find parent VNI Table")
72                 .getVirtualNetworkIdentifier().intValue();
73
74         final Optional<LispEidTableMapDetailsReplyDump> reply = dumpManager.getDump(id, ctx.getModificationCache(), L3_PARAMS);
75
76         if (!reply.isPresent() || reply.get().lispEidTableMapDetails.isEmpty()) {
77             return;
78         }
79
80         // Single item collector cant be used in this case,because vrf-subtable is container
81         // so read is invoked every time parent is defined
82         final List<LispEidTableMapDetails> details =
83                 reply.get().lispEidTableMapDetails.stream().filter(a -> a.vni == vni)
84                         .collect(Collectors.toList());
85         if (details.size() == 1) {
86             final LispEidTableMapDetails detail = details.get(0);
87             builder.setTableId(Integer.valueOf(detail.dpTable).longValue());
88
89             LOG.debug("Attributes for {} successfully loaded", id);
90         }
91     }
92
93     @Override
94     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder,
95                       @Nonnull final VrfSubtable readValue) {
96         ((VniTableBuilder) parentBuilder).setVrfSubtable(readValue);
97     }
98 }