sangho
Committed by Gerrit Code Review

[ONOS-4272] Check if the token store is valid or not before reissue the new token for OpenStack.

Change-Id: I2c4b9a5c22aaee6dce180b6833c1e9b66ddd26f4
...@@ -54,8 +54,12 @@ import javax.ws.rs.client.Invocation; ...@@ -54,8 +54,12 @@ import javax.ws.rs.client.Invocation;
54 import javax.ws.rs.client.WebTarget; 54 import javax.ws.rs.client.WebTarget;
55 import javax.ws.rs.core.MediaType; 55 import javax.ws.rs.core.MediaType;
56 import java.io.IOException; 56 import java.io.IOException;
57 +import java.text.ParseException;
58 +import java.text.SimpleDateFormat;
59 +import java.util.Calendar;
57 import java.util.Collection; 60 import java.util.Collection;
58 import java.util.Collections; 61 import java.util.Collections;
62 +import java.util.Date;
59 import java.util.List; 63 import java.util.List;
60 import java.util.Set; 64 import java.util.Set;
61 import java.util.concurrent.ExecutorService; 65 import java.util.concurrent.ExecutorService;
...@@ -89,13 +93,16 @@ public class OpenstackInterfaceManager implements OpenstackInterfaceService { ...@@ -89,13 +93,16 @@ public class OpenstackInterfaceManager implements OpenstackInterfaceService {
89 private static final String PATH_ACCESS = "access"; 93 private static final String PATH_ACCESS = "access";
90 private static final String PATH_TOKEN = "token"; 94 private static final String PATH_TOKEN = "token";
91 private static final String PATH_ID = "id"; 95 private static final String PATH_ID = "id";
96 + private static final String PATH_EXPIRES = "expires";
92 97
93 private static final String HEADER_AUTH_TOKEN = "X-Auth-Token"; 98 private static final String HEADER_AUTH_TOKEN = "X-Auth-Token";
99 + private static final String TOKEN_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
94 100
95 private final Logger log = getLogger(getClass()); 101 private final Logger log = getLogger(getClass());
96 private String neutronUrl; 102 private String neutronUrl;
97 private String keystoneUrl; 103 private String keystoneUrl;
98 private String tokenId; 104 private String tokenId;
105 + private String tokenExpires;
99 private String userName; 106 private String userName;
100 private String pass; 107 private String pass;
101 108
...@@ -283,7 +290,7 @@ public class OpenstackInterfaceManager implements OpenstackInterfaceService { ...@@ -283,7 +290,7 @@ public class OpenstackInterfaceManager implements OpenstackInterfaceService {
283 } 290 }
284 291
285 private String getToken() { 292 private String getToken() {
286 - if (isTokenInvalid()) { 293 + if (!isTokenValid()) {
287 String request = "{\"auth\": {\"tenantName\": \"admin\", " + 294 String request = "{\"auth\": {\"tenantName\": \"admin\", " +
288 "\"passwordCredentials\": {\"username\": \"" + 295 "\"passwordCredentials\": {\"username\": \"" +
289 userName + "\",\"password\": \"" + pass + "\"}}}"; 296 userName + "\",\"password\": \"" + pass + "\"}}}";
...@@ -294,6 +301,7 @@ public class OpenstackInterfaceManager implements OpenstackInterfaceService { ...@@ -294,6 +301,7 @@ public class OpenstackInterfaceManager implements OpenstackInterfaceService {
294 try { 301 try {
295 ObjectNode node = (ObjectNode) mapper.readTree(response); 302 ObjectNode node = (ObjectNode) mapper.readTree(response);
296 tokenId = node.path(PATH_ACCESS).path(PATH_TOKEN).path(PATH_ID).asText(); 303 tokenId = node.path(PATH_ACCESS).path(PATH_TOKEN).path(PATH_ID).asText();
304 + tokenExpires = node.path(PATH_ACCESS).path(PATH_TOKEN).path(PATH_EXPIRES).asText();
297 } catch (IOException e) { 305 } catch (IOException e) {
298 log.warn("getToken()", e); 306 log.warn("getToken()", e);
299 } 307 }
...@@ -303,10 +311,28 @@ public class OpenstackInterfaceManager implements OpenstackInterfaceService { ...@@ -303,10 +311,28 @@ public class OpenstackInterfaceManager implements OpenstackInterfaceService {
303 return tokenId; 311 return tokenId;
304 } 312 }
305 313
306 - private boolean isTokenInvalid() { 314 + private boolean isTokenValid() {
307 - //TODO: validation check for the existing token 315 +
316 + if (tokenExpires == null || tokenId == null || tokenExpires.isEmpty()) {
317 + return false;
318 + }
319 +
320 + try {
321 + SimpleDateFormat dateFormat = new SimpleDateFormat(TOKEN_DATE_FORMAT);
322 + Date exireDate = dateFormat.parse(tokenExpires);
323 +
324 + Calendar today = Calendar.getInstance();
325 + if (exireDate.after(today.getTime())) {
308 return true; 326 return true;
309 } 327 }
328 + } catch (ParseException e) {
329 + log.error("Token parse exception error : {}", e.getMessage());
330 + return false;
331 + }
332 +
333 + log.debug("token is Invalid");
334 + return false;
335 + }
310 336
311 @Override 337 @Override
312 public Collection<OpenstackPort> ports(String networkId) { 338 public Collection<OpenstackPort> ports(String networkId) {
......