VPP-378: update jvpp package names
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfacesstate / TapCustomizer.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.translate.v3po.interfacesstate;
18
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 java.util.Collections;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.concurrent.CompletionStage;
29 import java.util.stream.Collectors;
30 import javax.annotation.Nonnull;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppInterfaceStateAugmentationBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.Tap;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces.state._interface.TapBuilder;
36 import org.opendaylight.yangtools.concepts.Builder;
37 import org.opendaylight.yangtools.yang.binding.DataObject;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import io.fd.vpp.jvpp.VppBaseCallException;
40 import io.fd.vpp.jvpp.core.dto.SwInterfaceTapDetails;
41 import io.fd.vpp.jvpp.core.dto.SwInterfaceTapDetailsReplyDump;
42 import io.fd.vpp.jvpp.core.dto.SwInterfaceTapDump;
43 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47
48 public class TapCustomizer extends FutureJVppCustomizer
49         implements ReaderCustomizer<Tap, TapBuilder>, InterfaceDataTranslator, JvppReplyConsumer {
50
51     private static final Logger LOG = LoggerFactory.getLogger(TapCustomizer.class);
52     public static final String DUMPED_TAPS_CONTEXT_KEY = TapCustomizer.class.getName() + "dumpedTapsDuringGetAllIds";
53     private NamingContext interfaceContext;
54
55     public TapCustomizer(@Nonnull final FutureJVppCore jvpp, @Nonnull final NamingContext interfaceContext) {
56         super(jvpp);
57         this.interfaceContext = interfaceContext;
58     }
59
60     @Override
61     public void merge(@Nonnull Builder<? extends DataObject> parentBuilder, @Nonnull Tap readValue) {
62         ((VppInterfaceStateAugmentationBuilder) parentBuilder).setTap(readValue);
63     }
64
65     @Nonnull
66     @Override
67     public TapBuilder getBuilder(@Nonnull InstanceIdentifier<Tap> id) {
68         return new TapBuilder();
69     }
70
71     @Override
72     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Tap> id,
73                                       @Nonnull final TapBuilder builder,
74                                       @Nonnull final ReadContext ctx) throws ReadFailedException {
75         try {
76             final InterfaceKey key = id.firstKeyOf(Interface.class);
77             final int index = interfaceContext.getIndex(key.getName(), ctx.getMappingContext());
78             if (!isInterfaceOfType(getFutureJVpp(), ctx.getModificationCache(), id, index,
79                     org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.Tap.class, LOG)) {
80                 return;
81             }
82
83             LOG.debug("Reading attributes for tap interface: {}", key.getName());
84
85             @SuppressWarnings("unchecked")
86             Map<Integer, SwInterfaceTapDetails> mappedTaps =
87                     (Map<Integer, SwInterfaceTapDetails>) ctx.getModificationCache().get(DUMPED_TAPS_CONTEXT_KEY);
88
89             if (mappedTaps == null) {
90                 // Full Tap dump has to be performed here, no filter or anything is here to help so at least we cache it
91                 final SwInterfaceTapDump request = new SwInterfaceTapDump();
92                 final CompletionStage<SwInterfaceTapDetailsReplyDump> swInterfaceTapDetailsReplyDumpCompletionStage =
93                         getFutureJVpp().swInterfaceTapDump(request);
94                 final SwInterfaceTapDetailsReplyDump reply =
95                         getReplyForRead(swInterfaceTapDetailsReplyDumpCompletionStage.toCompletableFuture(), id);
96
97                 if (null == reply || null == reply.swInterfaceTapDetails) {
98                     mappedTaps = Collections.emptyMap();
99                 } else {
100                     final List<SwInterfaceTapDetails> swInterfaceTapDetails = reply.swInterfaceTapDetails;
101                     // Cache interfaces dump in per-tx context to later be used in readCurrentAttributes
102                     mappedTaps = swInterfaceTapDetails.stream()
103                             .collect(Collectors.toMap(t -> t.swIfIndex, swInterfaceDetails -> swInterfaceDetails));
104                 }
105
106                 ctx.getModificationCache().put(DUMPED_TAPS_CONTEXT_KEY, mappedTaps);
107             }
108
109             final SwInterfaceTapDetails swInterfaceTapDetails = mappedTaps.get(index);
110             LOG.trace("Tap interface: {} attributes returned from VPP: {}", key.getName(), swInterfaceTapDetails);
111
112             builder.setTapName(toString(swInterfaceTapDetails.devName));
113             LOG.debug("Tap interface: {}, id: {} attributes read as: {}", key.getName(), index, builder);
114         } catch (VppBaseCallException e) {
115             LOG.warn("Failed to readCurrentAttributes for: {}", id, e);
116             throw new ReadFailedException(id, e);
117         }
118     }
119 }