[ONOS-6375] Support topology overlay for mapping managemant app

Change-Id: I25cad3ebf450bc2658edf80e2085ec99f29f6aa6
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/gui/MappingsTopoMessageHandler.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/gui/MappingsTopoMessageHandler.java
new file mode 100644
index 0000000..8c71a17
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/gui/MappingsTopoMessageHandler.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2017-present 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.mapping.web.gui;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableSet;
+import org.onosproject.ui.RequestHandler;
+import org.onosproject.ui.UiMessageHandler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collection;
+
+/**
+ * Mapping management UI topology-overlay message handler.
+ */
+public class MappingsTopoMessageHandler extends UiMessageHandler {
+
+    private static final String MAPPINGS_TOPO_DISPLAY_START = "mappingsTopoDisplayStart";
+    private static final String MAPPINGS_TOPO_DISPLAY_STOP = "mappingsTopoDisplayStop";
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    @Override
+    protected Collection<RequestHandler> createRequestHandlers() {
+        return ImmutableSet.of(
+                new DisplayStartHandler(),
+                new DisplayStopHandler()
+        );
+    }
+
+    private final class DisplayStartHandler extends RequestHandler {
+
+        public DisplayStartHandler() {
+            super(MAPPINGS_TOPO_DISPLAY_START);
+        }
+
+        @Override
+        public void process(ObjectNode payload) {
+            log.debug("Start Display");
+        }
+    }
+
+    private final class DisplayStopHandler extends RequestHandler {
+
+        public DisplayStopHandler() {
+            super(MAPPINGS_TOPO_DISPLAY_STOP);
+        }
+
+        @Override
+        public void process(ObjectNode payload) {
+            log.debug("Stop Display");
+        }
+    }
+}
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/gui/MappingsTopoOverlay.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/gui/MappingsTopoOverlay.java
new file mode 100644
index 0000000..fe05bec
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/gui/MappingsTopoOverlay.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright 2017-present 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.mapping.web.gui;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.ui.UiTopoOverlay;
+import org.onosproject.ui.topo.ButtonId;
+import org.onosproject.ui.topo.PropertyPanel;
+
+import static org.onosproject.ui.topo.TopoConstants.Properties.FLOWS;
+import static org.onosproject.ui.topo.TopoConstants.Properties.INTENTS;
+import static org.onosproject.ui.topo.TopoConstants.Properties.LATITUDE;
+import static org.onosproject.ui.topo.TopoConstants.Properties.LONGITUDE;
+import static org.onosproject.ui.topo.TopoConstants.Properties.PORTS;
+import static org.onosproject.ui.topo.TopoConstants.Properties.TUNNELS;
+
+/**
+ * Customized topology overlay for mapping management app.
+ */
+public class MappingsTopoOverlay extends UiTopoOverlay {
+
+    // NOTE: this must match the ID defined in mappingTopo.js
+    private static final String OVERLAY_ID = "mapping-overlay";
+
+    private static final ButtonId MAPPINGS_BUTTON = new ButtonId("mappings");
+
+    /**
+     * Creates a new user interface topology view overlay descriptor, with
+     * the given identifier.
+     */
+    public MappingsTopoOverlay() {
+        super(OVERLAY_ID);
+    }
+
+    @Override
+    public void modifySummary(PropertyPanel pp) {
+        pp.removeProps(
+                INTENTS,
+                TUNNELS,
+                FLOWS
+        );
+    }
+
+    @Override
+    public void modifyDeviceDetails(PropertyPanel pp, DeviceId deviceId) {
+        pp.removeAllButtons();
+        pp.removeProps(LATITUDE, LONGITUDE, PORTS, FLOWS, TUNNELS);
+        pp.addButton(MAPPINGS_BUTTON);
+    }
+}
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/gui/MappingsTopoUI.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/gui/MappingsTopoUI.java
new file mode 100644
index 0000000..6132240
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/gui/MappingsTopoUI.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2017-present 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.mapping.web.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;
+
+/**
+ * Mechanism to stream data to the topology UI.
+ */
+@Component(immediate = true)
+public class MappingsTopoUI {
+
+    private static final String MAPPING_TOPO_ID = "mappingTopo";
+    private static final String RES_PATH = "gui";
+    private static final ClassLoader CL = MappingsTopoUI.class.getClassLoader();
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected UiExtensionService uiExtensionService;
+
+    // Factory for UI message handlers
+    private final UiMessageHandlerFactory messageHandlerFactory =
+            () -> ImmutableList.of(
+                    new MappingsTopoMessageHandler()
+            );
+
+    // Factory for UI topology overlays
+    private final UiTopoOverlayFactory topoOverlayFactory =
+            () -> ImmutableList.of(
+                    new MappingsTopoOverlay()
+            );
+
+    // List of application views
+    private final List<UiView> views = ImmutableList.of(
+            new UiViewHidden(MAPPING_TOPO_ID)
+    );
+
+    // Application UI extension
+    private final UiExtension uiExtension =
+            new UiExtension.Builder(CL, views)
+                    .topoOverlayFactory(topoOverlayFactory)
+                    .messageHandlerFactory(messageHandlerFactory)
+                    .resourcePath(RES_PATH)
+                    .build();
+
+    @Activate
+    protected void activate() {
+        uiExtensionService.register(uiExtension);
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        uiExtensionService.unregister(uiExtension);
+        log.info("Stopped");
+    }
+}
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/gui/MappingsUI.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/gui/MappingsUI.java
index 300133c..63f5397 100644
--- a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/gui/MappingsUI.java
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/gui/MappingsUI.java
@@ -26,13 +26,12 @@
 import org.onosproject.ui.UiExtensionService;
 import org.onosproject.ui.UiMessageHandlerFactory;
 import org.onosproject.ui.UiView;
+import org.onosproject.ui.UiViewHidden;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.List;
 
-import static org.onosproject.ui.UiView.Category.NETWORK;
-
 /**
  * Mechanism to stream data to the GUI.
  */
@@ -40,7 +39,6 @@
 @Service(value = MappingsUI.class)
 public class MappingsUI {
     private static final String MAPPING_ID = "mapping";
-    private static final String MAPPING_TEXT = "Mapping Management";
     private static final String RES_PATH = "gui";
     private static final ClassLoader CL = MappingsUI.class.getClassLoader();
 
@@ -55,7 +53,7 @@
 
     // List of application views
     private final List<UiView> views = ImmutableList.of(
-            new UiView(NETWORK, MAPPING_ID, MAPPING_TEXT)
+            new UiViewHidden(MAPPING_ID)
     );
 
     // Application UI extension
diff --git a/apps/mappingmanagement/web/src/main/resources/app/view/mapping/mappingTopo.html b/apps/mappingmanagement/web/src/main/resources/app/view/mapping/mappingTopo.html
new file mode 100644
index 0000000..9f78cac
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/resources/app/view/mapping/mappingTopo.html
@@ -0,0 +1,4 @@
+<!-- partial HTML -->
+<div id="ov-mapping-topo">
+    <p>This is a hidden view .. just a placeholder to house the javascript</p>
+</div>
diff --git a/apps/mappingmanagement/web/src/main/resources/app/view/mapping/mappingTopo.js b/apps/mappingmanagement/web/src/main/resources/app/view/mapping/mappingTopo.js
new file mode 100644
index 0000000..af4bf23
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/resources/app/view/mapping/mappingTopo.js
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2017-present 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.
+ */
+
+ (function () {
+     'use strict';
+
+     // injected refs
+     var $log, fs, flash, wss;
+
+     // constants
+     var displayStart = 'mappingsTopoDisplayStart',
+         displayStop = 'mappingsTopoDisplayStop';
+
+
+     // === ---------------------------
+     // === Helper functions
+
+     function sendDisplayStart() {
+         wss.sendEvent(displayStart);
+     }
+
+     function sendDisplayStop() {
+         wss.sendEvent(displayStop);
+     }
+
+     // === ---------------------------
+     // === Main API functions
+
+     function startDisplay() {
+         sendDisplayStart();
+         flash.flash('Showing mappings on devices');
+     }
+
+     function stopDisplay() {
+         sendDisplayStop();
+         flash.flash('Canceling mappings on devices');
+         return true;
+     }
+
+     angular.module('ovMappingTopo', [])
+         .factory('MappingTopoService',
+         ['$log', 'FnService', 'FlashService', 'WebSocketService',
+
+         function (_$log_, _fs_, _flash_, _wss_) {
+             $log = _$log_;
+             fs = _fs_;
+             flash = _flash_;
+             wss = _wss_;
+
+             return {
+                 startDisplay: startDisplay,
+                 stopDisplay: stopDisplay
+             };
+         }]);
+}());
\ No newline at end of file
diff --git a/apps/mappingmanagement/web/src/main/resources/app/view/mapping/mappingTopoOverlay.js b/apps/mappingmanagement/web/src/main/resources/app/view/mapping/mappingTopoOverlay.js
new file mode 100644
index 0000000..2562860
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/resources/app/view/mapping/mappingTopoOverlay.js
@@ -0,0 +1,94 @@
+/*
+ * Copyright 2017-present 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.
+ */
+
+/*
+ ONOS GUI -- Mapping Management Topology Overlay
+ */
+(function () {
+    'use strict';
+
+    // injected references
+    var $log, tov, mts, ns;
+
+    var viewbox = '-1 -1 19 19';
+
+    // mapping glyph (view box 20x20)
+    var mapping = 'M8,0C4.687,0,2,2.687,2,6c0,3.854,4.321,8.663,5,9.398C7.281,' +
+    '15.703,7.516,16,8,16s0.719-0.297,1-0.602  C9.679,14.663,14,9.854,14,6C14,' +
+    '2.687,11.313,0,8,0z M8,10c-2.209,0-4-1.791-4-4s1.791-4,4-4s4,1.791,4,' +
+    '4S10.209,10,8,10z M8,4  C6.896,4,6,4.896,6,6s0.896,2,2,2s2-0.896,2-2S9.104,4,8,4z';
+
+    // overlay definition
+    var overlay = {
+        overlayId: 'mapping-overlay',
+        glyphId: '*mapping',
+        tooltip: 'Mappings Overlay',
+        glyphs: {
+            mapping: {
+                vb: viewbox,
+                d: mapping
+            }
+        },
+        activate: function () {
+            $log.debug("Mapping topology overlay ACTIVATED");
+            mts.startDisplay();
+        },
+        deactivate: function () {
+            mts.stopDisplay();
+            $log.debug("Mapping topology overlay DEACTIVATED");
+        },
+        buttons: {
+            mappings: {
+                gid: '*mapping',
+                tt: 'Show mappings for this device',
+                cb: function (data) {
+                    $log.debug('Show mappings for this device. data:', data);
+                    ns.navTo("mapping", {devId: data.id});
+                }
+            }
+        },
+        hooks: {
+            // hooks for when the selection changes...
+            empty: function () {
+                selectionCallback('empty');
+            },
+            single: function (data) {
+                selectionCallback('single', data);
+            },
+            multi: function (selectOrder) {
+                selectionCallback('multi', selectOrder);
+                tov.addDetailButton('mappings');
+            }
+        }
+    };
+
+    function selectionCallback(x, d) {
+        $log.debug('Selection callback', x, d);
+    }
+
+    // invoke code to register with the overlay service
+    angular.module('ovMappingTopo')
+        .run(['$log', 'TopoOverlayService', 'MappingTopoService', 'NavService',
+
+        function (_$log_, _tov_, _mts_, _ns_) {
+            $log = _$log_;
+            tov = _tov_;
+            mts = _mts_;
+            ns = _ns_;
+            tov.register(overlay);
+        }]);
+
+}());
diff --git a/apps/mappingmanagement/web/src/main/resources/gui/js.html b/apps/mappingmanagement/web/src/main/resources/gui/js.html
index 23df18f..b22fb0e 100644
--- a/apps/mappingmanagement/web/src/main/resources/gui/js.html
+++ b/apps/mappingmanagement/web/src/main/resources/gui/js.html
@@ -1 +1,3 @@
-<script src="app/view/mapping/mapping.js"></script>
\ No newline at end of file
+<script src="app/view/mapping/mapping.js"></script>
+<script src="app/view/mapping/mappingTopo.js"></script>
+<script src="app/view/mapping/mappingTopoOverlay.js"></script>
\ No newline at end of file