HONEYCOMB-154: update revison of models that changed since 16.09
[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 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;
31 import java.util.Map;
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.rev161214.VppInterfaceStateAugmentationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.Tap;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.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;
45
46
47 public class TapCustomizer extends FutureJVppCustomizer
48         implements ReaderCustomizer<Tap, TapBuilder>, InterfaceDataTranslator, JvppReplyConsumer {
49
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;
53
54     public TapCustomizer(@Nonnull final FutureJVppCore jvpp, @Nonnull final NamingContext interfaceContext) {
55         super(jvpp);
56         this.interfaceContext = interfaceContext;
57     }
58
59     @Override
60     public void merge(@Nonnull Builder<? extends DataObject> parentBuilder, @Nonnull Tap readValue) {
61         ((VppInterfaceStateAugmentationBuilder) parentBuilder).setTap(readValue);
62     }
63
64     @Nonnull
65     @Override
66     public TapBuilder getBuilder(@Nonnull InstanceIdentifier<Tap> id) {
67         return new TapBuilder();
68     }
69
70     @Override
71     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Tap> id,
72                                       @Nonnull final TapBuilder builder,
73                                       @Nonnull final ReadContext ctx) throws ReadFailedException {
74
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.rev161214.Tap.class, LOG)) {
79             return;
80         }
81
82         LOG.debug("Reading attributes for tap interface: {}", key.getName());
83
84         @SuppressWarnings("unchecked")
85         Map<Integer, SwInterfaceTapDetails> mappedTaps =
86                 (Map<Integer, SwInterfaceTapDetails>) ctx.getModificationCache().get(DUMPED_TAPS_CONTEXT_KEY);
87
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);
95
96             if (null == reply || null == reply.swInterfaceTapDetails) {
97                 mappedTaps = Collections.emptyMap();
98             } else {
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));
103             }
104
105             ctx.getModificationCache().put(DUMPED_TAPS_CONTEXT_KEY, mappedTaps);
106         }
107
108         final SwInterfaceTapDetails swInterfaceTapDetails = mappedTaps.get(index);
109         LOG.trace("Tap interface: {} attributes returned from VPP: {}", key.getName(), swInterfaceTapDetails);
110
111         builder.setTapName(toString(swInterfaceTapDetails.devName));
112         LOG.debug("Tap interface: {}, id: {} attributes read as: {}", key.getName(), index, builder);
113     }
114 }