01d264189f14754a3f5502fb39f3ff2947cc1b2e
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2018 Bell Canada, Pantheon Technologies 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.fib.management.services;
18
19 import static io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor.NO_PARAMS;
20
21 import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer;
22 import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer;
23 import io.fd.hc2vpp.fib.management.FibManagementIIds;
24 import io.fd.hc2vpp.fib.management.request.FibTableRequest;
25 import io.fd.honeycomb.translate.ModificationCache;
26 import io.fd.honeycomb.translate.read.ReadFailedException;
27 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
28 import io.fd.honeycomb.translate.write.WriteFailedException;
29 import io.fd.vpp.jvpp.core.dto.Ip6FibDetailsReplyDump;
30 import io.fd.vpp.jvpp.core.dto.Ip6FibDump;
31 import io.fd.vpp.jvpp.core.dto.IpFibDetailsReplyDump;
32 import io.fd.vpp.jvpp.core.dto.IpFibDump;
33 import io.fd.vpp.jvpp.core.future.FutureJVppCore;
34 import java.util.Collections;
35 import java.util.stream.Stream;
36 import javax.annotation.Nonnegative;
37 import javax.annotation.Nonnull;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 // Todo HC2VPP-317: FibTableService was created as a temporary workaround to write Fib tables in VPP.
43 // We need to implement proper support for Fib table management.
44 public class FibTableServiceImpl extends FutureJVppCustomizer implements FibTableService, JvppReplyConsumer {
45
46     private static final Logger LOG = LoggerFactory.getLogger(FibTableServiceImpl.class);
47     private final DumpCacheManager<IpFibDetailsReplyDump, Void> v4DumpManager;
48     private final DumpCacheManager<Ip6FibDetailsReplyDump, Void> v6DumpManager;
49
50     public FibTableServiceImpl(@Nonnull FutureJVppCore futureJVppCore) {
51         super(futureJVppCore);
52         v4DumpManager = new DumpCacheManager.DumpCacheManagerBuilder<IpFibDetailsReplyDump, Void>()
53                 .acceptOnly(IpFibDetailsReplyDump.class)
54                 .withExecutor((identifier, params) -> getReplyForRead(
55                         futureJVppCore.ipFibDump(new IpFibDump()).toCompletableFuture(), identifier))
56                 .build();
57         v6DumpManager = new DumpCacheManager.DumpCacheManagerBuilder<Ip6FibDetailsReplyDump, Void>()
58                 .acceptOnly(Ip6FibDetailsReplyDump.class)
59                 .withExecutor((identifier, params) -> getReplyForRead(
60                         futureJVppCore.ip6FibDump(new Ip6FibDump()).toCompletableFuture(), identifier))
61                 .build();
62     }
63
64     @Override
65     public void write(InstanceIdentifier<?> identifier, @Nonnegative int tableId, @Nonnull String tableName,
66                       boolean isIpv6) throws WriteFailedException {
67         // Register FIB table in VPP
68         FibTableRequest fibTableRequest = new FibTableRequest(getFutureJVpp());
69         fibTableRequest.setFibName(tableName);
70         fibTableRequest.setIpv6(isIpv6);
71         fibTableRequest.setFibTable(tableId);
72         fibTableRequest.checkValid();
73         try {
74             fibTableRequest.write(identifier);
75             LOG.debug("Fib table written successfully. table-name: {}, table-id: {}, request: {}", tableName, tableId,
76                     fibTableRequest);
77         } catch (WriteFailedException e) {
78             LOG.warn("Fib table write failed. request: {}", fibTableRequest);
79             throw new WriteFailedException(identifier, "Failed to write FIB table to VPP.", e);
80         }
81     }
82
83     @Override
84     public void checkTableExist(@Nonnegative final int index, @Nonnull final ModificationCache cache)
85             throws ReadFailedException, FibTableService.FibTableDoesNotExistException {
86
87         if (Stream.concat(dumpV4FibTableIdsStream(cache), dumpV6FibTableIdsStream(cache))
88                 .noneMatch(id -> id == index)) {
89             throw new FibTableService.FibTableDoesNotExistException(index);
90         }
91     }
92
93     private Stream<Integer> dumpV6FibTableIdsStream(final ModificationCache cache) throws ReadFailedException {
94         return v6DumpManager.getDump(FibManagementIIds.FIB_MNGMNT, cache, NO_PARAMS)
95                 .toJavaUtil()
96                 .map(ip6FibDetailsReplyDump -> ip6FibDetailsReplyDump.ip6FibDetails)
97                 .orElse(Collections.emptyList())
98                 .stream()
99                 .map(ip6FibDetails -> ip6FibDetails.tableId);
100     }
101
102     private Stream<Integer> dumpV4FibTableIdsStream(final ModificationCache cache) throws ReadFailedException {
103         return v4DumpManager.getDump(FibManagementIIds.FIB_MNGMNT, cache, NO_PARAMS)
104                 .toJavaUtil()
105                 .map(ipFibDetailsReplyDump -> ipFibDetailsReplyDump.ipFibDetails)
106                 .orElse(Collections.emptyList())
107                 .stream()
108                 .map(ipFibDetails -> ipFibDetails.tableId);
109     }
110 }