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