Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 3 | * |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -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 |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 7 | * |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 9 | * |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 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. |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame] | 17 | package org.onosproject.ui.impl.topo.util; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 18 | |
| 19 | import org.onosproject.net.intent.Intent; |
Simon Hunt | 191c84a | 2015-08-21 08:24:48 -0700 | [diff] [blame] | 20 | import org.onosproject.ui.topo.NodeSelection; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.Collections; |
| 26 | import java.util.List; |
| 27 | |
| 28 | /** |
| 29 | * Encapsulates a selection of intents (paths) inferred from a selection |
| 30 | * of devices and/or hosts from the topology view. |
| 31 | */ |
| 32 | public class IntentSelection { |
| 33 | |
| 34 | private static final int ALL = -1; |
| 35 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 36 | private static final Logger log = |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 37 | LoggerFactory.getLogger(IntentSelection.class); |
| 38 | |
| 39 | private final NodeSelection nodes; |
| 40 | |
| 41 | private final List<Intent> intents; |
| 42 | private int index = ALL; |
| 43 | |
| 44 | /** |
| 45 | * Creates an intent selection group, based on selected nodes. |
| 46 | * |
| 47 | * @param nodes node selection |
| 48 | * @param filter intent filter |
| 49 | */ |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 50 | public IntentSelection(NodeSelection nodes, TopoIntentFilter filter) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 51 | this.nodes = nodes; |
Prince Pereira | 46c82d4 | 2016-09-19 13:30:50 +0530 | [diff] [blame] | 52 | intents = filter.findPathIntents( |
| 53 | nodes.hostsWithHover(), |
| 54 | nodes.devicesWithHover(), |
| 55 | nodes.linksWithHover()); |
Simon Hunt | d2862c3 | 2015-08-24 17:41:51 -0700 | [diff] [blame] | 56 | if (intents.size() == 1) { |
| 57 | index = 0; // pre-select a single intent |
| 58 | } |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Creates an intent selection group, for a single intent. |
| 63 | * |
| 64 | * @param intent the intent |
| 65 | */ |
| 66 | public IntentSelection(Intent intent) { |
| 67 | nodes = null; |
| 68 | intents = new ArrayList<>(1); |
| 69 | intents.add(intent); |
| 70 | index = 0; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns true if no intents are selected. |
| 75 | * |
| 76 | * @return true if nothing selected |
| 77 | */ |
| 78 | public boolean none() { |
| 79 | return intents.isEmpty(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns true if all intents in this select group are currently selected. |
| 84 | * This is the initial state, so that all intents are shown on the |
| 85 | * topology view with primary highlighting. |
| 86 | * |
| 87 | * @return true if all selected |
| 88 | */ |
| 89 | public boolean all() { |
| 90 | return index == ALL; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Returns true if there is a single intent in this select group, or if |
| 95 | * a specific intent has been marked (index != ALL). |
| 96 | * |
| 97 | * @return true if single intent marked |
| 98 | */ |
| 99 | public boolean single() { |
| 100 | return !all(); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Returns the number of intents in this selection group. |
| 105 | * |
| 106 | * @return number of intents |
| 107 | */ |
| 108 | public int size() { |
| 109 | return intents.size(); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns the index of the currently selected intent. |
| 114 | * |
| 115 | * @return the current index |
| 116 | */ |
| 117 | public int index() { |
| 118 | return index; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * The list of intents in this selection group. |
| 123 | * |
| 124 | * @return list of intents |
| 125 | */ |
| 126 | public List<Intent> intents() { |
| 127 | return Collections.unmodifiableList(intents); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Marks and returns the next intent in this group. Note that the |
| 132 | * selection wraps around to the beginning again, if necessary. |
| 133 | * |
| 134 | * @return the next intent in the group |
| 135 | */ |
| 136 | public Intent next() { |
| 137 | index += 1; |
| 138 | if (index >= intents.size()) { |
| 139 | index = 0; |
| 140 | } |
| 141 | return intents.get(index); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Marks and returns the previous intent in this group. Note that the |
| 146 | * selection wraps around to the end again, if necessary. |
| 147 | * |
| 148 | * @return the previous intent in the group |
| 149 | */ |
| 150 | public Intent prev() { |
| 151 | index -= 1; |
| 152 | if (index < 0) { |
| 153 | index = intents.size() - 1; |
| 154 | } |
| 155 | return intents.get(index); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns the currently marked intent, or null if "all" intents |
| 160 | * are marked. |
| 161 | * |
| 162 | * @return the currently marked intent |
| 163 | */ |
| 164 | public Intent current() { |
| 165 | return all() ? null : intents.get(index); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public String toString() { |
| 170 | return "IntentSelection{" + |
| 171 | "nodes=" + nodes + |
| 172 | ", #intents=" + intents.size() + |
| 173 | ", index=" + index + |
| 174 | '}'; |
| 175 | } |
| 176 | |
| 177 | } |