485e180eb40797d39940affb6a8e16fb26632cb9
[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.hc2vpp.l3.write.ipv4.subinterface;
18
19 import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer;
20 import io.fd.hc2vpp.common.translate.util.NamingContext;
21 import io.fd.hc2vpp.l3.utils.ip.write.IpWriter;
22 import io.fd.hc2vpp.v3po.util.SubInterfaceUtils;
23 import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer;
24 import io.fd.honeycomb.translate.write.WriteContext;
25 import io.fd.honeycomb.translate.write.WriteFailedException;
26 import io.fd.jvpp.core.future.FutureJVppCore;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterface;
29 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.interfaces._interface.sub.interfaces.SubInterfaceKey;
30 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.ip4.attributes.ipv4.Address;
31 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.ip4.attributes.ipv4.AddressKey;
32 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.ip4.attributes.ipv4.address.Subnet;
33 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.ip4.attributes.ipv4.address.subnet.Netmask;
34 import org.opendaylight.yang.gen.v1.http.fd.io.hc2vpp.yang.vpp.vlan.rev180319.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLength;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.Interface;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev180220.interfaces.InterfaceKey;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DottedQuad;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 /**
43  * Write customizer for sub-interface {@link Address}
44  */
45 public class SubInterfaceIpv4AddressCustomizer extends FutureJVppCustomizer
46         implements ListWriterCustomizer<Address, AddressKey>, IpWriter {
47
48     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceIpv4AddressCustomizer.class);
49     private final NamingContext interfaceContext;
50
51     public SubInterfaceIpv4AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
52                                              @Nonnull final NamingContext interfaceContext) {
53         super(futureJVppCore);
54         this.interfaceContext = interfaceContext;
55     }
56
57     @Override
58     public void writeCurrentAttributes(InstanceIdentifier<Address> id, Address dataAfter, WriteContext writeContext)
59             throws WriteFailedException {
60         setAddress(true, id, dataAfter, writeContext);
61     }
62
63     @Override
64     public void deleteCurrentAttributes(InstanceIdentifier<Address> id, Address dataBefore, WriteContext writeContext)
65             throws WriteFailedException {
66         setAddress(false, id, dataBefore, writeContext);
67     }
68
69     private void setAddress(boolean add, final InstanceIdentifier<Address> id, final Address address,
70                             final WriteContext writeContext) throws WriteFailedException {
71
72         final String interfaceName = id.firstKeyOf(Interface.class).getName();
73         final String subInterfaceName = getSubInterfaceName(id);
74         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, writeContext.getMappingContext());
75
76         Subnet subnet = address.getSubnet();
77
78         if (subnet instanceof PrefixLength) {
79             setPrefixLengthSubnet(add, id, interfaceName, subInterfaceIndex, address, (PrefixLength) subnet);
80         } else {
81             setNetmaskSubnet(add, id, interfaceName, subInterfaceIndex, address, (Netmask) subnet);
82         }
83     }
84
85     private String getSubInterfaceName(@Nonnull final InstanceIdentifier<Address> id) {
86         final InterfaceKey parentInterfacekey = id.firstKeyOf(Interface.class);
87         final SubInterfaceKey subInterfacekey = id.firstKeyOf(SubInterface.class);
88         return SubInterfaceUtils
89                 .getSubInterfaceName(parentInterfacekey.getName(), subInterfacekey.getIdentifier().intValue());
90     }
91
92     private void setNetmaskSubnet(final boolean add, @Nonnull final InstanceIdentifier<Address> id,
93                                   @Nonnull final String subInterfaceName, final int subInterfaceIndex,
94                                   @Nonnull final Address address, @Nonnull final Netmask subnet)
95             throws WriteFailedException {
96
97         LOG.debug("Setting Subnet(subnet-mask) for sub-interface: {}(id={}). Subnet: {}, address: {}",
98                 subInterfaceName, subInterfaceIndex, subnet, address);
99
100         final DottedQuad netmask = subnet.getNetmask();
101
102         final byte subnetLength = getSubnetMaskLength(netmask.getValue());
103         addDelAddress(getFutureJVpp(), add, id, subInterfaceIndex, address.getIp(), subnetLength);
104     }
105
106     private void setPrefixLengthSubnet(final boolean add, @Nonnull final InstanceIdentifier<Address> id,
107                                        @Nonnull final String subInterfaceName, final int subInterfaceIndex,
108                                        @Nonnull final Address address, @Nonnull final PrefixLength subnet)
109             throws WriteFailedException {
110         LOG.debug("Setting Subnet(prefix-length) for sub-interface: {}(id={}). Subnet: {}, address: {}",
111                 subInterfaceName, subInterfaceIndex, subnet, address);
112
113         addDelAddress(getFutureJVpp(), add, id, subInterfaceIndex, address.getIp(),
114                 subnet.getPrefixLength().byteValue());
115
116         LOG.debug("Subnet(prefix-length) set successfully for sub-interface: {}(id={}). Subnet: {}, address: {}",
117                 subInterfaceName, subInterfaceIndex, subnet, address);
118     }
119 }