blob: dd48768d7a940b82276ded4a682397ff0061251f [file] [log] [blame]
Simon Hunta17fa672015-08-19 18:42:22 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present 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;
Prince Pereira46c82d42016-09-19 13:30:50 +053052 intents = filter.findPathIntents(
53 nodes.hostsWithHover(),
54 nodes.devicesWithHover(),
55 nodes.linksWithHover());
Simon Huntd2862c32015-08-24 17:41:51 -070056 if (intents.size() == 1) {
57 index = 0; // pre-select a single intent
58 }
Simon Hunta17fa672015-08-19 18:42:22 -070059 }
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}