Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 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 | package org.onosproject.ui.impl; |
| 17 | |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableSet; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IpAddress; |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 22 | import org.onosproject.net.AnnotationKeys; |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 23 | import org.onosproject.net.Host; |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 24 | import org.onosproject.net.HostId; |
| 25 | import org.onosproject.net.HostLocation; |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 26 | import org.onosproject.net.config.NetworkConfigService; |
| 27 | import org.onosproject.net.config.basics.BasicHostConfig; |
Bri Prebilic Cole | 19a32dd | 2015-03-26 18:00:03 -0700 | [diff] [blame] | 28 | import org.onosproject.net.host.HostService; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.UiMessageHandler; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.table.CellFormatter; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.table.TableModel; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 33 | import org.onosproject.ui.table.TableRequestHandler; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 34 | import org.onosproject.ui.table.cell.HostLocationFormatter; |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 37 | |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 38 | import java.util.ArrayList; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 39 | import java.util.Collection; |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 40 | import java.util.Collections; |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 41 | import java.util.Iterator; |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 42 | import java.util.List; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 43 | import java.util.Set; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 44 | |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 45 | import static com.google.common.base.Strings.emptyToNull; |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 46 | import static com.google.common.base.Strings.isNullOrEmpty; |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 47 | import static org.onosproject.net.HostId.hostId; |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 48 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Message handler for host view related messages. |
| 51 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 52 | public class HostViewMessageHandler extends UiMessageHandler { |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 53 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 54 | private static final String HOST_DATA_REQ = "hostDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 55 | private static final String HOST_DATA_RESP = "hostDataResponse"; |
| 56 | private static final String HOSTS = "hosts"; |
| 57 | |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 58 | private static final String HOST_DETAILS_REQ = "hostDetailsRequest"; |
| 59 | private static final String HOST_DETAILS_RESP = "hostDetailsResponse"; |
| 60 | private static final String DETAILS = "details"; |
| 61 | |
| 62 | private static final String HOST_NAME_CHANGE_REQ = "hostNameChangeRequest"; |
| 63 | private static final String HOST_NAME_CHANGE_RESP = "hostNameChangeResponse"; |
| 64 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 65 | private static final String TYPE_IID = "_iconid_type"; |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 66 | private static final String NAME = "name"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 67 | private static final String ID = "id"; |
| 68 | private static final String MAC = "mac"; |
| 69 | private static final String VLAN = "vlan"; |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 70 | private static final String IP = "ip"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 71 | private static final String IPS = "ips"; |
| 72 | private static final String LOCATION = "location"; |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 73 | private static final String LOCATIONS = "locations"; |
| 74 | private static final String CONFIGURED = "configured"; |
| 75 | |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 76 | private static final String DASH = "-"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 77 | |
| 78 | private static final String HOST_ICON_PREFIX = "hostIcon_"; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 79 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 80 | private static final String[] COL_IDS = { |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 81 | TYPE_IID, NAME, ID, MAC, VLAN, CONFIGURED, IPS, LOCATION |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 82 | }; |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 83 | |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 84 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 85 | |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 86 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 87 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 88 | return ImmutableSet.of( |
| 89 | new HostDataRequest(), |
| 90 | new DetailRequestHandler(), |
| 91 | new NameChangeHandler() |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | private String getTypeIconId(Host host) { |
| 96 | String hostType = host.annotations().value(AnnotationKeys.TYPE); |
| 97 | return HOST_ICON_PREFIX + |
| 98 | (isNullOrEmpty(hostType) ? "endstation" : hostType); |
| 99 | } |
| 100 | |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 101 | // Returns the first of the given set of IP addresses as a string. |
| 102 | private String ip(Set<IpAddress> ipAddresses) { |
| 103 | Iterator<IpAddress> it = ipAddresses.iterator(); |
| 104 | return it.hasNext() ? it.next().toString() : "unknown"; |
| 105 | } |
| 106 | |
| 107 | private boolean useDefaultName(String nameAnnotated) { |
| 108 | return isNullOrEmpty(nameAnnotated) || DASH.equals(nameAnnotated); |
| 109 | } |
| 110 | |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 111 | // returns the "friendly name" for the host |
| 112 | private String getHostName(Host host) { |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 113 | String name = host.annotations().value(AnnotationKeys.NAME); |
| 114 | return useDefaultName(name) ? ip(host.ipAddresses()) : name; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 117 | // handler for host table requests |
| 118 | private final class HostDataRequest extends TableRequestHandler { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 119 | private static final String NO_ROWS_MESSAGE = "No hosts found"; |
| 120 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 121 | private HostDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 122 | super(HOST_DATA_REQ, HOST_DATA_RESP, HOSTS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 126 | protected String[] getColumnIds() { |
| 127 | return COL_IDS; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 128 | } |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 129 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 130 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 131 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 132 | return NO_ROWS_MESSAGE; |
| 133 | } |
| 134 | |
| 135 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 136 | protected TableModel createTableModel() { |
| 137 | TableModel tm = super.createTableModel(); |
| 138 | tm.setFormatter(LOCATION, HostLocationFormatter.INSTANCE); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 139 | tm.setFormatter(IPS, new IpSetFormatter()); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 140 | return tm; |
| 141 | } |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 142 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 143 | @Override |
| 144 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 145 | HostService hs = get(HostService.class); |
| 146 | for (Host host : hs.getHosts()) { |
| 147 | populateRow(tm.addRow(), host); |
| 148 | } |
| 149 | } |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 150 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 151 | private void populateRow(TableModel.Row row, Host host) { |
| 152 | row.cell(TYPE_IID, getTypeIconId(host)) |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 153 | .cell(NAME, getHostName(host)) |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 154 | .cell(ID, host.id()) |
| 155 | .cell(MAC, host.mac()) |
| 156 | .cell(VLAN, host.vlan()) |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 157 | .cell(CONFIGURED, host.configured()) |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 158 | .cell(IPS, host.ipAddresses()) |
| 159 | .cell(LOCATION, host.location()); |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 160 | // Note: leave complete list of all LOCATIONS to the details panel |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 161 | } |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 162 | |
| 163 | private final class IpSetFormatter implements CellFormatter { |
| 164 | private static final String COMMA = ", "; |
| 165 | |
| 166 | @Override |
| 167 | public String format(Object value) { |
| 168 | Set<IpAddress> ips = (Set<IpAddress>) value; |
| 169 | if (ips.isEmpty()) { |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 170 | return "(No IP Addresses)"; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 171 | } |
| 172 | StringBuilder sb = new StringBuilder(); |
| 173 | for (IpAddress ip : ips) { |
| 174 | sb.append(ip.toString()) |
| 175 | .append(COMMA); |
| 176 | } |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 177 | return removeTrailingComma(sb).toString(); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | private StringBuilder removeTrailingComma(StringBuilder sb) { |
| 181 | int pos = sb.lastIndexOf(COMMA); |
| 182 | sb.delete(pos, sb.length()); |
| 183 | return sb; |
| 184 | } |
| 185 | } |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 186 | } |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 187 | |
| 188 | |
| 189 | private final class DetailRequestHandler extends RequestHandler { |
| 190 | private DetailRequestHandler() { |
| 191 | super(HOST_DETAILS_REQ); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public void process(ObjectNode payload) { |
| 196 | String id = string(payload, ID, ""); |
| 197 | |
| 198 | HostId hostId = hostId(id); |
| 199 | HostService service = get(HostService.class); |
| 200 | Host host = service.getHost(hostId); |
| 201 | ObjectNode data = objectNode(); |
| 202 | |
| 203 | data.put(TYPE_IID, getTypeIconId(host)) |
| 204 | .put(NAME, getHostName(host)) |
| 205 | .put(ID, hostId.toString()) |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 206 | .put(IP, ip(host.ipAddresses())) |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 207 | .put(MAC, host.mac().toString()) |
| 208 | .put(VLAN, host.vlan().toString()) |
| 209 | .put(CONFIGURED, host.configured()) |
| 210 | .put(LOCATION, host.location().toString()); |
| 211 | |
| 212 | List<IpAddress> sortedIps = new ArrayList<>(host.ipAddresses()); |
| 213 | Collections.sort(sortedIps); |
| 214 | ArrayNode ips = arrayNode(); |
| 215 | for (IpAddress ip : sortedIps) { |
| 216 | ips.add(ip.toString()); |
| 217 | } |
| 218 | data.set(IPS, ips); |
| 219 | |
| 220 | List<HostLocation> sortedLocs = new ArrayList<>(host.locations()); |
| 221 | Collections.sort(sortedLocs); |
| 222 | ArrayNode locs = arrayNode(); |
| 223 | for (HostLocation hl : sortedLocs) { |
| 224 | locs.add(hl.toString()); |
| 225 | } |
| 226 | data.set(LOCATIONS, locs); |
| 227 | |
| 228 | ObjectNode root = objectNode(); |
| 229 | root.set(DETAILS, data); |
| 230 | |
| 231 | sendMessage(HOST_DETAILS_RESP, root); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | private final class NameChangeHandler extends RequestHandler { |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 236 | private NameChangeHandler() { |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 237 | super(HOST_NAME_CHANGE_REQ); |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public void process(ObjectNode payload) { |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 242 | HostId hostId = hostId(string(payload, ID, "")); |
| 243 | String name = emptyToNull(string(payload, NAME, null)); |
| 244 | log.debug("Name change request: {} -- '{}'", hostId, name); |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 245 | |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 246 | NetworkConfigService service = get(NetworkConfigService.class); |
| 247 | BasicHostConfig cfg = |
| 248 | service.addConfig(hostId, BasicHostConfig.class); |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 249 | |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 250 | // Name attribute missing from the payload (or empty string) |
| 251 | // means that the friendly name should be unset. |
| 252 | cfg.name(name); |
| 253 | cfg.apply(); |
| 254 | sendMessage(HOST_NAME_CHANGE_RESP, payload); |
Simon Hunt | 8694308 | 2017-06-15 13:18:42 -0700 | [diff] [blame] | 255 | } |
| 256 | } |
Thomas Vachuska | e586b79 | 2015-03-26 13:59:38 -0700 | [diff] [blame] | 257 | } |