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