2342708836322f217e71359d036cf24eb82d23b1
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfacesstate / ip / SubInterfaceIpv4AddressCustomizer.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.interfacesstate.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.read.ReadContext;
23 import io.fd.honeycomb.translate.read.ReadFailedException;
24 import io.fd.honeycomb.translate.spi.read.ListReaderCustomizer;
25 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
26 import io.fd.honeycomb.translate.v3po.interfacesstate.ip.dump.AddressDumpExecutor;
27 import io.fd.honeycomb.translate.v3po.interfacesstate.ip.dump.params.AddressDumpParams;
28 import io.fd.honeycomb.translate.vpp.util.FutureJVppCustomizer;
29 import io.fd.honeycomb.translate.vpp.util.NamingContext;
30 import io.fd.honeycomb.translate.vpp.util.SubInterfaceUtils;
31 import io.fd.vpp.jvpp.core.dto.IpAddressDetails;
32 import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump;
33 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
34 import java.util.List;
35 import javax.annotation.Nonnull;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.interfaces.state._interface.sub.interfaces.SubInterface;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.Ipv4Builder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.Address;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.AddressBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.AddressKey;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.vpp.vlan.rev150527.sub._interface.ip4.attributes.ipv4.address.subnet.PrefixLengthBuilder;
43 import org.opendaylight.yangtools.concepts.Builder;
44 import org.opendaylight.yangtools.yang.binding.DataObject;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * Read customizer for sub-interface Ipv4 addresses.
51  */
52 public class SubInterfaceIpv4AddressCustomizer extends FutureJVppCustomizer
53         implements ListReaderCustomizer<Address, AddressKey, AddressBuilder>, Ipv4Reader {
54
55     private static final Logger LOG = LoggerFactory.getLogger(SubInterfaceIpv4AddressCustomizer.class);
56     private static final String CACHE_KEY = SubInterfaceIpv4AddressCustomizer.class.getName();
57
58     private final NamingContext interfaceContext;
59     private final DumpCacheManager<IpAddressDetailsReplyDump, AddressDumpParams> dumpManager;
60
61     public SubInterfaceIpv4AddressCustomizer(@Nonnull final FutureJVppCore futureJVppCore,
62                                              @Nonnull final NamingContext interfaceContext) {
63         super(futureJVppCore);
64         this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null");
65         this.dumpManager = new DumpCacheManager.DumpCacheManagerBuilder<IpAddressDetailsReplyDump, AddressDumpParams>()
66                 .withExecutor(new AddressDumpExecutor(futureJVppCore))
67                 .build();
68     }
69
70     @Override
71     @Nonnull
72     public AddressBuilder getBuilder(@Nonnull InstanceIdentifier<Address> id) {
73         return new AddressBuilder();
74     }
75
76     @Override
77     public void readCurrentAttributes(@Nonnull InstanceIdentifier<Address> id, @Nonnull AddressBuilder builder,
78                                       @Nonnull ReadContext ctx)
79             throws ReadFailedException {
80         LOG.debug("Reading attributes for sub-interface address: {}", id);
81
82         final String subInterfaceName = getSubInterfaceName(id);
83         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, ctx.getMappingContext());
84         final Optional<IpAddressDetailsReplyDump> dumpOptional = dumpManager
85                 .getDump(id, CACHE_KEY, ctx.getModificationCache(), new AddressDumpParams(subInterfaceIndex, false));
86
87         final Optional<IpAddressDetails> ipAddressDetails =
88                 findIpAddressDetailsByIp(dumpOptional, id.firstKeyOf(Address.class).getIp());
89
90         if (ipAddressDetails.isPresent()) {
91             final IpAddressDetails detail = ipAddressDetails.get();
92             builder.setIp(arrayToIpv4AddressNoZone(detail.ip));
93             builder.setSubnet(new PrefixLengthBuilder().setPrefixLength(Short.valueOf(detail.prefixLength)).build());
94
95             if (LOG.isDebugEnabled()) {
96                 LOG.debug("Attributes for {} sub-interface (id={}) address {} successfully read: {}",
97                         subInterfaceName, subInterfaceIndex, id, builder.build());
98             }
99         }
100     }
101
102     @Override
103     public List<AddressKey> getAllIds(@Nonnull InstanceIdentifier<Address> id, @Nonnull ReadContext ctx)
104             throws ReadFailedException {
105         LOG.debug("Reading list of keys for sub-interface addresses: {}", id);
106
107         final String subInterfaceName = getSubInterfaceName(id);
108         final int subInterfaceIndex = interfaceContext.getIndex(subInterfaceName, ctx.getMappingContext());
109         final Optional<IpAddressDetailsReplyDump> dumpOptional = dumpManager
110                 .getDump(id, CACHE_KEY, ctx.getModificationCache(), new AddressDumpParams(subInterfaceIndex, false));
111
112         return getAllIpv4AddressIds(dumpOptional, AddressKey::new);
113     }
114
115     @Override
116     public void merge(@Nonnull Builder<? extends DataObject> builder, @Nonnull List<Address> readData) {
117         ((Ipv4Builder) builder).setAddress(readData);
118     }
119
120     private static String getSubInterfaceName(@Nonnull final InstanceIdentifier<Address> id) {
121         return SubInterfaceUtils.getSubInterfaceName(id.firstKeyOf(Interface.class).getName(),
122                 Math.toIntExact(id.firstKeyOf(SubInterface.class).getIdentifier()));
123     }
124 }