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