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.spi.write.ListWriterCustomizer;
25 import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
26 import io.fd.honeycomb.translate.v3po.util.NamingContext;
27 import io.fd.honeycomb.translate.v3po.util.WriteTimeoutException;
28 import io.fd.honeycomb.translate.write.WriteContext;
29 import io.fd.honeycomb.translate.write.WriteFailedException;
30 import io.fd.honeycomb.translate.MappingContext;
31 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
32 import java.util.concurrent.CompletionStage;
33 import javax.annotation.Nonnull;
34 import javax.xml.bind.DatatypeConverter;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTable;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTableKey;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.openvpp.jvpp.VppBaseCallException;
39 import org.openvpp.jvpp.core.dto.ClassifyAddDelTable;
40 import org.openvpp.jvpp.core.dto.ClassifyAddDelTableReply;
41 import org.openvpp.jvpp.core.future.FutureJVppCore;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * Writer customizer responsible for classify table create/delete. <br> Sends {@code classify_add_del_table} message to
47 * VPP.<br> Equivalent to invoking {@code vppctl classify table} command.
49 public class ClassifyTableWriter extends FutureJVppCustomizer
50 implements ListWriterCustomizer<ClassifyTable, ClassifyTableKey> {
52 private static final Logger LOG = LoggerFactory.getLogger(ClassifyTableWriter.class);
53 private final NamingContext classifyTableContext;
55 public ClassifyTableWriter(@Nonnull final FutureJVppCore futureJVppCore,
56 @Nonnull final NamingContext classifyTableContext) {
57 super(futureJVppCore);
58 this.classifyTableContext = checkNotNull(classifyTableContext, "classifyTableContext should not be null");
62 public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<ClassifyTable> id,
63 @Nonnull final ClassifyTable dataAfter, @Nonnull final WriteContext writeContext)
64 throws WriteFailedException {
65 LOG.debug("Creating classify table: iid={} dataAfter={}", id, dataAfter);
67 final int newTableIndex =
68 classifyAddDelTable(true, id, dataAfter, ~0 /* value not present */, writeContext.getMappingContext());
70 // Add classify table name <-> vpp index mapping to the naming context:
71 classifyTableContext.addName(newTableIndex, dataAfter.getName(), writeContext.getMappingContext());
72 LOG.debug("Successfully created classify table(id={]): iid={} dataAfter={}", newTableIndex, id, dataAfter);
73 } catch (VppBaseCallException e) {
74 throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
79 public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<ClassifyTable> id,
80 @Nonnull final ClassifyTable dataBefore, @Nonnull final ClassifyTable dataAfter,
81 @Nonnull final WriteContext writeContext) throws WriteFailedException {
82 throw new UnsupportedOperationException("Classify table update is not supported");
86 public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<ClassifyTable> id,
87 @Nonnull final ClassifyTable dataBefore,
88 @Nonnull final WriteContext writeContext) throws WriteFailedException {
89 LOG.debug("Removing classify table: iid={} dataBefore={}", id, dataBefore);
90 final String tableName = dataBefore.getName();
91 checkState(classifyTableContext.containsIndex(tableName, writeContext.getMappingContext()),
92 "Removing classify table {}, but index could not be found in the classify table context", tableName);
94 final int tableIndex = classifyTableContext.getIndex(tableName, writeContext.getMappingContext());
96 classifyAddDelTable(false, id, dataBefore, tableIndex, writeContext.getMappingContext());
98 // Remove deleted interface from interface context:
99 classifyTableContext.removeName(dataBefore.getName(), writeContext.getMappingContext());
100 LOG.debug("Successfully removed classify table(id={]): iid={} dataAfter={}", tableIndex, id, dataBefore);
101 } catch (VppBaseCallException e) {
102 throw new WriteFailedException.DeleteFailedException(id, e);
106 private int classifyAddDelTable(final boolean isAdd, @Nonnull final InstanceIdentifier<ClassifyTable> id,
107 @Nonnull final ClassifyTable table, final int tableId, final MappingContext ctx)
108 throws VppBaseCallException, WriteTimeoutException {
109 final CompletionStage<ClassifyAddDelTableReply> createClassifyTableReplyCompletionStage =
110 getFutureJVpp().classifyAddDelTable(getClassifyAddDelTableRequest(isAdd, tableId, table, ctx));
112 final ClassifyAddDelTableReply reply =
113 TranslateUtils.getReplyForWrite(createClassifyTableReplyCompletionStage.toCompletableFuture(), id);
114 return reply.newTableIndex;
118 private ClassifyAddDelTable getClassifyAddDelTableRequest(final boolean isAdd, final int tableIndex,
119 @Nonnull final ClassifyTable table,
120 @Nonnull final MappingContext ctx) {
121 final ClassifyAddDelTable request = new ClassifyAddDelTable();
122 request.isAdd = booleanToByte(isAdd);
123 request.tableIndex = tableIndex;
125 // mandatory, all u32 values are permitted:
126 request.nbuckets = table.getNbuckets().intValue();
127 request.memorySize = table.getMemorySize().intValue();
128 request.skipNVectors = table.getSkipNVectors().intValue();
131 // TODO implement node name to index conversion after https://jira.fd.io/browse/VPP-203 is fixed
132 request.missNextIndex = table.getMissNext().getPacketHandlingAction().getIntValue();
134 final String nextTable = table.getNextTable();
135 if (isAdd && nextTable != null) {
136 request.nextTableIndex = classifyTableContext.getIndex(nextTable, ctx);
138 request.nextTableIndex = ~0; // value not specified
140 request.mask = DatatypeConverter.parseHexBinary(table.getMask().getValue().replace(":", ""));
141 checkArgument(request.mask.length % 16 == 0, "Number of mask bytes must be multiple of 16.");
142 request.matchNVectors = request.mask.length / 16;