8ab3e2c123db747e3ebe692abae010646fa41b17
[hc2vpp.git] /
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.v3po.translate.v3po.interfaces;
18
19 import com.google.common.base.Optional;
20 import io.fd.honeycomb.v3po.translate.spi.write.ChildWriterCustomizer;
21 import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer;
22 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
23 import io.fd.honeycomb.v3po.translate.v3po.util.SubInterfaceUtils;
24 import io.fd.honeycomb.v3po.translate.write.WriteContext;
25 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
26 import javax.annotation.Nonnull;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.base.attributes.L2;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.openvpp.jvpp.future.FutureJVpp;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Customizer for writing vlan sub interface l2 configuration
40  */
41 public class SubInterfaceL2Customizer extends FutureJVppCustomizer implements ChildWriterCustomizer<L2> {
42
43     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceL2Customizer.class);
44     private final NamingContext interfaceContext;
45     private final InterconnectionWriteUtils icWriterUtils;
46
47     public SubInterfaceL2Customizer(final FutureJVpp vppApi, final NamingContext interfaceContext,
48                                     final NamingContext bridgeDomainContext) {
49         super(vppApi);
50         this.interfaceContext = interfaceContext;
51         this.icWriterUtils = new InterconnectionWriteUtils(vppApi, interfaceContext, bridgeDomainContext);
52     }
53
54     @Nonnull
55     @Override
56     public Optional<L2> extract(@Nonnull final InstanceIdentifier<L2> currentId, @Nonnull final DataObject parentData) {
57         return Optional.fromNullable(((SubInterface) parentData).getL2());
58     }
59
60     @Override
61     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<L2> id, @Nonnull final L2 dataAfter,
62                                        @Nonnull final WriteContext writeContext)
63             throws WriteFailedException {
64         final String subInterfaceName = getSubInterfaceName(id);
65         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
66         setL2(id, subInterfaceIndex, subInterfaceName, dataAfter, writeContext);
67     }
68
69     private String getSubInterfaceName(@Nonnull final InstanceIdentifier<L2> id) {
70         final InterfaceKey parentInterfacekey = id.firstKeyOf(Interface.class);
71         final SubInterfaceKey subInterfacekey = id.firstKeyOf(SubInterface.class);
72         return SubInterfaceUtils
73                 .getSubInterfaceName(parentInterfacekey.getName(), subInterfacekey.getIdentifier().intValue());
74     }
75
76     @Override
77     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<L2> id, @Nonnull final L2 dataBefore,
78                                         @Nonnull final L2 dataAfter, @Nonnull final WriteContext writeContext)
79             throws WriteFailedException {
80
81         final String subInterfaceName = getSubInterfaceName(id);
82         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
83         // TODO handle update properly (if possible)
84         setL2(id, subInterfaceIndex, subInterfaceName, dataAfter, writeContext);
85     }
86
87     @Override
88     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<L2> id, @Nonnull final L2 dataBefore,
89                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
90         final String subInterfaceName = getSubInterfaceName(id);
91         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
92         deleteL2(id, subInterfaceIndex, subInterfaceName, dataBefore, writeContext);
93     }
94
95     private void setL2(final InstanceIdentifier<L2> id, final int swIfIndex, final String ifcName, final L2 l2,
96                        final WriteContext writeContext)
97             throws WriteFailedException {
98         LOG.debug("Setting L2 for sub-interface: {}", ifcName);
99         icWriterUtils.setInterconnection(id, swIfIndex, ifcName, l2.getInterconnection(), writeContext);
100     }
101
102     private void deleteL2(final InstanceIdentifier<L2> id, final int swIfIndex, final String ifcName, final L2 l2Before,
103                        final WriteContext writeContext)
104             throws WriteFailedException {
105         LOG.debug("Deleting L2 for sub-interface: {}", ifcName);
106         icWriterUtils.deleteInterconnection(id, swIfIndex, ifcName, l2Before.getInterconnection(), writeContext);
107     }
108 }