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