blob: 6837074aa5be581d1b45d96b5ae27ea447bddc2e [file] [log] [blame]
maojianwei42e23442016-02-15 10:40:48 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
maojianwei42e23442016-02-15 10:40:48 +08003 *
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
7 *
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.
15 */
16package org.onosproject.fnl.base;
17
18import org.onosproject.net.ConnectPoint;
19import org.onosproject.net.DeviceId;
20import org.onosproject.net.HostId;
21import org.onosproject.net.flow.FlowEntry;
22import org.onosproject.net.flow.criteria.Criterion;
23
24import java.util.ArrayList;
25import java.util.Collections;
26import java.util.List;
27import java.util.Set;
28
29/**
30 * Common utility functions and constants.
31 */
32public final class NetworkDiagnosticUtils {
33
34 private NetworkDiagnosticUtils() {
35 // no instantiation
36 }
37
38 /**
39 * Returns a list of flow entries sorted by priority in descending order.
40 *
41 * @param flowEntries flow entries to be sorted
42 * @return flow entries in descending order
43 */
44 public static List<FlowEntry> sortFlowTable(Iterable<FlowEntry> flowEntries) {
45
46 List<FlowEntry> flows = new ArrayList<>();
47 flowEntries.forEach(flows::add);
48
49 Collections.sort(flows,
50 (f1, f2) -> f2.priority() - f1.priority());
51 return flows;
52 }
53
54 /**
55 * Returns a list of match fields sorted by their types in ascending order.
56 *
57 * @param criterionSet the criteria to be sorted
58 * @return the list of criteria in ascending order
59 */
60 public static List<Criterion> sortCriteria(Set<Criterion> criterionSet) {
61
62 List<Criterion> array = new ArrayList<>(criterionSet);
63 Collections.sort(array,
64 (c1, c2) -> c1.type().compareTo(c2.type()));
65 return array;
66 }
67
68 /**
69 * Returns true if the given connect point is a device point.
70 *
71 * @param connectPoint the connect point to be checked
72 * @return true if the connect point is a device point
73 */
74 public static boolean isDevice(ConnectPoint connectPoint) {
75 return connectPoint.elementId() instanceof DeviceId;
76 }
77
78 /**
79 * Returns true if the given connect point is a host point.
80 *
81 * @param connectPoint the connect point to be checked
82 * @return true if the connect point is a host point
83 */
84 public static boolean isHost(ConnectPoint connectPoint) {
85 // TODO - not debug yet
86 return connectPoint.elementId() instanceof HostId;
87 }
88}