blob: c3730ddaabc821246024509ebf96dc4039e32e93 [file] [log] [blame]
Charles Chan6c624992017-08-18 17:11:34 -07001/*
2 * Copyright 2017-present Open Networking Foundation
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
17package org.onosproject.segmentrouting;
18
Charles Chan6c624992017-08-18 17:11:34 -070019import com.google.common.collect.Sets;
pier6a2052b2019-06-28 22:17:31 +020020import org.onlab.packet.IpAddress;
Charles Chan910be6a2017-08-23 14:46:43 -070021import org.onlab.packet.IpPrefix;
Charles Chan910be6a2017-08-23 14:46:43 -070022import org.onosproject.routeservice.ResolvedRoute;
Charles Chan6c624992017-08-18 17:11:34 -070023import org.onosproject.routeservice.RouteInfo;
24import org.onosproject.routeservice.RouteServiceAdapter;
25import org.onosproject.routeservice.RouteTableId;
26
27import java.util.Collection;
Charles Chan910be6a2017-08-23 14:46:43 -070028import java.util.Map;
pier6a2052b2019-06-28 22:17:31 +020029import java.util.Optional;
Charles Chan7d20a4e2018-04-13 14:01:49 -040030import java.util.Set;
Charles Chan910be6a2017-08-23 14:46:43 -070031import java.util.stream.Collectors;
Charles Chan6c624992017-08-18 17:11:34 -070032
33/**
34 * Mock Route Service.
35 * We assume there is only one routing table named "default".
36 */
37public class MockRouteService extends RouteServiceAdapter {
Charles Chan7d20a4e2018-04-13 14:01:49 -040038 private Map<IpPrefix, Set<ResolvedRoute>> routeStore;
Charles Chan6c624992017-08-18 17:11:34 -070039
Charles Chan7d20a4e2018-04-13 14:01:49 -040040 MockRouteService(Map<IpPrefix, Set<ResolvedRoute>> routeStore) {
41 this.routeStore = routeStore;
Charles Chan6c624992017-08-18 17:11:34 -070042 }
43
44 @Override
45 public Collection<RouteInfo> getRoutes(RouteTableId id) {
Charles Chan7d20a4e2018-04-13 14:01:49 -040046 return routeStore.entrySet().stream().map(e -> {
47 IpPrefix prefix = e.getKey();
48 Set<ResolvedRoute> resolvedRoutes = e.getValue();
49 ResolvedRoute bestRoute = resolvedRoutes.stream().findFirst().orElse(null);
50 return new RouteInfo(prefix, bestRoute, resolvedRoutes);
Charles Chan910be6a2017-08-23 14:46:43 -070051 }).collect(Collectors.toSet());
Charles Chan6c624992017-08-18 17:11:34 -070052 }
53
54 @Override
55 public Collection<RouteTableId> getRouteTables() {
56 return Sets.newHashSet(new RouteTableId("default"));
57 }
pier6a2052b2019-06-28 22:17:31 +020058
59 @Override
60 public Optional<ResolvedRoute> longestPrefixLookup(IpAddress ip) {
61 return this.routeStore.get(ip.toIpPrefix()).stream()
62 .findFirst();
63 }
Charles Chan6c624992017-08-18 17:11:34 -070064}