Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 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 | */ |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 16 | package org.opencord.cordvtn.rest; |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.SerializationFeature; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.base.Strings; |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 22 | import org.glassfish.jersey.client.ClientProperties; |
Jonathan Hart | 19dcbae | 2017-07-12 09:52:59 -0700 | [diff] [blame] | 23 | import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 24 | import org.onosproject.rest.AbstractWebResource; |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 25 | import org.slf4j.Logger; |
| 26 | |
| 27 | import javax.ws.rs.client.Client; |
| 28 | import javax.ws.rs.client.ClientBuilder; |
Jonathan Hart | 19dcbae | 2017-07-12 09:52:59 -0700 | [diff] [blame] | 29 | import javax.ws.rs.client.Entity; |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 30 | import javax.ws.rs.client.Invocation; |
| 31 | import javax.ws.rs.client.WebTarget; |
Jonathan Hart | 19dcbae | 2017-07-12 09:52:59 -0700 | [diff] [blame] | 32 | import javax.ws.rs.core.MediaType; |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 33 | import javax.ws.rs.core.Response; |
| 34 | import java.io.IOException; |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 35 | |
| 36 | import static com.google.common.base.Preconditions.checkArgument; |
| 37 | import static com.google.common.net.MediaType.JSON_UTF_8; |
| 38 | import static java.net.HttpURLConnection.HTTP_OK; |
| 39 | import static org.slf4j.LoggerFactory.getLogger; |
| 40 | |
| 41 | /** |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 42 | * Implementation of REST client for XOS VTN networking service. |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 43 | */ |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 44 | public final class XosVtnNetworkingClient extends AbstractWebResource { |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 45 | |
| 46 | protected final Logger log = getLogger(getClass()); |
| 47 | |
Jonathan Hart | 19dcbae | 2017-07-12 09:52:59 -0700 | [diff] [blame] | 48 | private static final String URL_BASE = "/xosapi/v1/vtn/"; |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 49 | |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 50 | private static final String EMPTY_JSON_STRING = "{}"; |
| 51 | |
Jonathan Hart | 19dcbae | 2017-07-12 09:52:59 -0700 | [diff] [blame] | 52 | private static final String VTN_ID = "id"; |
| 53 | private static final String VTN_RESYNC = "resync"; |
| 54 | private static final String VTN_SERVICES = "vtnservices"; |
| 55 | |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 56 | private static final String ERR_LOG = "Received %s result with wrong format: %s"; |
| 57 | |
| 58 | private static final int DEFAULT_TIMEOUT_MS = 2000; |
| 59 | |
| 60 | private final String endpoint; |
| 61 | private final String user; |
| 62 | private final String password; |
| 63 | private final Client client = ClientBuilder.newClient(); |
| 64 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 65 | private XosVtnNetworkingClient(String endpoint, String user, String password) { |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 66 | this.endpoint = endpoint; |
| 67 | this.user = user; |
| 68 | this.password = password; |
| 69 | |
Jonathan Hart | 19dcbae | 2017-07-12 09:52:59 -0700 | [diff] [blame] | 70 | client.register(HttpAuthenticationFeature.basic(user, password)); |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 71 | client.property(ClientProperties.CONNECT_TIMEOUT, DEFAULT_TIMEOUT_MS); |
| 72 | client.property(ClientProperties.READ_TIMEOUT, DEFAULT_TIMEOUT_MS); |
| 73 | mapper().enable(SerializationFeature.INDENT_OUTPUT); |
| 74 | } |
| 75 | |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 76 | private String restGet(String path) { |
Jonathan Hart | 19dcbae | 2017-07-12 09:52:59 -0700 | [diff] [blame] | 77 | WebTarget wt = client.target("http://" + endpoint + URL_BASE).path(path); |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 78 | Invocation.Builder builder = wt.request(JSON_UTF_8.toString()); |
| 79 | try { |
| 80 | Response response = builder.get(); |
| 81 | if (response.getStatus() != HTTP_OK) { |
Hyunsun Moon | e72adf5 | 2017-02-22 08:30:25 +0900 | [diff] [blame] | 82 | log.warn("Failed to get resource {}", endpoint + URL_BASE + path); |
Jonathan Hart | 19dcbae | 2017-07-12 09:52:59 -0700 | [diff] [blame] | 83 | log.warn("reason {}", response.readEntity(String.class)); |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 84 | return EMPTY_JSON_STRING; |
| 85 | } |
| 86 | } catch (javax.ws.rs.ProcessingException e) { |
| 87 | return EMPTY_JSON_STRING; |
| 88 | } |
| 89 | return builder.get(String.class); |
| 90 | } |
| 91 | |
Jonathan Hart | 19dcbae | 2017-07-12 09:52:59 -0700 | [diff] [blame] | 92 | private String restPut(String path, JsonNode request) { |
| 93 | WebTarget wt = client.target("http://" + endpoint + URL_BASE).path(path); |
| 94 | Invocation.Builder builder = wt.request(MediaType.APPLICATION_JSON) |
| 95 | .accept(JSON_UTF_8.toString()); |
| 96 | |
| 97 | try { |
| 98 | Response response = builder.put(Entity.entity(request.toString(), |
| 99 | MediaType.APPLICATION_JSON_TYPE)); |
| 100 | String strResponse = response.readEntity(String.class); |
| 101 | if (response.getStatus() != HTTP_OK) { |
| 102 | throw new IllegalArgumentException("Failed to put resource " |
| 103 | + response.getStatus() + ": " + strResponse); |
| 104 | } |
| 105 | return strResponse; |
| 106 | } catch (javax.ws.rs.ProcessingException e) { |
| 107 | return EMPTY_JSON_STRING; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | public void requestSync() { |
| 112 | String response = restGet(VTN_SERVICES); |
| 113 | final String error = String.format(ERR_LOG, VTN_SERVICES, response); |
| 114 | try { |
| 115 | JsonNode jsonTree = mapper().readTree(response).path("items"); |
| 116 | if (!jsonTree.isArray() || jsonTree.size() < 1) { |
| 117 | throw new IllegalArgumentException(error); |
| 118 | } |
| 119 | JsonNode vtnService = jsonTree.get(0); |
| 120 | String vtnServiceId = vtnService.path(VTN_ID).asText(); |
| 121 | if (vtnServiceId == null || vtnServiceId.equals("")) { |
| 122 | throw new IllegalArgumentException(error); |
| 123 | } |
| 124 | log.info("Requesting sync for VTN service {}", vtnServiceId); |
| 125 | |
| 126 | ObjectNode request = mapper().createObjectNode() |
| 127 | .put(VTN_RESYNC, true); |
| 128 | |
| 129 | restPut(VTN_SERVICES + "/" + vtnServiceId, request); |
| 130 | } catch (IOException e) { |
| 131 | throw new IllegalArgumentException(error); |
| 132 | } |
| 133 | } |
| 134 | |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 135 | /** |
| 136 | * Returns endpoint url for the XOS service API access. |
| 137 | * |
| 138 | * @return endpoint url as a string |
| 139 | */ |
| 140 | public String endpoint() { |
| 141 | return endpoint; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns user name for the XOS service API access. |
| 146 | * |
| 147 | * @return user name as a string |
| 148 | */ |
| 149 | public String user() { |
| 150 | return user; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Returns password for the XOS service API access. |
| 155 | * |
| 156 | * @return password as a string |
| 157 | */ |
| 158 | public String password() { |
| 159 | return password; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Returns new XOS service networking builder instance. |
| 164 | * |
| 165 | * @return xos service networking builder |
| 166 | */ |
| 167 | public static Builder builder() { |
| 168 | return new Builder(); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Builder of the XOS service networking entities. |
| 173 | */ |
| 174 | public static final class Builder { |
| 175 | |
| 176 | private String endpoint; |
| 177 | private String user; |
| 178 | private String password; |
| 179 | |
| 180 | private Builder() { |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Builds immutable XOS service networking instance. |
| 185 | * |
| 186 | * @return xos service networking instance |
| 187 | */ |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 188 | public XosVtnNetworkingClient build() { |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 189 | checkArgument(!Strings.isNullOrEmpty(endpoint)); |
| 190 | checkArgument(!Strings.isNullOrEmpty(user)); |
| 191 | checkArgument(!Strings.isNullOrEmpty(password)); |
| 192 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 193 | return new XosVtnNetworkingClient(endpoint, user, password); |
Hyunsun Moon | 4a94c2e | 2016-10-21 17:37:05 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Returns XOS service networking builder with the supplied endpoint. |
| 198 | * |
| 199 | * @param endpoint endpoint url |
| 200 | * @return xos service networking builder |
| 201 | */ |
| 202 | public Builder endpoint(String endpoint) { |
| 203 | this.endpoint = endpoint; |
| 204 | return this; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Returns XOS service networking builder with the supplied user |
| 209 | * credential. |
| 210 | * |
| 211 | * @param user user |
| 212 | * @return xos service networking builder |
| 213 | */ |
| 214 | public Builder user(String user) { |
| 215 | this.user = user; |
| 216 | return this; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Returns XOS service networking builder with the supplied password |
| 221 | * credential. |
| 222 | * |
| 223 | * @param password password |
| 224 | * @return xos service networking builder |
| 225 | */ |
| 226 | public Builder password(String password) { |
| 227 | this.password = password; |
| 228 | return this; |
| 229 | } |
| 230 | } |
| 231 | } |