ab71e65872fb00a2ea56a398e4b88dc6a7d2df7d
[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.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.rev150105.interfaces._interface.Acl;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.openvpp.jvpp.VppBaseCallException;
32 import org.openvpp.jvpp.core.future.FutureJVppCore;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Customizer for enabling/disabling ACLs on given interface.
38  */
39 public class AclCustomizer extends FutureJVppCustomizer implements WriterCustomizer<Acl>, AclWriter {
40
41     private static final Logger LOG = LoggerFactory.getLogger(AclCustomizer.class);
42     private final NamingContext interfaceContext;
43     private final NamingContext classifyTableContext;
44
45     public AclCustomizer(@Nonnull final FutureJVppCore vppApi, @Nonnull final NamingContext interfaceContext,
46                          @Nonnull final NamingContext classifyTableContext) {
47         super(vppApi);
48         this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
49         this.classifyTableContext = checkNotNull(classifyTableContext, "classifyTableContext should not be null");
50     }
51
52     @Override
53     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Acl> id, @Nonnull final Acl dataAfter,
54                                        @Nonnull final WriteContext writeContext) throws WriteFailedException {
55         try {
56             setAcl(true, id, dataAfter, writeContext);
57         } catch (VppBaseCallException e) {
58             throw new WriteFailedException.CreateFailedException(id, dataAfter, e);
59         }
60     }
61
62     @Override
63     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Acl> id, @Nonnull final Acl dataBefore,
64                                         @Nonnull final Acl dataAfter, @Nonnull final WriteContext writeContext)
65         throws WriteFailedException {
66         throw new UnsupportedOperationException("Acl update is not supported. Please delete Acl container first.");
67     }
68
69     @Override
70     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Acl> id, @Nonnull final Acl dataBefore,
71                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
72         try {
73             setAcl(false, id, dataBefore, writeContext);
74         } catch (VppBaseCallException e) {
75             throw new WriteFailedException.DeleteFailedException(id, e);
76         }
77     }
78
79     private void setAcl(final boolean isAdd, @Nonnull final InstanceIdentifier<Acl> id, @Nonnull final Acl acl,
80                         @Nonnull final WriteContext writeContext)
81         throws VppBaseCallException, WriteTimeoutException {
82         final String ifName = id.firstKeyOf(Interface.class).getName();
83         final int ifIndex = interfaceContext.getIndex(ifName, writeContext.getMappingContext());
84
85         LOG.debug("Setting ACL(isAdd={}) on interface={}(id={}): {}", isAdd, ifName, ifIndex, acl);
86
87         inputAclSetInterface(getFutureJVpp(), isAdd, id, acl, ifIndex, classifyTableContext,
88             writeContext.getMappingContext());
89         LOG.debug("Successfully set ACL(isAdd={}) on interface={}(id={}): {}", isAdd, ifName, ifIndex, acl);
90     }
91 }