Simon Hunt | d5b9673 | 2016-07-08 13:22:27 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present 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 | package org.onosproject.ui.impl.topo; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import org.onlab.osgi.ServiceDirectory; |
| 23 | import org.onosproject.cluster.ClusterService; |
| 24 | import org.onosproject.cluster.NodeId; |
| 25 | import org.onosproject.incubator.net.PortStatisticsService; |
| 26 | import org.onosproject.incubator.net.tunnel.TunnelService; |
| 27 | import org.onosproject.mastership.MastershipService; |
| 28 | import org.onosproject.net.device.DeviceService; |
| 29 | import org.onosproject.net.flow.FlowRuleService; |
| 30 | import org.onosproject.net.host.HostService; |
| 31 | import org.onosproject.net.intent.IntentService; |
| 32 | import org.onosproject.net.link.LinkService; |
| 33 | import org.onosproject.net.region.Region; |
| 34 | import org.onosproject.net.statistic.StatisticService; |
| 35 | import org.onosproject.net.topology.TopologyService; |
| 36 | import org.onosproject.ui.model.topo.UiClusterMember; |
| 37 | import org.onosproject.ui.model.topo.UiDevice; |
| 38 | import org.onosproject.ui.model.topo.UiHost; |
| 39 | import org.onosproject.ui.model.topo.UiLink; |
| 40 | import org.onosproject.ui.model.topo.UiRegion; |
| 41 | import org.onosproject.ui.model.topo.UiTopoLayout; |
| 42 | |
| 43 | import java.util.List; |
| 44 | |
| 45 | import static com.google.common.base.Preconditions.checkNotNull; |
| 46 | |
| 47 | /** |
| 48 | * Facility for creating JSON messages to send to the topology view in the |
| 49 | * Web client. |
| 50 | */ |
| 51 | class Topo2Jsonifier { |
| 52 | |
| 53 | private final ObjectMapper mapper = new ObjectMapper(); |
| 54 | |
| 55 | private ServiceDirectory directory; |
| 56 | private ClusterService clusterService; |
| 57 | private DeviceService deviceService; |
| 58 | private LinkService linkService; |
| 59 | private HostService hostService; |
| 60 | private MastershipService mastershipService; |
| 61 | private IntentService intentService; |
| 62 | private FlowRuleService flowService; |
| 63 | private StatisticService flowStatsService; |
| 64 | private PortStatisticsService portStatsService; |
| 65 | private TopologyService topologyService; |
| 66 | private TunnelService tunnelService; |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * Creates an instance with a reference to the services directory, so that |
| 71 | * additional information about network elements may be looked up on |
| 72 | * on the fly. |
| 73 | * |
| 74 | * @param directory service directory |
| 75 | */ |
| 76 | Topo2Jsonifier(ServiceDirectory directory) { |
| 77 | this.directory = checkNotNull(directory, "Directory cannot be null"); |
| 78 | |
| 79 | clusterService = directory.get(ClusterService.class); |
| 80 | deviceService = directory.get(DeviceService.class); |
| 81 | linkService = directory.get(LinkService.class); |
| 82 | hostService = directory.get(HostService.class); |
| 83 | mastershipService = directory.get(MastershipService.class); |
| 84 | intentService = directory.get(IntentService.class); |
| 85 | flowService = directory.get(FlowRuleService.class); |
| 86 | flowStatsService = directory.get(StatisticService.class); |
| 87 | portStatsService = directory.get(PortStatisticsService.class); |
| 88 | topologyService = directory.get(TopologyService.class); |
| 89 | tunnelService = directory.get(TunnelService.class); |
| 90 | |
| 91 | } |
| 92 | |
| 93 | private ObjectNode objectNode() { |
| 94 | return mapper.createObjectNode(); |
| 95 | } |
| 96 | |
| 97 | private ArrayNode arrayNode() { |
| 98 | return mapper.createArrayNode(); |
| 99 | } |
| 100 | |
| 101 | private String nullIsEmpty(Object o) { |
| 102 | return o == null ? "" : o.toString(); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /** |
| 107 | * Returns a JSON representation of the cluster members (ONOS instances). |
| 108 | * |
| 109 | * @param instances the instance model objects |
| 110 | * @return a JSON representation of the data |
| 111 | */ |
| 112 | ObjectNode instances(List<UiClusterMember> instances) { |
| 113 | NodeId local = clusterService.getLocalNode().id(); |
| 114 | ObjectNode payload = objectNode(); |
| 115 | |
| 116 | ArrayNode members = arrayNode(); |
| 117 | payload.set("members", members); |
| 118 | for (UiClusterMember member : instances) { |
| 119 | members.add(json(member, member.id().equals(local))); |
| 120 | } |
| 121 | |
| 122 | return payload; |
| 123 | } |
| 124 | |
| 125 | private ObjectNode json(UiClusterMember member, boolean isUiAttached) { |
| 126 | return objectNode() |
| 127 | .put("id", member.id().toString()) |
| 128 | .put("ip", member.ip().toString()) |
| 129 | .put("online", member.isOnline()) |
| 130 | .put("ready", member.isReady()) |
| 131 | .put("uiAttached", isUiAttached) |
| 132 | .put("switches", member.deviceCount()); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns a JSON representation of the layout to use for displaying in |
| 137 | * the topology view. |
| 138 | * |
| 139 | * @param layout the layout to transform |
| 140 | * @return a JSON representation of the data |
| 141 | */ |
| 142 | ObjectNode layout(UiTopoLayout layout) { |
| 143 | return objectNode() |
| 144 | .put("id", layout.id().toString()) |
| 145 | .put("parent", nullIsEmpty(layout.parent())) |
| 146 | .put("region", nullIsEmpty(layout.regionId())) |
| 147 | .put("regionName", regionName(layout.region())); |
| 148 | } |
| 149 | |
| 150 | private String regionName(Region region) { |
| 151 | return region == null ? "" : region.name(); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Returns a JSON representation of the region to display in the topology |
| 156 | * view. |
| 157 | * |
| 158 | * @param region the region to transform to JSON |
| 159 | * @return a JSON representation of the data |
| 160 | */ |
| 161 | ObjectNode region(UiRegion region) { |
| 162 | ObjectNode payload = objectNode(); |
| 163 | |
| 164 | if (region == null) { |
| 165 | payload.put("note", "no-region"); |
| 166 | return payload; |
| 167 | } |
| 168 | |
| 169 | payload.put("id", region.id().toString()); |
| 170 | |
| 171 | ArrayNode layerOrder = arrayNode(); |
| 172 | payload.set("layerOrder", layerOrder); |
| 173 | region.layerOrder().forEach(layerOrder::add); |
| 174 | |
| 175 | ArrayNode devices = arrayNode(); |
| 176 | payload.set("devices", devices); |
| 177 | for (UiDevice device : region.devices()) { |
| 178 | devices.add(json(device)); |
| 179 | } |
| 180 | |
| 181 | ArrayNode hosts = arrayNode(); |
| 182 | payload.set("hosts", hosts); |
| 183 | for (UiHost host : region.hosts()) { |
| 184 | hosts.add(json(host)); |
| 185 | } |
| 186 | |
| 187 | ArrayNode links = arrayNode(); |
| 188 | payload.set("links", links); |
| 189 | for (UiLink link : region.links()) { |
| 190 | links.add(json(link)); |
| 191 | } |
| 192 | |
| 193 | return payload; |
| 194 | } |
| 195 | |
| 196 | private ObjectNode json(UiDevice device) { |
| 197 | ObjectNode node = objectNode() |
| 198 | .put("id", device.id().toString()) |
| 199 | .put("type", device.type()) |
| 200 | .put("online", device.isOnline()) |
| 201 | .put("master", device.master().toString()) |
| 202 | .put("layer", device.layer()); |
| 203 | |
| 204 | // TODO: complete device details |
| 205 | // addLabels(node, device); |
| 206 | // addProps(node, device); |
| 207 | // addGeoLocation(node, device); |
| 208 | // addMetaUi(node, device); |
| 209 | |
| 210 | return node; |
| 211 | } |
| 212 | |
| 213 | private void addLabels(ObjectNode node, UiDevice device) { |
| 214 | |
| 215 | } |
| 216 | |
| 217 | private ObjectNode json(UiHost host) { |
| 218 | return objectNode() |
| 219 | .put("id", host.id().toString()) |
| 220 | .put("layer", host.layer()); |
| 221 | // TODO: complete host details |
| 222 | } |
| 223 | |
| 224 | |
| 225 | private ObjectNode json(UiLink link) { |
| 226 | return objectNode() |
| 227 | .put("id", link.id().toString()); |
| 228 | // TODO: complete link details |
| 229 | } |
| 230 | |
| 231 | |
| 232 | } |