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.hc2vpp.v3po.interfacesstate.span;
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;
46 public final class MirroredInterfacesCustomizer
47 extends FutureJVppCustomizer
48 implements InitializingReaderCustomizer<MirroredInterfaces, MirroredInterfacesBuilder>, JvppReplyConsumer {
50 private static final Logger LOG = LoggerFactory.getLogger(MirroredInterfacesCustomizer.class);
52 private final NamingContext ifcContext;
54 public MirroredInterfacesCustomizer(@Nonnull final FutureJVppCore futureJVppCore, final NamingContext ifcContext) {
55 super(futureJVppCore);
56 this.ifcContext = ifcContext;
61 public MirroredInterfacesBuilder getBuilder(@Nonnull final InstanceIdentifier<MirroredInterfaces> id) {
62 return new MirroredInterfacesBuilder();
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);
71 ifcContext.getIndex(id.firstKeyOf(Interface.class).getName(), ctx.getMappingContext());
73 final SwInterfaceSpanDetailsReplyDump replyForRead;
74 if (ctx.getModificationCache().containsKey(getCacheKey())) {
75 replyForRead = (SwInterfaceSpanDetailsReplyDump) ctx.getModificationCache().get(getCacheKey());
77 replyForRead = getReplyForRead(getFutureJVpp().swInterfaceSpanDump(
78 new SwInterfaceSpanDump()).toCompletableFuture(), id);
79 ctx.getModificationCache().put(getCacheKey(), replyForRead);
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());
88 LOG.debug("Mirrored interfaces for: {} read as: {}", id, mirroredInterfaces);
90 if (!mirroredInterfaces.isEmpty()) {
91 builder.setMirroredInterface(mirroredInterfaces);
95 private String getCacheKey() {
96 return getClass().getName();
100 public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder,
101 @Nonnull final MirroredInterfaces readValue) {
102 ((SpanBuilder) parentBuilder).setMirroredInterfaces(readValue);
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)
114 .child(MirroredInterfaces.class);
115 return Initialized.create(cfgId, readValue);