blob: 01ae93d9555d075f38cc890790f4e30260daab89 [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());
Simon Huntd2862c32015-08-24 17:41:51 -070054 if (intents.size() == 1) {
55 index = 0; // pre-select a single intent
56 }
Simon Hunta17fa672015-08-19 18:42:22 -070057 }
58
59 /**
60 * Creates an intent selection group, for a single intent.
61 *
62 * @param intent the intent
63 */
64 public IntentSelection(Intent intent) {
65 nodes = null;
66 intents = new ArrayList<>(1);
67 intents.add(intent);
68 index = 0;
69 }
70
71 /**
72 * Returns true if no intents are selected.
73 *
74 * @return true if nothing selected
75 */
76 public boolean none() {
77 return intents.isEmpty();
78 }
79
80 /**
81 * Returns true if all intents in this select group are currently selected.
82 * This is the initial state, so that all intents are shown on the
83 * topology view with primary highlighting.
84 *
85 * @return true if all selected
86 */
87 public boolean all() {
88 return index == ALL;
89 }
90
91 /**
92 * Returns true if there is a single intent in this select group, or if
93 * a specific intent has been marked (index != ALL).
94 *
95 * @return true if single intent marked
96 */
97 public boolean single() {
98 return !all();
99 }
100
101 /**
102 * Returns the number of intents in this selection group.
103 *
104 * @return number of intents
105 */
106 public int size() {
107 return intents.size();
108 }
109
110 /**
111 * Returns the index of the currently selected intent.
112 *
113 * @return the current index
114 */
115 public int index() {
116 return index;
117 }
118
119 /**
120 * The list of intents in this selection group.
121 *
122 * @return list of intents
123 */
124 public List<Intent> intents() {
125 return Collections.unmodifiableList(intents);
126 }
127
128 /**
129 * Marks and returns the next intent in this group. Note that the
130 * selection wraps around to the beginning again, if necessary.
131 *
132 * @return the next intent in the group
133 */
134 public Intent next() {
135 index += 1;
136 if (index >= intents.size()) {
137 index = 0;
138 }
139 return intents.get(index);
140 }
141
142 /**
143 * Marks and returns the previous intent in this group. Note that the
144 * selection wraps around to the end again, if necessary.
145 *
146 * @return the previous intent in the group
147 */
148 public Intent prev() {
149 index -= 1;
150 if (index < 0) {
151 index = intents.size() - 1;
152 }
153 return intents.get(index);
154 }
155
156 /**
157 * Returns the currently marked intent, or null if "all" intents
158 * are marked.
159 *
160 * @return the currently marked intent
161 */
162 public Intent current() {
163 return all() ? null : intents.get(index);
164 }
165
166 @Override
167 public String toString() {
168 return "IntentSelection{" +
169 "nodes=" + nodes +
170 ", #intents=" + intents.size() +
171 ", index=" + index +
172 '}';
173 }
174
175}