Coding up ONOS GUI tutorial app.

Base table + dialog view
Base overlay

Change-Id: I1bac7dfc8ab916e9dce0df9b0be6a41be07433d7
diff --git a/onos-byon-gui/src/main/java/org/onosproject/byon/gui/NetworkOverlayMessageHandler.java b/onos-byon-gui/src/main/java/org/onosproject/byon/gui/NetworkOverlayMessageHandler.java
new file mode 100644
index 0000000..c0d2154
--- /dev/null
+++ b/onos-byon-gui/src/main/java/org/onosproject/byon/gui/NetworkOverlayMessageHandler.java
@@ -0,0 +1,100 @@
+/*
+ * Copyright 2014-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.byon.gui;
+
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableSet;
+import org.onlab.osgi.ServiceDirectory;
+import org.onos.byon.NetworkService;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.Host;
+import org.onosproject.net.HostId;
+import org.onosproject.net.Link;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.host.HostService;
+import org.onosproject.net.link.LinkService;
+import org.onosproject.ui.RequestHandler;
+import org.onosproject.ui.UiConnection;
+import org.onosproject.ui.UiMessageHandler;
+import org.onosproject.ui.topo.DeviceHighlight;
+import org.onosproject.ui.topo.Highlights;
+import org.onosproject.ui.topo.NodeBadge;
+import org.onosproject.ui.topo.TopoJson;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.TimerTask;
+
+/**
+ * Message handler for Network topology overlay events.
+ */
+public class NetworkOverlayMessageHandler extends UiMessageHandler {
+
+    private static final String BYON_FETCH_NETWORKS_REQ = "byonFetchNetworksRequest";
+    private static final String BYON_FETCH_NETWORKS_RESP = "byonFetchNetworksResponse";
+
+    private static final String NETWORKS = "networks";
+
+    private NetworkService networkService;
+    private HostService hostService1;
+
+    @Override
+    public void init(UiConnection connection, ServiceDirectory directory) {
+        super.init(connection, directory);
+        networkService = directory.get(NetworkService.class);
+        hostService1 = get(HostService.class);
+    }
+
+    @Override
+    protected Collection<RequestHandler> createRequestHandlers() {
+        return ImmutableSet.of(
+                new FetchNetworksHandler()
+        );
+    }
+
+    // === -------------------------
+    // === Handler classes
+
+    private class FetchNetworksHandler extends RequestHandler {
+        public FetchNetworksHandler() {
+            super(BYON_FETCH_NETWORKS_REQ);
+        }
+
+        @Override
+        public void process(long sid, ObjectNode payload) {
+            ObjectNode rootNode = objectNode();
+            ArrayNode networks = arrayNode();
+            rootNode.set(NETWORKS, networks);
+
+            for (String name : networkService.getNetworks()) {
+                networks.add(networkData(name));
+            }
+            sendMessage(BYON_FETCH_NETWORKS_RESP, 0, rootNode);
+        }
+
+        private ObjectNode networkData(String name) {
+            return objectNode()
+                    .put("name", name)
+                    .put("hostCount", networkService.getHosts(name).size());
+        }
+
+    }
+}
diff --git a/onos-byon-gui/src/main/java/org/onosproject/byon/gui/NetworkTableViewMessageHandler.java b/onos-byon-gui/src/main/java/org/onosproject/byon/gui/NetworkTableViewMessageHandler.java
new file mode 100644
index 0000000..31f17cf
--- /dev/null
+++ b/onos-byon-gui/src/main/java/org/onosproject/byon/gui/NetworkTableViewMessageHandler.java
@@ -0,0 +1,130 @@
+/*
+ * Copyright 2014-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.byon.gui;
+
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableSet;
+import org.onos.byon.NetworkService;
+import org.onosproject.net.Host;
+import org.onosproject.net.HostId;
+import org.onosproject.net.host.HostService;
+import org.onosproject.ui.RequestHandler;
+import org.onosproject.ui.UiMessageHandler;
+import org.onosproject.ui.table.TableModel;
+import org.onosproject.ui.table.TableRequestHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ * Message handler for UI Ref table-view.
+ */
+public class NetworkTableViewMessageHandler extends UiMessageHandler {
+
+    private static final String BYON_NETWORKS_DATA_REQ = "byonNetworkDataRequest";
+    private static final String BYON_NETWORKS_DATA_RESP = "byonNetworkDataResponse";
+    private static final String BYON_NETWORKS = "byonNetworks";
+
+    private static final String BYON_NETWORKS_DETAIL_REQ = "byonNetworkDetailsRequest";
+    private static final String BYON_NETWORKS_DETAIL_RESP = "byonNetworkDetailsResponse";
+    private static final String DETAILS = "details";
+
+    private static final String ID = "id";
+    private static final String HOST_COUNT = "hostCount";
+
+    private static final String[] COLUMN_IDS = {ID, HOST_COUNT};
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+
+    @Override
+    protected Collection<RequestHandler> createRequestHandlers() {
+        return ImmutableSet.of(
+                new ByonNetworkDataRequestHandler(),
+                new ByonNetworkDetailRequestHandler()
+        );
+    }
+
+    // handler for table view data requests
+    private final class ByonNetworkDataRequestHandler extends TableRequestHandler {
+
+        private ByonNetworkDataRequestHandler() {
+            super(BYON_NETWORKS_DATA_REQ, BYON_NETWORKS_DATA_RESP, BYON_NETWORKS);
+        }
+
+        @Override
+        protected String[] getColumnIds() {
+            return COLUMN_IDS;
+        }
+
+        @Override
+        protected void populateTable(TableModel tm, ObjectNode payload) {
+            NetworkService service = get(NetworkService.class);
+            for (String name: service.getNetworks()) {
+                int hostCount = service.getHosts(name).size();
+                populateRow(tm.addRow(), name, hostCount);
+            }
+        }
+
+        private void populateRow(TableModel.Row row, String name, int hostCount) {
+            row.cell(ID, name)
+                    .cell(HOST_COUNT, hostCount);
+        }
+    }
+
+    // handler for table view item details requests
+    private final class ByonNetworkDetailRequestHandler extends RequestHandler {
+
+        public static final String HOSTS = "hosts";
+
+        private ByonNetworkDetailRequestHandler() {
+            super(BYON_NETWORKS_DETAIL_REQ);
+        }
+
+        @Override
+        public void process(long sid, ObjectNode payload) {
+            String name = string(payload, ID, "(none)");
+
+            NetworkService networkService = get(NetworkService.class);
+            HostService hostService = get(HostService.class);
+            ObjectNode rootNode = objectNode();
+            ObjectNode data = objectNode();
+            rootNode.set(DETAILS, data);
+
+            ArrayNode hosts = arrayNode();
+            data.set(HOSTS, hosts);
+            data.put(ID, name);
+
+            for (HostId hostId : networkService.getHosts(name)) {
+                hosts.add(hostData(hostService.getHost(hostId)));
+            }
+            sendMessage(BYON_NETWORKS_DETAIL_RESP, 0, rootNode);
+        }
+
+        private ObjectNode hostData(Host host) {
+            return objectNode()
+                    .put("mac", host.mac().toString())
+                    .put("ip", host.ipAddresses().toString())
+                    .put("loc", host.location().toString());
+        }
+    }
+
+}
diff --git a/onos-byon-gui/src/main/java/org/onosproject/byon/gui/NetworkTopoOverlay.java b/onos-byon-gui/src/main/java/org/onosproject/byon/gui/NetworkTopoOverlay.java
new file mode 100644
index 0000000..42b582f
--- /dev/null
+++ b/onos-byon-gui/src/main/java/org/onosproject/byon/gui/NetworkTopoOverlay.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2014-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.byon.gui;
+
+import org.onosproject.ui.UiTopoOverlay;
+
+/**
+ * Network Topology Overlay hooks.
+ */
+public class NetworkTopoOverlay extends UiTopoOverlay {
+    // NOTE: this must match the ID defined in uiRefTopov.js
+    private static final String OVERLAY_ID = "byon-overlay";
+
+//    private static final String MY_TITLE = "My App Rocks!";
+//    private static final String MY_VERSION = "Beta-1.0.0042";
+//    private static final String MY_DEVICE_TITLE = "I changed the title";
+//
+//    private static final ButtonId FOO_BUTTON = new ButtonId("foo");
+//    private static final ButtonId BAR_BUTTON = new ButtonId("bar");
+
+    public NetworkTopoOverlay() {
+        super(OVERLAY_ID);
+    }
+
+/*
+    @Override
+    public void modifySummary(PropertyPanel pp) {
+        pp.title(MY_TITLE)
+                .typeId(TopoConstants.Glyphs.CROWN)
+                .removeProps(
+                        TOPOLOGY_SSCS,
+                        INTENTS,
+                        TUNNELS,
+                        FLOWS,
+                        VERSION
+                )
+                .addProp(VERSION, MY_VERSION);
+    }
+
+    @Override
+    public void modifyDeviceDetails(PropertyPanel pp, DeviceId deviceId) {
+        pp.title(MY_DEVICE_TITLE);
+        pp.removeProps(LATITUDE, LONGITUDE);
+
+        pp.addButton(FOO_BUTTON)
+                .addButton(BAR_BUTTON);
+
+        pp.removeButtons(TopoConstants.CoreButtons.SHOW_PORT_VIEW)
+                .removeButtons(TopoConstants.CoreButtons.SHOW_GROUP_VIEW);
+    }
+*/
+
+}
diff --git a/onos-byon-gui/src/main/java/org/onosproject/byon/gui/NetworkUiComponent.java b/onos-byon-gui/src/main/java/org/onosproject/byon/gui/NetworkUiComponent.java
new file mode 100644
index 0000000..9c939a1
--- /dev/null
+++ b/onos-byon-gui/src/main/java/org/onosproject/byon/gui/NetworkUiComponent.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2014-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.byon.gui;
+
+import com.google.common.collect.ImmutableList;
+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.onosproject.ui.UiExtension;
+import org.onosproject.ui.UiExtensionService;
+import org.onosproject.ui.UiMessageHandlerFactory;
+import org.onosproject.ui.UiTopoOverlayFactory;
+import org.onosproject.ui.UiView;
+import org.onosproject.ui.UiViewHidden;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+
+/**
+ * BYON GUI component. When activated, registers a {@link UiExtension} with
+ * the {@link UiExtensionService}, so that new content is injected into the
+ * ONOS Web UI. This example injects one new view as well as providing a
+ * topology view overlay.
+ */
+@Component(immediate = true)
+public class NetworkUiComponent {
+
+    private static final ClassLoader CL = NetworkUiComponent.class.getClassLoader();
+
+    // There should be matching directory names under ~/resources/app/view/
+    private static final String TABLE_VIEW_ID = "byonNetworks";
+    private static final String TOPOV_VIEW_ID = "byonTopov";
+
+    // Text to appear in the UI navigation pane
+    private static final String TABLE_VIEW_TEXT = "BYON Networks";
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected UiExtensionService uiExtensionService;
+
+    // List of application views
+    private final List<UiView> uiViews = ImmutableList.of(
+            new UiView(UiView.Category.OTHER, TABLE_VIEW_ID, TABLE_VIEW_TEXT),
+            new UiViewHidden(TOPOV_VIEW_ID)
+    );
+
+    // Factory for message handlers
+    private final UiMessageHandlerFactory messageHandlerFactory =
+            () -> ImmutableList.of(
+                    new NetworkTableViewMessageHandler(),
+                    new NetworkOverlayMessageHandler()
+            );
+
+    // Factory for topology overlays
+    private final UiTopoOverlayFactory topoOverlayFactory =
+            () -> ImmutableList.of(
+                    new NetworkTopoOverlay()
+            );
+
+    // Build our UI extension definition
+    private UiExtension extension =
+            new UiExtension.Builder(CL, uiViews)
+                    .messageHandlerFactory(messageHandlerFactory)
+                    .topoOverlayFactory(topoOverlayFactory)
+                    .build();
+
+    @Activate
+    protected void activate() {
+        uiExtensionService.register(extension);
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        uiExtensionService.unregister(extension);
+        log.info("Stopped");
+    }
+
+}