Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 21 | import org.onosproject.incubator.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; |
Claudine Chiu | 31ad527 | 2016-02-17 20:56:24 +0000 | [diff] [blame] | 26 | import org.onosproject.net.key.DeviceKey; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.ConnectPoint; |
| 28 | import org.onosproject.net.Element; |
| 29 | import org.onosproject.net.ElementId; |
| 30 | import org.onosproject.net.Port; |
| 31 | import org.onosproject.net.flow.FlowRule; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 32 | import org.onosproject.net.group.Group; |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 33 | import org.onosproject.net.region.Region; |
ssyoon90 | a98825a | 2015-08-26 00:48:15 +0900 | [diff] [blame] | 34 | import org.onosproject.net.statistic.TypedFlowEntryWithLoad; |
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 | |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 49 | public static final Comparator<ApplicationId> APP_ID_COMPARATOR = new Comparator<ApplicationId>() { |
| 50 | @Override |
| 51 | public int compare(ApplicationId id1, ApplicationId id2) { |
| 52 | return id1.id() - id2.id(); |
| 53 | } |
| 54 | }; |
| 55 | |
Thomas Vachuska | ebf5e54 | 2015-02-03 19:38:13 -0800 | [diff] [blame] | 56 | public static final Comparator<Application> APP_COMPARATOR = new Comparator<Application>() { |
| 57 | @Override |
| 58 | public int compare(Application app1, Application app2) { |
| 59 | return app1.id().id() - app2.id().id(); |
| 60 | } |
| 61 | }; |
| 62 | |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 63 | public static final Comparator<ElementId> ELEMENT_ID_COMPARATOR = new Comparator<ElementId>() { |
| 64 | @Override |
| 65 | public int compare(ElementId id1, ElementId id2) { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 66 | return id1.toString().compareTo(id2.toString()); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 67 | } |
| 68 | }; |
| 69 | |
| 70 | public static final Comparator<Element> ELEMENT_COMPARATOR = new Comparator<Element>() { |
| 71 | @Override |
| 72 | public int compare(Element e1, Element e2) { |
tom | 545708e | 2014-10-09 17:10:02 -0700 | [diff] [blame] | 73 | return e1.id().toString().compareTo(e2.id().toString()); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 74 | } |
| 75 | }; |
| 76 | |
| 77 | public static final Comparator<FlowRule> FLOW_RULE_COMPARATOR = new Comparator<FlowRule>() { |
| 78 | @Override |
| 79 | public int compare(FlowRule f1, FlowRule f2) { |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 80 | // Compare table IDs in ascending order |
| 81 | int tableCompare = f1.tableId() - f2.tableId(); |
| 82 | if (tableCompare != 0) { |
| 83 | return tableCompare; |
| 84 | } |
| 85 | // Compare priorities in descending order |
| 86 | int priorityCompare = f2.priority() - f1.priority(); |
| 87 | return (priorityCompare == 0) |
Saurav Das | 554f5e7 | 2015-10-27 10:28:19 -0700 | [diff] [blame] | 88 | ? Long.valueOf(f1.id().value()).compareTo(f2.id().value()) |
Jonathan Hart | c7840bd | 2016-01-21 23:26:29 -0800 | [diff] [blame] | 89 | : priorityCompare; |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 90 | } |
| 91 | }; |
| 92 | |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 93 | public static final Comparator<Group> GROUP_COMPARATOR = new Comparator<Group>() { |
| 94 | @Override |
| 95 | public int compare(Group g1, Group g2) { |
| 96 | return Long.valueOf(g1.id().id()).compareTo(Long.valueOf(g2.id().id())); |
| 97 | } |
| 98 | }; |
| 99 | |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 100 | public static final Comparator<Port> PORT_COMPARATOR = new Comparator<Port>() { |
| 101 | @Override |
| 102 | public int compare(Port p1, Port p2) { |
| 103 | long delta = p1.number().toLong() - p2.number().toLong(); |
| 104 | return delta == 0 ? 0 : (delta < 0 ? -1 : +1); |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | public static final Comparator<TopologyCluster> CLUSTER_COMPARATOR = new Comparator<TopologyCluster>() { |
| 109 | @Override |
| 110 | public int compare(TopologyCluster c1, TopologyCluster c2) { |
| 111 | return c1.id().index() - c2.id().index(); |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | public static final Comparator<ControllerNode> NODE_COMPARATOR = new Comparator<ControllerNode>() { |
| 116 | @Override |
| 117 | public int compare(ControllerNode ci1, ControllerNode ci2) { |
| 118 | return ci1.id().toString().compareTo(ci2.id().toString()); |
| 119 | } |
| 120 | }; |
| 121 | |
Jonathan Hart | 61d4ebc | 2014-10-29 11:08:26 -0700 | [diff] [blame] | 122 | public static final Comparator<ConnectPoint> CONNECT_POINT_COMPARATOR = new Comparator<ConnectPoint>() { |
| 123 | @Override |
| 124 | public int compare(ConnectPoint o1, ConnectPoint o2) { |
| 125 | int compareId = ELEMENT_ID_COMPARATOR.compare(o1.elementId(), o2.elementId()); |
| 126 | return (compareId != 0) ? |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 127 | compareId : |
| 128 | Long.signum(o1.port().toLong() - o2.port().toLong()); |
Jonathan Hart | 61d4ebc | 2014-10-29 11:08:26 -0700 | [diff] [blame] | 129 | } |
| 130 | }; |
| 131 | |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 132 | public static final Comparator<Interface> INTERFACES_COMPARATOR = (intf1, intf2) -> |
| 133 | CONNECT_POINT_COMPARATOR.compare(intf1.connectPoint(), intf2.connectPoint()); |
| 134 | |
ssyoon90 | a98825a | 2015-08-26 00:48:15 +0900 | [diff] [blame] | 135 | public static final Comparator<TypedFlowEntryWithLoad> TYPEFLOWENTRY_WITHLOAD_COMPARATOR = |
| 136 | new Comparator<TypedFlowEntryWithLoad>() { |
| 137 | @Override |
| 138 | public int compare(TypedFlowEntryWithLoad fe1, TypedFlowEntryWithLoad fe2) { |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 139 | long delta = fe1.load().rate() - fe2.load().rate(); |
ssyoon90 | a98825a | 2015-08-26 00:48:15 +0900 | [diff] [blame] | 140 | return delta == 0 ? 0 : (delta > 0 ? -1 : +1); |
| 141 | } |
| 142 | }; |
Brian Stanke | e312fc7 | 2016-02-16 15:07:13 -0500 | [diff] [blame] | 143 | |
| 144 | public static final Comparator<DeviceKey> DEVICE_KEY_COMPARATOR = new Comparator<DeviceKey>() { |
| 145 | @Override |
| 146 | public int compare(DeviceKey deviceKey1, DeviceKey deviceKey2) { |
| 147 | return deviceKey1.deviceKeyId().id().toString().compareTo(deviceKey2.deviceKeyId().id().toString()); |
| 148 | } |
| 149 | }; |
Jian Li | cf74444 | 2016-02-25 17:32:42 +0900 | [diff] [blame] | 150 | |
| 151 | public static final Comparator<Region> REGION_COMPARATOR = new Comparator<Region>() { |
| 152 | @Override |
| 153 | public int compare(Region region1, Region region2) { |
| 154 | return region1.id().toString().compareTo(region2.id().toString()); |
| 155 | } |
| 156 | }; |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 157 | |
Thomas Vachuska | eb851cd | 2016-07-21 15:41:05 -0700 | [diff] [blame] | 158 | public static final Comparator<UiTopoLayout> LAYOUT_COMPARATOR = new Comparator<UiTopoLayout>() { |
| 159 | @Override |
| 160 | public int compare(UiTopoLayout l1, UiTopoLayout l2) { |
| 161 | return l1.id().toString().compareTo(l2.id().toString()); |
| 162 | } |
| 163 | }; |
Brian Stanke | 5df1447 | 2016-03-11 19:34:38 -0500 | [diff] [blame] | 164 | public static final Comparator<TenantId> TENANT_ID_COMPARATOR = new Comparator<TenantId>() { |
| 165 | @Override |
| 166 | public int compare(TenantId tenant1, TenantId tenant2) { |
| 167 | return tenant1.id().toString().compareTo(tenant2.id().toString()); |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | public static final Comparator<VirtualNetwork> VIRTUAL_NETWORK_COMPARATOR = new Comparator<VirtualNetwork>() { |
| 172 | @Override |
| 173 | public int compare(VirtualNetwork virtualNetwork1, VirtualNetwork virtualNetwork2) { |
| 174 | return virtualNetwork1.tenantId().toString().compareTo(virtualNetwork2.tenantId().toString()); |
| 175 | } |
| 176 | }; |
| 177 | |
| 178 | public static final Comparator<VirtualDevice> VIRTUAL_DEVICE_COMPARATOR = new Comparator<VirtualDevice>() { |
| 179 | @Override |
| 180 | public int compare(VirtualDevice virtualDevice1, VirtualDevice virtualDevice2) { |
| 181 | return virtualDevice1.id().toString().compareTo(virtualDevice2.id().toString()); |
| 182 | } |
| 183 | }; |
| 184 | |
| 185 | public static final Comparator<VirtualPort> VIRTUAL_PORT_COMPARATOR = new Comparator<VirtualPort>() { |
| 186 | @Override |
| 187 | public int compare(VirtualPort virtualPort1, VirtualPort virtualPort2) { |
| 188 | return virtualPort1.number().toString().compareTo(virtualPort2.number().toString()); |
| 189 | } |
| 190 | }; |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 191 | } |