2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.v3po.interfaces.acl.ingress;
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;
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;
42 * Customizer for enabling/disabling ingress ACLs for given sub-interface (as defined in ietf-acl model).
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.
48 public class SubInterfaceIetfAclCustomizer implements WriterCustomizer<Ingress> {
50 private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceIetfAclCustomizer.class);
51 private final IngressIetfAclWriter aclWriter;
52 private final NamingContext interfaceContext;
54 public SubInterfaceIetfAclCustomizer(@Nonnull final IngressIetfAclWriter 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");
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());
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);
74 final AccessLists accessLists = dataAfter.getAccessLists();
75 checkArgument(accessLists != null && accessLists.getAcl() != null,
76 "ietf-acl container does not define acl list");
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();
84 .write(id, subInterfaceIndex, accessLists.getAcl(), accessLists.getDefaultAction(), accessLists.getMode(),
85 writeContext, getNumberOfTags(subInterface.getTags()), writeContext.getMappingContext());
89 public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Ingress> id,
90 @Nonnull final Ingress dataBefore, @Nonnull final Ingress dataAfter,
91 @Nonnull final WriteContext writeContext) throws WriteFailedException {
92 LOG.debug("Sub-interface ACLs update: removing previously configured ACLs");
93 deleteCurrentAttributes(id, dataBefore, writeContext);
94 LOG.debug("Sub-interface ACLs update: adding updated ACLs");
95 writeCurrentAttributes(id, dataAfter, writeContext);
96 LOG.debug("Sub-interface ACLs update was successful");
100 public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Ingress> id,
101 @Nonnull final Ingress dataBefore, @Nonnull final WriteContext writeContext)
102 throws WriteFailedException {
103 final String subInterfaceName = getSubInterfaceName(id);
104 final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
105 LOG.debug("Removing ACLs for sub-interface={}(id={}): {}", subInterfaceName, subInterfaceIndex, dataBefore);
106 aclWriter.deleteAcl(id, subInterfaceIndex, writeContext.getMappingContext());