c9cd66ec2d82cbf52bea66ed615dcdd9e5bd859c
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / acl / ingress / IetfAclCustomizer.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
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.rev150105.ietf.acl.base.attributes.AccessLists;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.ietf.acl.Ingress;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Customizer for enabling/disabling ingress ACLs for given interface (as defined in ietf-acl model).
36  *
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.
40  */
41 public class IetfAclCustomizer implements WriterCustomizer<Ingress> {
42
43     private static final Logger LOG = LoggerFactory.getLogger(IetfAclCustomizer.class);
44     private final IetfAClWriter aclWriter;
45     private final NamingContext interfaceContext;
46
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");
51     }
52
53     @Override
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);
59
60         final AccessLists accessLists = dataAfter.getAccessLists();
61         checkArgument(accessLists != null && accessLists.getAcl() != null,
62                 "ietf-acl container does not define acl list");
63
64         aclWriter.write(id, ifIndex, accessLists.getAcl(), accessLists.getMode(), writeContext);
65     }
66
67     @Override
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");
77     }
78
79     @Override
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);
87     }
88 }