HONEYCOMB-154: update revison of models that changed since 16.09
[honeycomb.git] / lisp / lisp2vpp / src / main / java / io / fd / honeycomb / lisp / translate / read / InterfaceCustomizer.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 com.google.common.base.Preconditions.checkNotNull;
21 import static com.google.common.base.Preconditions.checkState;
22
23 import com.google.common.base.Optional;
24 import io.fd.honeycomb.lisp.translate.read.dump.executor.LocatorDumpExecutor;
25 import io.fd.honeycomb.lisp.translate.read.dump.executor.params.LocatorDumpParams;
26 import io.fd.honeycomb.lisp.translate.read.dump.executor.params.LocatorDumpParams.LocatorDumpParamsBuilder;
27 import io.fd.honeycomb.translate.read.ReadContext;
28 import io.fd.honeycomb.translate.read.ReadFailedException;
29 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
30 import io.fd.honeycomb.translate.util.RWUtils;
31 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
32 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
33 import io.fd.honeycomb.translate.vpp.util.NamingContext;
34 import io.fd.vpp.jvpp.core.dto.LispLocatorDetails;
35 import io.fd.vpp.jvpp.core.dto.LispLocatorDetailsReplyDump;
36 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
37 import java.util.Collections;
38 import java.util.List;
39 import java.util.stream.Collectors;
40 import javax.annotation.Nonnull;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSet;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.LocatorSetBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.locator.set.Interface;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.locator.set.InterfaceBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.lisp.rev161214.locator.sets.grouping.locator.sets.locator.set.InterfaceKey;
46 import org.opendaylight.yangtools.concepts.Builder;
47 import org.opendaylight.yangtools.yang.binding.DataObject;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49
50
51 /**
52  * Customizer for reading {@code Interface}<br> Currently not supported by jvpp
53  */
54 public class InterfaceCustomizer
55         extends FutureJVppCustomizer
56         implements ListReaderCustomizer<Interface, InterfaceKey, InterfaceBuilder> {
57
58     private static final String KEY_BASE = InterfaceCustomizer.class.getName();
59
60     private final NamingContext interfaceContext;
61     private final NamingContext locatorSetContext;
62     private final DumpCacheManager<LispLocatorDetailsReplyDump, LocatorDumpParams> dumpCacheManager;
63
64     public InterfaceCustomizer(
65             @Nonnull final FutureJVppCore futureJvpp,
66             @Nonnull final NamingContext interfaceContext,
67             @Nonnull final NamingContext locatorSetContext) {
68         super(futureJvpp);
69         this.interfaceContext = checkNotNull(interfaceContext, "Interface context cannot be null");
70         this.locatorSetContext = checkNotNull(locatorSetContext, "Locator set context cannot be null");
71         this.dumpCacheManager =
72                 new DumpCacheManager.DumpCacheManagerBuilder<LispLocatorDetailsReplyDump, LocatorDumpParams>()
73                         .withExecutor(new LocatorDumpExecutor(futureJvpp))
74                         .build();
75     }
76
77     @Override
78     public InterfaceBuilder getBuilder(InstanceIdentifier<Interface> id) {
79         return new InterfaceBuilder();
80     }
81
82     @Override
83     public void readCurrentAttributes(InstanceIdentifier<Interface> id, InterfaceBuilder builder, ReadContext ctx)
84             throws ReadFailedException {
85
86         final String locatorSetName = id.firstKeyOf(LocatorSet.class).getName();
87         final String referencedInterfaceName = id.firstKeyOf(Interface.class).getInterfaceRef();
88
89         checkState(interfaceContext.containsIndex(referencedInterfaceName, ctx.getMappingContext()),
90                 "No interface mapping for name %s", referencedInterfaceName);
91         checkState(locatorSetContext.containsIndex(locatorSetName, ctx.getMappingContext()),
92                 "No locator set mapping for name %s", locatorSetName);
93
94         final int locatorSetIndexIndex = locatorSetContext.getIndex(locatorSetName, ctx.getMappingContext());
95         final int referencedInterfaceIndex =
96                 interfaceContext.getIndex(referencedInterfaceName, ctx.getMappingContext());
97
98         final LocatorDumpParams params =
99                 new LocatorDumpParamsBuilder().setLocatorSetIndex(locatorSetIndexIndex).build();
100
101         final Optional<LispLocatorDetailsReplyDump> reply =
102                 dumpCacheManager.getDump(id, KEY_BASE, ctx.getModificationCache(), params);
103
104         if (!reply.isPresent() || reply.get().lispLocatorDetails.isEmpty()) {
105             return;
106         }
107
108         final LispLocatorDetails details = reply.get()
109                 .lispLocatorDetails
110                 .stream()
111                 .filter(a -> a.swIfIndex == referencedInterfaceIndex)
112                 .collect(RWUtils.singleItemCollector());
113
114         final String interfaceRef = interfaceContext.getName(details.swIfIndex, ctx.getMappingContext());
115
116         builder.setPriority(Byte.valueOf(details.priority).shortValue());
117         builder.setWeight(Byte.valueOf(details.weight).shortValue());
118         builder.setInterfaceRef(interfaceRef);
119         builder.setKey(new InterfaceKey(interfaceRef));
120     }
121
122     @Override
123     public List<InterfaceKey> getAllIds(InstanceIdentifier<Interface> id, ReadContext context)
124             throws ReadFailedException {
125
126         checkState(id.firstKeyOf(LocatorSet.class) != null, "Cannot find reference to parent locator set");
127         final String name = id.firstKeyOf(LocatorSet.class).getName();
128
129         checkState(locatorSetContext.containsIndex(name, context.getMappingContext()), "No mapping for %s", name);
130         final LocatorDumpParams params = new LocatorDumpParamsBuilder()
131                 .setLocatorSetIndex(locatorSetContext.getIndex(name, context.getMappingContext())).build();
132
133         final Optional<LispLocatorDetailsReplyDump> reply =
134                 dumpCacheManager.getDump(id, KEY_BASE, context.getModificationCache(), params);
135
136         if (!reply.isPresent() || reply.get().lispLocatorDetails.isEmpty()) {
137             return Collections.emptyList();
138         }
139
140         return reply.get()
141                 .lispLocatorDetails
142                 .stream()
143                 .map(a -> new InterfaceKey(interfaceContext.getName(a.swIfIndex, context.getMappingContext())))
144                 .collect(Collectors.toList());
145     }
146
147     @Override
148     public void merge(Builder<? extends DataObject> builder, List<Interface> readData) {
149         ((LocatorSetBuilder) builder).setInterface(readData);
150     }
151 }