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.Initialized;
22 import io.fd.honeycomb.translate.spi.read.InitializingReaderCustomizer;
23 import io.fd.honeycomb.translate.util.RWUtils;
24 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
25 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
26 import io.fd.honeycomb.translate.vpp.util.NamingContext;
27 import io.fd.vpp.jvpp.core.dto.SwInterfaceDetails;
28 import io.fd.vpp.jvpp.core.dto.SwInterfaceTapDetails;
29 import io.fd.vpp.jvpp.core.dto.SwInterfaceTapDetailsReplyDump;
30 import io.fd.vpp.jvpp.core.dto.SwInterfaceTapDump;
31 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
32 import java.util.Collections;
33 import java.util.List;
35 import java.util.concurrent.CompletionStage;
36 import java.util.stream.Collectors;
37 import javax.annotation.Nonnull;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.PhysAddress;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceAugmentation;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceStateAugmentationBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.Tap;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.TapBuilder;
45 import org.opendaylight.yangtools.concepts.Builder;
46 import org.opendaylight.yangtools.yang.binding.DataObject;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 public class TapCustomizer extends FutureJVppCustomizer
53 implements InitializingReaderCustomizer<Tap, TapBuilder>, InterfaceDataTranslator, JvppReplyConsumer {
55 private static final Logger LOG = LoggerFactory.getLogger(TapCustomizer.class);
56 public static final String DUMPED_TAPS_CONTEXT_KEY = TapCustomizer.class.getName() + "dumpedTapsDuringGetAllIds";
57 private NamingContext interfaceContext;
59 public TapCustomizer(@Nonnull final FutureJVppCore jvpp, @Nonnull final NamingContext interfaceContext) {
61 this.interfaceContext = interfaceContext;
65 public void merge(@Nonnull Builder<? extends DataObject> parentBuilder, @Nonnull Tap readValue) {
66 ((VppInterfaceStateAugmentationBuilder) parentBuilder).setTap(readValue);
71 public TapBuilder getBuilder(@Nonnull InstanceIdentifier<Tap> id) {
72 return new TapBuilder();
76 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Tap> id,
77 @Nonnull final TapBuilder builder,
78 @Nonnull final ReadContext ctx) throws ReadFailedException {
80 final InterfaceKey key = id.firstKeyOf(Interface.class);
81 final int index = interfaceContext.getIndex(key.getName(), ctx.getMappingContext());
82 if (!isInterfaceOfType(getFutureJVpp(), ctx.getModificationCache(), id, index,
83 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.Tap.class, LOG)) {
87 LOG.debug("Reading attributes for tap interface: {}", key.getName());
89 @SuppressWarnings("unchecked")
90 Map<Integer, SwInterfaceTapDetails> mappedTaps =
91 (Map<Integer, SwInterfaceTapDetails>) ctx.getModificationCache().get(DUMPED_TAPS_CONTEXT_KEY);
93 if (mappedTaps == null) {
94 // Full Tap dump has to be performed here, no filter or anything is here to help so at least we cache it
95 final SwInterfaceTapDump request = new SwInterfaceTapDump();
96 final CompletionStage<SwInterfaceTapDetailsReplyDump> swInterfaceTapDetailsReplyDumpCompletionStage =
97 getFutureJVpp().swInterfaceTapDump(request);
98 final SwInterfaceTapDetailsReplyDump reply =
99 getReplyForRead(swInterfaceTapDetailsReplyDumpCompletionStage.toCompletableFuture(), id);
101 if (null == reply || null == reply.swInterfaceTapDetails) {
102 mappedTaps = Collections.emptyMap();
104 final List<SwInterfaceTapDetails> swInterfaceTapDetails = reply.swInterfaceTapDetails;
105 // Cache interfaces dump in per-tx context to later be used in readCurrentAttributes
106 mappedTaps = swInterfaceTapDetails.stream()
107 .collect(Collectors.toMap(t -> t.swIfIndex, swInterfaceDetails -> swInterfaceDetails));
110 ctx.getModificationCache().put(DUMPED_TAPS_CONTEXT_KEY, mappedTaps);
113 final SwInterfaceTapDetails swInterfaceTapDetails = mappedTaps.get(index);
114 LOG.trace("Tap interface: {} attributes returned from VPP: {}", key.getName(), swInterfaceTapDetails);
116 builder.setTapName(toString(swInterfaceTapDetails.devName));
117 LOG.debug("Tap interface: {}, id: {} attributes read as: {}", key.getName(), index, builder);
121 public Initialized<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.Tap> init(
122 @Nonnull final InstanceIdentifier<Tap> id, @Nonnull final Tap readValue, @Nonnull final ReadContext ctx) {
123 // The MAC address is set from interface details, those details are retrieved from cache
124 final InterfaceKey key = id.firstKeyOf(Interface.class);
125 final int index = interfaceContext.getIndex(key.getName(), ctx.getMappingContext());
126 final SwInterfaceDetails ifcDetails =
127 InterfaceCustomizer.getCachedInterfaceDump(ctx.getModificationCache()).get(index);
129 return Initialized.create(getCfgId(id),
130 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.TapBuilder()
131 .setMac(new PhysAddress(vppPhysAddrToYang(ifcDetails.l2Address)))
132 .setTapName(readValue.getTapName())
133 // tapBuilder.setDeviceInstance();
137 private InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.Tap> getCfgId(
138 final InstanceIdentifier<Tap> id) {
139 return InterfaceCustomizer.getCfgId(RWUtils.cutId(id, Interface.class))
140 .augmentation(VppInterfaceAugmentation.class)
141 .child(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.Tap.class);