blob: 530c296270878e907b1822a364d3e1cbe08e996b [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli;
tom1380eee2014-09-24 09:22:02 -070017
Brian O'Connorabafb502014-12-02 22:26:20 -080018import org.onosproject.cluster.ControllerNode;
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080019import org.onosproject.core.Application;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.core.ApplicationId;
Jonathan Harteb8c9472015-08-05 07:43:13 -070021import org.onosproject.incubator.net.intf.Interface;
Claudine Chiu31ad5272016-02-17 20:56:24 +000022import org.onosproject.net.key.DeviceKey;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.Element;
25import org.onosproject.net.ElementId;
26import org.onosproject.net.Port;
27import org.onosproject.net.flow.FlowRule;
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070028import org.onosproject.net.group.Group;
ssyoon90a98825a2015-08-26 00:48:15 +090029import org.onosproject.net.statistic.TypedFlowEntryWithLoad;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.topology.TopologyCluster;
tom1380eee2014-09-24 09:22:02 -070031
Jonathan Harteb8c9472015-08-05 07:43:13 -070032import java.util.Comparator;
33
tom1380eee2014-09-24 09:22:02 -070034/**
35 * Various comparators.
36 */
37public final class Comparators {
38
39 // Ban construction
40 private Comparators() {
41 }
42
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070043 public static final Comparator<ApplicationId> APP_ID_COMPARATOR = new Comparator<ApplicationId>() {
44 @Override
45 public int compare(ApplicationId id1, ApplicationId id2) {
46 return id1.id() - id2.id();
47 }
48 };
49
Thomas Vachuskaebf5e542015-02-03 19:38:13 -080050 public static final Comparator<Application> APP_COMPARATOR = new Comparator<Application>() {
51 @Override
52 public int compare(Application app1, Application app2) {
53 return app1.id().id() - app2.id().id();
54 }
55 };
56
tom1380eee2014-09-24 09:22:02 -070057 public static final Comparator<ElementId> ELEMENT_ID_COMPARATOR = new Comparator<ElementId>() {
58 @Override
59 public int compare(ElementId id1, ElementId id2) {
tom545708e2014-10-09 17:10:02 -070060 return id1.toString().compareTo(id2.toString());
tom1380eee2014-09-24 09:22:02 -070061 }
62 };
63
64 public static final Comparator<Element> ELEMENT_COMPARATOR = new Comparator<Element>() {
65 @Override
66 public int compare(Element e1, Element e2) {
tom545708e2014-10-09 17:10:02 -070067 return e1.id().toString().compareTo(e2.id().toString());
tom1380eee2014-09-24 09:22:02 -070068 }
69 };
70
71 public static final Comparator<FlowRule> FLOW_RULE_COMPARATOR = new Comparator<FlowRule>() {
72 @Override
73 public int compare(FlowRule f1, FlowRule f2) {
Jonathan Hartc7840bd2016-01-21 23:26:29 -080074 // Compare table IDs in ascending order
75 int tableCompare = f1.tableId() - f2.tableId();
76 if (tableCompare != 0) {
77 return tableCompare;
78 }
79 // Compare priorities in descending order
80 int priorityCompare = f2.priority() - f1.priority();
81 return (priorityCompare == 0)
Saurav Das554f5e72015-10-27 10:28:19 -070082 ? Long.valueOf(f1.id().value()).compareTo(f2.id().value())
Jonathan Hartc7840bd2016-01-21 23:26:29 -080083 : priorityCompare;
tom1380eee2014-09-24 09:22:02 -070084 }
85 };
86
Srikanth Vavilapalli10e75cd2015-04-13 16:21:24 -070087 public static final Comparator<Group> GROUP_COMPARATOR = new Comparator<Group>() {
88 @Override
89 public int compare(Group g1, Group g2) {
90 return Long.valueOf(g1.id().id()).compareTo(Long.valueOf(g2.id().id()));
91 }
92 };
93
tom1380eee2014-09-24 09:22:02 -070094 public static final Comparator<Port> PORT_COMPARATOR = new Comparator<Port>() {
95 @Override
96 public int compare(Port p1, Port p2) {
97 long delta = p1.number().toLong() - p2.number().toLong();
98 return delta == 0 ? 0 : (delta < 0 ? -1 : +1);
99 }
100 };
101
102 public static final Comparator<TopologyCluster> CLUSTER_COMPARATOR = new Comparator<TopologyCluster>() {
103 @Override
104 public int compare(TopologyCluster c1, TopologyCluster c2) {
105 return c1.id().index() - c2.id().index();
106 }
107 };
108
109 public static final Comparator<ControllerNode> NODE_COMPARATOR = new Comparator<ControllerNode>() {
110 @Override
111 public int compare(ControllerNode ci1, ControllerNode ci2) {
112 return ci1.id().toString().compareTo(ci2.id().toString());
113 }
114 };
115
Jonathan Hart61d4ebc2014-10-29 11:08:26 -0700116 public static final Comparator<ConnectPoint> CONNECT_POINT_COMPARATOR = new Comparator<ConnectPoint>() {
117 @Override
118 public int compare(ConnectPoint o1, ConnectPoint o2) {
119 int compareId = ELEMENT_ID_COMPARATOR.compare(o1.elementId(), o2.elementId());
120 return (compareId != 0) ?
Brian Stankee312fc72016-02-16 15:07:13 -0500121 compareId :
122 Long.signum(o1.port().toLong() - o2.port().toLong());
Jonathan Hart61d4ebc2014-10-29 11:08:26 -0700123 }
124 };
125
Jonathan Harteb8c9472015-08-05 07:43:13 -0700126 public static final Comparator<Interface> INTERFACES_COMPARATOR = (intf1, intf2) ->
127 CONNECT_POINT_COMPARATOR.compare(intf1.connectPoint(), intf2.connectPoint());
128
ssyoon90a98825a2015-08-26 00:48:15 +0900129 public static final Comparator<TypedFlowEntryWithLoad> TYPEFLOWENTRY_WITHLOAD_COMPARATOR =
130 new Comparator<TypedFlowEntryWithLoad>() {
131 @Override
132 public int compare(TypedFlowEntryWithLoad fe1, TypedFlowEntryWithLoad fe2) {
Brian Stankee312fc72016-02-16 15:07:13 -0500133 long delta = fe1.load().rate() - fe2.load().rate();
ssyoon90a98825a2015-08-26 00:48:15 +0900134 return delta == 0 ? 0 : (delta > 0 ? -1 : +1);
135 }
136 };
Brian Stankee312fc72016-02-16 15:07:13 -0500137
138 public static final Comparator<DeviceKey> DEVICE_KEY_COMPARATOR = new Comparator<DeviceKey>() {
139 @Override
140 public int compare(DeviceKey deviceKey1, DeviceKey deviceKey2) {
141 return deviceKey1.deviceKeyId().id().toString().compareTo(deviceKey2.deviceKeyId().id().toString());
142 }
143 };
tom1380eee2014-09-24 09:22:02 -0700144}