diff options
author | Marek Gradzki <mgradzki@cisco.com> | 2016-07-20 09:48:14 +0200 |
---|---|---|
committer | Maros Marsalek <mmarsale@cisco.com> | 2016-09-13 10:52:08 +0000 |
commit | 18f191d706d7b260d75a4a1f04245f6901fe654a (patch) | |
tree | 81980ea3453f09596857cf5cf6743a0ed036feb0 /v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier | |
parent | d4fbf9f664fedddea6698e8ccfa1ae44478f3642 (diff) |
HONEYCOMB-118: extend classifer model to support node names.
* configuration write is supported (updates local cache
with relative node mapping)
* operational read is supported only for nodes with existing
relative node mapping
* ACL support (using vpp-classfier or ietf-acl model)
is not affected by the patch (tables/sessions for access controll
refer to neighbour nodes as packet-handling-action, not vpp-node-name)
Change-Id: Ice0c846803cc7e8960c3571fd2a13ed46ba53702
Signed-off-by: Marek Gradzki <mgradzki@cisco.com>
Diffstat (limited to 'v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier')
8 files changed, 468 insertions, 72 deletions
diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/ClassifySessionReader.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/ClassifySessionReader.java index f8e94d1e1..558e6ba41 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/ClassifySessionReader.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/ClassifySessionReader.java @@ -23,11 +23,11 @@ import static io.fd.honeycomb.translate.v3po.interfacesstate.InterfaceUtils.prin import com.google.common.base.Optional; import com.google.common.primitives.UnsignedInts; +import io.fd.honeycomb.translate.MappingContext; import io.fd.honeycomb.translate.read.ReadContext; +import io.fd.honeycomb.translate.read.ReadFailedException; import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer; import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer; -import io.fd.honeycomb.translate.read.ReadFailedException; -import io.fd.honeycomb.translate.v3po.util.NamingContext; import io.fd.honeycomb.translate.v3po.util.TranslateUtils; import java.util.Arrays; import java.util.Collections; @@ -66,10 +66,10 @@ public class ClassifySessionReader extends FutureJVppCustomizer private static final Logger LOG = LoggerFactory.getLogger(ClassifySessionReader.class); static final String CACHE_KEY = ClassifySessionReader.class.getName(); - private final NamingContext classifyTableContext; + private final VppClassifierContextManager classifyTableContext; public ClassifySessionReader(@Nonnull final FutureJVppCore futureJVppCore, - @Nonnull final NamingContext classifyTableContext) { + @Nonnull final VppClassifierContextManager classifyTableContext) { super(futureJVppCore); this.classifyTableContext = checkNotNull(classifyTableContext, "classifyTableContext should not be null"); } @@ -102,10 +102,11 @@ public class ClassifySessionReader extends FutureJVppCustomizer if (classifySession.isPresent()) { final ClassifySessionDetails detail = classifySession.get(); - builder.setHitNext(readVppNode(detail.hitNextIndex, LOG)); + builder.setHitNext( + readVppNode(detail.tableId, detail.hitNextIndex, classifyTableContext, ctx.getMappingContext(), LOG).get()); if (detail.opaqueIndex != ~0) { // value is specified: - builder.setOpaqueIndex(readOpaqueIndex(detail.opaqueIndex)); + builder.setOpaqueIndex(readOpaqueIndex(detail.tableId, detail.opaqueIndex, ctx.getMappingContext())); } builder.setAdvance(detail.advance); builder.setMatch(key.getMatch()); @@ -116,13 +117,14 @@ public class ClassifySessionReader extends FutureJVppCustomizer } } - private OpaqueIndex readOpaqueIndex(final int opaqueIndex) { + private OpaqueIndex readOpaqueIndex(final int tableIndex, final int opaqueIndex, final MappingContext ctx) { // We first try to map the value to a vpp node, if that fails, simply wrap the u32 value // TODO: HONEYCOMB-118 the approach might fail if the opaqueIndex contains small value that collides // with some of the adjacent nodes - final VppNode node = readVppNode(opaqueIndex, LOG); - if (node != null) { - return new OpaqueIndex(node); + + final Optional<VppNode> node = readVppNode(tableIndex, opaqueIndex, classifyTableContext, ctx, LOG); + if (node.isPresent()) { + return new OpaqueIndex(node.get()); } else { return new OpaqueIndex(UnsignedInts.toLong(opaqueIndex)); } @@ -145,10 +147,10 @@ public class ClassifySessionReader extends FutureJVppCustomizer } final String tableName = tableKey.getName(); - checkState(classifyTableContext.containsIndex(tableName, ctx.getMappingContext()), + checkState(classifyTableContext.containsTable(tableName, ctx.getMappingContext()), "Reading classify sessions for table {}, but table index could not be found in the classify table context", tableName); - final int tableId = classifyTableContext.getIndex(tableName, ctx.getMappingContext()); + final int tableId = classifyTableContext.getTableIndex(tableName, ctx.getMappingContext()); LOG.debug("Dumping classify sessions for classify table id={}", tableId); try { @@ -157,8 +159,11 @@ public class ClassifySessionReader extends FutureJVppCustomizer classifySessionDump = TranslateUtils .getReplyForRead(getFutureJVpp().classifySessionDump(dumpRequest).toCompletableFuture(), id); - // update the cache: - ctx.getModificationCache().put(cacheKey, classifySessionDump); + if (classifySessionDump != null) { + // update the cache: + ctx.getModificationCache().put(cacheKey, classifySessionDump); + } + return classifySessionDump; } catch (VppBaseCallException e) { throw new ReadFailedException(id, e); diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/ClassifySessionWriter.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/ClassifySessionWriter.java index 8967dcf1e..d2029a368 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/ClassifySessionWriter.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/ClassifySessionWriter.java @@ -21,15 +21,15 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import static io.fd.honeycomb.translate.v3po.util.TranslateUtils.booleanToByte; -import io.fd.honeycomb.translate.v3po.util.WriteTimeoutException; -import io.fd.honeycomb.translate.write.WriteContext; +import com.google.common.base.Optional; +import io.fd.honeycomb.translate.MappingContext; import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer; -import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer; -import io.fd.honeycomb.translate.v3po.util.NamingContext; import io.fd.honeycomb.translate.v3po.util.TranslateUtils; +import io.fd.honeycomb.translate.write.WriteContext; import io.fd.honeycomb.translate.write.WriteFailedException; import java.util.concurrent.CompletionStage; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import javax.xml.bind.DatatypeConverter; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.OpaqueIndex; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.classify.table.base.attributes.ClassifySession; @@ -48,14 +48,14 @@ import org.slf4j.LoggerFactory; * Writer customizer responsible for classify session create/delete.<br> Sends {@code classify_add_del_session} message * to VPP.<br> Equivalent to invoking {@code vppctl classify table} command. */ -public class ClassifySessionWriter extends FutureJVppCustomizer +public class ClassifySessionWriter extends VppNodeWriter implements ListWriterCustomizer<ClassifySession, ClassifySessionKey> { private static final Logger LOG = LoggerFactory.getLogger(ClassifySessionWriter.class); - private final NamingContext classifyTableContext; + private final VppClassifierContextManager classifyTableContext; public ClassifySessionWriter(@Nonnull final FutureJVppCore futureJVppCore, - @Nonnull final NamingContext classifyTableContext) { + @Nonnull final VppClassifierContextManager classifyTableContext) { super(futureJVppCore); this.classifyTableContext = checkNotNull(classifyTableContext, "classifyTableContext should not be null"); } @@ -97,37 +97,51 @@ public class ClassifySessionWriter extends FutureJVppCustomizer private void classifyAddDelSession(final boolean isAdd, @Nonnull final InstanceIdentifier<ClassifySession> id, @Nonnull final ClassifySession classifySession, @Nonnull final WriteContext writeContext) - throws VppBaseCallException, WriteTimeoutException { + throws VppBaseCallException, WriteFailedException { final ClassifyTableKey tableKey = id.firstKeyOf(ClassifyTable.class); checkArgument(tableKey != null, "could not find classify table key in {}", id); final String tableName = tableKey.getName(); - checkState(classifyTableContext.containsIndex(tableName, writeContext.getMappingContext()), + checkState(classifyTableContext.containsTable(tableName, writeContext.getMappingContext()), "Could not find classify table index for {} in the classify table context", tableName); - final int tableIndex = classifyTableContext.getIndex(tableName, writeContext.getMappingContext()); + final int tableIndex = classifyTableContext.getTableIndex(tableName, writeContext.getMappingContext()); + + final ClassifyTable classifyTable = + getClassifyTable(writeContext, id.firstIdentifierOf(ClassifyTable.class), isAdd); + final int hitNextIndex = getNodeIndex(classifySession.getHitNext(), classifyTable, classifyTableContext, + writeContext.getMappingContext(), id); + final int opaqueIndex = + getOpaqueIndex(classifySession.getOpaqueIndex(), classifyTable, writeContext.getMappingContext(), id); final CompletionStage<ClassifyAddDelSessionReply> createClassifyTableReplyCompletionStage = getFutureJVpp() .classifyAddDelSession( - getClassifyAddDelSessionRequest(isAdd, tableIndex, classifySession)); + getClassifyAddDelSessionRequest(isAdd, classifySession, tableIndex, hitNextIndex, opaqueIndex)); TranslateUtils.getReplyForWrite(createClassifyTableReplyCompletionStage.toCompletableFuture(), id); } - private static ClassifyAddDelSession getClassifyAddDelSessionRequest(final boolean isAdd, final int tableIndex, - @Nonnull final ClassifySession classifySession) { + private ClassifyTable getClassifyTable(final WriteContext writeContext, + @Nonnull final InstanceIdentifier<ClassifyTable> id, + final boolean isAdd) { + final Optional<ClassifyTable> classifyTable; + if (isAdd) { + classifyTable = writeContext.readAfter(id); + } else { + classifyTable = writeContext.readBefore(id); + } + return classifyTable.get(); + } + + private static ClassifyAddDelSession getClassifyAddDelSessionRequest(final boolean isAdd, + @Nonnull final ClassifySession classifySession, + final int tableIndex, + final int hitNextIndex, + final int opaqueIndex) { ClassifyAddDelSession request = new ClassifyAddDelSession(); request.isAdd = booleanToByte(isAdd); request.tableIndex = tableIndex; - - // mandatory: - // TODO implement node name to index conversion after https://jira.fd.io/browse/VPP-203 is fixed - request.hitNextIndex = classifySession.getHitNext().getPacketHandlingAction().getIntValue(); - - if (classifySession.getOpaqueIndex() != null) { - request.opaqueIndex = getOpaqueIndexValue(classifySession.getOpaqueIndex()); - } else { - request.opaqueIndex = ~0; // value not specified - } + request.hitNextIndex = hitNextIndex; + request.opaqueIndex = opaqueIndex; // default 0: request.advance = classifySession.getAdvance(); @@ -136,12 +150,16 @@ public class ClassifySessionWriter extends FutureJVppCustomizer return request; } - private static int getOpaqueIndexValue(@Nonnull final OpaqueIndex opaqueIndex) { + private int getOpaqueIndex(@Nullable final OpaqueIndex opaqueIndex, final ClassifyTable classifyTable, + final MappingContext ctx, final InstanceIdentifier<ClassifySession> id) + throws VppBaseCallException, WriteFailedException { + if (opaqueIndex == null) { + return ~0; // value not specified + } if (opaqueIndex.getUint32() != null) { return opaqueIndex.getUint32().intValue(); } else { - // TODO: implement node name to index conversion after https://jira.fd.io/browse/VPP-203 is fixed - return opaqueIndex.getVppNode().getPacketHandlingAction().getIntValue(); + return getNodeIndex(opaqueIndex.getVppNode(), classifyTable, classifyTableContext, ctx, id); } } } diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/ClassifyTableReader.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/ClassifyTableReader.java index b7d54abb5..ad238d8bb 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/ClassifyTableReader.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/ClassifyTableReader.java @@ -20,12 +20,12 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; import static io.fd.honeycomb.translate.v3po.interfacesstate.InterfaceUtils.printHexBinary; +import com.google.common.base.Optional; import com.google.common.primitives.UnsignedInts; import io.fd.honeycomb.translate.read.ReadContext; -import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer; import io.fd.honeycomb.translate.read.ReadFailedException; +import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer; import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer; -import io.fd.honeycomb.translate.v3po.util.NamingContext; import io.fd.honeycomb.translate.v3po.util.TranslateUtils; import java.util.Arrays; import java.util.Collections; @@ -34,6 +34,7 @@ import java.util.stream.Collectors; import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.HexString; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppClassifierStateBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppNodeName; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.state.ClassifyTable; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.state.ClassifyTableBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.state.ClassifyTableKey; @@ -57,10 +58,10 @@ public class ClassifyTableReader extends FutureJVppCustomizer implements ListReaderCustomizer<ClassifyTable, ClassifyTableKey, ClassifyTableBuilder>, VppNodeReader { private static final Logger LOG = LoggerFactory.getLogger(ClassifyTableReader.class); - private final NamingContext classifyTableContext; + private final VppClassifierContextManager classifyTableContext; public ClassifyTableReader(@Nonnull final FutureJVppCore futureJVppCore, - @Nonnull final NamingContext classifyTableContext) { + @Nonnull final VppClassifierContextManager classifyTableContext) { super(futureJVppCore); this.classifyTableContext = checkNotNull(classifyTableContext, "classifyTableContext should not be null"); } @@ -89,11 +90,11 @@ public class ClassifyTableReader extends FutureJVppCustomizer final ClassifyTableInfo request = new ClassifyTableInfo(); final String tableName = key.getName(); - if (!classifyTableContext.containsIndex(tableName, ctx.getMappingContext())) { + if (!classifyTableContext.containsTable(tableName, ctx.getMappingContext())) { LOG.debug("Could not find classify table {} in the naming context", tableName); return; } - request.tableId = classifyTableContext.getIndex(tableName, ctx.getMappingContext()); + request.tableId = classifyTableContext.getTableIndex(tableName, ctx.getMappingContext()); try { final ClassifyTableInfoReply reply = @@ -105,14 +106,22 @@ public class ClassifyTableReader extends FutureJVppCustomizer builder.setNbuckets(UnsignedInts.toLong(reply.nbuckets)); builder.setSkipNVectors(UnsignedInts.toLong(reply.skipNVectors)); + // optional value read from context + final Optional<String> tableBaseNode = + classifyTableContext.getTableBaseNode(tableName, ctx.getMappingContext()); + if (tableBaseNode.isPresent()) { + builder.setClassifierNode(new VppNodeName(tableBaseNode.get())); + } - builder.setMissNext(readVppNode(reply.missNextIndex, LOG)); + builder.setMissNext( + readVppNode(reply.tableId, reply.missNextIndex, classifyTableContext, ctx.getMappingContext(), LOG) + .get()); builder.setMask(new HexString(printHexBinary(reply.mask))); builder.setActiveSessions(UnsignedInts.toLong(reply.activeSessions)); if (reply.nextTableIndex != ~0) { // next table index is present: - builder.setNextTable(classifyTableContext.getName(reply.nextTableIndex, ctx.getMappingContext())); + builder.setNextTable(classifyTableContext.getTableName(reply.nextTableIndex, ctx.getMappingContext())); } if (LOG.isTraceEnabled()) { @@ -133,7 +142,7 @@ public class ClassifyTableReader extends FutureJVppCustomizer .getReplyForRead(getFutureJVpp().classifyTableIds(new ClassifyTableIds()).toCompletableFuture(), id); if (classifyTableIdsReply.ids != null) { return Arrays.stream(classifyTableIdsReply.ids).mapToObj(i -> { - final String tableName = classifyTableContext.getName(i, context.getMappingContext()); + final String tableName = classifyTableContext.getTableName(i, context.getMappingContext()); LOG.trace("Classify table with name: {} and index: {} found in VPP", tableName, i); return new ClassifyTableKey(tableName); }).collect(Collectors.toList()); diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/ClassifyTableWriter.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/ClassifyTableWriter.java index 3c3f9813b..3dc3cf7d1 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/ClassifyTableWriter.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/ClassifyTableWriter.java @@ -21,14 +21,11 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkState; import static io.fd.honeycomb.translate.v3po.util.TranslateUtils.booleanToByte; +import io.fd.honeycomb.translate.MappingContext; import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer; -import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer; -import io.fd.honeycomb.translate.v3po.util.NamingContext; -import io.fd.honeycomb.translate.v3po.util.WriteTimeoutException; +import io.fd.honeycomb.translate.v3po.util.TranslateUtils; import io.fd.honeycomb.translate.write.WriteContext; import io.fd.honeycomb.translate.write.WriteFailedException; -import io.fd.honeycomb.translate.MappingContext; -import io.fd.honeycomb.translate.v3po.util.TranslateUtils; import java.util.concurrent.CompletionStage; import javax.annotation.Nonnull; import javax.xml.bind.DatatypeConverter; @@ -46,14 +43,14 @@ import org.slf4j.LoggerFactory; * Writer customizer responsible for classify table create/delete. <br> Sends {@code classify_add_del_table} message to * VPP.<br> Equivalent to invoking {@code vppctl classify table} command. */ -public class ClassifyTableWriter extends FutureJVppCustomizer +public class ClassifyTableWriter extends VppNodeWriter implements ListWriterCustomizer<ClassifyTable, ClassifyTableKey> { private static final Logger LOG = LoggerFactory.getLogger(ClassifyTableWriter.class); - private final NamingContext classifyTableContext; + private final VppClassifierContextManager classifyTableContext; public ClassifyTableWriter(@Nonnull final FutureJVppCore futureJVppCore, - @Nonnull final NamingContext classifyTableContext) { + @Nonnull final VppClassifierContextManager classifyTableContext) { super(futureJVppCore); this.classifyTableContext = checkNotNull(classifyTableContext, "classifyTableContext should not be null"); } @@ -68,7 +65,7 @@ public class ClassifyTableWriter extends FutureJVppCustomizer classifyAddDelTable(true, id, dataAfter, ~0 /* value not present */, writeContext.getMappingContext()); // Add classify table name <-> vpp index mapping to the naming context: - classifyTableContext.addName(newTableIndex, dataAfter.getName(), writeContext.getMappingContext()); + classifyTableContext.addTable(newTableIndex, dataAfter.getName(), dataAfter.getClassifierNode(), writeContext.getMappingContext()); LOG.debug("Successfully created classify table(id={]): iid={} dataAfter={}", newTableIndex, id, dataAfter); } catch (VppBaseCallException e) { throw new WriteFailedException.CreateFailedException(id, dataAfter, e); @@ -88,15 +85,15 @@ public class ClassifyTableWriter extends FutureJVppCustomizer @Nonnull final WriteContext writeContext) throws WriteFailedException { LOG.debug("Removing classify table: iid={} dataBefore={}", id, dataBefore); final String tableName = dataBefore.getName(); - checkState(classifyTableContext.containsIndex(tableName, writeContext.getMappingContext()), + checkState(classifyTableContext.containsTable(tableName, writeContext.getMappingContext()), "Removing classify table {}, but index could not be found in the classify table context", tableName); - final int tableIndex = classifyTableContext.getIndex(tableName, writeContext.getMappingContext()); + final int tableIndex = classifyTableContext.getTableIndex(tableName, writeContext.getMappingContext()); try { classifyAddDelTable(false, id, dataBefore, tableIndex, writeContext.getMappingContext()); // Remove deleted interface from interface context: - classifyTableContext.removeName(dataBefore.getName(), writeContext.getMappingContext()); + classifyTableContext.removeTable(dataBefore.getName(), writeContext.getMappingContext()); LOG.debug("Successfully removed classify table(id={]): iid={} dataAfter={}", tableIndex, id, dataBefore); } catch (VppBaseCallException e) { throw new WriteFailedException.DeleteFailedException(id, e); @@ -105,18 +102,23 @@ public class ClassifyTableWriter extends FutureJVppCustomizer private int classifyAddDelTable(final boolean isAdd, @Nonnull final InstanceIdentifier<ClassifyTable> id, @Nonnull final ClassifyTable table, final int tableId, final MappingContext ctx) - throws VppBaseCallException, WriteTimeoutException { + throws VppBaseCallException, WriteFailedException { + + final int missNextIndex = + getNodeIndex(table.getMissNext(), table, classifyTableContext, ctx, id); + final CompletionStage<ClassifyAddDelTableReply> createClassifyTableReplyCompletionStage = - getFutureJVpp().classifyAddDelTable(getClassifyAddDelTableRequest(isAdd, tableId, table, ctx)); + getFutureJVpp() + .classifyAddDelTable(getClassifyAddDelTableRequest(isAdd, tableId, table, missNextIndex, ctx)); final ClassifyAddDelTableReply reply = TranslateUtils.getReplyForWrite(createClassifyTableReplyCompletionStage.toCompletableFuture(), id); return reply.newTableIndex; - } private ClassifyAddDelTable getClassifyAddDelTableRequest(final boolean isAdd, final int tableIndex, @Nonnull final ClassifyTable table, + final int missNextIndex, @Nonnull final MappingContext ctx) { final ClassifyAddDelTable request = new ClassifyAddDelTable(); request.isAdd = booleanToByte(isAdd); @@ -128,12 +130,11 @@ public class ClassifyTableWriter extends FutureJVppCustomizer request.skipNVectors = table.getSkipNVectors().intValue(); // mandatory - // TODO implement node name to index conversion after https://jira.fd.io/browse/VPP-203 is fixed - request.missNextIndex = table.getMissNext().getPacketHandlingAction().getIntValue(); + request.missNextIndex = missNextIndex; final String nextTable = table.getNextTable(); if (isAdd && nextTable != null) { - request.nextTableIndex = classifyTableContext.getIndex(nextTable, ctx); + request.nextTableIndex = classifyTableContext.getTableIndex(nextTable, ctx); } else { request.nextTableIndex = ~0; // value not specified } diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/VppClassifierContextManager.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/VppClassifierContextManager.java new file mode 100644 index 000000000..7e9b6d6d5 --- /dev/null +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/VppClassifierContextManager.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2016 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.honeycomb.translate.v3po.vppclassifier; + +import com.google.common.base.Optional; +import io.fd.honeycomb.translate.MappingContext; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppNodeName; + +/** + * Manages metadata for vpp-classifier + */ +public interface VppClassifierContextManager { + + /** + * Creates metadata for classify table. + * + * @param id classify table index + * @param name classify table name + * @param classifierNode name of VPP node the table is defined for + * @param ctx mapping context providing context data for current transaction + */ + void addTable(final int id, @Nonnull final String name, @Nullable final VppNodeName classifierNode, + @Nonnull final MappingContext ctx); + + /** + * Check whether metadata for given classify table metadata is present. + * + * @param name classify table name + * @param ctx mapping context providing context data for current transaction + * @return true if present, false otherwise + */ + boolean containsTable(@Nonnull String name, @Nonnull final MappingContext ctx); + + /** + * Returns classify table index associated with the given name. + * + * @param name classify table name + * @param ctx mapping context providing context data for current transaction + * @return integer index value matching supplied classify table name + * @throws IllegalArgumentException if classify table was not found + */ + int getTableIndex(@Nonnull final String name, @Nonnull final MappingContext ctx); + + /** + * Retrieves classify table name for given id. If not present, artificial name will be generated. + * + * @param id classify table index + * @param ctx mapping context providing context data for current transaction + * @return classify table name matching supplied index + */ + String getTableName(final int id, @Nonnull final MappingContext ctx); + + /** + * Returns name of the base vpp node associated with the classify table. + * + * @param name classify table name + * @param ctx mapping context providing context data for current transaction + * @return name of VPP node the table is defined for + */ + Optional<String> getTableBaseNode(final String name, @Nonnull final MappingContext ctx); + + /** + * Removes classify table metadata from current context. + * + * @param name classify table name + * @param ctx mapping context providing context data for current transaction + */ + void removeTable(@Nonnull final String name, @Nonnull final MappingContext ctx); + + /** + * Adds relative node index to node name mapping for given classify table. + * + * @param tableName classify table name + * @param nodeIndex index of a vpp node, relative to table's base node + * @param nodeName name of a vpp node + * @param ctx mapping context providing context data for current transaction + */ + void addNodeName(@Nonnull String tableName, final int nodeIndex, @Nonnull final String nodeName, + @Nonnull final MappingContext ctx); + + /** + * Retrieves node name associated with the given classify table and node index. + * + * @param tableIndex classify table index + * @param nodeIndex relative index of a vpp node + * @param ctx mapping context providing context data for current transaction + * @return name of vpp node + */ + Optional<String> getNodeName(final int tableIndex, final int nodeIndex, @Nonnull final MappingContext ctx); +} diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/VppClassifierContextManagerImpl.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/VppClassifierContextManagerImpl.java new file mode 100644 index 000000000..9f61ab021 --- /dev/null +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/VppClassifierContextManagerImpl.java @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2016 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.honeycomb.translate.v3po.vppclassifier; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkState; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Optional; +import com.google.common.base.Preconditions; +import com.google.inject.Inject; +import io.fd.honeycomb.translate.MappingContext; +import io.fd.honeycomb.translate.read.ReaderFactory; +import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder; +import io.fd.honeycomb.translate.util.RWUtils; +import io.fd.honeycomb.translate.util.read.BindingBrokerReader; +import java.util.stream.Collector; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.inject.Named; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppNodeName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev160909.VppClassifierContext; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev160909.VppClassifierContextBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev160909.vpp.classifier.context.ClassifyTableContext; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev160909.vpp.classifier.context.ClassifyTableContextBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev160909.vpp.classifier.context.ClassifyTableContextKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev160909.vpp.classifier.context.classify.table.context.NodeContext; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev160909.vpp.classifier.context.classify.table.context.NodeContextBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev160909.vpp.classifier.context.classify.table.context.NodeContextKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier; + +/** + * Facade on top of {@link MappingContext} that manages {@link ClassifyTableContext}. + */ +public final class VppClassifierContextManagerImpl implements VppClassifierContextManager { + private static final Collector<ClassifyTableContext, ?, ClassifyTableContext> SINGLE_ITEM_COLLECTOR = + RWUtils.singleItemCollector(); + + @VisibleForTesting + static final InstanceIdentifier<VppClassifierContext> + VPP_CLASSIFIER_CONTEXT_IID = KeyedInstanceIdentifier.create(VppClassifierContext.class); + private final String artificialNamePrefix; + + /** + * Creates new VppClassifierContextManagerImpl. + * + * @param artificialNamePrefix artificial name to be used to generate names for classify tables without existing + * metadata + */ + public VppClassifierContextManagerImpl(@Nonnull final String artificialNamePrefix) { + this.artificialNamePrefix = + Preconditions.checkNotNull(artificialNamePrefix, "artificialNamePrefix should not be null"); + } + + private KeyedInstanceIdentifier<ClassifyTableContext, ClassifyTableContextKey> getMappingIid(final String name) { + return VPP_CLASSIFIER_CONTEXT_IID.child(ClassifyTableContext.class, new ClassifyTableContextKey(name)); + } + + @Override + public void addTable(final int id, @Nonnull final String name, @Nullable final VppNodeName classifierNode, + @Nonnull final MappingContext ctx) { + final KeyedInstanceIdentifier<ClassifyTableContext, ClassifyTableContextKey> mappingIid = getMappingIid(name); + final ClassifyTableContextBuilder tableCtx = new ClassifyTableContextBuilder().setIndex(id).setName(name); + if (classifierNode != null) { + tableCtx.setClassifierNodeName(classifierNode.getValue()); + } + ctx.put(mappingIid, tableCtx.build()); + } + + @Override + public boolean containsTable(@Nonnull final String name, @Nonnull final MappingContext ctx) { + final Optional<ClassifyTableContext> read = ctx.read(getMappingIid(name)); + return read.isPresent(); + } + + @Override + public int getTableIndex(@Nonnull final String name, @Nonnull final MappingContext ctx) { + final Optional<ClassifyTableContext> read = ctx.read(getMappingIid(name)); + checkArgument(read.isPresent(), "No mapping stored for name: %s", name); + return read.get().getIndex(); + } + + @Override + public String getTableName(final int id, @Nonnull final MappingContext ctx) { + if (!containsName(id, ctx)) { + final String artificialName = getArtificialName(id); + addTable(id, artificialName, null, ctx); + } + + final Optional<VppClassifierContext> read = ctx.read(VPP_CLASSIFIER_CONTEXT_IID); + checkState(read.isPresent(), "VppClassifierContext for index: %s is not present. But should be", id); + + return read.get().getClassifyTableContext().stream() + .filter(t -> t.getIndex().equals(id)) + .collect(SINGLE_ITEM_COLLECTOR).getName(); + } + + private boolean containsName(final int index, @Nonnull final MappingContext mappingContext) { + final Optional<VppClassifierContext> read = mappingContext.read(VPP_CLASSIFIER_CONTEXT_IID); + return read.isPresent() + ? read.get().getClassifyTableContext().stream().anyMatch(t -> t.getIndex().equals(index)) + : false; + } + + @Override + public Optional<String> getTableBaseNode(@Nonnull final String name, @Nonnull final MappingContext ctx) { + final Optional<ClassifyTableContext> read = ctx.read(getMappingIid(name)); + if (read.isPresent()) { + return Optional.fromNullable(read.get().getClassifierNodeName()); + } + return Optional.absent(); + } + + @Override + public void removeTable(@Nonnull final String name, @Nonnull final MappingContext ctx) { + ctx.delete(getMappingIid(name)); + } + + @Override + public void addNodeName(@Nonnull final String tableName, final int nodeIndex, + @Nonnull final String nodeName, + @Nonnull final MappingContext ctx) { + final KeyedInstanceIdentifier<NodeContext, NodeContextKey> iid = + getMappingIid(tableName).child(NodeContext.class, new NodeContextKey(nodeName)); + ctx.put(iid, new NodeContextBuilder().setName(nodeName).setIndex(nodeIndex).build()); + } + + @Override + public Optional<String> getNodeName(final int tableIndex, final int nodeIndex, @Nonnull final MappingContext ctx) { + if (!containsName(tableIndex, ctx)) { + return Optional.absent(); + } + final String tableName = getTableName(tableIndex, ctx); + final Optional<ClassifyTableContext> tableCtx = ctx.read(getMappingIid(tableName)); + return Optional.fromNullable(tableCtx.get().getNodeContext().stream() + .filter(n -> n.getIndex().equals(nodeIndex)) + .findFirst() + .map(nodes -> nodes.getName()) + .orElse(null)); + } + + private String getArtificialName(final int index) { + return artificialNamePrefix + index; + } + + public static final class ContextsReaderFactory implements ReaderFactory { + + @Inject + @Named("honeycomb-context") + private DataBroker contextBindingBrokerDependency; + + @Override + public void init(final ModifiableReaderRegistryBuilder registry) { + registry.add(new BindingBrokerReader<>(VPP_CLASSIFIER_CONTEXT_IID, + contextBindingBrokerDependency, + LogicalDatastoreType.OPERATIONAL, VppClassifierContextBuilder.class)); + } + } +} diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/VppNodeReader.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/VppNodeReader.java index 6061b8672..9fca4161e 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/VppNodeReader.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/VppNodeReader.java @@ -16,9 +16,12 @@ package io.fd.honeycomb.translate.v3po.vppclassifier; +import com.google.common.base.Optional; +import io.fd.honeycomb.translate.MappingContext; import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.PacketHandlingAction; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppNode; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppNodeName; import org.slf4j.Logger; interface VppNodeReader { @@ -29,13 +32,14 @@ interface VppNodeReader { * @param nodeIndex index of vpp node treated as signed integer. * @return vpp node representation */ - default VppNode readVppNode(final int nodeIndex, @Nonnull final Logger log) { + default Optional<VppNode> readVppNode(final int tableIndex, final int nodeIndex, + @Nonnull final VppClassifierContextManager vppClassifierContextManager, + @Nonnull final MappingContext ctx, @Nonnull final Logger log) { final PacketHandlingAction action = PacketHandlingAction.forValue(nodeIndex); if (action == null) { - // TODO: implement node index to name conversion after https://jira.fd.io/browse/VPP-203 is fixed - log.debug("VPP node index {} cannot be mapped to PacketHandlingAction", nodeIndex); - return null; + return vppClassifierContextManager.getNodeName(tableIndex, nodeIndex, ctx) + .transform(nodeName -> new VppNode(new VppNodeName(nodeName))); } - return new VppNode(action); + return Optional.of(new VppNode(action)); } } diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/VppNodeWriter.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/VppNodeWriter.java new file mode 100644 index 000000000..a4667bf4d --- /dev/null +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/translate/v3po/vppclassifier/VppNodeWriter.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2016 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.honeycomb.translate.v3po.vppclassifier; + +import static com.google.common.base.Preconditions.checkArgument; + +import io.fd.honeycomb.translate.MappingContext; +import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer; +import io.fd.honeycomb.translate.v3po.util.ReadTimeoutException; +import io.fd.honeycomb.translate.v3po.util.TranslateUtils; +import io.fd.honeycomb.translate.write.WriteFailedException; +import java.util.concurrent.CompletionStage; +import javax.annotation.Nonnull; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppNode; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTable; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.openvpp.jvpp.VppBaseCallException; +import org.openvpp.jvpp.core.dto.GetNextIndex; +import org.openvpp.jvpp.core.dto.GetNextIndexReply; +import org.openvpp.jvpp.core.future.FutureJVppCore; + +abstract class VppNodeWriter extends FutureJVppCustomizer { + + protected VppNodeWriter(@Nonnull final FutureJVppCore futureJvpp) { + super(futureJvpp); + } + + protected int getNodeIndex(@Nonnull final VppNode node, @Nonnull final ClassifyTable classifyTable, + @Nonnull final VppClassifierContextManager vppClassifierContextManager, + @Nonnull final MappingContext ctx, @Nonnull final InstanceIdentifier<?> id) + throws VppBaseCallException, WriteFailedException { + if (node.getPacketHandlingAction() != null) { + return node.getPacketHandlingAction().getIntValue(); + } else { + return nodeNameToIndex(classifyTable, node.getVppNodeName().getValue(), vppClassifierContextManager, ctx, id); + } + } + + private int nodeNameToIndex(@Nonnull final ClassifyTable classifyTable, @Nonnull final String nextNodeName, + @Nonnull final VppClassifierContextManager vppClassifierContextManager, + @Nonnull final MappingContext ctx, @Nonnull final InstanceIdentifier<?> id) + throws VppBaseCallException, WriteFailedException { + checkArgument(classifyTable != null && classifyTable.getClassifierNode() != null, "to use relative node names, table classifier node needs to be provided"); + final GetNextIndex request = new GetNextIndex(); + request.nodeName = classifyTable.getClassifierNode().getValue().getBytes(); + request.nextName = nextNodeName.getBytes(); + final CompletionStage<GetNextIndexReply> getNextIndexCompletionStage = + getFutureJVpp().getNextIndex(request); + + final GetNextIndexReply reply; + try { + reply = TranslateUtils.getReplyForRead(getNextIndexCompletionStage.toCompletableFuture(), id); + + // vpp does not provide relative node index to node name conversion (https://jira.fd.io/browse/VPP-219) + // as a workaround we need to add mapping to vpp-classfier-context + vppClassifierContextManager.addNodeName(classifyTable.getName(), reply.nextIndex, nextNodeName, ctx); + } catch (ReadTimeoutException e) { + throw new WriteFailedException(id, String.format("Failed to get node index for %s relative to %s", + nextNodeName, classifyTable.getClassifierNode()), e); + } + return reply.nextIndex; + } +} |