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