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.honeycomb.translate.MappingContext;
23 import io.fd.honeycomb.translate.read.ReadContext;
24 import io.fd.honeycomb.translate.read.ReadFailedException;
25 import io.fd.honeycomb.translate.spi.read.InitializingReaderCustomizer;
26 import io.fd.vpp.jvpp.core.dto.SwInterfaceSpanDetailsReplyDump;
27 import io.fd.vpp.jvpp.core.dto.SwInterfaceSpanDump;
28 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
29 import java.util.List;
30 import java.util.function.Function;
31 import java.util.stream.Collectors;
32 import javax.annotation.Nonnull;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev180703.SpanState;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev180703.span.state.attributes.MirroredInterfaces;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev180703.span.state.attributes.MirroredInterfacesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev180703.span.state.attributes.mirrored.interfaces.MirroredInterface;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev180703.span.state.attributes.mirrored.interfaces.MirroredInterfaceBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev180703.span.state.attributes.mirrored.interfaces.MirroredInterfaceKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 abstract class AbstractMirroredInterfacesCustomizer
44 extends FutureJVppCustomizer
45 implements InitializingReaderCustomizer<MirroredInterfaces, MirroredInterfacesBuilder>, JvppReplyConsumer {
47 private static final Logger LOG = LoggerFactory.getLogger(AbstractMirroredInterfacesCustomizer.class);
49 private final NamingContext ifcContext;
50 private final Function<InstanceIdentifier<MirroredInterfaces>, String> destinationInterfaceNameExtractor;
52 protected AbstractMirroredInterfacesCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
53 @Nonnull final NamingContext ifcContext,
54 @Nonnull final Function<InstanceIdentifier<MirroredInterfaces>, String> destinationInterfaceNameExtractor) {
55 super(futureJVppCore);
56 this.ifcContext = ifcContext;
57 this.destinationInterfaceNameExtractor = destinationInterfaceNameExtractor;
62 public MirroredInterfacesBuilder getBuilder(@Nonnull final InstanceIdentifier<MirroredInterfaces> id) {
63 return new MirroredInterfacesBuilder();
67 public void readCurrentAttributes(@Nonnull final InstanceIdentifier<MirroredInterfaces> id,
68 @Nonnull final MirroredInterfacesBuilder builder, @Nonnull final ReadContext ctx)
69 throws ReadFailedException {
70 LOG.trace("Reading mirrored interfaces under: {}", id);
71 final int dstId = destinationInterfaceIndex(id, 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<MirroredInterface> mirroredInterfaces =
83 replyForRead.swInterfaceSpanDetails.stream()
84 .filter(detail -> detail.swIfIndexTo == dstId)
85 .filter(detail -> detail.state != 0) // filters disabled(we use disabled as delete)
87 final String interfaceName =
88 ifcContext.getName(detail.swIfIndexFrom, ctx.getMappingContext());
89 return new MirroredInterfaceBuilder()
90 .setIfaceRef(interfaceName)
91 .withKey(new MirroredInterfaceKey(interfaceName))
92 .setState(SpanState.forValue(detail.state))
96 .collect(Collectors.toList());
98 LOG.debug("Mirrored interfaces for: {} read as: {}", id, mirroredInterfaces);
100 if (!mirroredInterfaces.isEmpty()) {
101 builder.setMirroredInterface(mirroredInterfaces);
105 private String getCacheKey() {
106 return getClass().getName();
109 private int destinationInterfaceIndex(@Nonnull final InstanceIdentifier<MirroredInterfaces> id,
110 @Nonnull final MappingContext mappingContext) {
111 final String destinationInterfaceName = destinationInterfaceNameExtractor.apply(id);
112 return ifcContext.getIndex(destinationInterfaceName, mappingContext);