maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
maojianwei | 42e2344 | 2016-02-15 10:40:48 +0800 | [diff] [blame] | 3 | * |
| 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 | */ |
| 16 | package org.onosproject.fnl.base; |
| 17 | |
| 18 | import org.onosproject.net.ConnectPoint; |
| 19 | import org.onosproject.net.DeviceId; |
| 20 | import org.onosproject.net.HostId; |
| 21 | import org.onosproject.net.flow.FlowEntry; |
| 22 | import org.onosproject.net.flow.criteria.Criterion; |
| 23 | |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.Collections; |
| 26 | import java.util.List; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | /** |
| 30 | * Common utility functions and constants. |
| 31 | */ |
| 32 | public 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 | } |