a5d13bb98fb99f0e10104ad7abe4155cb7f33d51
[honeycomb.git] / v3po / v3po2vpp / src / main / java / io / fd / honeycomb / translate / v3po / interfacesstate / ip / Ipv4Reader.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.util.RWUtils;
23 import io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor;
24 import io.fd.honeycomb.translate.v3po.interfacesstate.ip.dump.params.AddressDumpParams;
25 import io.fd.honeycomb.translate.vpp.util.Ipv4Translator;
26 import io.fd.honeycomb.translate.vpp.util.JvppReplyConsumer;
27 import io.fd.vpp.jvpp.core.dto.IpAddressDump;
28 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.function.Function;
32 import java.util.stream.Collectors;
33 import javax.annotation.Nonnull;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
35 import org.opendaylight.yangtools.yang.binding.Identifier;
36 import io.fd.vpp.jvpp.core.dto.IpAddressDetails;
37 import io.fd.vpp.jvpp.core.dto.IpAddressDetailsReplyDump;
38
39 /**
40  * Utility class providing Ipv4 read support.
41  */
42 interface Ipv4Reader extends Ipv4Translator, JvppReplyConsumer {
43
44     @Nonnull
45     default <T extends Identifier> List<T> getAllIpv4AddressIds(
46             final Optional<IpAddressDetailsReplyDump> dumpOptional,
47             @Nonnull final Function<Ipv4AddressNoZone, T> keyConstructor) {
48         if (dumpOptional.isPresent() && dumpOptional.get().ipAddressDetails != null) {
49             return dumpOptional.get().ipAddressDetails.stream()
50                     .map(detail -> keyConstructor.apply(arrayToIpv4AddressNoZone(detail.ip)))
51                     .collect(Collectors.toList());
52         } else {
53             return Collections.emptyList();
54         }
55     }
56
57     default Optional<IpAddressDetails> findIpAddressDetailsByIp(
58             final Optional<IpAddressDetailsReplyDump> dump,
59             @Nonnull final Ipv4AddressNoZone ip) {
60         checkNotNull(ip, "ip address should not be null");
61
62         if (dump.isPresent() && dump.get().ipAddressDetails != null) {
63             final List<IpAddressDetails> details = dump.get().ipAddressDetails;
64
65             return Optional.of(details.stream()
66                     .filter(singleDetail -> ip.equals(arrayToIpv4AddressNoZone(singleDetail.ip)))
67                     .collect(RWUtils.singleItemCollector()));
68         }
69         return Optional.absent();
70     }
71
72     default EntityDumpExecutor<IpAddressDetailsReplyDump, AddressDumpParams> createExecutor(
73             @Nonnull final FutureJVppCore vppApi) {
74         return (identifier, params) -> {
75             checkNotNull(params, "Address dump params cannot be null");
76
77             final IpAddressDump dumpRequest = new IpAddressDump();
78             dumpRequest.isIpv6 = booleanToByte(params.isIpv6());
79             dumpRequest.swIfIndex = params.getInterfaceIndex();
80
81             return getReplyForRead(vppApi.ipAddressDump(dumpRequest).toCompletableFuture(), identifier);
82         };
83     }
84 }