Seyeon Jeong | 83e7986 | 2020-02-28 01:17:34 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020-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.t3.api; |
| 18 | |
| 19 | import org.onlab.packet.IpAddress; |
| 20 | import org.onlab.packet.IpPrefix; |
| 21 | import org.onosproject.routeservice.ResolvedRoute; |
| 22 | |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.Collection; |
| 25 | import java.util.Comparator; |
| 26 | import java.util.Optional; |
| 27 | import java.util.Set; |
| 28 | import java.util.stream.Collectors; |
| 29 | |
| 30 | /** |
| 31 | * Represents Network Information Base (NIB) for routes |
| 32 | * and supports alternative functions to |
| 33 | * {@link org.onosproject.routeservice.RouteService} for offline data. |
| 34 | */ |
| 35 | public class RouteNib { |
| 36 | |
| 37 | // TODO with method optimization, store into subdivided structures at the first load |
| 38 | // unresolved Route is treated as ResolvedRoute with nextHopMac null |
| 39 | Set<ResolvedRoute> routes; |
| 40 | |
| 41 | // use the singleton helper to create the instance |
| 42 | protected RouteNib() { |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Sets a set of routes. |
| 47 | * |
| 48 | * @param routes route set |
| 49 | */ |
| 50 | public void setRoutes(Set<ResolvedRoute> routes) { |
| 51 | this.routes = routes; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Returns the set of routes. |
| 56 | * |
| 57 | * @return route set |
| 58 | */ |
| 59 | public Set<ResolvedRoute> getRoutes() { |
| 60 | return routes; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Performs a longest prefix lookup on the given IP address. |
| 65 | * |
| 66 | * @param ip IP address to look up |
| 67 | * @return most specific matching route, if one exists |
| 68 | */ |
| 69 | public Optional<ResolvedRoute> longestPrefixLookup(IpAddress ip) { |
| 70 | return routes.stream() |
| 71 | .filter(r -> r.prefix().contains(ip)) |
| 72 | .max(Comparator.comparing(r -> r.prefix().prefixLength())); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns all resolved routes stored for the given prefix, including the |
| 77 | * best selected route. |
| 78 | * |
| 79 | * @param prefix IP prefix to look up routes for |
| 80 | * @return all stored resolved routes for this prefix |
| 81 | */ |
| 82 | public Collection<ResolvedRoute> getAllResolvedRoutes(IpPrefix prefix) { |
| 83 | return routes.stream() |
| 84 | .filter(r -> r.prefix().contains(prefix) |
| 85 | && r.nextHopMac() != null |
| 86 | && r.nextHopVlan() != null) |
| 87 | .collect(Collectors.toCollection(ArrayList::new)); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Returns the singleton instance of multicast routes NIB. |
| 92 | * |
| 93 | * @return instance of multicast routes NIB |
| 94 | */ |
| 95 | public static RouteNib getInstance() { |
| 96 | return RouteNib.SingletonHelper.INSTANCE; |
| 97 | } |
| 98 | |
| 99 | private static class SingletonHelper { |
| 100 | private static final RouteNib INSTANCE = new RouteNib(); |
| 101 | } |
| 102 | |
| 103 | } |