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;
21 import io.fd.honeycomb.translate.MappingContext;
22 import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
23 import io.fd.honeycomb.translate.v3po.util.JvppReplyConsumer;
24 import io.fd.honeycomb.translate.v3po.util.ReadTimeoutException;
25 import io.fd.honeycomb.translate.write.WriteFailedException;
26 import java.util.concurrent.CompletionStage;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.VppNode;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev150603.vpp.classifier.ClassifyTable;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.openvpp.jvpp.VppBaseCallException;
32 import org.openvpp.jvpp.core.dto.GetNextIndex;
33 import org.openvpp.jvpp.core.dto.GetNextIndexReply;
34 import org.openvpp.jvpp.core.future.FutureJVppCore;
36 abstract class VppNodeWriter extends FutureJVppCustomizer implements JvppReplyConsumer {
38 protected VppNodeWriter(@Nonnull final FutureJVppCore futureJvpp) {
42 protected int getNodeIndex(@Nonnull final VppNode node, @Nonnull final ClassifyTable classifyTable,
43 @Nonnull final VppClassifierContextManager vppClassifierContextManager,
44 @Nonnull final MappingContext ctx, @Nonnull final InstanceIdentifier<?> id)
45 throws VppBaseCallException, WriteFailedException {
46 if (node.getPacketHandlingAction() != null) {
47 return node.getPacketHandlingAction().getIntValue();
49 return nodeNameToIndex(classifyTable, node.getVppNodeName().getValue(), vppClassifierContextManager, ctx,
54 private int nodeNameToIndex(@Nonnull final ClassifyTable classifyTable, @Nonnull final String nextNodeName,
55 @Nonnull final VppClassifierContextManager vppClassifierContextManager,
56 @Nonnull final MappingContext ctx, @Nonnull final InstanceIdentifier<?> id)
57 throws VppBaseCallException, WriteFailedException {
58 checkArgument(classifyTable != null && classifyTable.getClassifierNode() != null,
59 "to use relative node names, table classifier node needs to be provided");
60 final GetNextIndex request = new GetNextIndex();
61 request.nodeName = classifyTable.getClassifierNode().getValue().getBytes();
62 request.nextName = nextNodeName.getBytes();
63 final CompletionStage<GetNextIndexReply> getNextIndexCompletionStage =
64 getFutureJVpp().getNextIndex(request);
66 final GetNextIndexReply reply;
68 reply = getReplyForRead(getNextIndexCompletionStage.toCompletableFuture(), id);
70 // vpp does not provide relative node index to node name conversion (https://jira.fd.io/browse/VPP-219)
71 // as a workaround we need to add mapping to vpp-classfier-context
72 vppClassifierContextManager.addNodeName(classifyTable.getName(), reply.nextIndex, nextNodeName, ctx);
73 } catch (ReadTimeoutException e) {
74 throw new WriteFailedException(id, String.format("Failed to get node index for %s relative to %s",
75 nextNodeName, classifyTable.getClassifierNode()), e);
77 return reply.nextIndex;