bad919ffacc0318dd524ba86f9ce0bdd73c74a19
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfacesstate / SubInterfaceAclCustomizer.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.interfacesstate;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
21 import static io.fd.honeycomb.translate.v3po.util.SubInterfaceUtils.getSubInterfaceName;
22
23 import io.fd.honeycomb.translate.read.ReadContext;
24 import io.fd.honeycomb.translate.spi.read.ReaderCustomizer;
25 import io.fd.honeycomb.translate.read.ReadFailedException;
26 import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
27 import io.fd.honeycomb.translate.v3po.util.NamingContext;
28 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
29 import javax.annotation.Nonnull;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterface;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterfaceBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterfaceKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.Acl;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.AclBuilder;
37 import org.opendaylight.yangtools.concepts.Builder;
38 import org.opendaylight.yangtools.yang.binding.DataObject;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.openvpp.jvpp.VppBaseCallException;
41 import org.openvpp.jvpp.core.dto.ClassifyTableByInterface;
42 import org.openvpp.jvpp.core.dto.ClassifyTableByInterfaceReply;
43 import org.openvpp.jvpp.core.future.FutureJVppCore;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * Customizer for reading ACLs enabled on given sub-interface.
49  */
50 public class SubInterfaceAclCustomizer extends FutureJVppCustomizer
51     implements ReaderCustomizer<Acl, AclBuilder>, AclReader {
52
53     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceAclCustomizer.class);
54     private final NamingContext interfaceContext;
55     private final NamingContext classifyTableContext;
56
57     public SubInterfaceAclCustomizer(@Nonnull final FutureJVppCore jvpp, @Nonnull final NamingContext interfaceContext,
58                                      @Nonnull final NamingContext classifyTableContext) {
59         super(jvpp);
60         this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
61         this.classifyTableContext = checkNotNull(classifyTableContext, "classifyTableContext should not be null");
62     }
63
64     @Override
65     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final Acl readValue) {
66         ((SubInterfaceBuilder) parentBuilder).setAcl(readValue);
67     }
68
69     @Nonnull
70     @Override
71     public AclBuilder getBuilder(@Nonnull final InstanceIdentifier<Acl> id) {
72         return new AclBuilder();
73     }
74
75     @Override
76     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Acl> id, @Nonnull final AclBuilder builder,
77                                       @Nonnull final ReadContext ctx) throws ReadFailedException {
78         LOG.debug("Reading attributes for sub-interface ACL: {}", id);
79         final InterfaceKey parentInterfacekey = id.firstKeyOf(Interface.class);
80         checkArgument(parentInterfacekey != null, "No parent interface key found");
81         final SubInterfaceKey subInterfacekey = id.firstKeyOf(SubInterface.class);
82         checkArgument(subInterfacekey != null, "No sub-interface key found");
83         final String subInterfaceName =
84             getSubInterfaceName(parentInterfacekey.getName(), subInterfacekey.getIdentifier().intValue());
85
86         final ClassifyTableByInterface request = new ClassifyTableByInterface();
87         request.swIfIndex = interfaceContext.getIndex(subInterfaceName, ctx.getMappingContext());
88         try {
89             final ClassifyTableByInterfaceReply reply = TranslateUtils
90                 .getReplyForRead(getFutureJVpp().classifyTableByInterface(request).toCompletableFuture(), id);
91
92             builder.setL2Acl(readL2Acl(reply.l2TableId, classifyTableContext, ctx.getMappingContext()));
93             builder.setIp4Acl(readIp4Acl(reply.ip4TableId, classifyTableContext, ctx.getMappingContext()));
94             builder.setIp6Acl(readIp6Acl(reply.ip6TableId, classifyTableContext, ctx.getMappingContext()));
95
96             if (LOG.isTraceEnabled()) {
97                 LOG.trace("Attributes for ACL {} successfully read: {}", id, builder.build());
98             }
99         } catch (VppBaseCallException e) {
100             throw new ReadFailedException(id, e);
101         }
102     }
103 }