summaryrefslogtreecommitdiffstats
path: root/fib-management/fib-management-impl/src/main/java/io/fd/hc2vpp/fib/management/services/FibTableServiceImpl.java
blob: 01d264189f14754a3f5502fb39f3ff2947cc1b2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (c) 2018 Bell Canada, Pantheon Technologies and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.fd.hc2vpp.fib.management.services;

import static io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor.NO_PARAMS;

import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer;
import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer;
import io.fd.hc2vpp.fib.management.FibManagementIIds;
import io.fd.hc2vpp.fib.management.request.FibTableRequest;
import io.fd.honeycomb.translate.ModificationCache;
import io.fd.honeycomb.translate.read.ReadFailedException;
import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
import io.fd.honeycomb.translate.write.WriteFailedException;
import io.fd.vpp.jvpp.core.dto.Ip6FibDetailsReplyDump;
import io.fd.vpp.jvpp.core.dto.Ip6FibDump;
import io.fd.vpp.jvpp.core.dto.IpFibDetailsReplyDump;
import io.fd.vpp.jvpp.core.dto.IpFibDump;
import io.fd.vpp.jvpp.core.future.FutureJVppCore;
import java.util.Collections;
import java.util.stream.Stream;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// Todo HC2VPP-317: FibTableService was created as a temporary workaround to write Fib tables in VPP.
// We need to implement proper support for Fib table management.
public class FibTableServiceImpl extends FutureJVppCustomizer implements FibTableService, JvppReplyConsumer {

    private static final Logger LOG = LoggerFactory.getLogger(FibTableServiceImpl.class);
    private final DumpCacheManager<IpFibDetailsReplyDump, Void> v4DumpManager;
    private final DumpCacheManager<Ip6FibDetailsReplyDump, Void> v6DumpManager;

    public FibTableServiceImpl(@Nonnull FutureJVppCore futureJVppCore) {
        super(futureJVppCore);
        v4DumpManager = new DumpCacheManager.DumpCacheManagerBuilder<IpFibDetailsReplyDump, Void>()
                .acceptOnly(IpFibDetailsReplyDump.class)
                .withExecutor((identifier, params) -> getReplyForRead(
                        futureJVppCore.ipFibDump(new IpFibDump()).toCompletableFuture(), identifier))
                .build();
        v6DumpManager = new DumpCacheManager.DumpCacheManagerBuilder<Ip6FibDetailsReplyDump, Void>()
                .acceptOnly(Ip6FibDetailsReplyDump.class)
                .withExecutor((identifier, params) -> getReplyForRead(
                        futureJVppCore.ip6FibDump(new Ip6FibDump()).toCompletableFuture(), identifier))
                .build();
    }

    @Override
    public void write(InstanceIdentifier<?> identifier, @Nonnegative int tableId, @Nonnull String tableName,
                      boolean isIpv6) throws WriteFailedException {
        // Register FIB table in VPP
        FibTableRequest fibTableRequest = new FibTableRequest(getFutureJVpp());
        fibTableRequest.setFibName(tableName);
        fibTableRequest.setIpv6(isIpv6);
        fibTableRequest.setFibTable(tableId);
        fibTableRequest.checkValid();
        try {
            fibTableRequest.write(identifier);
            LOG.debug("Fib table written successfully. table-name: {}, table-id: {}, request: {}", tableName, tableId,
                    fibTableRequest);
        } catch (WriteFailedException e) {
            LOG.warn("Fib table write failed. request: {}", fibTableRequest);
            throw new WriteFailedException(identifier, "Failed to write FIB table to VPP.", e);
        }
    }

    @Override
    public void checkTableExist(@Nonnegative final int index, @Nonnull final ModificationCache cache)
            throws ReadFailedException, FibTableService.FibTableDoesNotExistException {

        if (Stream.concat(dumpV4FibTableIdsStream(cache), dumpV6FibTableIdsStream(cache))
                .noneMatch(id -> id == index)) {
            throw new FibTableService.FibTableDoesNotExistException(index);
        }
    }

    private Stream<Integer> dumpV6FibTableIdsStream(final ModificationCache cache) throws ReadFailedException {
        return v6DumpManager.getDump(FibManagementIIds.FIB_MNGMNT, cache, NO_PARAMS)
                .toJavaUtil()
                .map(ip6FibDetailsReplyDump -> ip6FibDetailsReplyDump.ip6FibDetails)
                .orElse(Collections.emptyList())
                .stream()
                .map(ip6FibDetails -> ip6FibDetails.tableId);
    }

    private Stream<Integer> dumpV4FibTableIdsStream(final ModificationCache cache) throws ReadFailedException {
        return v4DumpManager.getDump(FibManagementIIds.FIB_MNGMNT, cache, NO_PARAMS)
                .toJavaUtil()
                .map(ipFibDetailsReplyDump -> ipFibDetailsReplyDump.ipFibDetails)
                .orElse(Collections.emptyList())
                .stream()
                .map(ipFibDetails -> ipFibDetails.tableId);
    }
}