HONEYCOMB-58 - Routing Api
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / read / ItrRemoteLocatorSetCustomizer.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
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.translate.read.ReadContext;
24 import io.fd.honeycomb.translate.read.ReadFailedException;
25 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
26 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
27 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager.DumpCacheManagerBuilder;
28 import io.fd.honeycomb.translate.vpp.util.ByteDataTranslator;
29 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
30 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
31 import io.fd.vpp.jvpp.core.dto.LispGetMapRequestItrRlocs;
32 import io.fd.vpp.jvpp.core.dto.LispGetMapRequestItrRlocsReply;
33 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
34 import javax.annotation.Nonnull;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.itr.remote.locator.sets.grouping.ItrRemoteLocatorSet;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.itr.remote.locator.sets.grouping.ItrRemoteLocatorSetBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.lisp.feature.data.grouping.LispFeatureDataBuilder;
38 import org.opendaylight.yangtools.concepts.Builder;
39 import org.opendaylight.yangtools.yang.binding.DataObject;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41
42 public class ItrRemoteLocatorSetCustomizer extends FutureJVppCustomizer
43         implements ReaderCustomizer<ItrRemoteLocatorSet, ItrRemoteLocatorSetBuilder>, ByteDataTranslator,
44         JvppReplyConsumer {
45
46     private final DumpCacheManager<LispGetMapRequestItrRlocsReply, Void> dumpCacheManager;
47
48     public ItrRemoteLocatorSetCustomizer(@Nonnull final FutureJVppCore futureJVppCore) {
49         super(futureJVppCore);
50         dumpCacheManager = new DumpCacheManagerBuilder<LispGetMapRequestItrRlocsReply, Void>()
51                 .withExecutor(((identifier, params) -> getReplyForRead(
52                         futureJVppCore.lispGetMapRequestItrRlocs(new LispGetMapRequestItrRlocs()).toCompletableFuture(),
53                         identifier)))
54                 .build();
55     }
56
57     @Nonnull
58     @Override
59     public ItrRemoteLocatorSetBuilder getBuilder(@Nonnull final InstanceIdentifier<ItrRemoteLocatorSet> id) {
60         return new ItrRemoteLocatorSetBuilder();
61     }
62
63     @Override
64     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<ItrRemoteLocatorSet> id,
65                                       @Nonnull final ItrRemoteLocatorSetBuilder builder, @Nonnull final ReadContext ctx)
66             throws ReadFailedException {
67
68         final Optional<LispGetMapRequestItrRlocsReply> reply =
69                 dumpCacheManager.getDump(id, ctx.getModificationCache(), NO_PARAMS);
70         if (!reply.isPresent() || reply.get().locatorSetName == null) {
71             return;
72         }
73
74         builder.setRemoteLocatorSetName(toString(reply.get().locatorSetName));
75     }
76
77     @Override
78     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder,
79                       @Nonnull final ItrRemoteLocatorSet readValue) {
80         ((LispFeatureDataBuilder) parentBuilder).setItrRemoteLocatorSet(readValue);
81     }
82 }