Simon Hunt | d7f7bcc | 2015-05-08 14:13:17 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package org.onosproject.ui.impl.topo; |
| 19 | |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onosproject.cluster.ClusterEvent; |
| 28 | import org.onosproject.cluster.ClusterService; |
| 29 | import org.onosproject.cluster.ControllerNode; |
| 30 | import org.onosproject.event.AbstractListenerRegistry; |
| 31 | import org.onosproject.event.EventDeliveryService; |
| 32 | import org.onosproject.mastership.MastershipService; |
| 33 | import org.onosproject.net.Device; |
| 34 | import org.onosproject.net.Host; |
| 35 | import org.onosproject.net.Link; |
| 36 | import org.onosproject.net.device.DeviceEvent; |
| 37 | import org.onosproject.net.device.DeviceService; |
| 38 | import org.onosproject.net.host.HostEvent; |
| 39 | import org.onosproject.net.host.HostService; |
| 40 | import org.onosproject.net.link.LinkEvent; |
| 41 | import org.onosproject.net.link.LinkService; |
| 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
| 45 | import java.util.ArrayList; |
| 46 | import java.util.Collections; |
| 47 | import java.util.Comparator; |
| 48 | import java.util.List; |
| 49 | |
| 50 | import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_ADDED; |
| 51 | import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED; |
| 52 | import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED; |
| 53 | import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED; |
| 54 | |
| 55 | |
| 56 | /** |
| 57 | * Maintains a UI-centric model of the topology, as inferred from interactions |
| 58 | * with the different (device, host, link, ...) services. Will serve up this |
| 59 | * model to anyone who cares to {@link TopoUiListener listen}. |
| 60 | */ |
| 61 | @Component(immediate = true) |
| 62 | @Service |
| 63 | public class TopoUiModelManager implements TopoUiModelService { |
| 64 | |
| 65 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 66 | |
| 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected ClusterService clusterService; |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected DeviceService deviceService; |
| 72 | |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | protected LinkService linkService; |
| 75 | |
| 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 77 | protected HostService hostService; |
| 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected MastershipService mastershipService; |
| 81 | |
| 82 | |
| 83 | |
| 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 85 | protected EventDeliveryService eventDispatcher; |
| 86 | |
| 87 | |
| 88 | private AbstractListenerRegistry<TopoUiEvent, TopoUiListener> |
| 89 | listenerRegistry = new AbstractListenerRegistry<>(); |
| 90 | |
| 91 | |
| 92 | private final TopoMessageFactory messageFactory = new TopoMessageFactory(); |
| 93 | private final MetaDb metaDb = new MetaDb(); |
| 94 | |
| 95 | |
| 96 | @Activate |
| 97 | public void activate() { |
| 98 | eventDispatcher.addSink(TopoUiEvent.class, listenerRegistry); |
| 99 | messageFactory.injectServices( |
| 100 | metaDb, |
| 101 | clusterService, |
| 102 | deviceService, |
| 103 | linkService, |
| 104 | hostService, |
| 105 | mastershipService |
| 106 | ); |
| 107 | log.info("Started"); |
| 108 | } |
| 109 | |
| 110 | @Deactivate |
| 111 | public void deactivate() { |
| 112 | eventDispatcher.removeSink(TopoUiEvent.class); |
| 113 | log.info("Stopped"); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void addListener(TopoUiListener listener) { |
| 118 | listenerRegistry.addListener(listener); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public void removeListener(TopoUiListener listener) { |
| 123 | listenerRegistry.removeListener(listener); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public List<ObjectNode> getInitialState() { |
| 128 | List<ObjectNode> results = new ArrayList<>(); |
| 129 | addInstances(results); |
| 130 | addDevices(results); |
| 131 | addLinks(results); |
| 132 | addHosts(results); |
| 133 | return results; |
| 134 | } |
| 135 | |
| 136 | // ===================================================================== |
| 137 | |
| 138 | private static final Comparator<? super ControllerNode> NODE_COMPARATOR = |
| 139 | (o1, o2) -> o1.id().toString().compareTo(o2.id().toString()); |
| 140 | |
| 141 | // ===================================================================== |
| 142 | |
| 143 | private void addInstances(List<ObjectNode> results) { |
| 144 | List<ControllerNode> nodes = new ArrayList<>(clusterService.getNodes()); |
| 145 | Collections.sort(nodes, NODE_COMPARATOR); |
| 146 | for (ControllerNode node : nodes) { |
| 147 | ClusterEvent ev = new ClusterEvent(INSTANCE_ADDED, node); |
| 148 | results.add(messageFactory.instanceMessage(ev)); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | private void addDevices(List<ObjectNode> results) { |
| 153 | // Send optical first, others later -- for layered rendering |
| 154 | List<DeviceEvent> deferred = new ArrayList<>(); |
| 155 | |
| 156 | for (Device device : deviceService.getDevices()) { |
| 157 | DeviceEvent ev = new DeviceEvent(DEVICE_ADDED, device); |
| 158 | if (device.type() == Device.Type.ROADM) { |
| 159 | results.add(messageFactory.deviceMessage(ev)); |
| 160 | } else { |
| 161 | deferred.add(ev); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | for (DeviceEvent ev : deferred) { |
| 166 | results.add(messageFactory.deviceMessage(ev)); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | private void addLinks(List<ObjectNode> results) { |
| 171 | // Send optical first, others later -- for layered rendering |
| 172 | List<LinkEvent> deferred = new ArrayList<>(); |
| 173 | |
| 174 | for (Link link : linkService.getLinks()) { |
| 175 | LinkEvent ev = new LinkEvent(LINK_ADDED, link); |
| 176 | if (link.type() == Link.Type.OPTICAL) { |
| 177 | results.add(messageFactory.linkMessage(ev)); |
| 178 | } else { |
| 179 | deferred.add(ev); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | for (LinkEvent ev : deferred) { |
| 184 | results.add(messageFactory.linkMessage(ev)); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | private void addHosts(List<ObjectNode> results) { |
| 189 | for (Host host : hostService.getHosts()) { |
| 190 | HostEvent ev = new HostEvent(HOST_ADDED, host); |
| 191 | results.add(messageFactory.hostMessage(ev)); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // ===================================================================== |
| 196 | |
| 197 | private void post(TopoUiEvent event) { |
| 198 | if (event != null) { |
| 199 | eventDispatcher.post(event); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | // NOTE: session-independent state only |
| 204 | // private inner classes to listen to device/host/link events |
| 205 | // TODO.. |
| 206 | } |