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.honeycomb.translate.v3po.vppclassifier;
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
21 import static com.google.common.base.Preconditions.checkState;
22 import static io.fd.honeycomb.translate.v3po.util.TranslateUtils.booleanToByte;
24 import io.fd.honeycomb.translate.MappingContext;
25 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
26 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
27 import io.fd.honeycomb.translate.write.WriteContext;
28 import io.fd.honeycomb.translate.write.WriteFailedException;
29 import java.util.concurrent.CompletionStage;
30 import javax.annotation.Nonnull;
31 import javax.xml.bind.DatatypeConverter;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTable;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTableKey;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.openvpp.jvpp.VppBaseCallException;
36 import org.openvpp.jvpp.core.dto.ClassifyAddDelTable;
37 import org.openvpp.jvpp.core.dto.ClassifyAddDelTableReply;
38 import org.openvpp.jvpp.core.future.FutureJVppCore;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Writer customizer responsible for classify table create/delete. <br> Sends {@code classify_add_del_table} message to
44 * VPP.<br> Equivalent to invoking {@code vppctl classify table} command.
46 public class ClassifyTableWriter extends VppNodeWriter
47 implements ListWriterCustomizer<ClassifyTable, ClassifyTableKey> {
49 private static final Logger LOG = LoggerFactory.getLogger(ClassifyTableWriter.class);
50 private final VppClassifierContextManager classifyTableContext;
52 public ClassifyTableWriter(@Nonnull final FutureJVppCore futureJVppCore,
53 @Nonnull final VppClassifierContextManager classifyTableContext) {
54 super(futureJVppCore);
55 this.classifyTableContext = checkNotNull(classifyTableContext, "classifyTableContext should not be null");
59 public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<ClassifyTable> id,
60 @Nonnull final ClassifyTable dataAfter, @Nonnull final WriteContext writeContext)
61 throws WriteFailedException {
62 LOG.debug("Creating classify table: iid={} dataAfter={}", id, dataAfter);
64 final int newTableIndex =
65 classifyAddDelTable(true, id, dataAfter, ~0 /* value not present */, writeContext.getMappingContext());
67 // Add classify table name <-> vpp index mapping to the naming context:
68 classifyTableContext.addTable(newTableIndex, dataAfter.getName(), dataAfter.getClassifierNode(), writeContext.getMappingContext());
69 LOG.debug("Successfully created classify table(id={]): iid={} dataAfter={}", newTableIndex, id, dataAfter);
70 } catch (VppBaseCallException e) {
71 throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
76 public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<ClassifyTable> id,
77 @Nonnull final ClassifyTable dataBefore, @Nonnull final ClassifyTable dataAfter,
78 @Nonnull final WriteContext writeContext) throws WriteFailedException {
79 throw new UnsupportedOperationException("Classify table update is not supported");
83 public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<ClassifyTable> id,
84 @Nonnull final ClassifyTable dataBefore,
85 @Nonnull final WriteContext writeContext) throws WriteFailedException {
86 LOG.debug("Removing classify table: iid={} dataBefore={}", id, dataBefore);
87 final String tableName = dataBefore.getName();
88 checkState(classifyTableContext.containsTable(tableName, writeContext.getMappingContext()),
89 "Removing classify table {}, but index could not be found in the classify table context", tableName);
91 final int tableIndex = classifyTableContext.getTableIndex(tableName, writeContext.getMappingContext());
93 classifyAddDelTable(false, id, dataBefore, tableIndex, writeContext.getMappingContext());
95 // Remove deleted interface from interface context:
96 classifyTableContext.removeTable(dataBefore.getName(), writeContext.getMappingContext());
97 LOG.debug("Successfully removed classify table(id={]): iid={} dataAfter={}", tableIndex, id, dataBefore);
98 } catch (VppBaseCallException e) {
99 throw new WriteFailedException.DeleteFailedException(id, e);
103 private int classifyAddDelTable(final boolean isAdd, @Nonnull final InstanceIdentifier<ClassifyTable> id,
104 @Nonnull final ClassifyTable table, final int tableId, final MappingContext ctx)
105 throws VppBaseCallException, WriteFailedException {
107 final int missNextIndex =
108 getNodeIndex(table.getMissNext(), table, classifyTableContext, ctx, id);
110 final CompletionStage<ClassifyAddDelTableReply> createClassifyTableReplyCompletionStage =
112 .classifyAddDelTable(getClassifyAddDelTableRequest(isAdd, tableId, table, missNextIndex, ctx));
114 final ClassifyAddDelTableReply reply =
115 TranslateUtils.getReplyForWrite(createClassifyTableReplyCompletionStage.toCompletableFuture(), id);
116 return reply.newTableIndex;
119 private ClassifyAddDelTable getClassifyAddDelTableRequest(final boolean isAdd, final int tableIndex,
120 @Nonnull final ClassifyTable table,
121 final int missNextIndex,
122 @Nonnull final MappingContext ctx) {
123 final ClassifyAddDelTable request = new ClassifyAddDelTable();
124 request.isAdd = booleanToByte(isAdd);
125 request.tableIndex = tableIndex;
127 // mandatory, all u32 values are permitted:
128 request.nbuckets = table.getNbuckets().intValue();
129 request.memorySize = table.getMemorySize().intValue();
130 request.skipNVectors = table.getSkipNVectors().intValue();
133 request.missNextIndex = missNextIndex;
135 final String nextTable = table.getNextTable();
136 if (isAdd && nextTable != null) {
137 request.nextTableIndex = classifyTableContext.getTableIndex(nextTable, ctx);
139 request.nextTableIndex = ~0; // value not specified
141 request.mask = DatatypeConverter.parseHexBinary(table.getMask().getValue().replace(":", ""));
142 checkArgument(request.mask.length % 16 == 0, "Number of mask bytes must be multiple of 16.");
143 request.matchNVectors = request.mask.length / 16;