Modifying packaging to make the DHCP GUI work from WAR bundle-style packaging.

Change-Id: I1b9685fa1eebcac63ad41bc60db1f98b30aba656
diff --git a/apps/dhcp/src/main/java/org/onosproject/dhcp/DHCPService.java b/apps/dhcp/src/main/java/org/onosproject/dhcp/DHCPService.java
index 238da01..f40452a 100644
--- a/apps/dhcp/src/main/java/org/onosproject/dhcp/DHCPService.java
+++ b/apps/dhcp/src/main/java/org/onosproject/dhcp/DHCPService.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Open Networking Laboratory
+ * 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.
@@ -30,7 +30,7 @@
      *
      * @return collection of mappings.
      */
-    Map<MacAddress, Ip4Address> listMapping();
+    Map<MacAddress, IPAssignment> listMapping();
 
     /**
      * Returns the default lease time granted by the DHCP Server.
diff --git a/apps/dhcp/src/main/java/org/onosproject/dhcp/DHCPStore.java b/apps/dhcp/src/main/java/org/onosproject/dhcp/DHCPStore.java
index de864db..4114106 100644
--- a/apps/dhcp/src/main/java/org/onosproject/dhcp/DHCPStore.java
+++ b/apps/dhcp/src/main/java/org/onosproject/dhcp/DHCPStore.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Open Networking Laboratory
+ * 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.
@@ -77,7 +77,7 @@
      *
      * @return the collection of the mappings
      */
-    Map<MacAddress, Ip4Address> listMapping();
+    Map<MacAddress, IPAssignment> listMapping();
 
     /**
      * Assigns the requested IP to the MAC ID (if available) for an indefinite period of time.
diff --git a/apps/dhcp/src/main/java/org/onosproject/dhcp/cli/DHCPListAllMappings.java b/apps/dhcp/src/main/java/org/onosproject/dhcp/cli/DHCPListAllMappings.java
index 38efa96..397c156 100644
--- a/apps/dhcp/src/main/java/org/onosproject/dhcp/cli/DHCPListAllMappings.java
+++ b/apps/dhcp/src/main/java/org/onosproject/dhcp/cli/DHCPListAllMappings.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Open Networking Laboratory
+ * 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.
@@ -16,10 +16,10 @@
 package org.onosproject.dhcp.cli;
 
 import org.apache.karaf.shell.commands.Command;
-import org.onlab.packet.Ip4Address;
 import org.onlab.packet.MacAddress;
 import org.onosproject.cli.AbstractShellCommand;
 import org.onosproject.dhcp.DHCPService;
+import org.onosproject.dhcp.IPAssignment;
 
 import java.util.Map;
 
@@ -35,10 +35,10 @@
     protected void execute() {
 
         DHCPService dhcpService = AbstractShellCommand.get(DHCPService.class);
-        Map<MacAddress, Ip4Address> allocationMap = dhcpService.listMapping();
+        Map<MacAddress, IPAssignment> allocationMap = dhcpService.listMapping();
 
-        for (Map.Entry<MacAddress, Ip4Address> entry : allocationMap.entrySet()) {
-            print(DHCP_MAPPING_FORMAT, entry.getKey().toString(), entry.getValue().toString());
+        for (Map.Entry<MacAddress, IPAssignment> entry : allocationMap.entrySet()) {
+            print(DHCP_MAPPING_FORMAT, entry.getKey().toString(), entry.getValue().ipAddress().toString());
         }
     }
 }
diff --git a/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DHCPManager.java b/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DHCPManager.java
index 8084654..854cc79 100644
--- a/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DHCPManager.java
+++ b/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DHCPManager.java
@@ -38,6 +38,7 @@
 import org.onosproject.core.CoreService;
 import org.onosproject.dhcp.DHCPService;
 import org.onosproject.dhcp.DHCPStore;
+import org.onosproject.dhcp.IPAssignment;
 import org.onosproject.net.config.ConfigFactory;
 import org.onosproject.net.config.NetworkConfigEvent;
 import org.onosproject.net.config.NetworkConfigListener;
@@ -213,8 +214,7 @@
     }
 
     @Override
-    public Map<MacAddress, Ip4Address> listMapping() {
-
+    public Map<MacAddress, IPAssignment> listMapping() {
         return dhcpStore.listMapping();
     }
 
diff --git a/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DHCPUi.java b/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DHCPUi.java
new file mode 100644
index 0000000..8e338b3
--- /dev/null
+++ b/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DHCPUi.java
@@ -0,0 +1,74 @@
+/*
+ * 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.dhcp.impl;
+
+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.apache.felix.scr.annotations.Service;
+import org.onosproject.ui.UiExtension;
+import org.onosproject.ui.UiExtensionService;
+import org.onosproject.ui.UiMessageHandlerFactory;
+import org.onosproject.ui.UiView;
+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.
+ */
+@Component(immediate = true, enabled = true)
+@Service(value = DHCPUi.class)
+public class DHCPUi {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private static final ClassLoader CL = DHCPUi.class.getClassLoader();
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected UiExtensionService uiExtensionService;
+
+    private final UiMessageHandlerFactory messageHandlerFactory =
+            () -> ImmutableList.of(new DhcpViewMessageHandler());
+
+    private final List<UiView> views = ImmutableList.of(
+            new UiView(NETWORK, "dhcp", "DHCP Server")
+    );
+
+    private final UiExtension uiExtension =
+            new UiExtension.Builder(CL, views)
+                    .messageHandlerFactory(messageHandlerFactory)
+                    .resourcePath("gui")
+                    .build();
+
+    @Activate
+    protected void activate() {
+        uiExtensionService.register(uiExtension);
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        uiExtensionService.unregister(uiExtension);
+        log.info("Stopped");
+    }
+
+}
\ No newline at end of file
diff --git a/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpViewMessageHandler.java b/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpViewMessageHandler.java
new file mode 100644
index 0000000..50a6802
--- /dev/null
+++ b/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DhcpViewMessageHandler.java
@@ -0,0 +1,96 @@
+/*
+ * 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.dhcp.impl;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableSet;
+import org.onlab.packet.MacAddress;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.dhcp.DHCPService;
+import org.onosproject.dhcp.IPAssignment;
+import org.onosproject.ui.RequestHandler;
+import org.onosproject.ui.UiMessageHandler;
+import org.onosproject.ui.table.TableModel;
+import org.onosproject.ui.table.TableRequestHandler;
+
+import java.util.Collection;
+import java.util.Date;
+import java.util.Map;
+
+/**
+ * DHCPViewMessageHandler class implementation.
+ */
+public class DhcpViewMessageHandler extends UiMessageHandler {
+
+    private static final String DHCP_DATA_REQ = "dhcpDataRequest";
+    private static final String DHCP_DATA_RESP = "dhcpDataResponse";
+    private static final String DHCP = "dhcps";
+
+    private static final String MAC = "mac";
+    private static final String IP = "ip";
+    private static final String LEASE = "lease";
+
+    private static final String[] COL_IDS = {
+            MAC, IP, LEASE
+    };
+
+    @Override
+    protected Collection<RequestHandler> createRequestHandlers() {
+        return ImmutableSet.of(
+                new DataRequestHandler()
+        );
+    }
+
+    private final class DataRequestHandler extends TableRequestHandler {
+
+        private DataRequestHandler() {
+            super(DHCP_DATA_REQ, DHCP_DATA_RESP, DHCP);
+        }
+
+        @Override
+        protected String defaultColumnId() {
+            return MAC;
+        }
+
+        @Override
+        protected String[] getColumnIds() {
+            return COL_IDS;
+        }
+
+        @Override
+        protected void populateTable(TableModel tm, ObjectNode payload) {
+            DHCPService dhcpService = AbstractShellCommand.get(DHCPService.class);
+            Map<MacAddress, IPAssignment> allocationMap = dhcpService.listMapping();
+
+            for (Map.Entry<MacAddress, IPAssignment> entry : allocationMap.entrySet()) {
+                populateRow(tm.addRow(), entry);
+            }
+        }
+
+        private void populateRow(TableModel.Row row, Map.Entry<MacAddress, IPAssignment> entry) {
+            if (entry.getValue().leasePeriod() > 0) {
+                Date now = new Date(entry.getValue().timestamp().getTime() + entry.getValue().leasePeriod());
+                row.cell(MAC, entry.getKey())
+                        .cell(IP, entry.getValue().ipAddress())
+                        .cell(LEASE, now.toString());
+            } else {
+                row.cell(MAC, entry.getKey())
+                        .cell(IP, entry.getValue().ipAddress())
+                        .cell(LEASE, "Infinite Static Lease");
+            }
+        }
+    }
+}
diff --git a/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDHCPStore.java b/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDHCPStore.java
index a582892..5f6e145 100644
--- a/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDHCPStore.java
+++ b/apps/dhcp/src/main/java/org/onosproject/dhcp/impl/DistributedDHCPStore.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Open Networking Laboratory
+ * 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.
@@ -224,17 +224,17 @@
     }
 
     @Override
-    public Map<MacAddress, Ip4Address> listMapping() {
+    public Map<MacAddress, IPAssignment> listMapping() {
 
-        Map<MacAddress, Ip4Address> allMapping = new HashMap<>();
+        Map<MacAddress, IPAssignment> allMapping = new HashMap<>();
         for (Map.Entry<MacAddress, Versioned<IPAssignment>> entry: allocationMap.entrySet()) {
             IPAssignment assignment = entry.getValue().value();
             if (assignment.assignmentStatus() == IPAssignment.AssignmentStatus.Option_Assigned) {
-                allMapping.put(entry.getKey(), assignment.ipAddress());
+                allMapping.put(entry.getKey(), assignment);
             }
         }
-
         return allMapping;
+
     }
 
     @Override
diff --git a/apps/dhcp/src/main/java/org/onosproject/dhcp/rest/DHCPWebResource.java b/apps/dhcp/src/main/java/org/onosproject/dhcp/rest/DHCPWebResource.java
index fad6f9d..b18acd5 100644
--- a/apps/dhcp/src/main/java/org/onosproject/dhcp/rest/DHCPWebResource.java
+++ b/apps/dhcp/src/main/java/org/onosproject/dhcp/rest/DHCPWebResource.java
@@ -21,6 +21,7 @@
 import org.onlab.packet.Ip4Address;
 import org.onlab.packet.MacAddress;
 import org.onosproject.dhcp.DHCPService;
+import org.onosproject.dhcp.IPAssignment;
 import org.onosproject.rest.AbstractWebResource;
 
 import javax.ws.rs.Consumes;
@@ -71,11 +72,11 @@
     public Response listMappings() {
         ObjectNode root = mapper().createObjectNode();
 
-        final Map<MacAddress, Ip4Address> intents = service.listMapping();
+        final Map<MacAddress, IPAssignment> intents = service.listMapping();
         ArrayNode arrayNode = root.putArray("mappings");
         intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
                 .put("mac", i.getKey().toString())
-                .put("ip", i.getValue().toString())));
+                .put("ip", i.getValue().ipAddress().toString())));
 
         return ok(root.toString()).build();
     }
@@ -123,11 +124,11 @@
                 }
             }
 
-            final Map<MacAddress, Ip4Address> intents = service.listMapping();
+            final Map<MacAddress, IPAssignment> intents = service.listMapping();
             ArrayNode arrayNode = root.putArray("mappings");
             intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
                     .put("mac", i.getKey().toString())
-                    .put("ip", i.getValue().toString())));
+                    .put("ip", i.getValue().ipAddress().toString())));
         } catch (IOException e) {
             throw new IllegalArgumentException(e.getMessage());
         }
@@ -149,11 +150,11 @@
         if (!service.removeStaticMapping(MacAddress.valueOf(macID))) {
             throw new IllegalArgumentException("Static Mapping Removal Failed.");
         }
-        final Map<MacAddress, Ip4Address> intents = service.listMapping();
+        final Map<MacAddress, IPAssignment> intents = service.listMapping();
         ArrayNode arrayNode = root.putArray("mappings");
         intents.entrySet().forEach(i -> arrayNode.add(mapper().createObjectNode()
                 .put("mac", i.getKey().toString())
-                .put("ip", i.getValue().toString())));
+                .put("ip", i.getValue().ipAddress().toString())));
 
         return ok(root.toString()).build();
     }
diff --git a/apps/dhcp/src/main/resources/app/view/dhcp/dhcp.css b/apps/dhcp/src/main/resources/app/view/dhcp/dhcp.css
new file mode 100644
index 0000000..e0a2931
--- /dev/null
+++ b/apps/dhcp/src/main/resources/app/view/dhcp/dhcp.css
@@ -0,0 +1,27 @@
+/*
+ * 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.
+ */
+
+/*
+ ONOS GUI -- DHCP Server -- CSS file
+ */
+
+#ov-dhcp h2 {
+    display: inline-block;
+}
+
+#ov-dhcp div.ctrl-btns {
+    width: 45px;
+}
diff --git a/apps/dhcp/src/main/resources/app/view/dhcp/dhcp.html b/apps/dhcp/src/main/resources/app/view/dhcp/dhcp.html
new file mode 100644
index 0000000..3e14570
--- /dev/null
+++ b/apps/dhcp/src/main/resources/app/view/dhcp/dhcp.html
@@ -0,0 +1,47 @@
+<!-- DHCP Server partial HTML -->
+<div id="ov-dhcp">
+  <div class="tabular-header">
+    <h2>DHCP Mappings ({{tableData.length}} total)</h2>
+    <div class="ctrl-btns">
+      <div class="refresh" ng-class="{active: autoRefresh}"
+           icon icon-size="36" icon-id="refresh"
+           tooltip tt-msg="autoRefreshTip"
+           ng-click="toggleRefresh()"></div>
+    </div>
+  </div>
+
+  <div class="summary-list" onos-table-resize>
+    <div ng-show="loading" class="loading-wheel"
+         icon icon-id="loading" icon-size="75"></div>
+
+    <div class="table-header" onos-sortable-header>
+      <table>
+        <tr>
+          <td colId="mac" sortable>MAC Address</td>
+          <td colId="ip" sortable>IP Address</td>
+          <td colId="lease" sortable>Lease Expiry</td>
+        </tr>
+      </table>
+    </div>
+
+    <div class="table-body">
+      <table onos-flash-changes id-prop="mac">
+        <tr ng-if="!tableData.length" class="no-data">
+          <td colspan="2">
+            No mappings found
+          </td>
+        </tr>
+
+        <tr ng-repeat="dhcp in tableData track by $index"
+            ng-click="selectCallback($event, dhcp)"
+            ng-repeat-complete row-id="{{dhcp.mac}}">
+          <td>{{dhcp.mac}}</td>
+          <td>{{dhcp.ip}}</td>
+          <td>{{dhcp.lease}}</td>
+        </tr>
+      </table>
+    </div>
+
+  </div>
+
+</div>
diff --git a/apps/dhcp/src/main/resources/app/view/dhcp/dhcp.js b/apps/dhcp/src/main/resources/app/view/dhcp/dhcp.js
new file mode 100644
index 0000000..061d0de
--- /dev/null
+++ b/apps/dhcp/src/main/resources/app/view/dhcp/dhcp.js
@@ -0,0 +1,51 @@
+/*
+ * 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.
+ */
+
+/*
+ ONOS GUI -- DHCP Server View Module
+ */
+
+(function () {
+    'use strict';
+
+    // injected refs
+    var $log, $scope;
+
+    angular.module('ovDhcp', [])
+        .controller('OvDhcpCtrl',
+        ['$log', '$scope', 'TableBuilderService',
+
+            function (_$log_, _$scope_, tbs) {
+                $log = _$log_;
+                $scope = _$scope_;
+
+                function selCb($event, row) {
+                    $log.debug('Got a click on:', row);
+                }
+
+                tbs.buildTable({
+                    scope: $scope,
+                    tag: 'dhcp',
+                    selCb: selCb
+                });
+
+                $scope.$on('$destroy', function () {
+                    $log.debug('OvDhcpCtrl has been destroyed');
+                });
+
+                $log.log('OvDhcpCtrl has been created');
+            }]);
+}());
\ No newline at end of file
diff --git a/apps/dhcp/src/main/resources/gui/css.html b/apps/dhcp/src/main/resources/gui/css.html
new file mode 100644
index 0000000..d02ad44
--- /dev/null
+++ b/apps/dhcp/src/main/resources/gui/css.html
@@ -0,0 +1 @@
+<link rel="stylesheet" href="app/view/dhcp/dhcp.css">
\ No newline at end of file
diff --git a/apps/dhcp/src/main/resources/gui/js.html b/apps/dhcp/src/main/resources/gui/js.html
new file mode 100644
index 0000000..d37b576
--- /dev/null
+++ b/apps/dhcp/src/main/resources/gui/js.html
@@ -0,0 +1 @@
+<script src="app/view/dhcp/dhcp.js"></script>
\ No newline at end of file
diff --git a/apps/dhcp/src/test/java/org/onosproject/dhcp/impl/DHCPManagerTest.java b/apps/dhcp/src/test/java/org/onosproject/dhcp/impl/DHCPManagerTest.java
index 0abdc33..97ea812 100644
--- a/apps/dhcp/src/test/java/org/onosproject/dhcp/impl/DHCPManagerTest.java
+++ b/apps/dhcp/src/test/java/org/onosproject/dhcp/impl/DHCPManagerTest.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Open Networking Laboratory
+ * 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.
@@ -29,9 +29,10 @@
 import org.onlab.packet.UDP;
 import org.onosproject.core.CoreServiceAdapter;
 import org.onosproject.dhcp.DHCPStore;
-import org.onosproject.net.config.NetworkConfigRegistryAdapter;
+import org.onosproject.dhcp.IPAssignment;
 import org.onosproject.net.Host;
 import org.onosproject.net.HostId;
+import org.onosproject.net.config.NetworkConfigRegistryAdapter;
 import org.onosproject.net.host.HostDescription;
 import org.onosproject.net.host.HostProvider;
 import org.onosproject.net.host.HostProviderRegistry;
@@ -49,6 +50,7 @@
 
 import java.nio.ByteBuffer;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -238,9 +240,15 @@
         public void releaseIP(MacAddress macID) {
         }
 
-        public Map<MacAddress, Ip4Address> listMapping() {
-            Map<MacAddress, Ip4Address> map = new HashMap<>();
-            map.put(CLIENT1_MAC, Ip4Address.valueOf(EXPECTED_IP));
+        public Map<MacAddress, IPAssignment> listMapping() {
+            Map<MacAddress, IPAssignment> map = new HashMap<>();
+            IPAssignment assignment = IPAssignment.builder()
+                                        .ipAddress(Ip4Address.valueOf(EXPECTED_IP))
+                                        .assignmentStatus(IPAssignment.AssignmentStatus.Option_Assigned)
+                                        .leasePeriod(300)
+                                        .timestamp(new Date())
+                                        .build();
+            map.put(CLIENT1_MAC, assignment);
             return map;
         }