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/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