Simon Hunt
Committed by Gerrit Code Review

GUI -- Major re-write of server-side Topology View code. WIP.

Change-Id: I332e8e59dc2271d3897c3a5c2fafa775324ef72d
......@@ -39,6 +39,7 @@ public final class JsonUtils {
* @param payload event payload
* @return the object node representation
*/
@Deprecated
public static ObjectNode envelope(String type, long sid, ObjectNode payload) {
ObjectNode event = MAPPER.createObjectNode();
event.put("event", type);
......@@ -50,6 +51,20 @@ public final class JsonUtils {
}
/**
* Composes a message structure for the given message type and payload.
*
* @param type message type
* @param payload message payload
* @return the object node representation
*/
public static ObjectNode envelope(String type, ObjectNode payload) {
ObjectNode event = MAPPER.createObjectNode();
event.put("event", type);
event.set("payload", payload);
return event;
}
/**
* Returns the event type from the specified event.
* If the node does not have an "event" property, "unknown" is returned.
*
......
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.onosproject.ui.impl;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.ImmutableSet;
import org.onlab.osgi.ServiceDirectory;
import org.onosproject.core.CoreService;
import org.onosproject.ui.RequestHandler;
import org.onosproject.ui.UiConnection;
import org.onosproject.ui.UiMessageHandler;
import org.onosproject.ui.impl.topo.TopoUiEvent;
import org.onosproject.ui.impl.topo.TopoUiListener;
import org.onosproject.ui.impl.topo.TopoUiModelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Facility for handling inbound messages from the topology view, and
* generating outbound messages for the same.
*/
public class AltTopoViewMessageHandler extends UiMessageHandler {
private static final String TOPO_START = "topoStart";
private static final String TOPO_STOP = "topoStop";
private final Logger log = LoggerFactory.getLogger(getClass());
protected ServiceDirectory directory;
protected TopoUiModelService modelService;
private TopoUiListener modelListener;
private String version;
private boolean topoActive = false;
@Override
public void init(UiConnection connection, ServiceDirectory directory) {
super.init(connection, directory);
this.directory = checkNotNull(directory, "Directory cannot be null");
modelService = directory.get(TopoUiModelService.class);
modelListener = new ModelListener();
version = getVersion();
}
private String getVersion() {
String ver = directory.get(CoreService.class).version().toString();
return ver.replace(".SNAPSHOT", "*").replaceFirst("~.*$", "");
}
@Override
protected Collection<RequestHandler> getHandlers() {
return ImmutableSet.of(
new TopoStart(),
new TopoStop()
);
}
// =====================================================================
// Request Handlers for (topo view) events from the UI...
private final class TopoStart extends RequestHandler {
private TopoStart() {
super(TOPO_START);
}
@Override
public void process(long sid, ObjectNode payload) {
topoActive = true;
modelService.addListener(modelListener);
sendMessages(modelService.getInitialState());
log.debug(TOPO_START);
}
}
private final class TopoStop extends RequestHandler {
private TopoStop() {
super(TOPO_STOP);
}
@Override
public void process(long sid, ObjectNode payload) {
topoActive = false;
modelService.removeListener(modelListener);
log.debug(TOPO_STOP);
}
}
// =====================================================================
// Private Helper Methods...
private void sendMessages(Collection<ObjectNode> messages) {
if (topoActive) {
UiConnection connection = connection();
if (connection != null) {
messages.forEach(connection::sendMessage);
}
}
}
// =====================================================================
// Our listener for model events so we can push changes out to the UI...
private class ModelListener implements TopoUiListener {
@Override
public void event(TopoUiEvent event) {
log.debug("Handle Event: {}", event);
// TODO: handle event
}
}
}
......@@ -101,6 +101,7 @@ import static org.onosproject.net.link.LinkEvent.Type.LINK_REMOVED;
/**
* Facility for creating messages bound for the topology viewer.
*/
@Deprecated
public abstract class TopologyViewMessageHandlerBase extends UiMessageHandler {
protected static final Logger log =
......
......@@ -78,6 +78,7 @@ public class UiExtensionManager implements UiExtensionService, SpriteService {
UiMessageHandlerFactory messageHandlerFactory =
() -> ImmutableList.of(
new TopologyViewMessageHandler(),
// new AltTopoViewMessageHandler(),
new DeviceViewMessageHandler(),
new LinkViewMessageHandler(),
new HostViewMessageHandler(),
......
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.onosproject.ui.impl.topo;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* A database of meta information stored for topology objects.
*/
// package private
class MetaDb {
private static Map<String, ObjectNode> metaCache = new ConcurrentHashMap<>();
/**
* Adds meta UI information about the specified object to the given payload.
*
* @param id object identifier
* @param payload payload to which the info should be added
*/
public void addMetaUi(String id, ObjectNode payload) {
ObjectNode meta = metaCache.get(id);
if (meta != null) {
payload.set("metaUi", meta);
}
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.onosproject.ui.impl.topo;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.onosproject.event.AbstractEvent;
/**
* Describes Topology UI Model events.
*/
public class TopoUiEvent extends AbstractEvent<TopoUiEvent.Type, ObjectNode> {
/**
* Type of Topology UI Model events.
*/
public enum Type {
INSTANCE_ADDED,
INSTANCE_REMOVED,
DEVICE_ADDED,
DEVICE_UPDATED,
DEVICE_REMOVED,
LINK_ADDED,
LINK_UPDATED,
LINK_REMOVED,
HOST_ADDED,
HOST_UPDATED,
HOST_REMOVED
}
protected TopoUiEvent(Type type, ObjectNode subject) {
super(type, subject);
}
protected TopoUiEvent(Type type, ObjectNode subject, long time) {
super(type, subject, time);
}
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.onosproject.ui.impl.topo;
import org.onosproject.event.EventListener;
/**
* Defines a listener of Topology UI Model events.
*/
public interface TopoUiListener extends EventListener<TopoUiEvent> {
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.onosproject.ui.impl.topo;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.onosproject.cluster.ClusterEvent;
import org.onosproject.cluster.ClusterService;
import org.onosproject.cluster.ControllerNode;
import org.onosproject.event.AbstractListenerRegistry;
import org.onosproject.event.EventDeliveryService;
import org.onosproject.mastership.MastershipService;
import org.onosproject.net.Device;
import org.onosproject.net.Host;
import org.onosproject.net.Link;
import org.onosproject.net.device.DeviceEvent;
import org.onosproject.net.device.DeviceService;
import org.onosproject.net.host.HostEvent;
import org.onosproject.net.host.HostService;
import org.onosproject.net.link.LinkEvent;
import org.onosproject.net.link.LinkService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_ADDED;
import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED;
import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
/**
* Maintains a UI-centric model of the topology, as inferred from interactions
* with the different (device, host, link, ...) services. Will serve up this
* model to anyone who cares to {@link TopoUiListener listen}.
*/
@Component(immediate = true)
@Service
public class TopoUiModelManager implements TopoUiModelService {
private final Logger log = LoggerFactory.getLogger(getClass());
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected ClusterService clusterService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected DeviceService deviceService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected LinkService linkService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected HostService hostService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected MastershipService mastershipService;
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected EventDeliveryService eventDispatcher;
private AbstractListenerRegistry<TopoUiEvent, TopoUiListener>
listenerRegistry = new AbstractListenerRegistry<>();
private final TopoMessageFactory messageFactory = new TopoMessageFactory();
private final MetaDb metaDb = new MetaDb();
@Activate
public void activate() {
eventDispatcher.addSink(TopoUiEvent.class, listenerRegistry);
messageFactory.injectServices(
metaDb,
clusterService,
deviceService,
linkService,
hostService,
mastershipService
);
log.info("Started");
}
@Deactivate
public void deactivate() {
eventDispatcher.removeSink(TopoUiEvent.class);
log.info("Stopped");
}
@Override
public void addListener(TopoUiListener listener) {
listenerRegistry.addListener(listener);
}
@Override
public void removeListener(TopoUiListener listener) {
listenerRegistry.removeListener(listener);
}
@Override
public List<ObjectNode> getInitialState() {
List<ObjectNode> results = new ArrayList<>();
addInstances(results);
addDevices(results);
addLinks(results);
addHosts(results);
return results;
}
// =====================================================================
private static final Comparator<? super ControllerNode> NODE_COMPARATOR =
(o1, o2) -> o1.id().toString().compareTo(o2.id().toString());
// =====================================================================
private void addInstances(List<ObjectNode> results) {
List<ControllerNode> nodes = new ArrayList<>(clusterService.getNodes());
Collections.sort(nodes, NODE_COMPARATOR);
for (ControllerNode node : nodes) {
ClusterEvent ev = new ClusterEvent(INSTANCE_ADDED, node);
results.add(messageFactory.instanceMessage(ev));
}
}
private void addDevices(List<ObjectNode> results) {
// Send optical first, others later -- for layered rendering
List<DeviceEvent> deferred = new ArrayList<>();
for (Device device : deviceService.getDevices()) {
DeviceEvent ev = new DeviceEvent(DEVICE_ADDED, device);
if (device.type() == Device.Type.ROADM) {
results.add(messageFactory.deviceMessage(ev));
} else {
deferred.add(ev);
}
}
for (DeviceEvent ev : deferred) {
results.add(messageFactory.deviceMessage(ev));
}
}
private void addLinks(List<ObjectNode> results) {
// Send optical first, others later -- for layered rendering
List<LinkEvent> deferred = new ArrayList<>();
for (Link link : linkService.getLinks()) {
LinkEvent ev = new LinkEvent(LINK_ADDED, link);
if (link.type() == Link.Type.OPTICAL) {
results.add(messageFactory.linkMessage(ev));
} else {
deferred.add(ev);
}
}
for (LinkEvent ev : deferred) {
results.add(messageFactory.linkMessage(ev));
}
}
private void addHosts(List<ObjectNode> results) {
for (Host host : hostService.getHosts()) {
HostEvent ev = new HostEvent(HOST_ADDED, host);
results.add(messageFactory.hostMessage(ev));
}
}
// =====================================================================
private void post(TopoUiEvent event) {
if (event != null) {
eventDispatcher.post(event);
}
}
// NOTE: session-independent state only
// private inner classes to listen to device/host/link events
// TODO..
}
/*
* Copyright 2015 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.onosproject.ui.impl.topo;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.List;
/**t
* Defines the API for the Topology UI Model.
*/
public interface TopoUiModelService {
/**
* Registers the specified listener for Topology UI Model events.
*
* @param listener the listener
*/
void addListener(TopoUiListener listener);
/**
* Unregister the specified listener.
*
* @param listener the listener
*/
void removeListener(TopoUiListener listener);
/**
* Returns events describing the current state of the model.
* <p>
* These will be in the form of "addInstance", "addDevice", "addLink",
* and "addHost" events, as appropriate.
*
* @return initial state events
*/
List<ObjectNode> getInitialState();
}