9039e1995497b54c59f1c1bdfe3aa55231c2034a
[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.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.http.fd.io.hc2vpp.yang.v3po.rev190128.SpanState;
34 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190128.span.state.attributes.MirroredInterfaces;
35 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190128.span.state.attributes.MirroredInterfacesBuilder;
36 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190128.span.state.attributes.mirrored.interfaces.MirroredInterface;
37 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190128.span.state.attributes.mirrored.interfaces.MirroredInterfaceBuilder;
38 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.v3po.rev190128.span.state.attributes.mirrored.interfaces.MirroredInterfaceKey;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 abstract class AbstractMirroredInterfacesCustomizer
44         extends FutureJVppCustomizer
45         implements InitializingReaderCustomizer<MirroredInterfaces, MirroredInterfacesBuilder>, JvppReplyConsumer {
46
47     private static final Logger LOG = LoggerFactory.getLogger(AbstractMirroredInterfacesCustomizer.class);
48
49     private final NamingContext ifcContext;
50     private final Function<InstanceIdentifier<MirroredInterfaces>, String> destinationInterfaceNameExtractor;
51
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;
58     }
59
60     @Nonnull
61     @Override
62     public MirroredInterfacesBuilder getBuilder(@Nonnull final InstanceIdentifier<MirroredInterfaces> id) {
63         return new MirroredInterfacesBuilder();
64     }
65
66     @Override
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());
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<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)
86                         .map(detail -> {
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))
93                                             .build();
94                                 }
95                         )
96                         .collect(Collectors.toList());
97
98         LOG.debug("Mirrored interfaces for: {} read as: {}", id, mirroredInterfaces);
99
100         if (!mirroredInterfaces.isEmpty()) {
101             builder.setMirroredInterface(mirroredInterfaces);
102         }
103     }
104
105     private String getCacheKey() {
106         return getClass().getName();
107     }
108
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);
113     }
114 }