8942691012f1e04a009ec06fdb0f093b70f788bd
[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.interfaces.acl;
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.spi.write.WriterCustomizer;
23 import io.fd.honeycomb.translate.v3po.util.NamingContext;
24 import io.fd.honeycomb.translate.v3po.util.SubInterfaceUtils;
25 import io.fd.honeycomb.translate.write.WriteContext;
26 import io.fd.honeycomb.translate.write.WriteFailedException;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.ietf.acl.base.attributes.AccessLists;
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.IetfAcl;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.openvpp.jvpp.VppBaseCallException;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Customizer for enabling/disabling ACLs for given sub-interface (as defined in ietf-acl model).
41  *
42  * The customizer assumes it owns classify table management for sub-interfaces where ietf-acl container is present.
43  * Using low level classifier model or direct changes to classify tables in combination with ietf-acls are not supported
44  * and can result in unpredictable behaviour.
45  */
46 public class SubInterfaceIetfAclCustomizer implements WriterCustomizer<IetfAcl> {
47
48     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceIetfAclCustomizer.class);
49     private final IetfAClWriter aclWriter;
50     private final NamingContext interfaceContext;
51
52     public SubInterfaceIetfAclCustomizer(@Nonnull final IetfAClWriter aclWriter,
53                                          @Nonnull final NamingContext interfaceContext) {
54         this.aclWriter = checkNotNull(aclWriter, "aclWriter should not be null");
55         this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
56     }
57
58     private String getSubInterfaceName(@Nonnull final InstanceIdentifier<IetfAcl> id) {
59         final InterfaceKey parentInterfacekey = id.firstKeyOf(Interface.class);
60         final SubInterfaceKey subInterfacekey = id.firstKeyOf(SubInterface.class);
61         return SubInterfaceUtils
62             .getSubInterfaceName(parentInterfacekey.getName(), subInterfacekey.getIdentifier().intValue());
63     }
64
65     @Override
66     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<IetfAcl> id, @Nonnull final IetfAcl dataAfter,
67                                        @Nonnull final WriteContext writeContext) throws WriteFailedException {
68         final String subInterfaceName = getSubInterfaceName(id);
69         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
70         LOG.debug("Adding IETF-ACL for sub-interface: {}(id={}): {}", subInterfaceName, subInterfaceIndex, dataAfter);
71
72         final AccessLists accessLists = dataAfter.getAccessLists();
73         checkArgument(accessLists != null && accessLists.getAcl() != null,
74             "ietf-acl container does not define acl list");
75
76         try {
77             aclWriter.write(id, subInterfaceIndex, accessLists.getAcl(), writeContext);
78         } catch (VppBaseCallException e) {
79             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
80         }
81     }
82
83     @Override
84     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<IetfAcl> id,
85                                         @Nonnull final IetfAcl dataBefore, @Nonnull final IetfAcl dataAfter,
86                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
87         LOG.debug("Sub-interface ACLs update: removing previously configured ACLs");
88         deleteCurrentAttributes(id, dataBefore, writeContext);
89         LOG.debug("Sub-interface ACLs update: adding updated ACLs");
90         writeCurrentAttributes(id, dataAfter, writeContext);
91         LOG.debug("Sub-interface ACLs update was successful");
92     }
93
94     @Override
95     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<IetfAcl> id,
96                                         @Nonnull final IetfAcl dataBefore, @Nonnull final WriteContext writeContext)
97         throws WriteFailedException {
98         final String subInterfaceName = getSubInterfaceName(id);
99         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
100         LOG.debug("Removing ACLs for sub-interface={}(id={}): {}", subInterfaceName, subInterfaceIndex, dataBefore);
101         aclWriter.deleteAcl(id, subInterfaceIndex);
102     }
103 }