HONEYCOMB-118: extend classifer model to support node names.
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / AclCustomizer.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;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
21 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
22 import io.fd.honeycomb.translate.v3po.util.FutureJVppCustomizer;
23 import io.fd.honeycomb.translate.v3po.util.NamingContext;
24 import io.fd.honeycomb.translate.v3po.util.WriteTimeoutException;
25 import io.fd.honeycomb.translate.v3po.vppclassifier.VppClassifierContextManager;
26 import io.fd.honeycomb.translate.write.WriteContext;
27 import io.fd.honeycomb.translate.write.WriteFailedException;
28 import javax.annotation.Nonnull;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.interfaces._interface.Acl;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.openvpp.jvpp.VppBaseCallException;
33 import org.openvpp.jvpp.core.future.FutureJVppCore;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Customizer for enabling/disabling ACLs on given interface.
39  */
40 public class AclCustomizer extends FutureJVppCustomizer implements WriterCustomizer<Acl>, AclWriter {
41
42     private static final Logger LOG = LoggerFactory.getLogger(AclCustomizer.class);
43     private final NamingContext interfaceContext;
44     private final VppClassifierContextManager classifyTableContext;
45
46     public AclCustomizer(@Nonnull final FutureJVppCore vppApi, @Nonnull final NamingContext interfaceContext,
47                          @Nonnull final VppClassifierContextManager classifyTableContext) {
48         super(vppApi);
49         this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
50         this.classifyTableContext = checkNotNull(classifyTableContext, "classifyTableContext should not be null");
51     }
52
53     @Override
54     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Acl> id, @Nonnull final Acl dataAfter,
55                                        @Nonnull final WriteContext writeContext) throws WriteFailedException {
56         try {
57             setAcl(true, id, dataAfter, writeContext);
58         } catch (VppBaseCallException e) {
59             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
60         }
61     }
62
63     @Override
64     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Acl> id, @Nonnull final Acl dataBefore,
65                                         @Nonnull final Acl dataAfter, @Nonnull final WriteContext writeContext)
66         throws WriteFailedException {
67         throw new UnsupportedOperationException("Acl update is not supported. Please delete Acl container first.");
68     }
69
70     @Override
71     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Acl> id, @Nonnull final Acl dataBefore,
72                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
73         try {
74             setAcl(false, id, dataBefore, writeContext);
75         } catch (VppBaseCallException e) {
76             throw new WriteFailedException.DeleteFailedException(id, e);
77         }
78     }
79
80     private void setAcl(final boolean isAdd, @Nonnull final InstanceIdentifier<Acl> id, @Nonnull final Acl acl,
81                         @Nonnull final WriteContext writeContext)
82         throws VppBaseCallException, WriteTimeoutException {
83         final String ifName = id.firstKeyOf(Interface.class).getName();
84         final int ifIndex = interfaceContext.getIndex(ifName, writeContext.getMappingContext());
85
86         LOG.debug("Setting ACL(isAdd={}) on interface={}(id={}): {}", isAdd, ifName, ifIndex, acl);
87
88         inputAclSetInterface(getFutureJVpp(), isAdd, id, acl, ifIndex, classifyTableContext,
89             writeContext.getMappingContext());
90         LOG.debug("Successfully set ACL(isAdd={}) on interface={}(id={}): {}", isAdd, ifName, ifIndex, acl);
91     }
92 }