c0e4c3d14a47f09f933e3b88e6cd11afdbacf521
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / acl / egress / 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.egress;
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.v3po.interfaces.acl.common.IetfAclWriter;
24 import io.fd.honeycomb.translate.vpp.util.NamingContext;
25 import io.fd.honeycomb.translate.write.WriteContext;
26 import io.fd.honeycomb.translate.write.WriteFailedException;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev161214.interfaces._interface.ietf.acl.Egress;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.InterfaceMode;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.acl.rev161214.ietf.acl.base.attributes.AccessLists;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class IetfAclCustomizer implements WriterCustomizer<Egress> {
37     private static final Logger LOG = LoggerFactory.getLogger(IetfAclCustomizer.class);
38     private final IetfAclWriter aclWriter;
39     private final NamingContext interfaceContext;
40
41     public IetfAclCustomizer(final IetfAclWriter aclWriter, final NamingContext interfaceContext) {
42         this.aclWriter = checkNotNull(aclWriter, "aclWriter should not be null");
43         this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
44     }
45
46     @Override
47     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Egress> id, @Nonnull final Egress dataAfter,
48                                        @Nonnull final WriteContext writeContext) throws WriteFailedException {
49         final String ifName = id.firstKeyOf(Interface.class).getName();
50         final int ifIndex = interfaceContext.getIndex(ifName, writeContext.getMappingContext());
51         LOG.debug("Adding egress ACLs for interface={}(id={}): {}", ifName, ifIndex, dataAfter);
52
53         final AccessLists accessLists = dataAfter.getAccessLists();
54         checkArgument(accessLists != null && accessLists.getAcl() != null,
55             "ietf-acl container does not define acl list");
56
57         if (!InterfaceMode.L2.equals(accessLists.getMode())) {
58             LOG.debug("Writing egress Acls is supported only in L2 mode. Ignoring config: {}", dataAfter);
59             return;
60         }
61
62         aclWriter.write(id, ifIndex, accessLists.getAcl(), accessLists.getDefaultAction(), accessLists.getMode(),
63             writeContext, writeContext.getMappingContext());
64     }
65
66     @Override
67     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Egress> id, @Nonnull final Egress dataBefore,
68                                         @Nonnull final Egress dataAfter, @Nonnull final WriteContext writeContext)
69         throws WriteFailedException {
70         LOG.debug("ACLs update: removing previously configured ACLs");
71         deleteCurrentAttributes(id, dataBefore, writeContext);
72         LOG.debug("ACLs update: adding updated ACLs");
73         writeCurrentAttributes(id, dataAfter, writeContext);
74         LOG.debug("ACLs update was successful");
75     }
76
77     @Override
78     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Egress> id, @Nonnull final Egress dataBefore,
79                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
80         final String ifName = id.firstKeyOf(Interface.class).getName();
81         final int ifIndex = interfaceContext.getIndex(ifName, writeContext.getMappingContext());
82         LOG.debug("Removing ACLs for interface={}(id={}): {}", ifName, ifIndex, dataBefore);
83         aclWriter.deleteAcl(id, ifIndex, writeContext.getMappingContext());
84     }
85 }