blob: bfeca1fbc80c995712a3b3056f57e06ed3be299e [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;
Charles Chan910be6a2017-08-23 14:46:43 -070020import org.onlab.packet.IpPrefix;
Charles Chan910be6a2017-08-23 14:46:43 -070021import org.onosproject.routeservice.ResolvedRoute;
Charles Chan6c624992017-08-18 17:11:34 -070022import org.onosproject.routeservice.RouteInfo;
23import org.onosproject.routeservice.RouteServiceAdapter;
24import org.onosproject.routeservice.RouteTableId;
25
26import java.util.Collection;
Charles Chan910be6a2017-08-23 14:46:43 -070027import java.util.Map;
Charles Chan7d20a4e2018-04-13 14:01:49 -040028import java.util.Set;
Charles Chan910be6a2017-08-23 14:46:43 -070029import java.util.stream.Collectors;
Charles Chan6c624992017-08-18 17:11:34 -070030
31/**
32 * Mock Route Service.
33 * We assume there is only one routing table named "default".
34 */
35public class MockRouteService extends RouteServiceAdapter {
Charles Chan7d20a4e2018-04-13 14:01:49 -040036 private Map<IpPrefix, Set<ResolvedRoute>> routeStore;
Charles Chan6c624992017-08-18 17:11:34 -070037
Charles Chan7d20a4e2018-04-13 14:01:49 -040038 MockRouteService(Map<IpPrefix, Set<ResolvedRoute>> routeStore) {
39 this.routeStore = routeStore;
Charles Chan6c624992017-08-18 17:11:34 -070040 }
41
42 @Override
43 public Collection<RouteInfo> getRoutes(RouteTableId id) {
Charles Chan7d20a4e2018-04-13 14:01:49 -040044 return routeStore.entrySet().stream().map(e -> {
45 IpPrefix prefix = e.getKey();
46 Set<ResolvedRoute> resolvedRoutes = e.getValue();
47 ResolvedRoute bestRoute = resolvedRoutes.stream().findFirst().orElse(null);
48 return new RouteInfo(prefix, bestRoute, resolvedRoutes);
Charles Chan910be6a2017-08-23 14:46:43 -070049 }).collect(Collectors.toSet());
Charles Chan6c624992017-08-18 17:11:34 -070050 }
51
52 @Override
53 public Collection<RouteTableId> getRouteTables() {
54 return Sets.newHashSet(new RouteTableId("default"));
55 }
56}