VPP-378: update jvpp package names
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfaces / ip / Ipv4AddressCustomizer.java
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 com.google.common.base.Optional;
22 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
23 import io.fd.honeycomb.translate.util.RWUtils;
24 import io.fd.honeycomb.translate.v3po.interfaces.ip.subnet.validation.SubnetValidationException;
25 import io.fd.honeycomb.translate.v3po.interfaces.ip.subnet.validation.SubnetValidator;
26 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
27 import io.fd.honeycomb.translate.vpp.util.NamingContext;
28 import io.fd.honeycomb.translate.write.WriteContext;
29 import io.fd.honeycomb.translate.write.WriteFailedException;
30 import javax.annotation.Nonnull;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.Ipv4;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.Address;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.AddressKey;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.Subnet;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.subnet.Netmask;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.ip.rev140616.interfaces._interface.ipv4.address.subnet.PrefixLength;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DottedQuad;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
40 import io.fd.vpp.jvpp.VppBaseCallException;
41 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * Customizer for writing {@link Address}
47  */
48 public class Ipv4AddressCustomizer extends FutureJVppCustomizer
49         implements ListWriterCustomizer<Address, AddressKey>, Ipv4Writer {
50
51     private static final Logger LOG = LoggerFactory.getLogger(Ipv4AddressCustomizer.class);
52     private final NamingContext interfaceContext;
53     private final SubnetValidator subnetValidator;
54
55     Ipv4AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore, @Nonnull final NamingContext interfaceContext,
56                           @Nonnull final SubnetValidator subnetValidator) {
57         super(futureJVppCore);
58         this.interfaceContext = checkNotNull(interfaceContext, "Interface context cannot be null");
59         this.subnetValidator = checkNotNull(subnetValidator, "Subnet validator cannot be null");
60     }
61
62     public Ipv4AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
63                                  @Nonnull final NamingContext interfaceContext) {
64         this(futureJVppCore, interfaceContext, new SubnetValidator());
65     }
66
67     @Override
68     public void writeCurrentAttributes(InstanceIdentifier<Address> id, Address dataAfter, WriteContext writeContext)
69             throws WriteFailedException {
70
71         final String interfaceName = id.firstKeyOf(Interface.class).getName();
72         final int interfaceIndex = interfaceContext.getIndex(interfaceName, writeContext.getMappingContext());
73
74         // checks whether address is not from same subnet of some address already defined on this interface
75         try {
76
77             final InstanceIdentifier<Ipv4> parentId = RWUtils.cutId(id, InstanceIdentifier.create(Ipv4.class));
78             final Optional<Ipv4> ipv4Optional = writeContext.readAfter(parentId);
79
80             //no need to check isPresent() - we are inside of address customizer, therefore there must be Address data
81             //that is being processed by infrastructure
82
83             subnetValidator.checkNotAddingToSameSubnet(ipv4Optional.get().getAddress());
84         } catch (SubnetValidationException e) {
85             throw new WriteFailedException(id, e);
86         }
87
88         setAddress(true, id, interfaceName, interfaceIndex, dataAfter, writeContext);
89     }
90
91     @Override
92     public void updateCurrentAttributes(InstanceIdentifier<Address> id, Address dataBefore, Address dataAfter,
93                                         WriteContext writeContext) throws WriteFailedException {
94         throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter,
95                 new UnsupportedOperationException("Operation not supported"));
96     }
97
98     @Override
99     public void deleteCurrentAttributes(InstanceIdentifier<Address> id, Address dataBefore, WriteContext writeContext)
100             throws WriteFailedException {
101
102         final String interfaceName = id.firstKeyOf(Interface.class).getName();
103         final int interfaceIndex = interfaceContext.getIndex(interfaceName, writeContext.getMappingContext());
104
105         setAddress(false, id, interfaceName, interfaceIndex, dataBefore, writeContext);
106     }
107
108     private void setAddress(boolean add, final InstanceIdentifier<Address> id, final String interfaceName,
109                             final int interfaceIndex, final Address address,
110                             final WriteContext writeContext) throws WriteFailedException {
111
112         Subnet subnet = address.getSubnet();
113
114         if (subnet instanceof PrefixLength) {
115             setPrefixLengthSubnet(add, id, interfaceName, interfaceIndex, address, (PrefixLength) subnet);
116         } else if (subnet instanceof Netmask) {
117             setNetmaskSubnet(add, id, interfaceName, interfaceIndex, address, (Netmask) subnet);
118         } else {
119             LOG.error("Unable to handle subnet of type {}", subnet.getClass());
120             throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass());
121         }
122     }
123
124     private void setNetmaskSubnet(final boolean add, @Nonnull final InstanceIdentifier<Address> id,
125                                   @Nonnull final String interfaceName, final int interfaceIndex,
126                                   @Nonnull final Address address, @Nonnull final Netmask subnet)
127             throws WriteFailedException {
128         try {
129             LOG.debug("Setting Subnet(subnet-mask) for interface: {}(id={}). Subnet: {}, address: {}",
130                     interfaceName, interfaceIndex, subnet, address);
131
132             final DottedQuad netmask = subnet.getNetmask();
133             checkNotNull(netmask, "netmask value should not be null");
134
135             final byte subnetLength = getSubnetMaskLength(netmask.getValue());
136             addDelAddress(getFutureJVpp(), add, id, interfaceIndex, address.getIp(), subnetLength);
137         } catch (VppBaseCallException e) {
138             LOG.warn("Failed to set Subnet(subnet-mask) for interface: {}(id={}). Subnet: {}, address: {}",
139                     interfaceName, interfaceIndex, subnet, address);
140             throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass(), e);
141         }
142     }
143
144     private void setPrefixLengthSubnet(final boolean add, @Nonnull final InstanceIdentifier<Address> id,
145                                        @Nonnull final String interfaceName, final int interfaceIndex,
146                                        @Nonnull final Address address, @Nonnull final PrefixLength subnet)
147             throws WriteFailedException {
148         try {
149             LOG.debug("Setting Subnet(prefix-length) for interface: {}(id={}). Subnet: {}, address: {}",
150                     interfaceName, interfaceIndex, subnet, address);
151
152             addDelAddress(getFutureJVpp(), add, id, interfaceIndex, address.getIp(),
153                     subnet.getPrefixLength().byteValue());
154
155             LOG.debug("Subnet(prefix-length) set successfully for interface: {}(id={}). Subnet: {}, address: {}",
156                     interfaceName, interfaceIndex, subnet, address);
157         } catch (VppBaseCallException e) {
158             LOG.warn("Failed to set Subnet(prefix-length) for interface: {}(id={}). Subnet: {}, address: {}",
159                     interfaceName, interfaceIndex, subnet, address);
160             throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass(), e);
161         }
162     }
163 }