2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.translate.v3po.interfaces.ip;
19 import static com.google.common.base.Preconditions.checkNotNull;
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;
45 * Customizer for writing {@link Address}
47 public class Ipv4AddressCustomizer extends FutureJVppCustomizer
48 implements ListWriterCustomizer<Address, AddressKey>, Ipv4Writer {
50 private static final Logger LOG = LoggerFactory.getLogger(Ipv4AddressCustomizer.class);
51 private final NamingContext interfaceContext;
52 private final SubnetValidator subnetValidator;
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");
61 public Ipv4AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
62 @Nonnull final NamingContext interfaceContext) {
63 this(futureJVppCore, interfaceContext, new SubnetValidator());
67 public void writeCurrentAttributes(InstanceIdentifier<Address> id, Address dataAfter, WriteContext writeContext)
68 throws WriteFailedException {
70 final String interfaceName = id.firstKeyOf(Interface.class).getName();
71 final int interfaceIndex = interfaceContext.getIndex(interfaceName, writeContext.getMappingContext());
73 // checks whether address is not from same subnet of some address already defined on this interface
76 final InstanceIdentifier<Ipv4> parentId = RWUtils.cutId(id, InstanceIdentifier.create(Ipv4.class));
77 final Optional<Ipv4> ipv4Optional = writeContext.readAfter(parentId);
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
82 subnetValidator.checkNotAddingToSameSubnet(ipv4Optional.get().getAddress());
83 } catch (SubnetValidationException e) {
84 throw new WriteFailedException(id, e);
87 setAddress(true, id, interfaceName, interfaceIndex, dataAfter, writeContext);
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"));
98 public void deleteCurrentAttributes(InstanceIdentifier<Address> id, Address dataBefore, WriteContext writeContext)
99 throws WriteFailedException {
101 final String interfaceName = id.firstKeyOf(Interface.class).getName();
102 final int interfaceIndex = interfaceContext.getIndex(interfaceName, writeContext.getMappingContext());
104 setAddress(false, id, interfaceName, interfaceIndex, dataBefore, writeContext);
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 {
111 Subnet subnet = address.getSubnet();
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);
118 LOG.error("Unable to handle subnet of type {}", subnet.getClass());
119 throw new WriteFailedException(id, "Unable to handle subnet of type " + subnet.getClass());
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 {
128 LOG.debug("Setting Subnet(subnet-mask) for interface: {}(id={}). Subnet: {}, address: {}",
129 interfaceName, interfaceIndex, subnet, address);
131 final DottedQuad netmask = subnet.getNetmask();
132 checkNotNull(netmask, "netmask value should not be null");
134 final byte subnetLength = getSubnetMaskLength(netmask.getValue());
135 addDelAddress(getFutureJVpp(), add, id, interfaceIndex, address.getIp(), subnetLength);
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);
145 addDelAddress(getFutureJVpp(), add, id, interfaceIndex, address.getIp(),
146 subnet.getPrefixLength().byteValue());
148 LOG.debug("Subnet(prefix-length) set successfully for interface: {}(id={}). Subnet: {}, address: {}",
149 interfaceName, interfaceIndex, subnet, address);