d29d213eaf0d7c6cf45be6760945045471f7218b
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / AclWriter.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.interfaces;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20 import static io.fd.honeycomb.translate.v3po.util.TranslateUtils.booleanToByte;
21
22 import io.fd.honeycomb.translate.v3po.util.WriteTimeoutException;
23 import io.fd.honeycomb.translate.MappingContext;
24 import io.fd.honeycomb.translate.v3po.util.NamingContext;
25 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
26 import java.util.concurrent.CompletionStage;
27 import javax.annotation.Nonnegative;
28 import javax.annotation.Nonnull;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.AclBaseAttributes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.acl.base.attributes.Ip4Acl;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.acl.base.attributes.Ip6Acl;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.acl.base.attributes.L2Acl;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.openvpp.jvpp.VppBaseCallException;
35 import org.openvpp.jvpp.core.dto.InputAclSetInterface;
36 import org.openvpp.jvpp.core.dto.InputAclSetInterfaceReply;
37 import org.openvpp.jvpp.core.future.FutureJVppCore;
38
39 interface AclWriter {
40
41     default void inputAclSetInterface(@Nonnull final FutureJVppCore futureJVppCore, final boolean isAdd,
42                                       @Nonnull final InstanceIdentifier<?> id, @Nonnull final AclBaseAttributes acl,
43                                       @Nonnegative final int ifIndex, @Nonnull final NamingContext classifyTableContext,
44                                       @Nonnull final MappingContext mappingContext)
45         throws VppBaseCallException, WriteTimeoutException {
46         final InputAclSetInterface request = new InputAclSetInterface();
47         request.isAdd = booleanToByte(isAdd);
48         request.swIfIndex = ifIndex;
49         request.l2TableIndex = ~0; // skip
50         request.ip4TableIndex = ~0; // skip
51         request.ip6TableIndex = ~0; // skip
52
53         final L2Acl l2Acl = acl.getL2Acl();
54         if (l2Acl != null) {
55             final String tableName = checkNotNull(l2Acl.getClassifyTable(), "L2 classify table is null");
56             request.l2TableIndex = classifyTableContext.getIndex(tableName, mappingContext);
57         }
58         final Ip4Acl ip4Acl = acl.getIp4Acl();
59         if (ip4Acl != null) {
60             final String tableName = checkNotNull(ip4Acl.getClassifyTable(), "IPv4 classify table is null");
61             request.ip4TableIndex = classifyTableContext.getIndex(tableName, mappingContext);
62         }
63         final Ip6Acl ip6Acl = acl.getIp6Acl();
64         if (ip6Acl != null) {
65             final String tableName = checkNotNull(ip6Acl.getClassifyTable(), "IPv6 classify table is null");
66             request.ip6TableIndex = classifyTableContext.getIndex(tableName, mappingContext);
67         }
68
69         final CompletionStage<InputAclSetInterfaceReply> inputAclSetInterfaceReplyCompletionStage =
70             futureJVppCore.inputAclSetInterface(request);
71
72         TranslateUtils.getReplyForWrite(inputAclSetInterfaceReplyCompletionStage.toCompletableFuture(), id);
73     }
74 }