HONEYCOMB-58 - Routing Api
[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.read.ReadFailedException;
23 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
24 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
25 import io.fd.honeycomb.translate.write.WriteFailedException;
26 import io.fd.vpp.jvpp.VppBaseCallException;
27 import io.fd.vpp.jvpp.core.dto.GetNextIndex;
28 import io.fd.vpp.jvpp.core.dto.GetNextIndexReply;
29 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
30 import java.util.concurrent.CompletionStage;
31 import javax.annotation.Nonnull;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.VppNode;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.rev161214.vpp.classifier.ClassifyTable;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35
36 abstract class VppNodeWriter extends FutureJVppCustomizer implements JvppReplyConsumer {
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,
50                     id);
51         }
52     }
53
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 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);
65
66         final GetNextIndexReply reply;
67         try {
68             reply = getReplyForRead(getNextIndexCompletionStage.toCompletableFuture(), id);
69
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 (ReadFailedException e) {
74             throw new WriteFailedException(id, String.format("Failed to get node index for %s relative to %s",
75                     nextNodeName, classifyTable.getClassifierNode()), e);
76         }
77         return reply.nextIndex;
78     }
79 }