VPP-378: update jvpp package names
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / acl / 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;
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.IetfAcl;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import io.fd.vpp.jvpp.VppBaseCallException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * Customizer for enabling/disabling ACLs for given interface (as defined in ietf-acl model).
37  *
38  * The customizer assumes it owns classify table management for interfaces where ietf-acl container is present. Using
39  * low level classifier model or direct changes to classify tables in combination with ietf-acls are not supported and
40  * can result in unpredictable behaviour.
41  */
42 public class IetfAclCustomizer implements WriterCustomizer<IetfAcl> {
43
44     private static final Logger LOG = LoggerFactory.getLogger(IetfAclCustomizer.class);
45     private final IetfAClWriter aclWriter;
46     private final NamingContext interfaceContext;
47
48     public IetfAclCustomizer(@Nonnull final IetfAClWriter aclWriter,
49                              @Nonnull final NamingContext interfaceContext) {
50         this.aclWriter = checkNotNull(aclWriter, "aclWriter should not be null");
51         this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
52     }
53
54     @Override
55     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<IetfAcl> id, @Nonnull final IetfAcl dataAfter,
56                                        @Nonnull final WriteContext writeContext) throws WriteFailedException {
57         final String ifName = id.firstKeyOf(Interface.class).getName();
58         final int ifIndex = interfaceContext.getIndex(ifName, writeContext.getMappingContext());
59         LOG.debug("Adding ACLs for interface={}(id={}): {}", ifName, ifIndex, dataAfter);
60
61         final AccessLists accessLists = dataAfter.getAccessLists();
62         checkArgument(accessLists != null && accessLists.getAcl() != null,
63             "ietf-acl container does not define acl list");
64
65         try {
66             aclWriter.write(id, ifIndex, accessLists.getAcl(), writeContext);
67         } catch (VppBaseCallException e) {
68             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
69         }
70     }
71
72     @Override
73     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<IetfAcl> id,
74                                         @Nonnull final IetfAcl dataBefore, @Nonnull final IetfAcl dataAfter,
75                                         @Nonnull final WriteContext writeContext)
76         throws WriteFailedException {
77         LOG.debug("ACLs update: removing previously configured ACLs");
78         deleteCurrentAttributes(id, dataBefore, writeContext);
79         LOG.debug("ACLs update: adding updated ACLs");
80         writeCurrentAttributes(id, dataAfter, writeContext);
81         LOG.debug("ACLs update was successful");
82     }
83
84     @Override
85     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<IetfAcl> id,
86                                         @Nonnull final IetfAcl dataBefore,
87                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
88         final String ifName = id.firstKeyOf(Interface.class).getName();
89         final int ifIndex = interfaceContext.getIndex(ifName, writeContext.getMappingContext());
90         LOG.debug("Removing ACLs for interface={}(id={}): {}", ifName, ifIndex, dataBefore);
91         aclWriter.deleteAcl(id, ifIndex);
92     }
93 }