Skip to content

Commit ea3273c

Browse files
authored
Aospace platform gtroute update (#18)
* update method comments & update reamde docs * update GTRoute from NSRoute
1 parent 7607d92 commit ea3273c

File tree

8 files changed

+440
-30
lines changed

8 files changed

+440
-30
lines changed
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/*
2+
* Copyright (c) 2022 Institute of Software Chinese Academy of Sciences (ISCAS)
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 xyz.eulix.platform.services.cache;
18+
19+
import io.quarkus.redis.client.RedisClient;
20+
import io.vertx.redis.client.Response;
21+
import org.jboss.logging.Logger;
22+
import xyz.eulix.platform.common.support.CommonUtils;
23+
import xyz.eulix.platform.common.support.log.Logged;
24+
import xyz.eulix.platform.common.support.serialization.OperationUtils;
25+
import xyz.eulix.platform.services.config.ApplicationProperties;
26+
27+
import javax.enterprise.context.ApplicationScoped;
28+
import javax.inject.Inject;
29+
import java.util.ArrayList;
30+
import java.util.Arrays;
31+
import java.util.List;
32+
import java.util.stream.Collectors;
33+
34+
/**
35+
* Network Server Route 缓存客户端(Redis)
36+
*
37+
* For more information:
38+
* <a href="https://quarkus.io/guides/redis">https://quarkus.io/guides/redis</a>.
39+
*
40+
*/
41+
@ApplicationScoped
42+
public class GTRClient {
43+
private static final Logger LOG = Logger.getLogger("app.log");
44+
45+
public static final String SEPARATOR = ":";
46+
47+
public static final String GTR_PREV = "GTR:";
48+
49+
public static final String GTR_PREV_CLIENTS = "SpaceUUIDs:";
50+
51+
public static final String GTR_PREV_APP_TOKENS = "AppDomains:";
52+
53+
@Inject
54+
RedisClient redisClient;
55+
56+
@Inject
57+
OperationUtils operationUtils;
58+
59+
@Inject
60+
ApplicationProperties properties;
61+
62+
public GTRouteBasic getGTRouteBasic(String subdomain) {
63+
String key = GTR_PREV + subdomain;
64+
Response response = redisClient.get(key);
65+
GTRouteBasic gtRouteBasic = new GTRouteBasic(subdomain, response != null ? getNetworkBasic(response.toString()) : null);
66+
LOG.debugv("get GTRouteBasic success, key:{0}, value:{1}", key, gtRouteBasic.getNetworkBasic());
67+
return gtRouteBasic;
68+
}
69+
70+
public NetworkBasic getNetworkBasic(String networkBasicStr) {
71+
return operationUtils.jsonToObject(networkBasicStr, NetworkBasic.class);
72+
}
73+
74+
public void setGTRouteBasic(GTRouteBasic gtRouteBasic) {
75+
String key = GTR_PREV + gtRouteBasic.getSubdomain();
76+
String value = operationUtils.objectToJson(gtRouteBasic.getNetworkBasic());
77+
redisClient.set(Arrays.asList(key, value));
78+
LOG.debugv("set GTRouteBasic success, key:{0}, value:{1}", key, value);
79+
}
80+
81+
public void setGTRouteClients(GTRouteClients gtRouteClients) {
82+
String key = GTR_PREV_CLIENTS + gtRouteClients.getBoxUUID() + SEPARATOR + gtRouteClients.getUserId();
83+
if (CommonUtils.isNullOrEmpty(gtRouteClients.getClientUUIDs())) {
84+
LOG.warnv("set GTRouteClients fail due to clientUUIDs is empty, key:{0}", key);
85+
return;
86+
}
87+
List<String> lists = new ArrayList<>();
88+
lists.add(key);
89+
lists.addAll(gtRouteClients.getClientUUIDs());
90+
redisClient.sadd(lists);
91+
LOG.debugv("set GTRouteClients success, key:{0}, value:{1}", key, gtRouteClients.getClientUUIDs());
92+
}
93+
94+
@Logged
95+
public void setRedirect(String subdomain, String serverAddr, String clientId, String newUserDomain, NSRRedirectStateEnum redirectState,
96+
String boxUUID, String userId) {
97+
NetworkBasic networkBasic = new NetworkBasic(serverAddr, clientId, newUserDomain, redirectState.getState(), boxUUID, userId);
98+
GTRouteBasic gtRouteBasic = new GTRouteBasic(subdomain, networkBasic);
99+
setGTRouteBasic(gtRouteBasic);
100+
}
101+
102+
public void addClientUUID(String boxUUID, String userId, String clientUUID) {
103+
String key = GTR_PREV_CLIENTS + boxUUID + SEPARATOR + userId;
104+
List<String> lists = new ArrayList<>();
105+
lists.add(key);
106+
lists.add(clientUUID);
107+
redisClient.sadd(lists);
108+
redisClient.persist(key);
109+
LOG.debugv("add GTRouteClient success, key:{0}, value:{1}", key, clientUUID);
110+
}
111+
112+
public void removeClientUUID(String boxUUID, String userId, String clientUUID){
113+
String key = GTR_PREV_CLIENTS + boxUUID + SEPARATOR + userId;
114+
List<String> lists = new ArrayList<>();
115+
lists.add(key);
116+
lists.add(clientUUID);
117+
redisClient.srem(lists);
118+
redisClient.persist(key);
119+
LOG.debugv("remove GTRouteClient success, key:{0}, value:{1}", key, clientUUID);
120+
}
121+
122+
public void addAppToken(String boxUUID, String appToken) {
123+
String key = GTR_PREV_APP_TOKENS + boxUUID;
124+
List<String> lists = new ArrayList<>();
125+
lists.add(key);
126+
lists.add(appToken);
127+
redisClient.sadd(lists);
128+
redisClient.persist(key);
129+
LOG.debugv("add GTRouteAppToken success, key:{0}, value:{1}", key, appToken);
130+
}
131+
132+
public void removeAppToken(String boxUUID, String appToken){
133+
String key = GTR_PREV_APP_TOKENS + boxUUID;
134+
List<String> lists = new ArrayList<>();
135+
lists.add(key);
136+
lists.add(appToken);
137+
redisClient.srem(lists);
138+
redisClient.persist(key);
139+
LOG.debugv("remove GTRouteAppToken success, key:{0}, value:{1}", key, appToken);
140+
}
141+
142+
public void expireGTRouteBasic(String subdomain, String expireSeconds) {
143+
String key = GTR_PREV + subdomain;
144+
redisClient.expire(key, expireSeconds);
145+
LOG.debugv("expire GTRouteBasic success, key:{0}, expire time:{1}s", key, expireSeconds);
146+
}
147+
148+
public void expireGTRouteClients(String boxUUID, String userId, String expireSeconds) {
149+
String key = GTR_PREV_CLIENTS + boxUUID + SEPARATOR + userId;
150+
redisClient.expire(key, expireSeconds);
151+
LOG.debugv("expire GTRouteClients success, key:{0}, expire time:{1}s", key, expireSeconds);
152+
}
153+
154+
public void expireGTRouteAppTokens(String boxUUID, String expireSeconds) {
155+
String key = GTR_PREV_APP_TOKENS + boxUUID;
156+
redisClient.expire(key, expireSeconds);
157+
LOG.debugv("expire GTRouteAppTokens success, key:{0}, expire time:{1}s", key, expireSeconds);
158+
}
159+
160+
public void clearGTRouteBasic(List<String> subdomains) {
161+
if (subdomains.isEmpty()) {
162+
LOG.debugv("subdomains is empty");
163+
return;
164+
}
165+
List<String> keys = subdomains.stream().map(subdomain -> GTR_PREV + subdomain).collect(Collectors.toList());
166+
Integer count = redisClient.del(keys).toInteger();
167+
LOG.debugv("clear NSRouteBasic success, keys:{0}, count:{1}", keys, count);
168+
}
169+
170+
public void clearGTRouteClients(String boxUUID, String userId) {
171+
String key = GTR_PREV_CLIENTS + boxUUID + SEPARATOR + userId;
172+
Integer count = redisClient.del(Arrays.asList(key)).toInteger();
173+
LOG.debugv("clear NSRouteClients success, key:{0}, count:{1}", key, count);
174+
}
175+
176+
public void clearNSRouteAppTokens(List<String> boxUUIDs) {
177+
if (boxUUIDs.isEmpty()) {
178+
LOG.debugv("boxUUIDs is empty");
179+
return;
180+
}
181+
List<String> keys = boxUUIDs.stream().map(boxUUID -> GTR_PREV_APP_TOKENS + boxUUID).collect(Collectors.toList());
182+
Integer count = redisClient.del(keys).toInteger();
183+
LOG.debugv("clear NSRouteAppTokens success, keys:{0}, count:{1}", keys, count);
184+
}
185+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2022 Institute of Software Chinese Academy of Sciences (ISCAS)
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 xyz.eulix.platform.services.cache;
18+
19+
import lombok.Getter;
20+
import lombok.Setter;
21+
import org.eclipse.microprofile.openapi.annotations.media.Schema;
22+
23+
import java.util.Set;
24+
25+
/**
26+
* 以 `Redis sets` 的形式在 redis 存储
27+
* key - 表名:AppDomains, 字段: boxUUID
28+
* value - `sets` 中存储的元素为空间应用的域名,`app-token`
29+
*/
30+
@Setter
31+
@Getter
32+
public class GTRouteAppTokens {
33+
@Schema(description = "boxUUID")
34+
private String boxUUID;
35+
36+
@Schema(description = "appTokens")
37+
private Set<String> appTokens;
38+
39+
public GTRouteAppTokens(Set<String> appTokens) {
40+
this.appTokens = appTokens;
41+
}
42+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2022 Institute of Software Chinese Academy of Sciences (ISCAS)
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 xyz.eulix.platform.services.cache;
18+
19+
import lombok.Getter;
20+
import lombok.Setter;
21+
import org.eclipse.microprofile.openapi.annotations.media.Schema;
22+
23+
/**
24+
* key - 表名: GTR, 字段:空间的用户 subdomain
25+
* value - json 格式,包括主要字段如下:
26+
* - gtSvrNode - 目标域名, 即 network server (GT)node,如 `eulixnetwork-server.bp-cicada-rc.svc.cluster.local:80`
27+
* - gtCliId - network-client 配对的id, 如 `fae1e91d8ccf464daa1b3d0d9e3509fa`
28+
* - redirectDomain - 重定向的用户域名, 如 `b5r9qd8h.myself.com`
29+
* - redirectDomainStatus - 重定向域名状态,1表示正常,2表示已过期
30+
* - boxUUID - 盒子设备的 boxUUID
31+
* - userId - 盒子上用户对外的 id
32+
*/
33+
@Setter
34+
@Getter
35+
public class GTRouteBasic {
36+
@Schema(description = "用户subdomain")
37+
private String subdomain;
38+
39+
@Schema(description = "network server 地址 & network client id等基础数据")
40+
private NetworkBasic networkBasic;
41+
42+
public GTRouteBasic(String subdomain, NetworkBasic networkBasic) {
43+
this.subdomain = subdomain;
44+
this.networkBasic = networkBasic;
45+
}
46+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2022 Institute of Software Chinese Academy of Sciences (ISCAS)
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 xyz.eulix.platform.services.cache;
18+
19+
import lombok.Getter;
20+
import lombok.Setter;
21+
import org.eclipse.microprofile.openapi.annotations.media.Schema;
22+
23+
import java.util.Set;
24+
25+
/**
26+
* 以 `Redis sets` 的形式在 redis 存储
27+
* key - 表名:SpaceUUIDs, 字段: boxUUID+userId
28+
* value - `sets` 中存储的元素为 client-uuid
29+
*/
30+
@Setter
31+
@Getter
32+
public class GTRouteClients {
33+
@Schema(description = "boxUUID")
34+
private String boxUUID;
35+
36+
@Schema(description = "userId")
37+
private String userId;
38+
39+
@Schema(description = "clientUUIDs")
40+
private Set<String> clientUUIDs;
41+
42+
public GTRouteClients(String boxUUID, String userId, Set<String> clientUUIDs) {
43+
this.boxUUID = boxUUID;
44+
this.userId = userId;
45+
this.clientUUIDs = clientUUIDs;
46+
}
47+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2022 Institute of Software Chinese Academy of Sciences (ISCAS)
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 xyz.eulix.platform.services.cache;
18+
19+
import com.fasterxml.jackson.annotation.JsonCreator;
20+
import com.fasterxml.jackson.annotation.JsonProperty;
21+
import lombok.Data;
22+
23+
/**
24+
* key - 表名: GTR, 字段:空间的用户 subdomain
25+
* value - json 格式,包括主要字段如下:
26+
* - gtSvrNode - 目标域名, 即 network server (GT)node,如 `eulixnetwork-server.bp-cicada-rc.svc.cluster.local:80`
27+
* - gtCliId - network-client 配对的id, 如 `fae1e91d8ccf464daa1b3d0d9e3509fa`
28+
* - redirectDomain - 重定向的用户域名, 如 `b5r9qd8h.myself.com`
29+
* - redirectDomainStatus - 重定向域名状态,1表示正常,2表示已过期
30+
* - boxUUID - 盒子设备的 boxUUID
31+
* - userId - 盒子上用户对外的 id
32+
*/
33+
@Data
34+
public class NetworkBasic {
35+
String gtSvrNode;
36+
String gtCliId;
37+
String redirectDomain;
38+
Integer redirectDomainStatus;
39+
String boxUUID;
40+
String userId;
41+
42+
public NetworkBasic(String gtSvrNode, String gtCliId, String boxUUID, String userId) {
43+
this.gtSvrNode = gtSvrNode;
44+
this.gtCliId = gtCliId;
45+
this.boxUUID = boxUUID;
46+
this.userId = userId;
47+
}
48+
49+
@JsonCreator
50+
public NetworkBasic(@JsonProperty("gtSvrNode") String gtSvrNode,
51+
@JsonProperty("gtCliId") String gtCliId,
52+
@JsonProperty("redirectDomain") String redirectDomain,
53+
@JsonProperty("redirectDomainStatus") Integer redirectDomainStatus,
54+
@JsonProperty("boxUUID") String boxUUID,
55+
@JsonProperty("userId") String userId) {
56+
this.gtSvrNode = gtSvrNode;
57+
this.gtCliId = gtCliId;
58+
this.redirectDomain = redirectDomain;
59+
this.redirectDomainStatus = redirectDomainStatus;
60+
this.boxUUID = boxUUID;
61+
this.userId = userId;
62+
}
63+
}

0 commit comments

Comments
 (0)