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