c621612c704b5ac5a423e1cb85c518cd14c63295
[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.VppBaseCallException;
35 import org.openvpp.jvpp.future.FutureJVpp;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Customizer for writing vlan sub interface l2 configuration
41  */
42 public class SubInterfaceL2Customizer extends FutureJVppCustomizer implements ChildWriterCustomizer<L2> {
43
44     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceL2Customizer.class);
45     private final NamingContext interfaceContext;
46     private final InterconnectionWriteUtils icWriterUtils;
47
48     public SubInterfaceL2Customizer(final FutureJVpp vppApi, final NamingContext interfaceContext,
49                                     final NamingContext bridgeDomainContext) {
50         super(vppApi);
51         this.interfaceContext = interfaceContext;
52         this.icWriterUtils = new InterconnectionWriteUtils(vppApi, interfaceContext, bridgeDomainContext);
53     }
54
55     @Nonnull
56     @Override
57     public Optional<L2> extract(@Nonnull final InstanceIdentifier<L2> currentId, @Nonnull final DataObject parentData) {
58         return Optional.fromNullable(((SubInterface) parentData).getL2());
59     }
60
61     @Override
62     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<L2> id, @Nonnull final L2 dataAfter,
63                                        @Nonnull final WriteContext writeContext)
64             throws WriteFailedException {
65         final String subInterfaceName = getSubInterfaceName(id);
66         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
67         setL2(id, subInterfaceIndex, subInterfaceName, dataAfter, writeContext);
68     }
69
70     private String getSubInterfaceName(@Nonnull final InstanceIdentifier<L2> id) {
71         final InterfaceKey parentInterfacekey = id.firstKeyOf(Interface.class);
72         final SubInterfaceKey subInterfacekey = id.firstKeyOf(SubInterface.class);
73         return SubInterfaceUtils
74                 .getSubInterfaceName(parentInterfacekey.getName(), subInterfacekey.getIdentifier().intValue());
75     }
76
77     @Override
78     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<L2> id, @Nonnull final L2 dataBefore,
79                                         @Nonnull final L2 dataAfter, @Nonnull final WriteContext writeContext)
80             throws WriteFailedException {
81
82         final String subInterfaceName = getSubInterfaceName(id);
83         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
84         // TODO handle update properly (if possible)
85         setL2(id, subInterfaceIndex, subInterfaceName, dataAfter, writeContext);
86     }
87
88     @Override
89     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<L2> id, @Nonnull final L2 dataBefore,
90                                         @Nonnull final WriteContext writeContext) {
91         // TODO implement delete (if possible)
92     }
93
94     private void setL2(final InstanceIdentifier<L2> id, final int swIfIndex, final String ifcName, final L2 l2,
95                        final WriteContext writeContext)
96             throws WriteFailedException {
97         LOG.debug("Setting L2 for sub-interface: {}", ifcName);
98         icWriterUtils.setInterconnection(id, swIfIndex, ifcName, l2.getInterconnection(), writeContext);
99     }
100 }