Thomas Vachuska | 8871691 | 2015-11-13 08:15:01 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014-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 | package org.onosproject.byon.gui; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.base.Strings; |
| 22 | import com.google.common.collect.ImmutableSet; |
| 23 | import org.onlab.osgi.ServiceDirectory; |
| 24 | import org.onos.byon.NetworkService; |
| 25 | import org.onosproject.net.Device; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.Host; |
| 28 | import org.onosproject.net.HostId; |
| 29 | import org.onosproject.net.Link; |
| 30 | import org.onosproject.net.device.DeviceService; |
| 31 | import org.onosproject.net.host.HostService; |
| 32 | import org.onosproject.net.link.LinkService; |
| 33 | import org.onosproject.ui.RequestHandler; |
| 34 | import org.onosproject.ui.UiConnection; |
| 35 | import org.onosproject.ui.UiMessageHandler; |
| 36 | import org.onosproject.ui.topo.DeviceHighlight; |
| 37 | import org.onosproject.ui.topo.Highlights; |
| 38 | import org.onosproject.ui.topo.NodeBadge; |
| 39 | import org.onosproject.ui.topo.TopoJson; |
| 40 | |
| 41 | import java.util.Collection; |
| 42 | import java.util.HashSet; |
| 43 | import java.util.Set; |
| 44 | import java.util.TimerTask; |
| 45 | |
| 46 | /** |
| 47 | * Message handler for Network topology overlay events. |
| 48 | */ |
| 49 | public class NetworkOverlayMessageHandler extends UiMessageHandler { |
| 50 | |
| 51 | private static final String BYON_FETCH_NETWORKS_REQ = "byonFetchNetworksRequest"; |
| 52 | private static final String BYON_FETCH_NETWORKS_RESP = "byonFetchNetworksResponse"; |
| 53 | |
| 54 | private static final String NETWORKS = "networks"; |
| 55 | |
| 56 | private NetworkService networkService; |
| 57 | private HostService hostService1; |
| 58 | |
| 59 | @Override |
| 60 | public void init(UiConnection connection, ServiceDirectory directory) { |
| 61 | super.init(connection, directory); |
| 62 | networkService = directory.get(NetworkService.class); |
| 63 | hostService1 = get(HostService.class); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | protected Collection<RequestHandler> createRequestHandlers() { |
| 68 | return ImmutableSet.of( |
| 69 | new FetchNetworksHandler() |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | // === ------------------------- |
| 74 | // === Handler classes |
| 75 | |
| 76 | private class FetchNetworksHandler extends RequestHandler { |
| 77 | public FetchNetworksHandler() { |
| 78 | super(BYON_FETCH_NETWORKS_REQ); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public void process(long sid, ObjectNode payload) { |
| 83 | ObjectNode rootNode = objectNode(); |
| 84 | ArrayNode networks = arrayNode(); |
| 85 | rootNode.set(NETWORKS, networks); |
| 86 | |
| 87 | for (String name : networkService.getNetworks()) { |
| 88 | networks.add(networkData(name)); |
| 89 | } |
| 90 | sendMessage(BYON_FETCH_NETWORKS_RESP, 0, rootNode); |
| 91 | } |
| 92 | |
| 93 | private ObjectNode networkData(String name) { |
| 94 | return objectNode() |
| 95 | .put("name", name) |
| 96 | .put("hostCount", networkService.getHosts(name).size()); |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | } |