blob: 3edabd3e4634cdcd4ea784c869e8f30e0765e4c1 [file] [log] [blame]
Seyeon Jeong8d3cad22020-02-28 01:17:34 -08001/*
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
17package org.onosproject.t3.api;
18
19import org.onlab.packet.IpAddress;
20import org.onlab.packet.IpPrefix;
21import org.onosproject.routeservice.ResolvedRoute;
22
23import java.util.ArrayList;
24import java.util.Collection;
25import java.util.Comparator;
26import java.util.Optional;
27import java.util.Set;
28import 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 */
35public 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}