Update gvnfm-driver .gitreview file
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / main / java / org / openo / nfvo / jujuvnfmadapter / common / servicetoken / module / AccessTokens.java
1 /*
2  * Copyright 2016 Huawei Technologies Co., Ltd.
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 package org.openo.nfvo.jujuvnfmadapter.common.servicetoken.module;
18
19 import net.sf.json.JSONObject;
20
21 import org.apache.commons.lang3.StringUtils;
22 import org.openo.nfvo.jujuvnfmadapter.common.VNFJsonUtil;
23
24 /**
25  * 
26  * Access tokens class.<br>
27  * <p>
28  * </p>
29  * 
30  * @author
31  * @version     NFVO 0.5  Sep 12, 2016
32  */
33 public class AccessTokens {
34
35     private String accesTokens;
36
37     private int expire;
38
39     private long createTime;
40
41     /**
42      * 
43      * Constructor<br>
44      * <p>
45      * </p>
46      * 
47      * @since  NFVO 0.5
48      */
49     public AccessTokens() {
50         throw new UnsupportedOperationException();
51     }
52
53     /**
54      * 
55      * Constructor<br>
56      * <p>
57      * </p>
58      * 
59      * @param token
60      * @param ttl
61      * @since  NFVO 0.5
62      */
63     public AccessTokens(String token, int ttl) {
64         this.accesTokens = token;
65         this.expire = ttl;
66         this.createTime = System.currentTimeMillis();
67     }
68
69     /**
70      * 
71      * Constructor<br>
72      * <p>
73      * </p>
74      * 
75      * @param accessToken
76      * @param expire
77      * @param createTime
78      * @since  NFVO 0.5
79      */
80     public AccessTokens(String accessToken, Integer expire, Long createTime) {
81         this.accesTokens = accessToken;
82         this.expire = expire == null ? 0 : expire;
83         this.createTime = createTime == null ? 0 : createTime;
84     }
85
86     public String getAccessToken() {
87         return this.accesTokens;
88     }
89
90     public void setAccessToken(String token) {
91         this.accesTokens = token;
92     }
93
94     public int getExpire() {
95         return this.expire;
96     }
97
98     public long getCreateTime() {
99         return this.createTime;
100     }
101
102     /**
103      * 
104      * Check time validity.<br>
105      * 
106      * @return
107      * @since  NFVO 0.5
108      */
109     public boolean valid() {
110         if(this.expire == 0) {
111             return true;
112         }
113         return System.currentTimeMillis() - this.createTime <= 1000L * this.expire;
114     }
115
116     @Override
117     public String toString() {
118         return '{'+ StringUtils.trimToEmpty(this.getAccessToken()) + '\'' + ",'expire': '" + this.getExpire() + '\'' +
119                 ",'createTime': '" + this.getCreateTime() + '\'' + '}';
120     }
121
122     /**
123      * 
124      * To entity.<br>
125      * 
126      * @param jsonObject
127      * @return
128      * @since  NFVO 0.5
129      */
130     public static AccessTokens toEntity(JSONObject jsonObject) {
131         String token = VNFJsonUtil.getJsonFieldStr(jsonObject, "accessToken");
132         Integer expire = VNFJsonUtil.getJsonFieldInt(jsonObject, "expire");
133         Long createTime = VNFJsonUtil.getJsonFieldLong(jsonObject, "createTime");
134         return new AccessTokens(token, expire, createTime);
135     }
136 }