summaryrefslogtreecommitdiffstats
path: root/vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/read/InterfacePolicerCustomizer.java
blob: 5620af9e773ee9968adad586dfa1a58681aec70c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.hc2vpp.policer.read;

import static com.google.common.base.Preconditions.checkNotNull;

import io.fd.hc2vpp.common.translate.util.ByteDataTranslator;
import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer;
import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer;
import io.fd.hc2vpp.common.translate.util.NamingContext;
import io.fd.hc2vpp.vpp.classifier.context.VppClassifierContextManager;
import io.fd.honeycomb.translate.ModificationCache;
import io.fd.honeycomb.translate.read.ReadContext;
import io.fd.honeycomb.translate.read.ReadFailedException;
import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
import io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor;
import io.fd.jvpp.core.dto.PolicerClassifyDetailsReplyDump;
import io.fd.jvpp.core.dto.PolicerClassifyDump;
import io.fd.jvpp.core.future.FutureJVppCore;
import java.util.Optional;
import javax.annotation.Nonnull;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang._interface.policer.rev170315.PolicerInterfaceAugmentationBuilder;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang._interface.policer.rev170315._interface.policer.attributes.Policer;
import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang._interface.policer.rev170315._interface.policer.attributes.PolicerBuilder;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface;
import org.opendaylight.yangtools.concepts.Builder;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;

final class InterfacePolicerCustomizer extends FutureJVppCustomizer
    implements ReaderCustomizer<Policer, PolicerBuilder>,
    JvppReplyConsumer, ByteDataTranslator {

    private static final byte TABLE_IP4 = 0;
    private static final byte TABLE_IP6 = 1;
    private static final byte TABLE_L2 = 2;

    private final DumpCacheManager<PolicerClassifyDetailsReplyDump, Byte> dumpManager;
    private final NamingContext interfaceContext;
    private final VppClassifierContextManager classifyTableContext;

    InterfacePolicerCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
                               @Nonnull final NamingContext interfaceContext,
                               @Nonnull final VppClassifierContextManager classifyTableContext) {
        super(futureJVppCore);
        this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
        this.classifyTableContext = checkNotNull(classifyTableContext, "classifyTableContext should not be null");
        dumpManager = new DumpCacheManager.DumpCacheManagerBuilder<PolicerClassifyDetailsReplyDump, Byte>()
            .withExecutor(executor())
            .acceptOnly(PolicerClassifyDetailsReplyDump.class)
            .build();
    }

    private EntityDumpExecutor<PolicerClassifyDetailsReplyDump, Byte> executor() {
        return (id, type) -> {
            PolicerClassifyDump request = new PolicerClassifyDump();
            request.type = type;
            return getReplyForRead(getFutureJVpp().policerClassifyDump(request).toCompletableFuture(), id);
        };
    }

    @Nonnull
    @Override
    public PolicerBuilder getBuilder(@Nonnull final InstanceIdentifier<Policer> instanceIdentifier) {
        return new PolicerBuilder();
    }

    @Override
    public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Policer> id,
                                      @Nonnull final PolicerBuilder builder,
                                      @Nonnull final ReadContext ctx)
        throws ReadFailedException {
        final String ifcName = id.firstKeyOf(Interface.class).getName();
        final int ifcIndex = interfaceContext.getIndex(ifcName, ctx.getMappingContext());
        final Optional<Integer> ip4 = readTableIndex(id, ifcIndex, TABLE_IP4, ctx.getModificationCache());
        if (ip4.isPresent()) {
            builder.setIp4Table(classifyTableContext.getTableName(ip4.get(), ctx.getMappingContext()));
        }
        final Optional<Integer> ip6 = readTableIndex(id, ifcIndex, TABLE_IP6, ctx.getModificationCache());
        if (ip6.isPresent()) {
            builder.setIp6Table(classifyTableContext.getTableName(ip6.get(), ctx.getMappingContext()));
        }
        final Optional<Integer> l2 = readTableIndex(id, ifcIndex, TABLE_L2, ctx.getModificationCache());
        if (l2.isPresent()) {
            builder.setL2Table(classifyTableContext.getTableName(l2.get(), ctx.getMappingContext()));
        }
    }

    private Optional<Integer> readTableIndex(@Nonnull final InstanceIdentifier<Policer> id, final int ifcIndex,
                                             final byte type,
                                             final ModificationCache cache) throws ReadFailedException {
        final java.util.Optional<PolicerClassifyDetailsReplyDump> dump =
            dumpManager.getDump(id, cache, type);
        if (!dump.isPresent() || dump.get().policerClassifyDetails.isEmpty()) {
            return Optional.empty();
        }
        return dump.get().policerClassifyDetails.stream().filter(detail -> detail.swIfIndex == ifcIndex).findFirst()
            .map(details -> details.tableIndex);
    }

    @Override
    public void merge(@Nonnull final Builder<? extends DataObject> builder, @Nonnull final Policer policer) {
        ((PolicerInterfaceAugmentationBuilder) builder).setPolicer(policer);
    }
}