ONOS-2486 Adding swagger-based REST API documentation.
Change-Id: I237d973d73549ad30ddc638c1c201f024d344c70
diff --git a/core/api/src/main/java/org/onosproject/rest/AbstractApiDocRegistrator.java b/core/api/src/main/java/org/onosproject/rest/AbstractApiDocRegistrator.java
new file mode 100644
index 0000000..7349c69
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/rest/AbstractApiDocRegistrator.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.rest;
+
+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;
+
+/**
+ * Self-registering REST API provider.
+ */
+@Component(immediate = true, componentAbstract = true)
+public abstract class AbstractApiDocRegistrator {
+
+ protected final ApiDocProvider provider;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected ApiDocService service;
+
+ /**
+ * Creates registrator for the specified REST API doc provider.
+ *
+ * @param provider REST API provider
+ */
+ protected AbstractApiDocRegistrator(ApiDocProvider provider) {
+ this.provider = provider;
+ }
+
+ @Activate
+ protected void activate() {
+ service.register(provider);
+ }
+
+ @Deactivate
+ protected void deactivate() {
+ service.unregister(provider);
+ }
+}
diff --git a/core/api/src/main/java/org/onosproject/rest/AbstractInjectionResource.java b/core/api/src/main/java/org/onosproject/rest/AbstractInjectionResource.java
new file mode 100644
index 0000000..de182f0
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/rest/AbstractInjectionResource.java
@@ -0,0 +1,78 @@
+/*
+ * 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.rest;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * Resource for serving semi-static resources.
+ */
+public class AbstractInjectionResource extends AbstractWebResource {
+
+ /**
+ * Returns the index into the supplied string where the end of the
+ * specified pattern is located.
+ *
+ * @param string string to split
+ * @param start index where to start looking for pattern
+ * @param stopPattern optional pattern where to stop
+ * @return index where the split should occur
+ */
+ protected int split(String string, int start, String stopPattern) {
+ int i = stopPattern != null ? string.indexOf(stopPattern, start) : string.length();
+ checkArgument(i >= 0, "Unable to locate pattern %s", stopPattern);
+ return i + (stopPattern != null ? stopPattern.length() : 0);
+ }
+
+ /**
+ * Produces an input stream from the bytes of the specified sub-string.
+ *
+ * @param string source string
+ * @param start index where to start stream
+ * @param end index where to end stream
+ * @return input stream
+ */
+ protected InputStream stream(String string, int start, int end) {
+ return new ByteArrayInputStream(string.substring(start, end).getBytes());
+ }
+
+ /**
+ * Auxiliary enumeration to sequence input streams.
+ */
+ protected class StreamEnumeration implements Enumeration<InputStream> {
+ private final Iterator<InputStream> iterator;
+
+ public StreamEnumeration(List<InputStream> streams) {
+ this.iterator = streams.iterator();
+ }
+
+ @Override
+ public boolean hasMoreElements() {
+ return iterator.hasNext();
+ }
+
+ @Override
+ public InputStream nextElement() {
+ return iterator.next();
+ }
+ }
+}
diff --git a/core/api/src/main/java/org/onosproject/rest/ApiDocProvider.java b/core/api/src/main/java/org/onosproject/rest/ApiDocProvider.java
new file mode 100644
index 0000000..50cac4c
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/rest/ApiDocProvider.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.rest;
+
+import com.google.common.annotations.Beta;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.InputStream;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Entity capable of providing REST API documentation resources.
+ */
+@Beta
+public class ApiDocProvider {
+
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ private static final String DOCS = "/apidoc/swagger.json";
+ private static final String MODEL = "/apidoc/model.json";
+
+ private final String key;
+ private final String name;
+ private final ClassLoader classLoader;
+
+ /**
+ * Creates a new REST API documentation provider.
+ *
+ * @param key REST API key
+ * @param name REST API name
+ * @param classLoader class loader
+ */
+ public ApiDocProvider(String key, String name, ClassLoader classLoader) {
+ this.key = checkNotNull(key, "Key cannot be null");
+ this.name = checkNotNull(name, "Name cannot be null");
+ this.classLoader = checkNotNull(classLoader, "Class loader cannot be null");
+ }
+
+ /**
+ * Returns the REST API key.
+ *
+ * @return REST API key
+ */
+ public String key() {
+ return key;
+ }
+
+ /**
+ * Returns the REST API name.
+ *
+ * @return REST API name
+ */
+ public String name() {
+ return name;
+ }
+
+ /**
+ * Returns input stream containing Swagger UI compatible JSON.
+ *
+ * @return input stream with Swagger JSON data
+ */
+ public InputStream docs() {
+ return get(DOCS);
+ }
+
+ /**
+ * Returns input stream containing JSON model schema.
+ *
+ * @return input stream with JSON model schema
+ */
+ public InputStream model() {
+ return get(MODEL);
+ }
+
+ private InputStream get(String resource) {
+ InputStream stream = classLoader.getResourceAsStream(resource);
+ if (stream == null) {
+ log.warn("Unable to find JSON resource {}", resource);
+ }
+ return stream;
+ }
+
+}
diff --git a/core/api/src/main/java/org/onosproject/rest/ApiDocService.java b/core/api/src/main/java/org/onosproject/rest/ApiDocService.java
new file mode 100644
index 0000000..f726853
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/rest/ApiDocService.java
@@ -0,0 +1,58 @@
+/*
+ * 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.rest;
+
+import com.google.common.annotations.Beta;
+
+import java.util.Set;
+
+/**
+ * Service for registering REST API documentation resources.
+ */
+@Beta
+public interface ApiDocService {
+
+ /**
+ * Registers the specified REST API documentation provider.
+ *
+ * @param provider REST API documentation provider
+ */
+ void register(ApiDocProvider provider);
+
+ /**
+ * Unregisters the specified REST API documentation provider.
+ *
+ * @param provider REST API documentation provider
+ */
+ void unregister(ApiDocProvider provider);
+
+ /**
+ * Returns the set of all registered REST API documentation providers.
+ *
+ * @return set of registered documentation providers
+ */
+ Set<ApiDocProvider> getDocProviders();
+
+ /**
+ * Returns the specified REST API documentation provider with the specified
+ * key.
+ *
+ * @param key REST API key
+ * @return documentation provider
+ */
+ ApiDocProvider getDocProvider(String key);
+
+}