HONEYCOMB-118: extend classifer model to support node names.
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / vppclassifier / VppNodeWriter.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
21 import io.fd.honeycomb.translate.MappingContext;
22 import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
23 import io.fd.honeycomb.translate.v3po.util.ReadTimeoutException;
24 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
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;
35
36 abstract class VppNodeWriter extends FutureJVppCustomizer {
37
38     protected VppNodeWriter(@Nonnull final FutureJVppCore futureJvpp) {
39         super(futureJvpp);
40     }
41
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();
48         } else {
49             return nodeNameToIndex(classifyTable, node.getVppNodeName().getValue(), vppClassifierContextManager, ctx, id);
50         }
51     }
52
53     private int nodeNameToIndex(@Nonnull final ClassifyTable classifyTable, @Nonnull final String nextNodeName,
54                                 @Nonnull final VppClassifierContextManager vppClassifierContextManager,
55                                 @Nonnull final MappingContext ctx, @Nonnull final InstanceIdentifier<?> id)
56         throws VppBaseCallException, WriteFailedException {
57         checkArgument(classifyTable != null && classifyTable.getClassifierNode() != null, "to use relative node names, table classifier node needs to be provided");
58         final GetNextIndex request = new GetNextIndex();
59         request.nodeName = classifyTable.getClassifierNode().getValue().getBytes();
60         request.nextName = nextNodeName.getBytes();
61         final CompletionStage<GetNextIndexReply> getNextIndexCompletionStage =
62             getFutureJVpp().getNextIndex(request);
63
64         final GetNextIndexReply reply;
65         try {
66             reply = TranslateUtils.getReplyForRead(getNextIndexCompletionStage.toCompletableFuture(), id);
67
68             // vpp does not provide relative node index to node name conversion (https://jira.fd.io/browse/VPP-219)
69             // as a workaround we need to add mapping to vpp-classfier-context
70             vppClassifierContextManager.addNodeName(classifyTable.getName(), reply.nextIndex, nextNodeName, ctx);
71         } catch (ReadTimeoutException e) {
72             throw new WriteFailedException(id, String.format("Failed to get node index for %s relative to %s",
73                 nextNodeName, classifyTable.getClassifierNode()), e);
74         }
75         return reply.nextIndex;
76     }
77 }