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