VPP-378: update jvpp package names
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / acl / SubInterfaceIetfAclCustomizer.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 import static com.google.common.base.Preconditions.checkState;
22 import static io.fd.honeycomb.translate.vpp.util.SubInterfaceUtils.getNumberOfTags;
23
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.v3po.rev150105.ietf.acl.base.attributes.AccessLists;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.IetfAcl;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import io.fd.vpp.jvpp.VppBaseCallException;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * Customizer for enabling/disabling ACLs for given sub-interface (as defined in ietf-acl model).
44  *
45  * The customizer assumes it owns classify table management for sub-interfaces where ietf-acl container is present.
46  * Using low level classifier model or direct changes to classify tables in combination with ietf-acls are not supported
47  * and can result in unpredictable behaviour.
48  */
49 public class SubInterfaceIetfAclCustomizer implements WriterCustomizer<IetfAcl> {
50
51     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceIetfAclCustomizer.class);
52     private final IetfAClWriter aclWriter;
53     private final NamingContext interfaceContext;
54
55     public SubInterfaceIetfAclCustomizer(@Nonnull final IetfAClWriter aclWriter,
56                                          @Nonnull final NamingContext interfaceContext) {
57         this.aclWriter = checkNotNull(aclWriter, "aclWriter should not be null");
58         this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
59     }
60
61     private String getSubInterfaceName(@Nonnull final InstanceIdentifier<IetfAcl> id) {
62         final InterfaceKey parentInterfacekey = id.firstKeyOf(Interface.class);
63         final SubInterfaceKey subInterfacekey = id.firstKeyOf(SubInterface.class);
64         return SubInterfaceUtils
65             .getSubInterfaceName(parentInterfacekey.getName(), subInterfacekey.getIdentifier().intValue());
66     }
67
68     @Override
69     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<IetfAcl> id, @Nonnull final IetfAcl dataAfter,
70                                        @Nonnull final WriteContext writeContext) throws WriteFailedException {
71         final String subInterfaceName = getSubInterfaceName(id);
72         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
73         LOG.debug("Adding IETF-ACL for sub-interface: {}(id={}): {}", subInterfaceName, subInterfaceIndex, dataAfter);
74
75         final AccessLists accessLists = dataAfter.getAccessLists();
76         checkArgument(accessLists != null && accessLists.getAcl() != null,
77             "ietf-acl container does not define acl list");
78
79         final Optional<SubInterface> subInterfaceOptional =
80             writeContext.readAfter(id.firstIdentifierOf(SubInterface.class));
81         checkState(subInterfaceOptional.isPresent(), "Could not read SubInterface data object for %s", id);
82         final SubInterface subInterface = subInterfaceOptional.get();
83
84         try {
85             aclWriter.write(id, subInterfaceIndex, accessLists.getAcl(), writeContext,
86                 getNumberOfTags(subInterface.getTags()));
87         } catch (VppBaseCallException e) {
88             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
89         }
90     }
91
92     @Override
93     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<IetfAcl> id,
94                                         @Nonnull final IetfAcl dataBefore, @Nonnull final IetfAcl dataAfter,
95                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
96         LOG.debug("Sub-interface ACLs update: removing previously configured ACLs");
97         deleteCurrentAttributes(id, dataBefore, writeContext);
98         LOG.debug("Sub-interface ACLs update: adding updated ACLs");
99         writeCurrentAttributes(id, dataAfter, writeContext);
100         LOG.debug("Sub-interface ACLs update was successful");
101     }
102
103     @Override
104     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<IetfAcl> id,
105                                         @Nonnull final IetfAcl dataBefore, @Nonnull final WriteContext writeContext)
106         throws WriteFailedException {
107         final String subInterfaceName = getSubInterfaceName(id);
108         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
109         LOG.debug("Removing ACLs for sub-interface={}(id={}): {}", subInterfaceName, subInterfaceIndex, dataBefore);
110         aclWriter.deleteAcl(id, subInterfaceIndex);
111     }
112 }