0413870d7507cac89ce71e86767396afd4b229d1
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / SubInterfaceL2Customizer.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 io.fd.honeycomb.translate.spi.write.WriterCustomizer;
20 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
21 import io.fd.honeycomb.translate.vpp.util.NamingContext;
22 import io.fd.honeycomb.translate.vpp.util.SubInterfaceUtils;
23 import io.fd.honeycomb.translate.write.WriteContext;
24 import io.fd.honeycomb.translate.write.WriteFailedException;
25 import javax.annotation.Nonnull;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.L2;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Customizer for writing vlan sub interface l2 configuration
38  */
39 public class SubInterfaceL2Customizer extends FutureJVppCustomizer implements WriterCustomizer<L2> {
40
41     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceL2Customizer.class);
42     private final NamingContext interfaceContext;
43     private final InterconnectionWriteUtils icWriterUtils;
44
45     public SubInterfaceL2Customizer(final FutureJVppCore vppApi, final NamingContext interfaceContext,
46                                     final NamingContext bridgeDomainContext) {
47         super(vppApi);
48         this.interfaceContext = interfaceContext;
49         this.icWriterUtils = new InterconnectionWriteUtils(vppApi, interfaceContext, bridgeDomainContext);
50     }
51
52     @Override
53     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<L2> id, @Nonnull final L2 dataAfter,
54                                        @Nonnull final WriteContext writeContext)
55             throws WriteFailedException {
56         final String subInterfaceName = getSubInterfaceName(id);
57         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
58         setL2(id, subInterfaceIndex, subInterfaceName, dataAfter, writeContext);
59     }
60
61     private String getSubInterfaceName(@Nonnull final InstanceIdentifier<L2> 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 updateCurrentAttributes(@Nonnull final InstanceIdentifier<L2> id, @Nonnull final L2 dataBefore,
70                                         @Nonnull final L2 dataAfter, @Nonnull final WriteContext writeContext)
71             throws WriteFailedException {
72
73         final String subInterfaceName = getSubInterfaceName(id);
74         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
75         // Setting L2 to new values
76         setL2(id, subInterfaceIndex, subInterfaceName, dataAfter, writeContext);
77     }
78
79     @Override
80     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<L2> id, @Nonnull final L2 dataBefore,
81                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
82         final String subInterfaceName = getSubInterfaceName(id);
83         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
84         deleteL2(id, subInterfaceIndex, subInterfaceName, dataBefore, writeContext);
85     }
86
87     private void setL2(final InstanceIdentifier<L2> id, final int swIfIndex, final String ifcName, final L2 l2,
88                        final WriteContext writeContext)
89             throws WriteFailedException {
90         LOG.debug("Setting L2 for sub-interface: {}", ifcName);
91         icWriterUtils.setInterconnection(id, swIfIndex, ifcName, l2.getInterconnection(), writeContext);
92     }
93
94     private void deleteL2(final InstanceIdentifier<L2> id, final int swIfIndex, final String ifcName, final L2 l2Before,
95                        final WriteContext writeContext)
96             throws WriteFailedException {
97         LOG.debug("Deleting L2 for sub-interface: {}", ifcName);
98         icWriterUtils.deleteInterconnection(id, swIfIndex, ifcName, l2Before.getInterconnection(), writeContext);
99     }
100 }