Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.segmentrouting; |
| 18 | |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 20 | import org.onlab.packet.IpPrefix; |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 21 | import org.onosproject.routeservice.ResolvedRoute; |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 22 | import org.onosproject.routeservice.RouteInfo; |
| 23 | import org.onosproject.routeservice.RouteServiceAdapter; |
| 24 | import org.onosproject.routeservice.RouteTableId; |
| 25 | |
| 26 | import java.util.Collection; |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 27 | import java.util.Map; |
Charles Chan | 8e43488 | 2018-04-13 14:01:49 -0400 | [diff] [blame^] | 28 | import java.util.Set; |
Charles Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 29 | import java.util.stream.Collectors; |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Mock Route Service. |
| 33 | * We assume there is only one routing table named "default". |
| 34 | */ |
| 35 | public class MockRouteService extends RouteServiceAdapter { |
Charles Chan | 8e43488 | 2018-04-13 14:01:49 -0400 | [diff] [blame^] | 36 | private Map<IpPrefix, Set<ResolvedRoute>> routeStore; |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 37 | |
Charles Chan | 8e43488 | 2018-04-13 14:01:49 -0400 | [diff] [blame^] | 38 | MockRouteService(Map<IpPrefix, Set<ResolvedRoute>> routeStore) { |
| 39 | this.routeStore = routeStore; |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public Collection<RouteInfo> getRoutes(RouteTableId id) { |
Charles Chan | 8e43488 | 2018-04-13 14:01:49 -0400 | [diff] [blame^] | 44 | 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 Chan | 2fde6d4 | 2017-08-23 14:46:43 -0700 | [diff] [blame] | 49 | }).collect(Collectors.toSet()); |
Charles Chan | 1a3b02a | 2017-08-18 17:11:34 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public Collection<RouteTableId> getRouteTables() { |
| 54 | return Sets.newHashSet(new RouteTableId("default")); |
| 55 | } |
| 56 | } |