2 * Copyright (c) 2017 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.hc2vpp.vpp.classifier.write.acl.ingress;
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
22 import io.fd.hc2vpp.common.translate.util.NamingContext;
23 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
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.vpp.classifier.acl.rev161214.ietf.acl.base.attributes.AccessLists;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.classifier.acl.rev161214.vpp.acl.attributes.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 IngressIetfAclWriter aclWriter;
45 private final NamingContext interfaceContext;
47 public IetfAclCustomizer(@Nonnull final IngressIetfAclWriter 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.getDefaultAction(), accessLists.getMode(),
65 writeContext, writeContext.getMappingContext());
69 public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Ingress> id,
70 @Nonnull final Ingress dataBefore, @Nonnull final Ingress dataAfter,
71 @Nonnull final WriteContext writeContext)
72 throws WriteFailedException {
73 LOG.debug("ACLs update: removing previously configured ACLs");
74 deleteCurrentAttributes(id, dataBefore, writeContext);
75 LOG.debug("ACLs update: adding updated ACLs");
76 writeCurrentAttributes(id, dataAfter, writeContext);
77 LOG.debug("ACLs update was successful");
81 public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Ingress> id,
82 @Nonnull final Ingress dataBefore,
83 @Nonnull final WriteContext writeContext) throws WriteFailedException {
84 final String ifName = id.firstKeyOf(Interface.class).getName();
85 final int ifIndex = interfaceContext.getIndex(ifName, writeContext.getMappingContext());
86 LOG.debug("Removing ACLs for interface={}(id={}): {}", ifName, ifIndex, dataBefore);
87 aclWriter.deleteAcl(id, ifIndex, writeContext.getMappingContext());