b7d54abb54950f01ccd68cfd0f33490573b33ae8
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / vppclassifier / ClassifyTableReader.java
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.honeycomb.translate.v3po.vppclassifier;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
21 import static io.fd.honeycomb.translate.v3po.interfacesstate.InterfaceUtils.printHexBinary;
22
23 import com.google.common.primitives.UnsignedInts;
24 import io.fd.honeycomb.translate.read.ReadContext;
25 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
26 import io.fd.honeycomb.translate.read.ReadFailedException;
27 import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
28 import io.fd.honeycomb.translate.v3po.util.NamingContext;
29 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.stream.Collectors;
34 import javax.annotation.Nonnull;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppClassifierStateBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.state.ClassifyTable;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.state.ClassifyTableBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.state.ClassifyTableKey;
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.openvpp.jvpp.VppBaseCallException;
44 import org.openvpp.jvpp.core.dto.ClassifyTableIds;
45 import org.openvpp.jvpp.core.dto.ClassifyTableIdsReply;
46 import org.openvpp.jvpp.core.dto.ClassifyTableInfo;
47 import org.openvpp.jvpp.core.dto.ClassifyTableInfoReply;
48 import org.openvpp.jvpp.core.future.FutureJVppCore;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 /**
53  * Reader customizer responsible for classify table read.<br> to VPP.<br> Equivalent to invoking {@code vppctl show
54  * class table} command.
55  */
56 public class ClassifyTableReader extends FutureJVppCustomizer
57     implements ListReaderCustomizer<ClassifyTable, ClassifyTableKey, ClassifyTableBuilder>, VppNodeReader {
58
59     private static final Logger LOG = LoggerFactory.getLogger(ClassifyTableReader.class);
60     private final NamingContext classifyTableContext;
61
62     public ClassifyTableReader(@Nonnull final FutureJVppCore futureJVppCore,
63                                @Nonnull final NamingContext classifyTableContext) {
64         super(futureJVppCore);
65         this.classifyTableContext = checkNotNull(classifyTableContext, "classifyTableContext should not be null");
66     }
67
68
69     @Override
70     public void merge(@Nonnull final Builder<? extends DataObject> builder,
71                       @Nonnull final List<ClassifyTable> readData) {
72         ((VppClassifierStateBuilder) builder).setClassifyTable(readData);
73     }
74
75     @Nonnull
76     @Override
77     public ClassifyTableBuilder getBuilder(@Nonnull final InstanceIdentifier<ClassifyTable> id) {
78         return new ClassifyTableBuilder();
79     }
80
81     @Override
82     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<ClassifyTable> id,
83                                       @Nonnull final ClassifyTableBuilder builder, @Nonnull final ReadContext ctx)
84         throws ReadFailedException {
85         LOG.debug("Reading attributes for classify table: {}", id);
86
87         final ClassifyTableKey key = id.firstKeyOf(ClassifyTable.class);
88         checkArgument(key != null, "could not find ClassifyTable key in {}", id);
89         final ClassifyTableInfo request = new ClassifyTableInfo();
90
91         final String tableName = key.getName();
92         if (!classifyTableContext.containsIndex(tableName, ctx.getMappingContext())) {
93             LOG.debug("Could not find classify table {} in the naming context", tableName);
94             return;
95         }
96         request.tableId = classifyTableContext.getIndex(tableName, ctx.getMappingContext());
97
98         try {
99             final ClassifyTableInfoReply reply =
100                 TranslateUtils.getReplyForRead(getFutureJVpp().classifyTableInfo(request).toCompletableFuture(), id);
101
102             // mandatory values:
103             builder.setName(tableName);
104             builder.setKey(key);
105             builder.setNbuckets(UnsignedInts.toLong(reply.nbuckets));
106             builder.setSkipNVectors(UnsignedInts.toLong(reply.skipNVectors));
107
108
109             builder.setMissNext(readVppNode(reply.missNextIndex, LOG));
110             builder.setMask(new HexString(printHexBinary(reply.mask)));
111             builder.setActiveSessions(UnsignedInts.toLong(reply.activeSessions));
112
113             if (reply.nextTableIndex != ~0) {
114                 // next table index is present:
115                 builder.setNextTable(classifyTableContext.getName(reply.nextTableIndex, ctx.getMappingContext()));
116             }
117
118             if (LOG.isTraceEnabled()) {
119                 LOG.trace("Attributes for classify table {} successfully read: {}", id, builder.build());
120             }
121         } catch (VppBaseCallException e) {
122             throw new ReadFailedException(id, e);
123         }
124     }
125
126     @Nonnull
127     @Override
128     public List<ClassifyTableKey> getAllIds(@Nonnull final InstanceIdentifier<ClassifyTable> id,
129                                             @Nonnull final ReadContext context) throws ReadFailedException {
130         LOG.debug("Reading list of keys for classify tables: {}", id);
131         try {
132             final ClassifyTableIdsReply classifyTableIdsReply = TranslateUtils
133                 .getReplyForRead(getFutureJVpp().classifyTableIds(new ClassifyTableIds()).toCompletableFuture(), id);
134             if (classifyTableIdsReply.ids != null) {
135                 return Arrays.stream(classifyTableIdsReply.ids).mapToObj(i -> {
136                     final String tableName = classifyTableContext.getName(i, context.getMappingContext());
137                     LOG.trace("Classify table with name: {} and index: {} found in VPP", tableName, i);
138                     return new ClassifyTableKey(tableName);
139                 }).collect(Collectors.toList());
140             } else {
141                 return Collections.emptyList();
142             }
143         } catch (VppBaseCallException e) {
144             throw new ReadFailedException(id, e);
145         }
146     }
147 }