blob: 1610648710e8000c2738d51ccf4bf1bd880724ab [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska7d693f52014-10-21 19:17:57 -070015 */
Ray Milkeyc7477292016-03-11 10:53:43 -080016package org.onosproject.utils;
tom1380eee2014-09-24 09:22:02 -070017
Brian O'Connorabafb502014-12-02 22:26:20 -080018import org.onosproject.cluster.ControllerNode;
slowr370d9082017-10-20 16:44:54 -070019import org.onosproject.cluster.Member;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080020import org.onosproject.core.Application;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.core.ApplicationId;
Ray Milkeyfacf2862017-08-03 11:58:29 -070022import org.onosproject.net.intf.Interface;
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080023import org.onosproject.net.TenantId;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.Element;
26import org.onosproject.net.ElementId;
27import org.onosproject.net.Port;
28import org.onosproject.net.flow.FlowRule;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070029import org.onosproject.net.group.Group;
Simon Huntba8c8052016-08-15 18:29:06 -070030import org.onosproject.net.key.DeviceKey;
Jian Licf744442016-02-25 17:32:42 +090031import org.onosproject.net.region.Region;
Sangsik Yoonb1b823f2016-05-16 18:55:39 +090032import org.onosproject.net.statistic.FlowEntryWithLoad;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.topology.TopologyCluster;
Thomas Vachuskaeb851cd2016-07-21 15:41:05 -070034import org.onosproject.ui.model.topo.UiTopoLayout;
tom1380eee2014-09-24 09:22:02 -070035
Jonathan Harteb8c9472015-08-05 07:43:13 -070036import java.util.Comparator;
37
tom1380eee2014-09-24 09:22:02 -070038/**
39 * Various comparators.
40 */
41public final class Comparators {
42
43 // Ban construction
44 private Comparators() {
45 }
46
Simon Huntba8c8052016-08-15 18:29:06 -070047 public static final Comparator<ApplicationId> APP_ID_COMPARATOR =
48 (id1, id2) -> id1.id() - id2.id();
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070049
Simon Huntba8c8052016-08-15 18:29:06 -070050 public static final Comparator<Application> APP_COMPARATOR =
51 (app1, app2) -> app1.id().id() - app2.id().id();
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080052
Simon Huntba8c8052016-08-15 18:29:06 -070053 public static final Comparator<ElementId> ELEMENT_ID_COMPARATOR =
54 (id1, id2) -> id1.toString().compareTo(id2.toString());
tom1380eee2014-09-24 09:22:02 -070055
Simon Huntba8c8052016-08-15 18:29:06 -070056 public static final Comparator<Element> ELEMENT_COMPARATOR =
57 (e1, e2) -> e1.id().toString().compareTo(e2.id().toString());
tom1380eee2014-09-24 09:22:02 -070058
Simon Huntba8c8052016-08-15 18:29:06 -070059 public static final Comparator<FlowRule> FLOW_RULE_COMPARATOR =
60 (f1, f2) -> {
61 // Compare table IDs in ascending order
Charles Chandc579c52018-08-28 13:49:29 -070062 int tableCompare = f1.table().compareTo(f2.table());
Simon Huntba8c8052016-08-15 18:29:06 -070063 if (tableCompare != 0) {
64 return tableCompare;
ssyoon90a98825a2015-08-26 00:48:15 +090065 }
Simon Huntba8c8052016-08-15 18:29:06 -070066 // Compare priorities in descending order
67 int priorityCompare = f2.priority() - f1.priority();
68 return (priorityCompare == 0)
Charles Chandc579c52018-08-28 13:49:29 -070069 ? Long.compare(f1.id().value(), f2.id().value())
Simon Huntba8c8052016-08-15 18:29:06 -070070 : priorityCompare;
ssyoon90a98825a2015-08-26 00:48:15 +090071 };
Brian Stankee312fc72016-02-16 15:07:13 -050072
Sangsik Yoonb1b823f2016-05-16 18:55:39 +090073 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 Huntba8c8052016-08-15 18:29:06 -070079 public static final Comparator<Group> GROUP_COMPARATOR =
80 (g1, g2) -> Long.valueOf(g1.id().id()).compareTo((long) g2.id().id());
Jian Licf744442016-02-25 17:32:42 +090081
Simon Huntba8c8052016-08-15 18:29:06 -070082 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 Stanke5df14472016-03-11 19:34:38 -050087
Simon Huntba8c8052016-08-15 18:29:06 -070088 public static final Comparator<TopologyCluster> CLUSTER_COMPARATOR =
89 (c1, c2) -> c1.id().index() - c2.id().index();
Brian Stanke5df14472016-03-11 19:34:38 -050090
Simon Huntba8c8052016-08-15 18:29:06 -070091 public static final Comparator<ControllerNode> NODE_COMPARATOR =
92 (ci1, ci2) -> ci1.id().toString().compareTo(ci2.id().toString());
Brian Stanke5df14472016-03-11 19:34:38 -050093
slowr370d9082017-10-20 16:44:54 -070094 public static final Comparator<Member> MEMBERSHIP_COMPARATOR =
95 (ci1, ci2) -> ci1.nodeId().toString().compareTo(ci2.nodeId().toString());
96
Simon Huntba8c8052016-08-15 18:29:06 -070097 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 Stanke5df14472016-03-11 19:34:38 -0500104
Simon Huntba8c8052016-08-15 18:29:06 -0700105 public static final Comparator<Interface> INTERFACES_COMPARATOR =
106 (intf1, intf2) ->
107 CONNECT_POINT_COMPARATOR.compare(intf1.connectPoint(), intf2.connectPoint());
108
Simon Huntba8c8052016-08-15 18:29:06 -0700109 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
tom1380eee2014-09-24 09:22:02 -0700121}