blob: c0597aaa8bc7e95fc9aa6260cec13da465b7a578 [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 com.fasterxml.jackson.databind.JsonNode;
21import com.fasterxml.jackson.databind.node.ArrayNode;
22import com.fasterxml.jackson.databind.node.ObjectNode;
23import org.onosproject.net.Device;
24import org.onosproject.net.Host;
25import org.onosproject.net.device.DeviceService;
26import org.onosproject.net.host.HostService;
27import org.onosproject.ui.JsonUtils;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31import java.util.Collections;
32import java.util.HashSet;
33import java.util.Set;
34
35import static com.google.common.base.Strings.isNullOrEmpty;
36import static org.onosproject.net.DeviceId.deviceId;
37import static org.onosproject.net.HostId.hostId;
38
39/**
40 * Encapsulates a selection of devices and/or hosts from the topology view.
41 */
42public class NodeSelection {
43
Simon Hunt4fc86852015-08-20 17:57:52 -070044 private static final Logger log =
Simon Hunta17fa672015-08-19 18:42:22 -070045 LoggerFactory.getLogger(NodeSelection.class);
46
47 private static final String IDS = "ids";
48 private static final String HOVER = "hover";
49
50 private final DeviceService deviceService;
51 private final HostService hostService;
52
53 private final Set<String> ids;
54 private final String hover;
55
56 private final Set<Device> devices = new HashSet<>();
57 private final Set<Host> hosts = new HashSet<>();
58
59 /**
60 * Creates a node selection entity, from the given payload, using the
61 * supplied device and host services.
62 *
63 * @param payload message payload
64 * @param deviceService device service
65 * @param hostService host service
66 */
67 public NodeSelection(ObjectNode payload,
68 DeviceService deviceService,
69 HostService hostService) {
70 this.deviceService = deviceService;
71 this.hostService = hostService;
72
73 ids = extractIds(payload);
74 hover = extractHover(payload);
75
76 Set<String> unmatched = findDevices(ids);
77 unmatched = findHosts(unmatched);
78 if (unmatched.size() > 0) {
79 log.debug("Skipping unmatched IDs {}", unmatched);
80 }
81
82 if (!isNullOrEmpty(hover)) {
83 unmatched = new HashSet<>();
84 unmatched.add(hover);
85 unmatched = findDevices(unmatched);
86 unmatched = findHosts(unmatched);
87 if (unmatched.size() > 0) {
88 log.debug("Skipping unmatched HOVER {}", unmatched);
89 }
90 }
91 }
92
93 /**
94 * Returns a view of the selected devices.
95 *
96 * @return selected devices
97 */
98 public Set<Device> devices() {
99 return Collections.unmodifiableSet(devices);
100 }
101
102 /**
103 * Returns a view of the selected hosts.
104 *
105 * @return selected hosts
106 */
107 public Set<Host> hosts() {
108 return Collections.unmodifiableSet(hosts);
109 }
110
111 /**
112 * Returns true if nothing is selected.
113 *
114 * @return true if nothing selected
115 */
116 public boolean none() {
117 return devices().size() == 0 && hosts().size() == 0;
118 }
119
120 @Override
121 public String toString() {
122 return "NodeSelection{" +
123 "ids=" + ids +
124 ", hover='" + hover + '\'' +
125 ", #devices=" + devices.size() +
126 ", #hosts=" + hosts.size() +
127 '}';
128 }
129
130 // == helper methods
131
132 private Set<String> extractIds(ObjectNode payload) {
133 ArrayNode array = (ArrayNode) payload.path(IDS);
134 if (array == null || array.size() == 0) {
135 return Collections.emptySet();
136 }
137
138 Set<String> ids = new HashSet<>();
139 for (JsonNode node : array) {
140 ids.add(node.asText());
141 }
142 return ids;
143 }
144
145 private String extractHover(ObjectNode payload) {
146 return JsonUtils.string(payload, HOVER);
147 }
148
149 private Set<String> findDevices(Set<String> ids) {
150 Set<String> unmatched = new HashSet<>();
151 Device device;
152
153 for (String id : ids) {
154 try {
155 device = deviceService.getDevice(deviceId(id));
156 if (device != null) {
157 devices.add(device);
158 } else {
159 log.debug("Device with ID {} not found", id);
160 }
161 } catch (IllegalArgumentException e) {
162 unmatched.add(id);
163 }
164 }
165 return unmatched;
166 }
167
168 private Set<String> findHosts(Set<String> ids) {
169 Set<String> unmatched = new HashSet<>();
170 Host host;
171
172 for (String id : ids) {
173 try {
174 host = hostService.getHost(hostId(id));
175 if (host != null) {
176 hosts.add(host);
177 } else {
178 log.debug("Host with ID {} not found", id);
179 }
180 } catch (IllegalArgumentException e) {
181 unmatched.add(id);
182 }
183 }
184 return unmatched;
185 }
Simon Hunta17fa672015-08-19 18:42:22 -0700186}