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