Adding support for user interface extensions.

Change-Id: I1e41d16efc11be31ad4c2fb0c09e86e3dfd26706
diff --git a/core/api/src/main/java/org/onosproject/ui/UiExtension.java b/core/api/src/main/java/org/onosproject/ui/UiExtension.java
new file mode 100644
index 0000000..4e2f570
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/UiExtension.java
@@ -0,0 +1,98 @@
+/*
+ * 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;
+
+import com.google.common.collect.ImmutableList;
+
+import java.io.InputStream;
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * User interface extension.
+ */
+public class UiExtension {
+
+    private final String prefix;
+    private final ClassLoader classLoader;
+    private final List<UiView> views;
+
+    /**
+     * Creates a user interface extension for loading CSS and JS injections
+     * from {@code css.html} and {@code js.html} resources, respectively.
+     *
+     * @param classLoader class-loader for user interface resources
+     */
+    public UiExtension(List<UiView> views, ClassLoader classLoader) {
+        this(views, null, classLoader);
+    }
+
+    /**
+     * Creates a user interface extension using custom resource prefix. It
+     * loads CSS and JS injections from {@code path/css.html} and
+     * {@code prefix/js.html} resources, respectively.
+     *
+     * @param views       list of user interface views
+     * @param path        resource path prefix
+     * @param classLoader class-loader for user interface resources
+     */
+    public UiExtension(List<UiView> views, String path, ClassLoader classLoader) {
+        this.views = checkNotNull(ImmutableList.copyOf(views), "Views cannot be null");
+        this.prefix = path != null ? (path + "/") : "";
+        this.classLoader = checkNotNull(classLoader, "Class loader must be specified");
+    }
+
+    /**
+     * Returns input stream containing CSS inclusion statements.
+     *
+     * @return CSS inclusion statements
+     */
+    public InputStream css() {
+        return classLoader.getResourceAsStream(prefix + "css.html");
+    }
+
+    /**
+     * Returns input stream containing JavaScript inclusion statements.
+     *
+     * @return JavaScript inclusion statements
+     */
+    public InputStream js() {
+        return classLoader.getResourceAsStream(prefix + "js.html");
+    }
+
+    /**
+     * Returns list of user interface views contributed by this extension.
+     *
+     * @return contributed view descriptors
+     */
+    public List<UiView> views() {
+        return views;
+    }
+
+    /**
+     * Returns input stream containing specified view-specific resource.
+     *
+     * @param viewId view identifier
+     * @param path   resource path, relative to the view directory
+     * @return resource input stream
+     */
+    public InputStream resource(String viewId, String path) {
+        InputStream is = classLoader.getResourceAsStream(prefix + "views/" + viewId + "/" + path);
+        return is;
+    }
+
+}
diff --git a/core/api/src/main/java/org/onosproject/ui/UiExtensionService.java b/core/api/src/main/java/org/onosproject/ui/UiExtensionService.java
new file mode 100644
index 0000000..330fbb7
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/UiExtensionService.java
@@ -0,0 +1,53 @@
+/*
+ * 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;
+
+import java.util.List;
+
+/**
+ * Service for registering user interface extensions.
+ */
+public interface UiExtensionService {
+
+    /**
+     * Registers the specified user interface extension.
+     *
+     * @param extension GUI extension to register
+     */
+    void register(UiExtension extension);
+
+    /**
+     * Unregisters the specified user interface extension.
+     *
+     * @param extension GUI extension to unregister
+     */
+    void unregister(UiExtension extension);
+
+    /**
+     * Returns the list of user interface extensions.
+     *
+     * @return list of extensions
+     */
+    List<UiExtension> getExtensions();
+
+    /**
+     * Returns the user interface extension that contributed the specified view.
+     *
+     * @param viewId view identifier
+     * @return user interface extension
+     */
+    UiExtension getViewExtension(String viewId);
+}
diff --git a/core/api/src/main/java/org/onosproject/ui/UiView.java b/core/api/src/main/java/org/onosproject/ui/UiView.java
new file mode 100644
index 0000000..2f2bc0e
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/UiView.java
@@ -0,0 +1,83 @@
+/*
+ * 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;
+
+import com.google.common.base.MoreObjects;
+
+import java.util.Objects;
+
+/**
+ * Represents user interface view addition.
+ */
+public class UiView {
+
+    private final String id;
+    private final String label;
+
+    /**
+     * Creates a new user interface view descriptor.
+     *
+     * @param id    view identifier
+     * @param label view label
+     */
+    public UiView(String id, String label) {
+        this.id = id;
+        this.label = label;
+    }
+
+    /**
+     * Returns the view identifier.
+     *
+     * @return view id
+     */
+    public String id() {
+        return id;
+    }
+
+    /**
+     * Returns the view label.
+     *
+     * @return view label
+     */
+    public String label() {
+        return label;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(id);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+        final UiView other = (UiView) obj;
+        return Objects.equals(this.id, other.id);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("id", id)
+                .add("label", label)
+                .toString();
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/ui/package-info.java b/core/api/src/main/java/org/onosproject/ui/package-info.java
new file mode 100644
index 0000000..dd832a5
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * Mechanism for managing dynamically registered user interface extensions.
+ */
+package org.onosproject.ui;
diff --git a/core/api/src/test/java/org/onosproject/ui/UiExtensionTest.java b/core/api/src/test/java/org/onosproject/ui/UiExtensionTest.java
new file mode 100644
index 0000000..1ea1d2c
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/ui/UiExtensionTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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;
+
+import com.google.common.collect.ImmutableList;
+import org.junit.Test;
+
+import java.io.IOException;
+
+import static com.google.common.io.ByteStreams.toByteArray;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests the default user interface extension descriptor.
+ */
+public class UiExtensionTest {
+
+    @Test
+    public void basics() throws IOException {
+        UiExtension ext = new UiExtension(ImmutableList.of(new UiView("foo", "Foo View")),
+                                          getClass().getClassLoader());
+        String css = new String(toByteArray(ext.css()));
+        assertTrue("incorrect css stream", css.contains("foo-css"));
+        String js = new String(toByteArray(ext.js()));
+        assertTrue("incorrect js stream", js.contains("foo-js"));
+        assertEquals("incorrect views stream", "foo", ext.views().get(0).id());
+    }
+
+    @Test
+    public void withPath() throws IOException {
+        UiExtension ext = new UiExtension(ImmutableList.of(new UiView("foo", "Foo View")),
+                                          "custom", getClass().getClassLoader());
+        String css = new String(toByteArray(ext.css()));
+        assertTrue("incorrect css stream", css.contains("custom-css"));
+        String js = new String(toByteArray(ext.js()));
+        assertTrue("incorrect js stream", js.contains("custom-js"));
+        assertEquals("incorrect views stream", "foo", ext.views().get(0).id());
+    }
+}
\ No newline at end of file
diff --git a/core/api/src/test/resources/css.html b/core/api/src/test/resources/css.html
new file mode 100644
index 0000000..3079957
--- /dev/null
+++ b/core/api/src/test/resources/css.html
@@ -0,0 +1,2 @@
+foo-css
+bar-css
\ No newline at end of file
diff --git a/core/api/src/test/resources/custom/css.html b/core/api/src/test/resources/custom/css.html
new file mode 100644
index 0000000..e67f4a2
--- /dev/null
+++ b/core/api/src/test/resources/custom/css.html
@@ -0,0 +1 @@
+custom-css
diff --git a/core/api/src/test/resources/custom/js.html b/core/api/src/test/resources/custom/js.html
new file mode 100644
index 0000000..f87844b
--- /dev/null
+++ b/core/api/src/test/resources/custom/js.html
@@ -0,0 +1 @@
+custom-js
\ No newline at end of file
diff --git a/core/api/src/test/resources/js.html b/core/api/src/test/resources/js.html
new file mode 100644
index 0000000..50b63b3
--- /dev/null
+++ b/core/api/src/test/resources/js.html
@@ -0,0 +1,2 @@
+foo-js
+bar-js
\ No newline at end of file