blob: 04724dfee99cee9b292631cde9bc7ddceede8f18 [file] [log] [blame]
Simon Hunta17fa672015-08-19 18:42:22 -07001/*
Simon Hunted804d52016-03-30 09:51:40 -07002 * Copyright 2016 Open Networking Laboratory
Simon Hunta17fa672015-08-19 18:42:22 -07003 *
Simon Hunted804d52016-03-30 09:51:40 -07004 * 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 Hunta17fa672015-08-19 18:42:22 -07007 *
Simon Hunted804d52016-03-30 09:51:40 -07008 * http://www.apache.org/licenses/LICENSE-2.0
Simon Hunta17fa672015-08-19 18:42:22 -07009 *
Simon Hunted804d52016-03-30 09:51:40 -070010 * 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 Hunta17fa672015-08-19 18:42:22 -070015 */
16
Simon Hunted804d52016-03-30 09:51:40 -070017package org.onosproject.ui.impl.topo.util;
Simon Hunta17fa672015-08-19 18:42:22 -070018
19import org.onosproject.net.intent.Intent;
Simon Hunt191c84a2015-08-21 08:24:48 -070020import org.onosproject.ui.topo.NodeSelection;
Simon Hunta17fa672015-08-19 18:42:22 -070021import 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;
Simon Hunt72297212015-08-25 10:15:33 -070052 intents = filter.findPathIntents(nodes.hostsWithHover(), nodes.devicesWithHover());
Simon Huntd2862c32015-08-24 17:41:51 -070053 if (intents.size() == 1) {
54 index = 0; // pre-select a single intent
55 }
Simon Hunta17fa672015-08-19 18:42:22 -070056 }
57
58 /**
59 * Creates an intent selection group, for a single intent.
60 *
61 * @param intent the intent
62 */
63 public IntentSelection(Intent intent) {
64 nodes = null;
65 intents = new ArrayList<>(1);
66 intents.add(intent);
67 index = 0;
68 }
69
70 /**
71 * Returns true if no intents are selected.
72 *
73 * @return true if nothing selected
74 */
75 public boolean none() {
76 return intents.isEmpty();
77 }
78
79 /**
80 * Returns true if all intents in this select group are currently selected.
81 * This is the initial state, so that all intents are shown on the
82 * topology view with primary highlighting.
83 *
84 * @return true if all selected
85 */
86 public boolean all() {
87 return index == ALL;
88 }
89
90 /**
91 * Returns true if there is a single intent in this select group, or if
92 * a specific intent has been marked (index != ALL).
93 *
94 * @return true if single intent marked
95 */
96 public boolean single() {
97 return !all();
98 }
99
100 /**
101 * Returns the number of intents in this selection group.
102 *
103 * @return number of intents
104 */
105 public int size() {
106 return intents.size();
107 }
108
109 /**
110 * Returns the index of the currently selected intent.
111 *
112 * @return the current index
113 */
114 public int index() {
115 return index;
116 }
117
118 /**
119 * The list of intents in this selection group.
120 *
121 * @return list of intents
122 */
123 public List<Intent> intents() {
124 return Collections.unmodifiableList(intents);
125 }
126
127 /**
128 * Marks and returns the next intent in this group. Note that the
129 * selection wraps around to the beginning again, if necessary.
130 *
131 * @return the next intent in the group
132 */
133 public Intent next() {
134 index += 1;
135 if (index >= intents.size()) {
136 index = 0;
137 }
138 return intents.get(index);
139 }
140
141 /**
142 * Marks and returns the previous intent in this group. Note that the
143 * selection wraps around to the end again, if necessary.
144 *
145 * @return the previous intent in the group
146 */
147 public Intent prev() {
148 index -= 1;
149 if (index < 0) {
150 index = intents.size() - 1;
151 }
152 return intents.get(index);
153 }
154
155 /**
156 * Returns the currently marked intent, or null if "all" intents
157 * are marked.
158 *
159 * @return the currently marked intent
160 */
161 public Intent current() {
162 return all() ? null : intents.get(index);
163 }
164
165 @Override
166 public String toString() {
167 return "IntentSelection{" +
168 "nodes=" + nodes +
169 ", #intents=" + intents.size() +
170 ", index=" + index +
171 '}';
172 }
173
174}