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;
22 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
23 import io.fd.honeycomb.translate.vpp.util.NamingContext;
24 import io.fd.honeycomb.translate.write.WriteContext;
25 import io.fd.honeycomb.translate.write.WriteFailedException;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.ietf.acl.base.attributes.AccessLists;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.ietf.acl.Ingress;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Customizer for enabling/disabling ingress ACLs for given interface (as defined in ietf-acl model).
37 * The customizer assumes it owns classify table management for interfaces where ietf-acl container is present. Using
38 * low level classifier model or direct changes to classify tables in combination with ietf-acls are not supported and
39 * can result in unpredictable behaviour.
41 public class IetfAclCustomizer implements WriterCustomizer<Ingress> {
43 private static final Logger LOG = LoggerFactory.getLogger(IetfAclCustomizer.class);
44 private final IetfAClWriter aclWriter;
45 private final NamingContext interfaceContext;
47 public IetfAclCustomizer(@Nonnull final IetfAClWriter aclWriter,
48 @Nonnull final NamingContext interfaceContext) {
49 this.aclWriter = checkNotNull(aclWriter, "aclWriter should not be null");
50 this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
54 public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Ingress> id, @Nonnull final Ingress dataAfter,
55 @Nonnull final WriteContext writeContext) throws WriteFailedException {
56 final String ifName = id.firstKeyOf(Interface.class).getName();
57 final int ifIndex = interfaceContext.getIndex(ifName, writeContext.getMappingContext());
58 LOG.debug("Adding ACLs for interface={}(id={}): {}", ifName, ifIndex, dataAfter);
60 final AccessLists accessLists = dataAfter.getAccessLists();
61 checkArgument(accessLists != null && accessLists.getAcl() != null,
62 "ietf-acl container does not define acl list");
64 aclWriter.write(id, ifIndex, accessLists.getAcl(), accessLists.getMode(), writeContext);
68 public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Ingress> id,
69 @Nonnull final Ingress dataBefore, @Nonnull final Ingress dataAfter,
70 @Nonnull final WriteContext writeContext)
71 throws WriteFailedException {
72 LOG.debug("ACLs update: removing previously configured ACLs");
73 deleteCurrentAttributes(id, dataBefore, writeContext);
74 LOG.debug("ACLs update: adding updated ACLs");
75 writeCurrentAttributes(id, dataAfter, writeContext);
76 LOG.debug("ACLs update was successful");
80 public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Ingress> id,
81 @Nonnull final Ingress dataBefore,
82 @Nonnull final WriteContext writeContext) throws WriteFailedException {
83 final String ifName = id.firstKeyOf(Interface.class).getName();
84 final int ifIndex = interfaceContext.getIndex(ifName, writeContext.getMappingContext());
85 LOG.debug("Removing ACLs for interface={}(id={}): {}", ifName, ifIndex, dataBefore);
86 aclWriter.deleteAcl(id, ifIndex);