ff0855a74b088190fd745f7dd002cd9e15717c0c
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / read / LocatorSetCustomizer.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 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.trait.LocatorSetReader;
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.read.cache.DumpCacheManager;
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.NamingContext;
31 import io.fd.vpp.jvpp.core.dto.LispLocatorSetDetails;
32 import io.fd.vpp.jvpp.core.dto.LispLocatorSetDetailsReplyDump;
33 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.stream.Collectors;
37 import javax.annotation.Nonnull;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.LocatorSetsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSet;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSetBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSetKey;
42 import org.opendaylight.yangtools.concepts.Builder;
43 import org.opendaylight.yangtools.yang.binding.DataObject;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 public class LocatorSetCustomizer extends FutureJVppCustomizer
49         implements ListReaderCustomizer<LocatorSet, LocatorSetKey, LocatorSetBuilder>, ByteDataTranslator,
50         LocatorSetReader {
51
52     private static final Logger LOG = LoggerFactory.getLogger(LocatorSetCustomizer.class);
53
54     private final DumpCacheManager<LispLocatorSetDetailsReplyDump, Void> dumpManager;
55     private final NamingContext locatorSetContext;
56
57     public LocatorSetCustomizer(@Nonnull final FutureJVppCore futureJvpp,
58                                 @Nonnull final NamingContext locatorSetContext) {
59         super(futureJvpp);
60         this.locatorSetContext = checkNotNull(locatorSetContext, "Locator Set mapping context cannot be null");
61         this.dumpManager = new DumpCacheManager.DumpCacheManagerBuilder<LispLocatorSetDetailsReplyDump, Void>()
62                 .withExecutor(createExecutor(futureJvpp))
63                 .build();
64     }
65
66     @Override
67     public LocatorSetBuilder getBuilder(InstanceIdentifier<LocatorSet> id) {
68         return new LocatorSetBuilder();
69     }
70
71     @Override
72     public void readCurrentAttributes(InstanceIdentifier<LocatorSet> id, LocatorSetBuilder builder, ReadContext ctx)
73             throws ReadFailedException {
74         LOG.debug("Reading attributes for Locator Set {}", id);
75
76         final Optional<LispLocatorSetDetailsReplyDump> dumpOptional =
77                 dumpManager.getDump(id, ctx.getModificationCache(), NO_PARAMS);
78
79         if (!dumpOptional.isPresent() || dumpOptional.get().lispLocatorSetDetails.isEmpty()) {
80             return;
81         }
82
83         String keyName = id.firstKeyOf(LocatorSet.class).getName();
84         LispLocatorSetDetailsReplyDump dump = dumpOptional.get();
85
86         java.util.Optional<LispLocatorSetDetails> details = dump.lispLocatorSetDetails.stream()
87                 .filter(n -> keyName.equals(toString(n.lsName)))
88                 .findFirst();
89
90         if (details.isPresent()) {
91             final String name = toString(details.get().lsName);
92
93             builder.setName(name);
94             builder.setKey(new LocatorSetKey(name));
95         } else {
96             LOG.warn("Locator Set {} not found in dump", id);
97         }
98     }
99
100     @Override
101     public List<LocatorSetKey> getAllIds(InstanceIdentifier<LocatorSet> id, ReadContext context)
102             throws ReadFailedException {
103         LOG.debug("Dumping Locator Set {}", id);
104
105         final Optional<LispLocatorSetDetailsReplyDump> dumpOptional =
106                 dumpManager.getDump(id, context.getModificationCache(), NO_PARAMS);
107
108         if (!dumpOptional.isPresent() || dumpOptional.get().lispLocatorSetDetails.isEmpty()) {
109             return Collections.emptyList();
110         }
111
112         return dumpOptional.get().lispLocatorSetDetails.stream()
113                 .map(set -> {
114
115                     final String locatorSetName = toString(set.lsName);
116                     //creates mapping for existing locator-set(if it is'nt already existing one)
117                     if (!locatorSetContext.containsIndex(locatorSetName, context.getMappingContext())) {
118                         locatorSetContext.addName(set.lsIndex, locatorSetName, context.getMappingContext());
119                     }
120
121                     LOG.trace("Locator Set with name: {}, VPP name: {} and index: {} found in VPP",
122                             locatorSetContext.getName(set.lsIndex, context.getMappingContext()),
123                             locatorSetName,
124                             set.lsIndex);
125
126                     return set;
127                 })
128                 .map(set -> new LocatorSetKey(toString(set.lsName)))
129                 .collect(Collectors.toList());
130     }
131
132     @Override
133     public void merge(Builder<? extends DataObject> builder, List<LocatorSet> readData) {
134         ((LocatorSetsBuilder) builder).setLocatorSet(readData);
135     }
136 }