HONEYCOMB-154: update revison of models that changed since 16.09
[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.dump.executor.LocatorSetsDumpExecutor;
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
51     //TODO - temporary as public because of hack in write customizer in *.write.LocatorSetCustomizer
52     public static final String LOCATOR_SETS_CACHE_ID = LocatorSetCustomizer.class.getName();
53     private static final Logger LOG = LoggerFactory.getLogger(LocatorSetCustomizer.class);
54
55     private final DumpCacheManager<LispLocatorSetDetailsReplyDump, Void> dumpManager;
56     private final NamingContext locatorSetContext;
57
58     public LocatorSetCustomizer(@Nonnull final FutureJVppCore futureJvpp,
59                                 @Nonnull final NamingContext locatorSetContext) {
60         super(futureJvpp);
61         this.locatorSetContext = checkNotNull(locatorSetContext, "Locator Set mapping context cannot be null");
62         this.dumpManager = new DumpCacheManager.DumpCacheManagerBuilder<LispLocatorSetDetailsReplyDump, Void>()
63                 .withExecutor(new LocatorSetsDumpExecutor(futureJvpp))
64                 .build();
65     }
66
67     @Override
68     public LocatorSetBuilder getBuilder(InstanceIdentifier<LocatorSet> id) {
69         return new LocatorSetBuilder();
70     }
71
72     @Override
73     public void readCurrentAttributes(InstanceIdentifier<LocatorSet> id, LocatorSetBuilder builder, ReadContext ctx)
74             throws ReadFailedException {
75         LOG.debug("Reading attributes for Locator Set {}", id);
76
77         final Optional<LispLocatorSetDetailsReplyDump> dumpOptional =
78                 dumpManager.getDump(id, LOCATOR_SETS_CACHE_ID, ctx.getModificationCache(), NO_PARAMS);
79
80         if (!dumpOptional.isPresent() || dumpOptional.get().lispLocatorSetDetails.isEmpty()) {
81             return;
82         }
83
84         String keyName = id.firstKeyOf(LocatorSet.class).getName();
85         LispLocatorSetDetailsReplyDump dump = dumpOptional.get();
86
87         java.util.Optional<LispLocatorSetDetails> details = dump.lispLocatorSetDetails.stream()
88                 .filter(n -> keyName.equals(toString(n.lsName)))
89                 .findFirst();
90
91         if (details.isPresent()) {
92             final String name = toString(details.get().lsName);
93
94             builder.setName(name);
95             builder.setKey(new LocatorSetKey(name));
96         } else {
97             LOG.warn("Locator Set {} not found in dump", id);
98         }
99     }
100
101     @Override
102     public List<LocatorSetKey> getAllIds(InstanceIdentifier<LocatorSet> id, ReadContext context)
103             throws ReadFailedException {
104         LOG.debug("Dumping Locator Set {}", id);
105
106         final Optional<LispLocatorSetDetailsReplyDump> dumpOptional =
107                 dumpManager.getDump(id, LOCATOR_SETS_CACHE_ID, context.getModificationCache(), NO_PARAMS);
108
109         if (!dumpOptional.isPresent() || dumpOptional.get().lispLocatorSetDetails.isEmpty()) {
110             return Collections.emptyList();
111         }
112
113         return dumpOptional.get().lispLocatorSetDetails.stream()
114                 .map(set -> {
115
116                     final String locatorSetName = toString(set.lsName);
117                     //creates mapping for existing locator-set(if it is'nt already existing one)
118                     if (!locatorSetContext.containsIndex(locatorSetName, context.getMappingContext())) {
119                         locatorSetContext.addName(set.lsIndex, locatorSetName, context.getMappingContext());
120                     }
121
122                     LOG.trace("Locator Set with name: {}, VPP name: {} and index: {} found in VPP",
123                             locatorSetContext.getName(set.lsIndex, context.getMappingContext()),
124                             locatorSetName,
125                             set.lsIndex);
126
127                     return set;
128                 })
129                 .map(set -> new LocatorSetKey(toString(set.lsName)))
130                 .collect(Collectors.toList());
131     }
132
133     @Override
134     public void merge(Builder<? extends DataObject> builder, List<LocatorSet> readData) {
135         ((LocatorSetsBuilder) builder).setLocatorSet(readData);
136     }
137 }