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