2396bcfb7e1443cd797e65b54a1311b2345f0585
[hc2vpp.git] /
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.hc2vpp.v3po.interfacesstate.span;
18
19 import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer;
20 import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer;
21 import io.fd.hc2vpp.common.translate.util.NamingContext;
22 import io.fd.hc2vpp.v3po.interfacesstate.InterfaceCustomizer;
23 import io.fd.honeycomb.translate.read.ReadContext;
24 import io.fd.honeycomb.translate.read.ReadFailedException;
25 import io.fd.honeycomb.translate.spi.read.Initialized;
26 import io.fd.honeycomb.translate.spi.read.InitializingReaderCustomizer;
27 import io.fd.honeycomb.translate.util.RWUtils;
28 import io.fd.vpp.jvpp.core.dto.SwInterfaceSpanDetailsReplyDump;
29 import io.fd.vpp.jvpp.core.dto.SwInterfaceSpanDump;
30 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
31 import java.util.List;
32 import java.util.stream.Collectors;
33 import javax.annotation.Nonnull;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.VppInterfaceAugmentation;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.Span;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces.state._interface.SpanBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.span.attributes.MirroredInterfaces;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.span.attributes.MirroredInterfacesBuilder;
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 public final class MirroredInterfacesCustomizer
47         extends FutureJVppCustomizer
48         implements InitializingReaderCustomizer<MirroredInterfaces, MirroredInterfacesBuilder>, JvppReplyConsumer {
49
50     private static final Logger LOG = LoggerFactory.getLogger(MirroredInterfacesCustomizer.class);
51
52     private final NamingContext ifcContext;
53
54     public MirroredInterfacesCustomizer(@Nonnull final FutureJVppCore futureJVppCore, final NamingContext ifcContext) {
55         super(futureJVppCore);
56         this.ifcContext = ifcContext;
57     }
58
59     @Nonnull
60     @Override
61     public MirroredInterfacesBuilder getBuilder(@Nonnull final InstanceIdentifier<MirroredInterfaces> id) {
62         return new MirroredInterfacesBuilder();
63     }
64
65     @Override
66     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<MirroredInterfaces> id,
67                                       @Nonnull final MirroredInterfacesBuilder builder, @Nonnull final ReadContext ctx)
68             throws ReadFailedException {
69         LOG.trace("Reading mirrored interfaces under: {}", id);
70         final int dstId =
71                 ifcContext.getIndex(id.firstKeyOf(Interface.class).getName(), ctx.getMappingContext());
72
73         final SwInterfaceSpanDetailsReplyDump replyForRead;
74         if (ctx.getModificationCache().containsKey(getCacheKey())) {
75             replyForRead = (SwInterfaceSpanDetailsReplyDump) ctx.getModificationCache().get(getCacheKey());
76         } else {
77             replyForRead = getReplyForRead(getFutureJVpp().swInterfaceSpanDump(
78                     new SwInterfaceSpanDump()).toCompletableFuture(), id);
79             ctx.getModificationCache().put(getCacheKey(), replyForRead);
80         }
81
82         final List<String> mirroredInterfaces =
83                 replyForRead.swInterfaceSpanDetails.stream()
84                         .filter(detail -> detail.swIfIndexTo == dstId)
85                         .map(detail -> ifcContext.getName(detail.swIfIndexFrom, ctx.getMappingContext()))
86                         .collect(Collectors.toList());
87
88         LOG.debug("Mirrored interfaces for: {} read as: {}", id, mirroredInterfaces);
89
90         if (!mirroredInterfaces.isEmpty()) {
91             builder.setMirroredInterface(mirroredInterfaces);
92         }
93     }
94
95     private String getCacheKey() {
96         return getClass().getName();
97     }
98
99     @Override
100     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder,
101                       @Nonnull final MirroredInterfaces readValue) {
102         ((SpanBuilder) parentBuilder).setMirroredInterfaces(readValue);
103     }
104
105     @Nonnull
106     @Override
107     public Initialized<? extends DataObject> init(@Nonnull final InstanceIdentifier<MirroredInterfaces> id,
108                                                   @Nonnull final MirroredInterfaces readValue,
109                                                   @Nonnull final ReadContext ctx) {
110         final InstanceIdentifier<MirroredInterfaces> cfgId =
111                 InterfaceCustomizer.getCfgId(RWUtils.cutId(id, Interface.class))
112                         .augmentation(VppInterfaceAugmentation.class)
113                         .child(Span.class)
114                         .child(MirroredInterfaces.class);
115         return Initialized.create(cfgId, readValue);
116     }
117 }