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