bf7c0eb46ebf4217c01edc4fa39d6e8b697ee8e1
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / acl / egress / 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.egress;
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.v3po.interfaces.acl.common.IetfAclWriter;
27 import io.fd.honeycomb.translate.vpp.util.NamingContext;
28 import io.fd.honeycomb.translate.vpp.util.SubInterfaceUtils;
29 import io.fd.honeycomb.translate.write.WriteContext;
30 import io.fd.honeycomb.translate.write.WriteFailedException;
31 import javax.annotation.Nonnull;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.InterfaceMode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.ietf.acl.base.attributes.AccessLists;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.sub.interfaces.SubInterface;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.interfaces._interface.sub.interfaces.SubInterfaceKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev161214.sub._interface.base.attributes.ietf.acl.Egress;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class SubInterfaceIetfAclCustomizer implements WriterCustomizer<Egress> {
44     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceIetfAclCustomizer.class);
45     private final IetfAclWriter aclWriter;
46     private final NamingContext interfaceContext;
47
48     public SubInterfaceIetfAclCustomizer(final IetfAclWriter aclWriter, 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     private String getSubInterfaceName(@Nonnull final InstanceIdentifier<Egress> id) {
54         final InterfaceKey parentInterfacekey = id.firstKeyOf(Interface.class);
55         final SubInterfaceKey subInterfacekey = id.firstKeyOf(SubInterface.class);
56         return SubInterfaceUtils
57             .getSubInterfaceName(parentInterfacekey.getName(), subInterfacekey.getIdentifier().intValue());
58     }
59
60     @Override
61     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Egress> id, @Nonnull final Egress dataAfter,
62                                        @Nonnull final WriteContext writeContext) throws WriteFailedException {
63         final String subInterfaceName = getSubInterfaceName(id);
64         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
65         LOG.debug("Adding IETF-ACL for sub-interface: {}(id={}): {}", subInterfaceName, subInterfaceIndex, dataAfter);
66
67         final AccessLists accessLists = dataAfter.getAccessLists();
68         checkArgument(accessLists != null && accessLists.getAcl() != null,
69             "ietf-acl container does not define acl list");
70
71         final Optional<SubInterface> subInterfaceOptional =
72             writeContext.readAfter(id.firstIdentifierOf(SubInterface.class));
73         checkState(subInterfaceOptional.isPresent(), "Could not read SubInterface data object for %s", id);
74         final SubInterface subInterface = subInterfaceOptional.get();
75
76         if (!InterfaceMode.L2.equals(accessLists.getMode())) {
77             LOG.debug("Writing egress Acls is supported only in L2 mode. Ignoring config: {}", dataAfter);
78             return;
79         }
80
81         aclWriter
82             .write(id, subInterfaceIndex, accessLists.getAcl(), accessLists.getDefaultAction(), accessLists.getMode(),
83                 writeContext, getNumberOfTags(subInterface.getTags()), writeContext.getMappingContext());
84     }
85
86     @Override
87     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Egress> id, @Nonnull final Egress dataBefore,
88                                         @Nonnull final Egress dataAfter, @Nonnull final WriteContext writeContext)
89         throws WriteFailedException {
90         LOG.debug("Sub-interface ACLs update: removing previously configured ACLs");
91         deleteCurrentAttributes(id, dataBefore, writeContext);
92         LOG.debug("Sub-interface ACLs update: adding updated ACLs");
93         writeCurrentAttributes(id, dataAfter, writeContext);
94         LOG.debug("Sub-interface ACLs update was successful");
95     }
96
97     @Override
98     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Egress> id, @Nonnull final Egress dataBefore,
99                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
100         final String subInterfaceName = getSubInterfaceName(id);
101         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
102         LOG.debug("Removing ACLs for sub-interface={}(id={}): {}", subInterfaceName, subInterfaceIndex, dataBefore);
103         aclWriter.deleteAcl(id, subInterfaceIndex, writeContext.getMappingContext());
104     }
105 }