Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Ray Milkey | c747729 | 2016-03-11 10:53:43 -0800 | [diff] [blame] | 16 | package org.onosproject.utils; |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 17 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 18 | import org.onosproject.cluster.ControllerNode; |
slowr | 370d908 | 2017-10-20 16:44:54 -0700 | [diff] [blame] | 19 | import org.onosproject.cluster.Member; |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 20 | import org.onosproject.core.Application; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.core.ApplicationId; |
Ray Milkey | facf286 | 2017-08-03 11:58:29 -0700 | [diff] [blame] | 22 | import org.onosproject.net.intf.Interface; |
Thomas Vachuska | 52f2cd1 | 2018-11-08 21:20:04 -0800 | [diff] [blame] | 23 | import org.onosproject.net.TenantId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.Element; |
| 26 | import org.onosproject.net.ElementId; |
| 27 | import org.onosproject.net.Port; |
| 28 | import org.onosproject.net.flow.FlowRule; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 29 | import org.onosproject.net.group.Group; |
Simon Hunt | ba8c805 | 2016-08-15 18:29:06 -0700 | [diff] [blame] | 30 | import org.onosproject.net.key.DeviceKey; |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 31 | import org.onosproject.net.region.Region; |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 32 | import org.onosproject.net.statistic.FlowEntryWithLoad; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.topology.TopologyCluster; |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 34 | import org.onosproject.ui.model.topo.UiTopoLayout; |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 35 | |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 36 | import java.util.Comparator; |
| 37 | |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Various comparators. |
| 40 | */ |
| 41 | public final class Comparators { |
| 42 | |
| 43 | // Ban construction |
| 44 | private Comparators() { |
| 45 | } |
| 46 | |
Simon Hunt | ba8c805 | 2016-08-15 18:29:06 -0700 | [diff] [blame] | 47 | public static final Comparator<ApplicationId> APP_ID_COMPARATOR = |
| 48 | (id1, id2) -> id1.id() - id2.id(); |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 49 | |
Simon Hunt | ba8c805 | 2016-08-15 18:29:06 -0700 | [diff] [blame] | 50 | public static final Comparator<Application> APP_COMPARATOR = |
| 51 | (app1, app2) -> app1.id().id() - app2.id().id(); |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 52 | |
Simon Hunt | ba8c805 | 2016-08-15 18:29:06 -0700 | [diff] [blame] | 53 | public static final Comparator<ElementId> ELEMENT_ID_COMPARATOR = |
| 54 | (id1, id2) -> id1.toString().compareTo(id2.toString()); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 55 | |
Simon Hunt | ba8c805 | 2016-08-15 18:29:06 -0700 | [diff] [blame] | 56 | public static final Comparator<Element> ELEMENT_COMPARATOR = |
| 57 | (e1, e2) -> e1.id().toString().compareTo(e2.id().toString()); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 58 | |
Simon Hunt | ba8c805 | 2016-08-15 18:29:06 -0700 | [diff] [blame] | 59 | public static final Comparator<FlowRule> FLOW_RULE_COMPARATOR = |
| 60 | (f1, f2) -> { |
| 61 | // Compare table IDs in ascending order |
Charles Chan | dc579c5 | 2018-08-28 13:49:29 -0700 | [diff] [blame] | 62 | int tableCompare = f1.table().compareTo(f2.table()); |
Simon Hunt | ba8c805 | 2016-08-15 18:29:06 -0700 | [diff] [blame] | 63 | if (tableCompare != 0) { |
| 64 | return tableCompare; |
ssyoon90 | a98825a | 2015-08-26 00:48:15 +0900 | [diff] [blame] | 65 | } |
Simon Hunt | ba8c805 | 2016-08-15 18:29:06 -0700 | [diff] [blame] | 66 | // Compare priorities in descending order |
| 67 | int priorityCompare = f2.priority() - f1.priority(); |
| 68 | return (priorityCompare == 0) |
Charles Chan | dc579c5 | 2018-08-28 13:49:29 -0700 | [diff] [blame] | 69 | ? Long.compare(f1.id().value(), f2.id().value()) |
Simon Hunt | ba8c805 | 2016-08-15 18:29:06 -0700 | [diff] [blame] | 70 | : priorityCompare; |
ssyoon90 | a98825a | 2015-08-26 00:48:15 +0900 | [diff] [blame] | 71 | }; |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 72 | |
Sangsik Yoon | b1b823f | 2016-05-16 18:55:39 +0900 | [diff] [blame] | 73 | public static final Comparator<FlowEntryWithLoad> FLOWENTRY_WITHLOAD_COMPARATOR = |
| 74 | (fe1, fe2) -> { |
| 75 | long delta = fe1.load().rate() - fe2.load().rate(); |
| 76 | return delta == 0 ? 0 : (delta > 0 ? -1 : +1); |
| 77 | }; |
| 78 | |
Simon Hunt | ba8c805 | 2016-08-15 18:29:06 -0700 | [diff] [blame] | 79 | public static final Comparator<Group> GROUP_COMPARATOR = |
| 80 | (g1, g2) -> Long.valueOf(g1.id().id()).compareTo((long) g2.id().id()); |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 81 | |
Simon Hunt | ba8c805 | 2016-08-15 18:29:06 -0700 | [diff] [blame] | 82 | public static final Comparator<Port> PORT_COMPARATOR = |
| 83 | (p1, p2) -> { |
| 84 | long delta = p1.number().toLong() - p2.number().toLong(); |
| 85 | return delta == 0 ? 0 : (delta < 0 ? -1 : +1); |
| 86 | }; |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 87 | |
Simon Hunt | ba8c805 | 2016-08-15 18:29:06 -0700 | [diff] [blame] | 88 | public static final Comparator<TopologyCluster> CLUSTER_COMPARATOR = |
| 89 | (c1, c2) -> c1.id().index() - c2.id().index(); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 90 | |
Simon Hunt | ba8c805 | 2016-08-15 18:29:06 -0700 | [diff] [blame] | 91 | public static final Comparator<ControllerNode> NODE_COMPARATOR = |
| 92 | (ci1, ci2) -> ci1.id().toString().compareTo(ci2.id().toString()); |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 93 | |
slowr | 370d908 | 2017-10-20 16:44:54 -0700 | [diff] [blame] | 94 | public static final Comparator<Member> MEMBERSHIP_COMPARATOR = |
| 95 | (ci1, ci2) -> ci1.nodeId().toString().compareTo(ci2.nodeId().toString()); |
| 96 | |
Simon Hunt | ba8c805 | 2016-08-15 18:29:06 -0700 | [diff] [blame] | 97 | public static final Comparator<ConnectPoint> CONNECT_POINT_COMPARATOR = |
| 98 | (o1, o2) -> { |
| 99 | int compareId = ELEMENT_ID_COMPARATOR.compare(o1.elementId(), o2.elementId()); |
| 100 | return (compareId != 0) ? |
| 101 | compareId : |
| 102 | Long.signum(o1.port().toLong() - o2.port().toLong()); |
| 103 | }; |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 104 | |
Simon Hunt | ba8c805 | 2016-08-15 18:29:06 -0700 | [diff] [blame] | 105 | public static final Comparator<Interface> INTERFACES_COMPARATOR = |
| 106 | (intf1, intf2) -> |
| 107 | CONNECT_POINT_COMPARATOR.compare(intf1.connectPoint(), intf2.connectPoint()); |
| 108 | |
Simon Hunt | ba8c805 | 2016-08-15 18:29:06 -0700 | [diff] [blame] | 109 | public static final Comparator<DeviceKey> DEVICE_KEY_COMPARATOR = |
| 110 | (dk1, dk2) -> dk1.deviceKeyId().id().compareTo(dk2.deviceKeyId().id()); |
| 111 | |
| 112 | public static final Comparator<Region> REGION_COMPARATOR = |
| 113 | (r1, r2) -> r1.id().toString().compareTo(r2.id().toString()); |
| 114 | |
| 115 | public static final Comparator<UiTopoLayout> LAYOUT_COMPARATOR = |
| 116 | (l1, l2) -> l1.id().toString().compareTo(l2.id().toString()); |
| 117 | |
| 118 | public static final Comparator<TenantId> TENANT_ID_COMPARATOR = |
| 119 | (t1, t2) -> t1.id().compareTo(t2.id()); |
| 120 | |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 121 | } |