56ad73c9b61c2ae55f1d44005ee2e83f23b02274
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfacesstate / ip / Ipv4ReadUtils.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.util.RWUtils;
24 import io.fd.honeycomb.translate.ModificationCache;
25 import io.fd.honeycomb.translate.read.ReadFailedException;
26 import io.fd.honeycomb.translate.v3po.util.ReadTimeoutException;
27 import io.fd.honeycomb.translate.v3po.util.TranslateUtils;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.function.Function;
31 import java.util.stream.Collectors;
32 import javax.annotation.Nonnull;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
34 import org.opendaylight.yangtools.yang.binding.Identifier;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.openvpp.jvpp.VppBaseCallException;
37 import org.openvpp.jvpp.dto.IpAddressDetails;
38 import org.openvpp.jvpp.dto.IpAddressDetailsReplyDump;
39 import org.openvpp.jvpp.dto.IpAddressDump;
40 import org.openvpp.jvpp.future.FutureJVpp;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * Utility class providing Ipv4 read support.
46  */
47 final class Ipv4ReadUtils {
48
49     static final String CACHE_KEY = Ipv4ReadUtils.class.getName();
50     private static final Logger LOG = LoggerFactory.getLogger(Ipv4ReadUtils.class);
51
52     private Ipv4ReadUtils() {
53         throw new UnsupportedOperationException("This utility class cannot be instantiated");
54     }
55
56     // Many VPP APIs do not provide get operation for single item. Dump requests for all items are used instead.
57     // To improve HC performance, caching dump requests is a common pattern.
58     // TODO: use more generic caching implementation, once provided
59     static Optional<IpAddressDetailsReplyDump> dumpAddresses(@Nonnull final FutureJVpp futureJvpp,
60                                                              @Nonnull final InstanceIdentifier<?> id,
61                                                              @Nonnull final String interfaceName,
62                                                              final int interfaceIndex, @Nonnull final ReadContext ctx)
63         throws ReadFailedException {
64
65         final String cacheKey = CACHE_KEY + interfaceName;
66         Optional<IpAddressDetailsReplyDump> dumpFromCache = dumpAddressFromCache(cacheKey, ctx.getModificationCache());
67
68         if (dumpFromCache.isPresent()) {
69             return dumpFromCache;
70         }
71
72         Optional<IpAddressDetailsReplyDump> dumpFromOperational;
73         try {
74             dumpFromOperational = dumpAddressFromOperationalData(futureJvpp, id, interfaceIndex);
75         } catch (VppBaseCallException e) {
76             throw new ReadFailedException(id, e);
77         }
78
79         if (dumpFromOperational.isPresent()) {
80             ctx.getModificationCache().put(cacheKey, dumpFromOperational.get());
81         }
82
83         return dumpFromOperational;
84     }
85
86     private static Optional<IpAddressDetailsReplyDump> dumpAddressFromCache(@Nonnull final String cacheKey,
87                                                                             @Nonnull final ModificationCache cache) {
88         LOG.debug("Retrieving Ipv4 addresses from cache for {}", cacheKey);
89         return Optional.fromNullable((IpAddressDetailsReplyDump) cache.get(cacheKey));
90     }
91
92     private static Optional<IpAddressDetailsReplyDump> dumpAddressFromOperationalData(
93         @Nonnull final FutureJVpp futureJvpp, @Nonnull final InstanceIdentifier<?> id, final int interfaceIndex)
94         throws VppBaseCallException, ReadTimeoutException {
95         LOG.debug("Dumping Ipv4 addresses for interface id={}", interfaceIndex);
96         final IpAddressDump dumpRequest = new IpAddressDump();
97         dumpRequest.isIpv6 = 0;
98         dumpRequest.swIfIndex = interfaceIndex;
99         return Optional.fromNullable(
100             TranslateUtils.getReplyForRead(futureJvpp.ipAddressDump(dumpRequest).toCompletableFuture(), id));
101     }
102
103     @Nonnull static <T extends Identifier> List<T> getAllIpv4AddressIds(
104         final Optional<IpAddressDetailsReplyDump> dumpOptional,
105         @Nonnull final Function<Ipv4AddressNoZone, T> keyConstructor) {
106         if (dumpOptional.isPresent() && dumpOptional.get().ipAddressDetails != null) {
107             return dumpOptional.get().ipAddressDetails.stream()
108                 .map(detail -> keyConstructor.apply(TranslateUtils.arrayToIpv4AddressNoZone(detail.ip)))
109                 .collect(Collectors.toList());
110         } else {
111             return Collections.emptyList();
112         }
113     }
114
115     static Optional<IpAddressDetails> findIpAddressDetailsByIp(
116         final Optional<IpAddressDetailsReplyDump> dump,
117         @Nonnull final Ipv4AddressNoZone ip) {
118         checkNotNull(ip, "ip address should not be null");
119
120         if (dump.isPresent() && dump.get().ipAddressDetails != null) {
121             final List<IpAddressDetails> details = dump.get().ipAddressDetails;
122
123             return Optional.of(details.stream()
124                 .filter(singleDetail -> ip.equals(TranslateUtils.arrayToIpv4AddressNoZone(singleDetail.ip)))
125                 .collect(RWUtils.singleItemCollector()));
126         }
127         return Optional.absent();
128     }
129
130 }