52623e3d6d9c2087114617c1db98636cb508d5da
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2017 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.hc2vpp.vpp.classifier.read.acl;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
21 import static io.fd.hc2vpp.v3po.util.SubInterfaceUtils.getSubInterfaceName;
22
23 import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer;
24 import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer;
25 import io.fd.hc2vpp.common.translate.util.NamingContext;
26 import io.fd.hc2vpp.v3po.interfacesstate.SubInterfaceCustomizer;
27 import io.fd.hc2vpp.vpp.classifier.context.VppClassifierContextManager;
28 import io.fd.honeycomb.translate.read.ReadContext;
29 import io.fd.honeycomb.translate.read.ReadFailedException;
30 import io.fd.honeycomb.translate.spi.read.Initialized;
31 import io.fd.honeycomb.translate.spi.read.InitializingReaderCustomizer;
32 import io.fd.honeycomb.translate.util.RWUtils;
33 import io.fd.vpp.jvpp.core.dto.ClassifyTableByInterface;
34 import io.fd.vpp.jvpp.core.dto.ClassifyTableByInterfaceReply;
35 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
36 import javax.annotation.Nonnull;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.InterfaceKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170503.vpp.acl.attributes.Acl;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170503.vpp.acl.attributes.AclBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170503.vpp.acl.attributes.acl.Ingress;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev170503.vpp.acl.attributes.acl.IngressBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.subinterface.acl.rev170315.VppSubinterfaceAclAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev170509.interfaces.state._interface.sub.interfaces.SubInterface;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev170509.interfaces.state._interface.sub.interfaces.SubInterfaceKey;
46 import org.opendaylight.yangtools.concepts.Builder;
47 import org.opendaylight.yangtools.yang.binding.DataObject;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 /**
53  * Customizer for reading ingress ACLs enabled on given sub-interface.
54  */
55 public class SubInterfaceAclCustomizer extends FutureJVppCustomizer
56         implements InitializingReaderCustomizer<Ingress, IngressBuilder>, AclReader, JvppReplyConsumer {
57
58     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceAclCustomizer.class);
59     private final NamingContext interfaceContext;
60     private final VppClassifierContextManager classifyTableContext;
61
62     public SubInterfaceAclCustomizer(@Nonnull final FutureJVppCore jvpp, @Nonnull final NamingContext interfaceContext,
63                                      @Nonnull final VppClassifierContextManager classifyTableContext) {
64         super(jvpp);
65         this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
66         this.classifyTableContext = checkNotNull(classifyTableContext, "classifyTableContext should not be null");
67     }
68
69     @Override
70     public void merge(@Nonnull final Builder<? extends DataObject> parentBuilder, @Nonnull final Ingress readValue) {
71         ((AclBuilder) parentBuilder).setIngress(readValue);
72     }
73
74     @Nonnull
75     @Override
76     public IngressBuilder getBuilder(@Nonnull final InstanceIdentifier<Ingress> id) {
77         return new IngressBuilder();
78     }
79
80     @Override
81     public void readCurrentAttributes(@Nonnull final InstanceIdentifier<Ingress> id,
82                                       @Nonnull final IngressBuilder builder,
83                                       @Nonnull final ReadContext ctx) throws ReadFailedException {
84         LOG.debug("Reading attributes for sub-interface ACL: {}", id);
85         final InterfaceKey parentInterfacekey = id.firstKeyOf(Interface.class);
86         checkArgument(parentInterfacekey != null, "No parent interface key found");
87         final SubInterfaceKey subInterfacekey = id.firstKeyOf(SubInterface.class);
88         checkArgument(subInterfacekey != null, "No sub-interface key found");
89         final String subInterfaceName =
90                 getSubInterfaceName(parentInterfacekey.getName(), subInterfacekey.getIdentifier().intValue());
91
92         final ClassifyTableByInterface request = new ClassifyTableByInterface();
93         request.swIfIndex = interfaceContext.getIndex(subInterfaceName, ctx.getMappingContext());
94
95         final ClassifyTableByInterfaceReply reply =
96                 getReplyForRead(getFutureJVpp().classifyTableByInterface(request).toCompletableFuture(), id);
97
98         builder.setL2Acl(readL2Acl(reply.l2TableId, classifyTableContext, ctx.getMappingContext()));
99         builder.setIp4Acl(readIp4Acl(reply.ip4TableId, classifyTableContext, ctx.getMappingContext()));
100         builder.setIp6Acl(readIp6Acl(reply.ip6TableId, classifyTableContext, ctx.getMappingContext()));
101
102         if (LOG.isTraceEnabled()) {
103             LOG.trace("Attributes for ACL {} successfully read: {}", id, builder.build());
104         }
105     }
106
107     @Override
108     public Initialized<Ingress> init(
109             @Nonnull final InstanceIdentifier<Ingress> id,
110             @Nonnull final Ingress readValue,
111             @Nonnull final ReadContext ctx) {
112         return Initialized.create(getCfgId(id), readValue);
113     }
114
115     static InstanceIdentifier<Ingress> getCfgId(
116             final InstanceIdentifier<Ingress> id) {
117         return SubInterfaceCustomizer.getCfgId(RWUtils.cutId(id, SubInterface.class))
118                 .augmentation(VppSubinterfaceAclAugmentation.class)
119                 .child(Acl.class)
120                 .child(Ingress.class);
121     }
122 }