2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.v3po.interfacesstate;
19 import io.fd.honeycomb.translate.read.ReadContext;
20 import io.fd.honeycomb.translate.read.ReadFailedException;
21 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
22 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
23 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
24 import io.fd.honeycomb.translate.vpp.util.NamingContext;
25 import io.fd.vpp.jvpp.core.dto.SwInterfaceTapDetails;
26 import io.fd.vpp.jvpp.core.dto.SwInterfaceTapDetailsReplyDump;
27 import io.fd.vpp.jvpp.core.dto.SwInterfaceTapDump;
28 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
29 import java.util.Collections;
30 import java.util.List;
32 import java.util.concurrent.CompletionStage;
33 import java.util.stream.Collectors;
34 import javax.annotation.Nonnull;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceStateAugmentationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.Tap;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.TapBuilder;
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;
47 public class TapCustomizer extends FutureJVppCustomizer
48 implements ReaderCustomizer<Tap, TapBuilder>, InterfaceDataTranslator, JvppReplyConsumer {
50 private static final Logger LOG = LoggerFactory.getLogger(TapCustomizer.class);
51 public static final String DUMPED_TAPS_CONTEXT_KEY = TapCustomizer.class.getName() + "dumpedTapsDuringGetAllIds";
52 private NamingContext interfaceContext;
54 public TapCustomizer(@Nonnull final FutureJVppCore jvpp, @Nonnull final NamingContext interfaceContext) {
56 this.interfaceContext = interfaceContext;
60 public void merge(@Nonnull Builder<? extends DataObject> parentBuilder, @Nonnull Tap readValue) {
61 ((VppInterfaceStateAugmentationBuilder) parentBuilder).setTap(readValue);
66 public TapBuilder getBuilder(@Nonnull InstanceIdentifier<Tap> id) {
67 return new TapBuilder();
71 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Tap> id,
72 @Nonnull final TapBuilder builder,
73 @Nonnull final ReadContext ctx) throws ReadFailedException {
75 final InterfaceKey key = id.firstKeyOf(Interface.class);
76 final int index = interfaceContext.getIndex(key.getName(), ctx.getMappingContext());
77 if (!isInterfaceOfType(getFutureJVpp(), ctx.getModificationCache(), id, index,
78 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.Tap.class, LOG)) {
82 LOG.debug("Reading attributes for tap interface: {}", key.getName());
84 @SuppressWarnings("unchecked")
85 Map<Integer, SwInterfaceTapDetails> mappedTaps =
86 (Map<Integer, SwInterfaceTapDetails>) ctx.getModificationCache().get(DUMPED_TAPS_CONTEXT_KEY);
88 if (mappedTaps == null) {
89 // Full Tap dump has to be performed here, no filter or anything is here to help so at least we cache it
90 final SwInterfaceTapDump request = new SwInterfaceTapDump();
91 final CompletionStage<SwInterfaceTapDetailsReplyDump> swInterfaceTapDetailsReplyDumpCompletionStage =
92 getFutureJVpp().swInterfaceTapDump(request);
93 final SwInterfaceTapDetailsReplyDump reply =
94 getReplyForRead(swInterfaceTapDetailsReplyDumpCompletionStage.toCompletableFuture(), id);
96 if (null == reply || null == reply.swInterfaceTapDetails) {
97 mappedTaps = Collections.emptyMap();
99 final List<SwInterfaceTapDetails> swInterfaceTapDetails = reply.swInterfaceTapDetails;
100 // Cache interfaces dump in per-tx context to later be used in readCurrentAttributes
101 mappedTaps = swInterfaceTapDetails.stream()
102 .collect(Collectors.toMap(t -> t.swIfIndex, swInterfaceDetails -> swInterfaceDetails));
105 ctx.getModificationCache().put(DUMPED_TAPS_CONTEXT_KEY, mappedTaps);
108 final SwInterfaceTapDetails swInterfaceTapDetails = mappedTaps.get(index);
109 LOG.trace("Tap interface: {} attributes returned from VPP: {}", key.getName(), swInterfaceTapDetails);
111 builder.setTapName(toString(swInterfaceTapDetails.devName));
112 LOG.debug("Tap interface: {}, id: {} attributes read as: {}", key.getName(), index, builder);