962801a381dfedb2d5a27dc9f9e8bd8e9e10047f
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / 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.interfaces;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
21 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
22 import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
23 import io.fd.honeycomb.translate.v3po.util.NamingContext;
24 import io.fd.honeycomb.translate.v3po.util.SubInterfaceUtils;
25 import io.fd.honeycomb.translate.v3po.util.WriteTimeoutException;
26 import io.fd.honeycomb.translate.write.WriteContext;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import javax.annotation.Nonnull;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.Acl;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.openvpp.jvpp.VppBaseCallException;
36 import org.openvpp.jvpp.core.future.FutureJVppCore;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Customizer for enabling/disabling ACLs on given sub-interface.
42  */
43 public class SubInterfaceAclCustomizer extends FutureJVppCustomizer
44     implements WriterCustomizer<Acl>, AclWriter {
45
46     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceAclCustomizer.class);
47     private final NamingContext interfaceContext;
48     private final NamingContext classifyTableContext;
49
50     public SubInterfaceAclCustomizer(@Nonnull final FutureJVppCore vppApi,
51                                      @Nonnull final NamingContext interfaceContext,
52                                      @Nonnull final NamingContext classifyTableContext) {
53         super(vppApi);
54         this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
55         this.classifyTableContext = checkNotNull(classifyTableContext, "classifyTableContext should not be null");
56     }
57
58     @Override
59     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Acl> id, @Nonnull final Acl dataAfter,
60                                        @Nonnull final WriteContext writeContext) throws WriteFailedException {
61         try {
62             setAcl(true, id, dataAfter, writeContext);
63         } catch (VppBaseCallException e) {
64             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
65         }
66     }
67
68     @Override
69     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Acl> id, @Nonnull final Acl dataBefore,
70                                         @Nonnull final Acl dataAfter, @Nonnull final WriteContext writeContext)
71         throws WriteFailedException {
72         throw new UnsupportedOperationException("Acl update is not supported. Please delete Acl container first.");
73     }
74
75     @Override
76     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Acl> id, @Nonnull final Acl dataBefore,
77                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
78         try {
79             setAcl(false, id, dataBefore, writeContext);
80         } catch (VppBaseCallException e) {
81             throw new WriteFailedException.DeleteFailedException(id, e);
82         }
83     }
84
85     private void setAcl(final boolean isAdd, @Nonnull final InstanceIdentifier<Acl> id, @Nonnull final Acl acl,
86                         @Nonnull final WriteContext writeContext)
87         throws VppBaseCallException, WriteTimeoutException {
88         final InterfaceKey parentInterfacekey = id.firstKeyOf(Interface.class);
89         final SubInterfaceKey subInterfacekey = id.firstKeyOf(SubInterface.class);
90         final String subInterfaceName = SubInterfaceUtils
91             .getSubInterfaceName(parentInterfacekey.getName(), subInterfacekey.getIdentifier().intValue());
92         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
93
94         LOG.debug("Setting ACL(isAdd={}) on sub-interface={}(id={}): {}",
95             isAdd, subInterfaceName, subInterfaceIndex, acl);
96         inputAclSetInterface(getFutureJVpp(), isAdd, id, acl, subInterfaceIndex, classifyTableContext,
97             writeContext.getMappingContext());
98         LOG.debug("Successfully set ACL(isAdd={}) on sub-interface={}(id={}): {}",
99             isAdd, subInterfaceName, subInterfaceIndex, acl);
100     }
101 }