CORD-433 Implemented XOS REST client and cordvtn service API

Change-Id: I4324240e1cdc9c0b059757565e20fdab995b9e5d
diff --git a/apps/xosclient/src/main/java/org/onosproject/xosclient/impl/DefaultVtnServiceApi.java b/apps/xosclient/src/main/java/org/onosproject/xosclient/impl/DefaultVtnServiceApi.java
new file mode 100644
index 0000000..d327dae
--- /dev/null
+++ b/apps/xosclient/src/main/java/org/onosproject/xosclient/impl/DefaultVtnServiceApi.java
@@ -0,0 +1,131 @@
+/*
+ * Copyright 2016-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.xosclient.impl;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Strings;
+import com.google.common.collect.Sets;
+import org.onosproject.xosclient.api.VtnServiceApi;
+import org.onosproject.xosclient.api.XosAccess;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Provides CORD VTN service and service dependency APIs.
+ */
+public final class DefaultVtnServiceApi extends XosApi implements VtnServiceApi {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    private static DefaultVtnServiceApi instance = null;
+
+    /**
+     * Default constructor.
+     */
+    private DefaultVtnServiceApi(String baseUrl, XosAccess access) {
+        super(baseUrl, access);
+    }
+
+    /**
+     * Returns VTN service API instance. Creates a new instance only if base url or
+     * access has been changed.
+     *
+     * @param baseUrl base url
+     * @param access access
+     * @return vtn service api
+     */
+    public static synchronized DefaultVtnServiceApi getInstance(String baseUrl, XosAccess access) {
+        checkNotNull(access, "XOS access information is null");
+        checkArgument(!Strings.isNullOrEmpty(baseUrl), "VTN service API base url is null or empty");
+
+        if (instance == null ||
+                !instance.baseUrl.equals(baseUrl) ||
+                !instance.access.equals(access)) {
+            instance = new DefaultVtnServiceApi(baseUrl, access);
+        }
+        return instance;
+    }
+
+    @Override
+    public Set<String> services() {
+        String response = restGet(EMPTY_STRING);
+        log.trace("Get services {}", response);
+
+        ObjectMapper mapper = new ObjectMapper();
+        try {
+            JsonNode nodes = mapper.readTree(response);
+            return Sets.newHashSet(nodes.fieldNames());
+        } catch (IOException e) {
+            log.warn("Failed to get service list");
+            return Sets.newHashSet();
+        }
+    }
+
+    @Override
+    public Set<String> getProviderServices(String tServiceId) {
+        checkNotNull(tServiceId);
+
+        String response = restGet(tServiceId);
+        log.trace("Get provider services {}", response);
+
+        ObjectMapper mapper = new ObjectMapper();
+        Set<String> pServices = Sets.newHashSet();
+
+        try {
+            JsonNode nodes = mapper.readTree(response);
+            nodes.forEach(node -> pServices.add(node.asText()));
+        } catch (IOException e) {
+            log.warn("Failed to get service dependency");
+        }
+        return pServices;
+    }
+
+    @Override
+    public Set<String> getTenantServices(String tServiceId) {
+        checkNotNull(tServiceId);
+
+        String response = restGet(EMPTY_STRING);
+        log.trace("Get tenant services {}", response);
+
+        ObjectMapper mapper = new ObjectMapper();
+        Set<String> tServices = Sets.newHashSet();
+        try {
+            JsonNode nodes = mapper.readTree(response);
+            Iterator<Map.Entry<String, JsonNode>> iterator = nodes.fields();
+
+            while (iterator.hasNext()) {
+                Map.Entry<String, JsonNode> entry = iterator.next();
+                entry.getValue().forEach(pService -> {
+                    if (pService.asText().equals(tServiceId)) {
+                        tServices.add(entry.getKey());
+                    }
+                });
+            }
+        } catch (IOException e) {
+            log.warn("Failed to get service list");
+        }
+        return tServices;
+    }
+}
diff --git a/apps/xosclient/src/main/java/org/onosproject/xosclient/impl/XosApi.java b/apps/xosclient/src/main/java/org/onosproject/xosclient/impl/XosApi.java
new file mode 100644
index 0000000..01c2bbb
--- /dev/null
+++ b/apps/xosclient/src/main/java/org/onosproject/xosclient/impl/XosApi.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2016-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.xosclient.impl;
+
+import org.onosproject.xosclient.api.XosAccess;
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Invocation;
+import javax.ws.rs.client.WebTarget;
+
+import static com.google.common.net.MediaType.JSON_UTF_8;
+
+/**
+ * XOS common REST API implementation.
+ */
+public class XosApi {
+
+    protected static final String EMPTY_STRING = "";
+
+    protected final String baseUrl;
+    protected final XosAccess access;
+    protected final Client client;
+
+    /**
+     * Default constructor.
+     *
+     * @param baseUrl base url of this api
+     * @param xosAccess xos access
+     */
+    public XosApi(String baseUrl, XosAccess xosAccess) {
+        this.baseUrl = baseUrl;
+        this.access = xosAccess;
+        this.client = ClientBuilder.newClient();
+    }
+
+    /**
+     * Returns the access of this api.
+     *
+     * @return xos access
+     */
+    public XosAccess access() {
+        return this.access;
+    }
+
+    /**
+     * Returns the base url of this api.
+     *
+     * @return base url
+     */
+    public String baseUrl() {
+        return this.baseUrl;
+    }
+
+    /**
+     * Returns response of the REST get operation with a given additional path.
+     *
+     * @param path path or null
+     * @return response json string
+     */
+    public String restGet(String path) {
+        WebTarget wt = client.target(access.endpoint() + baseUrl).path(path);
+        Invocation.Builder builder = wt.request(JSON_UTF_8.toString());
+
+        return builder.get(String.class);
+    }
+}
diff --git a/apps/xosclient/src/main/java/org/onosproject/xosclient/impl/XosClient.java b/apps/xosclient/src/main/java/org/onosproject/xosclient/impl/XosClient.java
new file mode 100644
index 0000000..13f3aa8
--- /dev/null
+++ b/apps/xosclient/src/main/java/org/onosproject/xosclient/impl/XosClient.java
@@ -0,0 +1,170 @@
+/*
+ * Copyright 2016-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.xosclient.impl;
+
+import com.google.common.base.Strings;
+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.Modified;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
+import org.onlab.util.Tools;
+import org.onosproject.cfg.ComponentConfigService;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreService;
+import org.onosproject.net.config.ConfigFactory;
+import org.onosproject.net.config.NetworkConfigEvent;
+import org.onosproject.net.config.NetworkConfigListener;
+import org.onosproject.net.config.NetworkConfigRegistry;
+import org.onosproject.net.config.basics.SubjectFactories;
+import org.onosproject.xosclient.api.VtnServiceApi;
+import org.onosproject.xosclient.api.XosAccess;
+import org.onosproject.xosclient.api.XosAccessConfig;
+import org.onosproject.xosclient.api.XosClientService;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+
+import java.util.Dictionary;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Provides interactions with XOS.
+ */
+@Component(immediate = true)
+@Service
+public class XosClient implements XosClientService {
+
+    protected final Logger log = getLogger(getClass());
+
+    private static final String VTN_BASE_URL = "vtnBaseUrl";
+    private static final String DEFAULT_VTN_BASE_URL = "/xoslib/rs/vtn/services/";
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected CoreService coreService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected ComponentConfigService componentConfigService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected NetworkConfigRegistry configRegistry;
+
+    @Property(name = VTN_BASE_URL, value = DEFAULT_VTN_BASE_URL,
+            label = "XOS VTN service API base url")
+    private String vtnBaseUrl = DEFAULT_VTN_BASE_URL;
+
+    private final ConfigFactory configFactory =
+            new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, XosAccessConfig.class, "xosclient") {
+                @Override
+                public XosAccessConfig createConfig() {
+                    return new XosAccessConfig();
+                }
+            };
+
+    private final NetworkConfigListener configListener = new InternalConfigListener();
+
+    private ApplicationId appId;
+    private XosAccess access = null;
+
+    @Activate
+    protected void activate(ComponentContext context) {
+        appId = coreService.registerApplication("org.onosproject.xosclient");
+
+        componentConfigService.registerProperties(getClass());
+        modified(context);
+
+        configRegistry.registerConfigFactory(configFactory);
+        configRegistry.addListener(configListener);
+
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        log.info("Stopped");
+    }
+
+    @Modified
+    protected void modified(ComponentContext context) {
+        Dictionary<?, ?> properties = context.getProperties();
+
+        String updatedUrl = Tools.get(properties, VTN_BASE_URL);
+        if (!Strings.isNullOrEmpty(updatedUrl)) {
+            vtnBaseUrl = updatedUrl;
+        }
+
+        log.info("Modified");
+    }
+
+    @Override
+    public XosAccess access() {
+        return access;
+    }
+
+    @Override
+    public synchronized boolean setAccess(XosAccess xosAccess) {
+        checkNotNull(xosAccess);
+
+        // TODO authentication later before using the access
+        access = xosAccess;
+        return true;
+    }
+
+    @Override
+    public VtnServiceApi vtnServiceApi() {
+        checkNotNull(access, "XOS API access is not set");
+        return DefaultVtnServiceApi.getInstance(vtnBaseUrl, access);
+    }
+
+    /*
+     * adds more XOS service APIs below.
+     */
+
+    private void readConfiguration() {
+        XosAccessConfig config = configRegistry.getConfig(appId, XosAccessConfig.class);
+        if (config == null) {
+            log.debug("No configuration found");
+            return;
+        }
+
+        setAccess(config.xosAccess());
+    }
+
+    private class InternalConfigListener implements NetworkConfigListener {
+
+        @Override
+        public void event(NetworkConfigEvent event) {
+            if (!event.configClass().equals(XosAccessConfig.class)) {
+                return;
+            }
+
+            switch (event.type()) {
+                case CONFIG_ADDED:
+                case CONFIG_UPDATED:
+                    log.info("Network configuration changed");
+                    readConfiguration();
+                    break;
+                default:
+                    break;
+            }
+        }
+    }
+}
diff --git a/apps/xosclient/src/main/java/org/onosproject/xosclient/impl/package-info.java b/apps/xosclient/src/main/java/org/onosproject/xosclient/impl/package-info.java
new file mode 100644
index 0000000..3939420
--- /dev/null
+++ b/apps/xosclient/src/main/java/org/onosproject/xosclient/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-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.
+ */
+
+/**
+ * XOS client application.
+ */
+package org.onosproject.xosclient.impl;
\ No newline at end of file