3d41b899f7229bf293af8cf69614684b38ab45b7
[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.ip;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20 import static io.fd.honeycomb.v3po.translate.v3po.interfaces.ip.Ipv4WriteUtils.addDelAddress;
21 import static io.fd.honeycomb.v3po.translate.v3po.interfaces.ip.Ipv4WriteUtils.getSubnetMaskLength;
22
23 import io.fd.honeycomb.v3po.translate.spi.write.ListWriterCustomizer;
24 import io.fd.honeycomb.v3po.translate.v3po.util.FutureJVppCustomizer;
25 import io.fd.honeycomb.v3po.translate.v3po.util.NamingContext;
26 import io.fd.honeycomb.v3po.translate.v3po.util.SubInterfaceUtils;
27 import io.fd.honeycomb.v3po.translate.write.WriteContext;
28 import io.fd.honeycomb.v3po.translate.write.WriteFailedException;
29 import javax.annotation.Nonnull;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.InterfaceKey;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DottedQuad;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterface;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces._interface.sub.interfaces.SubInterfaceKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.Address;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.AddressKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.address.Subnet;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.address.subnet.Netmask;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLength;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.openvpp.jvpp.VppBaseCallException;
42 import org.openvpp.jvpp.future.FutureJVpp;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * Write customizer for sub-interface {@link Address}
48  */
49 public class SubInterfaceIpv4AddressCustomizer extends FutureJVppCustomizer
50     implements ListWriterCustomizer<Address, AddressKey> {
51
52     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceIpv4AddressCustomizer.class);
53     private final NamingContext interfaceContext;
54
55     public SubInterfaceIpv4AddressCustomizer(@Nonnull final FutureJVpp futureJvpp,
56                                              @Nonnull final NamingContext interfaceContext) {
57         super(futureJvpp);
58         this.interfaceContext = checkNotNull(interfaceContext, "interface context should not be null");
59     }
60
61     @Override
62     public void writeCurrentAttributes(InstanceIdentifier<Address> id, Address dataAfter, WriteContext writeContext)
63         throws WriteFailedException {
64         setAddress(true, id, dataAfter, writeContext);
65     }
66
67     @Override
68     public void updateCurrentAttributes(InstanceIdentifier<Address> id, Address dataBefore, Address dataAfter,
69                                         WriteContext writeContext) throws WriteFailedException {
70         throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
71             new UnsupportedOperationException("Operation not supported"));
72     }
73
74     @Override
75     public void deleteCurrentAttributes(InstanceIdentifier<Address> id, Address dataBefore, WriteContext writeContext)
76         throws WriteFailedException {
77         setAddress(false, id, dataBefore, writeContext);
78     }
79
80     private void setAddress(boolean add, final InstanceIdentifier<Address> id, final Address address,
81                             final WriteContext writeContext) throws WriteFailedException {
82
83         final String interfaceName = id.firstKeyOf(Interface.class).getName();
84         final String subInterfaceName = getSubInterfaceName(id);
85         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
86
87         Subnet subnet = address.getSubnet();
88
89         if (subnet instanceof PrefixLength) {
90             setPrefixLengthSubnet(add, id, interfaceName, subInterfaceIndex, address, (PrefixLength) subnet);
91         } else if (subnet instanceof Netmask) {
92             setNetmaskSubnet(add, id, interfaceName, subInterfaceIndex, address, (Netmask) subnet);
93         } else {
94             // FIXME how does choice extensibility work
95             // FIXME it is not even possible to create a dedicated
96             // customizer for Interconnection, since it's not a DataObject
97             // FIXME we might need a choice customizer
98             // THis choice is already from augment, so its probably not
99             // possible to augment augmented choice
100             LOG.error("Unable to handle subnet of type {}", subnet.getClass());
101             throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass());
102         }
103     }
104
105     private String getSubInterfaceName(@Nonnull final InstanceIdentifier<Address> id) {
106         final InterfaceKey parentInterfacekey = id.firstKeyOf(Interface.class);
107         final SubInterfaceKey subInterfacekey = id.firstKeyOf(SubInterface.class);
108         return SubInterfaceUtils
109             .getSubInterfaceName(parentInterfacekey.getName(), subInterfacekey.getIdentifier().intValue());
110     }
111
112     private void setNetmaskSubnet(final boolean add, @Nonnull final InstanceIdentifier<Address> id,
113                                   @Nonnull final String subInterfaceName, final int subInterfaceIndex,
114                                   @Nonnull final Address address, @Nonnull final Netmask subnet)
115         throws WriteFailedException {
116         try {
117             LOG.debug("Setting Subnet(subnet-mask) for sub-interface: {}(id={}). Subnet: {}, address: {}",
118                 subInterfaceName, subInterfaceIndex, subnet, address);
119
120             final DottedQuad netmask = subnet.getNetmask();
121             checkNotNull(netmask, "netmask value should not be null");
122
123             final byte subnetLength = getSubnetMaskLength(netmask.getValue());
124             addDelAddress(getFutureJVpp(), add, id, subInterfaceIndex, address.getIp(), subnetLength);
125
126         } catch (VppBaseCallException e) {
127             LOG.warn("Failed to set Subnet(subnet-mask) for sub-interface: {}(id={}). Subnet: {}, address: {}",
128                 subInterfaceName, subInterfaceIndex, subnet, address);
129             throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass(), e);
130         }
131     }
132
133     private void setPrefixLengthSubnet(final boolean add, @Nonnull final InstanceIdentifier<Address> id,
134                                        @Nonnull final String subInterfaceName, final int subInterfaceIndex,
135                                        @Nonnull final Address address, @Nonnull final PrefixLength subnet)
136         throws WriteFailedException {
137         try {
138             LOG.debug("Setting Subnet(prefix-length) for sub-interface: {}(id={}). Subnet: {}, address: {}",
139                 subInterfaceName, subInterfaceIndex, subnet, address);
140
141             addDelAddress(getFutureJVpp(), add, id, subInterfaceIndex, address.getIp(),
142                 subnet.getPrefixLength().byteValue());
143
144             LOG.debug("Subnet(prefix-length) set successfully for sub-interface: {}(id={}). Subnet: {}, address: {}",
145                 subInterfaceName, subInterfaceIndex, subnet, address);
146         } catch (VppBaseCallException e) {
147             LOG.warn("Failed to set Subnet(prefix-length) for sub-interface: {}(id={}). Subnet: {}, address: {}",
148                 subInterfaceName, subInterfaceIndex, subnet, address);
149             throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass(), e);
150         }
151     }
152 }