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