Committed by
Gerrit Code Review
CORD-433 Implemented XOS REST client and cordvtn service API
Change-Id: I4324240e1cdc9c0b059757565e20fdab995b9e5d
Showing
12 changed files
with
759 additions
and
1 deletions
| ... | @@ -72,7 +72,8 @@ | ... | @@ -72,7 +72,8 @@ |
| 72 | <module>influxdbmetrics</module> | 72 | <module>influxdbmetrics</module> |
| 73 | <module>gangliametrics</module> | 73 | <module>gangliametrics</module> |
| 74 | <module>graphitemetrics</module> | 74 | <module>graphitemetrics</module> |
| 75 | - </modules> | 75 | + <module>xosclient</module> |
| 76 | + </modules> | ||
| 76 | 77 | ||
| 77 | <properties> | 78 | <properties> |
| 78 | <web.context>default</web.context> | 79 | <web.context>default</web.context> | ... | ... |
apps/xosclient/pom.xml
0 → 100644
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | +<!-- | ||
| 3 | + ~ Copyright 2016 Open Networking Laboratory | ||
| 4 | + ~ | ||
| 5 | + ~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 6 | + ~ you may not use this file except in compliance with the License. | ||
| 7 | + ~ You may obtain a copy of the License at | ||
| 8 | + ~ | ||
| 9 | + ~ http://www.apache.org/licenses/LICENSE-2.0 | ||
| 10 | + ~ | ||
| 11 | + ~ Unless required by applicable law or agreed to in writing, software | ||
| 12 | + ~ distributed under the License is distributed on an "AS IS" BASIS, | ||
| 13 | + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 14 | + ~ See the License for the specific language governing permissions and | ||
| 15 | + ~ limitations under the License. | ||
| 16 | + --> | ||
| 17 | +<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| 18 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 19 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 20 | + <modelVersion>4.0.0</modelVersion> | ||
| 21 | + | ||
| 22 | + <parent> | ||
| 23 | + <artifactId>onos-apps</artifactId> | ||
| 24 | + <groupId>org.onosproject</groupId> | ||
| 25 | + <version>1.6.0-SNAPSHOT</version> | ||
| 26 | + <relativePath>../pom.xml</relativePath> | ||
| 27 | + </parent> | ||
| 28 | + | ||
| 29 | + <artifactId>onos-app-xos-client</artifactId> | ||
| 30 | + <packaging>bundle</packaging> | ||
| 31 | + | ||
| 32 | + <description>XOS client service</description> | ||
| 33 | + | ||
| 34 | + <properties> | ||
| 35 | + <onos.app.name>org.onosproject.xosclient</onos.app.name> | ||
| 36 | + <onos.app.title>XOS Client App</onos.app.title> | ||
| 37 | + <onos.app.category>Utility</onos.app.category> | ||
| 38 | + <onos.app.url>http://onosproject.org</onos.app.url> | ||
| 39 | + <onos.app.readme>XOS Client Application.</onos.app.readme> | ||
| 40 | + </properties> | ||
| 41 | + | ||
| 42 | + <dependencies> | ||
| 43 | + <dependency> | ||
| 44 | + <groupId>org.osgi</groupId> | ||
| 45 | + <artifactId>org.osgi.compendium</artifactId> | ||
| 46 | + </dependency> | ||
| 47 | + </dependencies> | ||
| 48 | + | ||
| 49 | +</project> |
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.xosclient.api; | ||
| 17 | + | ||
| 18 | +import java.util.Set; | ||
| 19 | + | ||
| 20 | +/** | ||
| 21 | + * Service for interacting with XOS VTN service and service dependency. | ||
| 22 | + */ | ||
| 23 | +public interface VtnServiceApi { | ||
| 24 | + | ||
| 25 | + /** | ||
| 26 | + * Returns all services list. | ||
| 27 | + * | ||
| 28 | + * @return service list | ||
| 29 | + */ | ||
| 30 | + Set<String> services(); | ||
| 31 | + | ||
| 32 | + /** | ||
| 33 | + * Returns dependent tenant services of a given provider service. | ||
| 34 | + * | ||
| 35 | + * @param pServiceId service id | ||
| 36 | + * @return service list | ||
| 37 | + */ | ||
| 38 | + Set<String> getTenantServices(String pServiceId); | ||
| 39 | + | ||
| 40 | + /** | ||
| 41 | + * Returns dependent provider services of a given tenant service. | ||
| 42 | + * | ||
| 43 | + * @param tServiceId service id | ||
| 44 | + * @return set of services | ||
| 45 | + */ | ||
| 46 | + Set<String> getProviderServices(String tServiceId); | ||
| 47 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.xosclient.api; | ||
| 17 | + | ||
| 18 | +import com.google.common.base.MoreObjects; | ||
| 19 | + | ||
| 20 | +import java.util.Objects; | ||
| 21 | + | ||
| 22 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 23 | + | ||
| 24 | +/** | ||
| 25 | + * Objects holding XOS API access information. | ||
| 26 | + */ | ||
| 27 | +public class XosAccess { | ||
| 28 | + | ||
| 29 | + private final String endpoint; | ||
| 30 | + private final String username; | ||
| 31 | + private final String password; | ||
| 32 | + | ||
| 33 | + /** | ||
| 34 | + * Default constructor. | ||
| 35 | + * | ||
| 36 | + * @param endpoint XOS service endpoint | ||
| 37 | + * @param adminUser admin user name | ||
| 38 | + * @param adminPassword admin password | ||
| 39 | + */ | ||
| 40 | + public XosAccess(String endpoint, String adminUser, String adminPassword) { | ||
| 41 | + this.endpoint = checkNotNull(endpoint); | ||
| 42 | + this.username = checkNotNull(adminUser); | ||
| 43 | + this.password = checkNotNull(adminPassword); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + /** | ||
| 47 | + * Returns XOS service endpoint. | ||
| 48 | + * | ||
| 49 | + * @return endpoint | ||
| 50 | + */ | ||
| 51 | + public String endpoint() { | ||
| 52 | + return this.endpoint; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + /** | ||
| 56 | + * Returns admin user name. | ||
| 57 | + * | ||
| 58 | + * @return user name | ||
| 59 | + */ | ||
| 60 | + public String username() { | ||
| 61 | + return this.username; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + /** | ||
| 65 | + * Returns admin password. | ||
| 66 | + * | ||
| 67 | + * @return password | ||
| 68 | + */ | ||
| 69 | + public String password() { | ||
| 70 | + return this.password; | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + @Override | ||
| 74 | + public int hashCode() { | ||
| 75 | + return Objects.hash(endpoint, username, password); | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + @Override | ||
| 79 | + public boolean equals(Object obj) { | ||
| 80 | + if (this == obj) { | ||
| 81 | + return true; | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + if ((obj instanceof XosAccess)) { | ||
| 85 | + XosAccess that = (XosAccess) obj; | ||
| 86 | + if (Objects.equals(endpoint, that.endpoint) && | ||
| 87 | + Objects.equals(username, that.username) && | ||
| 88 | + Objects.equals(password, that.password)) { | ||
| 89 | + return true; | ||
| 90 | + } | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + return false; | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + @Override | ||
| 97 | + public String toString() { | ||
| 98 | + return MoreObjects.toStringHelper(getClass()) | ||
| 99 | + .add("endpoint", endpoint) | ||
| 100 | + .add("username", username) | ||
| 101 | + .add("password", password) | ||
| 102 | + .toString(); | ||
| 103 | + } | ||
| 104 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.xosclient.api; | ||
| 17 | + | ||
| 18 | +import com.fasterxml.jackson.databind.JsonNode; | ||
| 19 | +import org.onosproject.core.ApplicationId; | ||
| 20 | +import org.onosproject.net.config.Config; | ||
| 21 | +import org.slf4j.Logger; | ||
| 22 | + | ||
| 23 | +import static org.slf4j.LoggerFactory.getLogger; | ||
| 24 | + | ||
| 25 | +/** | ||
| 26 | + * XOS API access information. | ||
| 27 | + */ | ||
| 28 | +public class XosAccessConfig extends Config<ApplicationId> { | ||
| 29 | + | ||
| 30 | + protected final Logger log = getLogger(getClass()); | ||
| 31 | + | ||
| 32 | + private static final String XOS_SERVICE_ENDPOINT = "serviceEndpoint"; | ||
| 33 | + private static final String XOS_ADMIN_USER = "adminUser"; | ||
| 34 | + private static final String XOS_ADMIN_PASSWORD = "adminPassword"; | ||
| 35 | + | ||
| 36 | + /** | ||
| 37 | + * Returns XOS access information. | ||
| 38 | + * | ||
| 39 | + * @return XOS access, or null | ||
| 40 | + */ | ||
| 41 | + public XosAccess xosAccess() { | ||
| 42 | + try { | ||
| 43 | + return new XosAccess(getConfig(object, XOS_SERVICE_ENDPOINT), | ||
| 44 | + getConfig(object, XOS_ADMIN_USER), | ||
| 45 | + getConfig(object, XOS_ADMIN_PASSWORD)); | ||
| 46 | + } catch (NullPointerException e) { | ||
| 47 | + log.error("Failed to get XOS access"); | ||
| 48 | + return null; | ||
| 49 | + } | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + /** | ||
| 53 | + * Returns value of a given path. If the path is missing, show log and return | ||
| 54 | + * null. | ||
| 55 | + * | ||
| 56 | + * @param path path | ||
| 57 | + * @return value or null | ||
| 58 | + */ | ||
| 59 | + private String getConfig(JsonNode jsonNode, String path) { | ||
| 60 | + jsonNode = jsonNode.path(path); | ||
| 61 | + | ||
| 62 | + if (jsonNode.isMissingNode()) { | ||
| 63 | + log.error("{} is not configured", path); | ||
| 64 | + return null; | ||
| 65 | + } else { | ||
| 66 | + return jsonNode.asText(); | ||
| 67 | + } | ||
| 68 | + } | ||
| 69 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.xosclient.api; | ||
| 17 | + | ||
| 18 | +/** | ||
| 19 | + * Provides interactions with XOS. | ||
| 20 | + */ | ||
| 21 | +public interface XosClientService { | ||
| 22 | + | ||
| 23 | + /** | ||
| 24 | + * Returns XOS API access information of the client. | ||
| 25 | + * | ||
| 26 | + * @return xos access | ||
| 27 | + */ | ||
| 28 | + XosAccess access(); | ||
| 29 | + | ||
| 30 | + /** | ||
| 31 | + * Sets the XOS API access information to the client service. | ||
| 32 | + * | ||
| 33 | + * @return true if it is set and authenticated, otherwise false | ||
| 34 | + */ | ||
| 35 | + boolean setAccess(XosAccess xosAccess); | ||
| 36 | + | ||
| 37 | + /** | ||
| 38 | + * Returns CORD VTN service API. | ||
| 39 | + * | ||
| 40 | + * @return cord vtn service api | ||
| 41 | + */ | ||
| 42 | + VtnServiceApi vtnServiceApi(); | ||
| 43 | + | ||
| 44 | + /* | ||
| 45 | + * adds more XOS service APIs below. | ||
| 46 | + */ | ||
| 47 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +/** | ||
| 18 | + * XOS client application API. | ||
| 19 | + */ | ||
| 20 | +package org.onosproject.xosclient.api; | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.xosclient.impl; | ||
| 17 | + | ||
| 18 | +import com.fasterxml.jackson.databind.JsonNode; | ||
| 19 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 20 | +import com.google.common.base.Strings; | ||
| 21 | +import com.google.common.collect.Sets; | ||
| 22 | +import org.onosproject.xosclient.api.VtnServiceApi; | ||
| 23 | +import org.onosproject.xosclient.api.XosAccess; | ||
| 24 | +import org.slf4j.Logger; | ||
| 25 | +import org.slf4j.LoggerFactory; | ||
| 26 | + | ||
| 27 | +import java.io.IOException; | ||
| 28 | +import java.util.Iterator; | ||
| 29 | +import java.util.Map; | ||
| 30 | +import java.util.Set; | ||
| 31 | + | ||
| 32 | +import static com.google.common.base.Preconditions.checkArgument; | ||
| 33 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 34 | + | ||
| 35 | +/** | ||
| 36 | + * Provides CORD VTN service and service dependency APIs. | ||
| 37 | + */ | ||
| 38 | +public final class DefaultVtnServiceApi extends XosApi implements VtnServiceApi { | ||
| 39 | + | ||
| 40 | + private final Logger log = LoggerFactory.getLogger(getClass()); | ||
| 41 | + | ||
| 42 | + private static DefaultVtnServiceApi instance = null; | ||
| 43 | + | ||
| 44 | + /** | ||
| 45 | + * Default constructor. | ||
| 46 | + */ | ||
| 47 | + private DefaultVtnServiceApi(String baseUrl, XosAccess access) { | ||
| 48 | + super(baseUrl, access); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + /** | ||
| 52 | + * Returns VTN service API instance. Creates a new instance only if base url or | ||
| 53 | + * access has been changed. | ||
| 54 | + * | ||
| 55 | + * @param baseUrl base url | ||
| 56 | + * @param access access | ||
| 57 | + * @return vtn service api | ||
| 58 | + */ | ||
| 59 | + public static synchronized DefaultVtnServiceApi getInstance(String baseUrl, XosAccess access) { | ||
| 60 | + checkNotNull(access, "XOS access information is null"); | ||
| 61 | + checkArgument(!Strings.isNullOrEmpty(baseUrl), "VTN service API base url is null or empty"); | ||
| 62 | + | ||
| 63 | + if (instance == null || | ||
| 64 | + !instance.baseUrl.equals(baseUrl) || | ||
| 65 | + !instance.access.equals(access)) { | ||
| 66 | + instance = new DefaultVtnServiceApi(baseUrl, access); | ||
| 67 | + } | ||
| 68 | + return instance; | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | + @Override | ||
| 72 | + public Set<String> services() { | ||
| 73 | + String response = restGet(EMPTY_STRING); | ||
| 74 | + log.trace("Get services {}", response); | ||
| 75 | + | ||
| 76 | + ObjectMapper mapper = new ObjectMapper(); | ||
| 77 | + try { | ||
| 78 | + JsonNode nodes = mapper.readTree(response); | ||
| 79 | + return Sets.newHashSet(nodes.fieldNames()); | ||
| 80 | + } catch (IOException e) { | ||
| 81 | + log.warn("Failed to get service list"); | ||
| 82 | + return Sets.newHashSet(); | ||
| 83 | + } | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + @Override | ||
| 87 | + public Set<String> getProviderServices(String tServiceId) { | ||
| 88 | + checkNotNull(tServiceId); | ||
| 89 | + | ||
| 90 | + String response = restGet(tServiceId); | ||
| 91 | + log.trace("Get provider services {}", response); | ||
| 92 | + | ||
| 93 | + ObjectMapper mapper = new ObjectMapper(); | ||
| 94 | + Set<String> pServices = Sets.newHashSet(); | ||
| 95 | + | ||
| 96 | + try { | ||
| 97 | + JsonNode nodes = mapper.readTree(response); | ||
| 98 | + nodes.forEach(node -> pServices.add(node.asText())); | ||
| 99 | + } catch (IOException e) { | ||
| 100 | + log.warn("Failed to get service dependency"); | ||
| 101 | + } | ||
| 102 | + return pServices; | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + @Override | ||
| 106 | + public Set<String> getTenantServices(String tServiceId) { | ||
| 107 | + checkNotNull(tServiceId); | ||
| 108 | + | ||
| 109 | + String response = restGet(EMPTY_STRING); | ||
| 110 | + log.trace("Get tenant services {}", response); | ||
| 111 | + | ||
| 112 | + ObjectMapper mapper = new ObjectMapper(); | ||
| 113 | + Set<String> tServices = Sets.newHashSet(); | ||
| 114 | + try { | ||
| 115 | + JsonNode nodes = mapper.readTree(response); | ||
| 116 | + Iterator<Map.Entry<String, JsonNode>> iterator = nodes.fields(); | ||
| 117 | + | ||
| 118 | + while (iterator.hasNext()) { | ||
| 119 | + Map.Entry<String, JsonNode> entry = iterator.next(); | ||
| 120 | + entry.getValue().forEach(pService -> { | ||
| 121 | + if (pService.asText().equals(tServiceId)) { | ||
| 122 | + tServices.add(entry.getKey()); | ||
| 123 | + } | ||
| 124 | + }); | ||
| 125 | + } | ||
| 126 | + } catch (IOException e) { | ||
| 127 | + log.warn("Failed to get service list"); | ||
| 128 | + } | ||
| 129 | + return tServices; | ||
| 130 | + } | ||
| 131 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.xosclient.impl; | ||
| 17 | + | ||
| 18 | +import org.onosproject.xosclient.api.XosAccess; | ||
| 19 | + | ||
| 20 | +import javax.ws.rs.client.Client; | ||
| 21 | +import javax.ws.rs.client.ClientBuilder; | ||
| 22 | +import javax.ws.rs.client.Invocation; | ||
| 23 | +import javax.ws.rs.client.WebTarget; | ||
| 24 | + | ||
| 25 | +import static com.google.common.net.MediaType.JSON_UTF_8; | ||
| 26 | + | ||
| 27 | +/** | ||
| 28 | + * XOS common REST API implementation. | ||
| 29 | + */ | ||
| 30 | +public class XosApi { | ||
| 31 | + | ||
| 32 | + protected static final String EMPTY_STRING = ""; | ||
| 33 | + | ||
| 34 | + protected final String baseUrl; | ||
| 35 | + protected final XosAccess access; | ||
| 36 | + protected final Client client; | ||
| 37 | + | ||
| 38 | + /** | ||
| 39 | + * Default constructor. | ||
| 40 | + * | ||
| 41 | + * @param baseUrl base url of this api | ||
| 42 | + * @param xosAccess xos access | ||
| 43 | + */ | ||
| 44 | + public XosApi(String baseUrl, XosAccess xosAccess) { | ||
| 45 | + this.baseUrl = baseUrl; | ||
| 46 | + this.access = xosAccess; | ||
| 47 | + this.client = ClientBuilder.newClient(); | ||
| 48 | + } | ||
| 49 | + | ||
| 50 | + /** | ||
| 51 | + * Returns the access of this api. | ||
| 52 | + * | ||
| 53 | + * @return xos access | ||
| 54 | + */ | ||
| 55 | + public XosAccess access() { | ||
| 56 | + return this.access; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + /** | ||
| 60 | + * Returns the base url of this api. | ||
| 61 | + * | ||
| 62 | + * @return base url | ||
| 63 | + */ | ||
| 64 | + public String baseUrl() { | ||
| 65 | + return this.baseUrl; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + /** | ||
| 69 | + * Returns response of the REST get operation with a given additional path. | ||
| 70 | + * | ||
| 71 | + * @param path path or null | ||
| 72 | + * @return response json string | ||
| 73 | + */ | ||
| 74 | + public String restGet(String path) { | ||
| 75 | + WebTarget wt = client.target(access.endpoint() + baseUrl).path(path); | ||
| 76 | + Invocation.Builder builder = wt.request(JSON_UTF_8.toString()); | ||
| 77 | + | ||
| 78 | + return builder.get(String.class); | ||
| 79 | + } | ||
| 80 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | +package org.onosproject.xosclient.impl; | ||
| 17 | + | ||
| 18 | +import com.google.common.base.Strings; | ||
| 19 | +import org.apache.felix.scr.annotations.Activate; | ||
| 20 | +import org.apache.felix.scr.annotations.Component; | ||
| 21 | +import org.apache.felix.scr.annotations.Deactivate; | ||
| 22 | +import org.apache.felix.scr.annotations.Modified; | ||
| 23 | +import org.apache.felix.scr.annotations.Property; | ||
| 24 | +import org.apache.felix.scr.annotations.Reference; | ||
| 25 | +import org.apache.felix.scr.annotations.ReferenceCardinality; | ||
| 26 | +import org.apache.felix.scr.annotations.Service; | ||
| 27 | +import org.onlab.util.Tools; | ||
| 28 | +import org.onosproject.cfg.ComponentConfigService; | ||
| 29 | +import org.onosproject.core.ApplicationId; | ||
| 30 | +import org.onosproject.core.CoreService; | ||
| 31 | +import org.onosproject.net.config.ConfigFactory; | ||
| 32 | +import org.onosproject.net.config.NetworkConfigEvent; | ||
| 33 | +import org.onosproject.net.config.NetworkConfigListener; | ||
| 34 | +import org.onosproject.net.config.NetworkConfigRegistry; | ||
| 35 | +import org.onosproject.net.config.basics.SubjectFactories; | ||
| 36 | +import org.onosproject.xosclient.api.VtnServiceApi; | ||
| 37 | +import org.onosproject.xosclient.api.XosAccess; | ||
| 38 | +import org.onosproject.xosclient.api.XosAccessConfig; | ||
| 39 | +import org.onosproject.xosclient.api.XosClientService; | ||
| 40 | +import org.osgi.service.component.ComponentContext; | ||
| 41 | +import org.slf4j.Logger; | ||
| 42 | + | ||
| 43 | +import java.util.Dictionary; | ||
| 44 | + | ||
| 45 | +import static org.slf4j.LoggerFactory.getLogger; | ||
| 46 | + | ||
| 47 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
| 48 | + | ||
| 49 | +/** | ||
| 50 | + * Provides interactions with XOS. | ||
| 51 | + */ | ||
| 52 | +@Component(immediate = true) | ||
| 53 | +@Service | ||
| 54 | +public class XosClient implements XosClientService { | ||
| 55 | + | ||
| 56 | + protected final Logger log = getLogger(getClass()); | ||
| 57 | + | ||
| 58 | + private static final String VTN_BASE_URL = "vtnBaseUrl"; | ||
| 59 | + private static final String DEFAULT_VTN_BASE_URL = "/xoslib/rs/vtn/services/"; | ||
| 60 | + | ||
| 61 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
| 62 | + protected CoreService coreService; | ||
| 63 | + | ||
| 64 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
| 65 | + protected ComponentConfigService componentConfigService; | ||
| 66 | + | ||
| 67 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
| 68 | + protected NetworkConfigRegistry configRegistry; | ||
| 69 | + | ||
| 70 | + @Property(name = VTN_BASE_URL, value = DEFAULT_VTN_BASE_URL, | ||
| 71 | + label = "XOS VTN service API base url") | ||
| 72 | + private String vtnBaseUrl = DEFAULT_VTN_BASE_URL; | ||
| 73 | + | ||
| 74 | + private final ConfigFactory configFactory = | ||
| 75 | + new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, XosAccessConfig.class, "xosclient") { | ||
| 76 | + @Override | ||
| 77 | + public XosAccessConfig createConfig() { | ||
| 78 | + return new XosAccessConfig(); | ||
| 79 | + } | ||
| 80 | + }; | ||
| 81 | + | ||
| 82 | + private final NetworkConfigListener configListener = new InternalConfigListener(); | ||
| 83 | + | ||
| 84 | + private ApplicationId appId; | ||
| 85 | + private XosAccess access = null; | ||
| 86 | + | ||
| 87 | + @Activate | ||
| 88 | + protected void activate(ComponentContext context) { | ||
| 89 | + appId = coreService.registerApplication("org.onosproject.xosclient"); | ||
| 90 | + | ||
| 91 | + componentConfigService.registerProperties(getClass()); | ||
| 92 | + modified(context); | ||
| 93 | + | ||
| 94 | + configRegistry.registerConfigFactory(configFactory); | ||
| 95 | + configRegistry.addListener(configListener); | ||
| 96 | + | ||
| 97 | + log.info("Started"); | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + @Deactivate | ||
| 101 | + protected void deactivate() { | ||
| 102 | + log.info("Stopped"); | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + @Modified | ||
| 106 | + protected void modified(ComponentContext context) { | ||
| 107 | + Dictionary<?, ?> properties = context.getProperties(); | ||
| 108 | + | ||
| 109 | + String updatedUrl = Tools.get(properties, VTN_BASE_URL); | ||
| 110 | + if (!Strings.isNullOrEmpty(updatedUrl)) { | ||
| 111 | + vtnBaseUrl = updatedUrl; | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + log.info("Modified"); | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + @Override | ||
| 118 | + public XosAccess access() { | ||
| 119 | + return access; | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + @Override | ||
| 123 | + public synchronized boolean setAccess(XosAccess xosAccess) { | ||
| 124 | + checkNotNull(xosAccess); | ||
| 125 | + | ||
| 126 | + // TODO authentication later before using the access | ||
| 127 | + access = xosAccess; | ||
| 128 | + return true; | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + @Override | ||
| 132 | + public VtnServiceApi vtnServiceApi() { | ||
| 133 | + checkNotNull(access, "XOS API access is not set"); | ||
| 134 | + return DefaultVtnServiceApi.getInstance(vtnBaseUrl, access); | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + /* | ||
| 138 | + * adds more XOS service APIs below. | ||
| 139 | + */ | ||
| 140 | + | ||
| 141 | + private void readConfiguration() { | ||
| 142 | + XosAccessConfig config = configRegistry.getConfig(appId, XosAccessConfig.class); | ||
| 143 | + if (config == null) { | ||
| 144 | + log.debug("No configuration found"); | ||
| 145 | + return; | ||
| 146 | + } | ||
| 147 | + | ||
| 148 | + setAccess(config.xosAccess()); | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + private class InternalConfigListener implements NetworkConfigListener { | ||
| 152 | + | ||
| 153 | + @Override | ||
| 154 | + public void event(NetworkConfigEvent event) { | ||
| 155 | + if (!event.configClass().equals(XosAccessConfig.class)) { | ||
| 156 | + return; | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + switch (event.type()) { | ||
| 160 | + case CONFIG_ADDED: | ||
| 161 | + case CONFIG_UPDATED: | ||
| 162 | + log.info("Network configuration changed"); | ||
| 163 | + readConfiguration(); | ||
| 164 | + break; | ||
| 165 | + default: | ||
| 166 | + break; | ||
| 167 | + } | ||
| 168 | + } | ||
| 169 | + } | ||
| 170 | +} |
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +/** | ||
| 18 | + * XOS client application. | ||
| 19 | + */ | ||
| 20 | +package org.onosproject.xosclient.impl; | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 1 | +/* | ||
| 2 | + * Copyright 2016-present Open Networking Laboratory | ||
| 3 | + * | ||
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| 5 | + * you may not use this file except in compliance with the License. | ||
| 6 | + * You may obtain a copy of the License at | ||
| 7 | + * | ||
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
| 9 | + * | ||
| 10 | + * Unless required by applicable law or agreed to in writing, software | ||
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | + * See the License for the specific language governing permissions and | ||
| 14 | + * limitations under the License. | ||
| 15 | + */ | ||
| 16 | + | ||
| 17 | +/** | ||
| 18 | + * XOS API client application. | ||
| 19 | + */ | ||
| 20 | +package org.onosproject.xosclient; | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment