Skip to content

Commit

Permalink
Aospace platform gtroute update (#18)
Browse files Browse the repository at this point in the history
* update method comments & update reamde docs

* update GTRoute from NSRoute
  • Loading branch information
gatsby068 authored Sep 1, 2023
1 parent 7607d92 commit ea3273c
Show file tree
Hide file tree
Showing 8 changed files with 440 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/*
* Copyright (c) 2022 Institute of Software Chinese Academy of Sciences (ISCAS)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package xyz.eulix.platform.services.cache;

import io.quarkus.redis.client.RedisClient;
import io.vertx.redis.client.Response;
import org.jboss.logging.Logger;
import xyz.eulix.platform.common.support.CommonUtils;
import xyz.eulix.platform.common.support.log.Logged;
import xyz.eulix.platform.common.support.serialization.OperationUtils;
import xyz.eulix.platform.services.config.ApplicationProperties;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* Network Server Route 缓存客户端(Redis)
*
* For more information:
* <a href="https://quarkus.io/guides/redis">https://quarkus.io/guides/redis</a>.
*
*/
@ApplicationScoped
public class GTRClient {
private static final Logger LOG = Logger.getLogger("app.log");

public static final String SEPARATOR = ":";

public static final String GTR_PREV = "GTR:";

public static final String GTR_PREV_CLIENTS = "SpaceUUIDs:";

public static final String GTR_PREV_APP_TOKENS = "AppDomains:";

@Inject
RedisClient redisClient;

@Inject
OperationUtils operationUtils;

@Inject
ApplicationProperties properties;

public GTRouteBasic getGTRouteBasic(String subdomain) {
String key = GTR_PREV + subdomain;
Response response = redisClient.get(key);
GTRouteBasic gtRouteBasic = new GTRouteBasic(subdomain, response != null ? getNetworkBasic(response.toString()) : null);
LOG.debugv("get GTRouteBasic success, key:{0}, value:{1}", key, gtRouteBasic.getNetworkBasic());
return gtRouteBasic;
}

public NetworkBasic getNetworkBasic(String networkBasicStr) {
return operationUtils.jsonToObject(networkBasicStr, NetworkBasic.class);
}

public void setGTRouteBasic(GTRouteBasic gtRouteBasic) {
String key = GTR_PREV + gtRouteBasic.getSubdomain();
String value = operationUtils.objectToJson(gtRouteBasic.getNetworkBasic());
redisClient.set(Arrays.asList(key, value));
LOG.debugv("set GTRouteBasic success, key:{0}, value:{1}", key, value);
}

public void setGTRouteClients(GTRouteClients gtRouteClients) {
String key = GTR_PREV_CLIENTS + gtRouteClients.getBoxUUID() + SEPARATOR + gtRouteClients.getUserId();
if (CommonUtils.isNullOrEmpty(gtRouteClients.getClientUUIDs())) {
LOG.warnv("set GTRouteClients fail due to clientUUIDs is empty, key:{0}", key);
return;
}
List<String> lists = new ArrayList<>();
lists.add(key);
lists.addAll(gtRouteClients.getClientUUIDs());
redisClient.sadd(lists);
LOG.debugv("set GTRouteClients success, key:{0}, value:{1}", key, gtRouteClients.getClientUUIDs());
}

@Logged
public void setRedirect(String subdomain, String serverAddr, String clientId, String newUserDomain, NSRRedirectStateEnum redirectState,
String boxUUID, String userId) {
NetworkBasic networkBasic = new NetworkBasic(serverAddr, clientId, newUserDomain, redirectState.getState(), boxUUID, userId);
GTRouteBasic gtRouteBasic = new GTRouteBasic(subdomain, networkBasic);
setGTRouteBasic(gtRouteBasic);
}

public void addClientUUID(String boxUUID, String userId, String clientUUID) {
String key = GTR_PREV_CLIENTS + boxUUID + SEPARATOR + userId;
List<String> lists = new ArrayList<>();
lists.add(key);
lists.add(clientUUID);
redisClient.sadd(lists);
redisClient.persist(key);
LOG.debugv("add GTRouteClient success, key:{0}, value:{1}", key, clientUUID);
}

public void removeClientUUID(String boxUUID, String userId, String clientUUID){
String key = GTR_PREV_CLIENTS + boxUUID + SEPARATOR + userId;
List<String> lists = new ArrayList<>();
lists.add(key);
lists.add(clientUUID);
redisClient.srem(lists);
redisClient.persist(key);
LOG.debugv("remove GTRouteClient success, key:{0}, value:{1}", key, clientUUID);
}

public void addAppToken(String boxUUID, String appToken) {
String key = GTR_PREV_APP_TOKENS + boxUUID;
List<String> lists = new ArrayList<>();
lists.add(key);
lists.add(appToken);
redisClient.sadd(lists);
redisClient.persist(key);
LOG.debugv("add GTRouteAppToken success, key:{0}, value:{1}", key, appToken);
}

public void removeAppToken(String boxUUID, String appToken){
String key = GTR_PREV_APP_TOKENS + boxUUID;
List<String> lists = new ArrayList<>();
lists.add(key);
lists.add(appToken);
redisClient.srem(lists);
redisClient.persist(key);
LOG.debugv("remove GTRouteAppToken success, key:{0}, value:{1}", key, appToken);
}

public void expireGTRouteBasic(String subdomain, String expireSeconds) {
String key = GTR_PREV + subdomain;
redisClient.expire(key, expireSeconds);
LOG.debugv("expire GTRouteBasic success, key:{0}, expire time:{1}s", key, expireSeconds);
}

public void expireGTRouteClients(String boxUUID, String userId, String expireSeconds) {
String key = GTR_PREV_CLIENTS + boxUUID + SEPARATOR + userId;
redisClient.expire(key, expireSeconds);
LOG.debugv("expire GTRouteClients success, key:{0}, expire time:{1}s", key, expireSeconds);
}

public void expireGTRouteAppTokens(String boxUUID, String expireSeconds) {
String key = GTR_PREV_APP_TOKENS + boxUUID;
redisClient.expire(key, expireSeconds);
LOG.debugv("expire GTRouteAppTokens success, key:{0}, expire time:{1}s", key, expireSeconds);
}

public void clearGTRouteBasic(List<String> subdomains) {
if (subdomains.isEmpty()) {
LOG.debugv("subdomains is empty");
return;
}
List<String> keys = subdomains.stream().map(subdomain -> GTR_PREV + subdomain).collect(Collectors.toList());
Integer count = redisClient.del(keys).toInteger();
LOG.debugv("clear NSRouteBasic success, keys:{0}, count:{1}", keys, count);
}

public void clearGTRouteClients(String boxUUID, String userId) {
String key = GTR_PREV_CLIENTS + boxUUID + SEPARATOR + userId;
Integer count = redisClient.del(Arrays.asList(key)).toInteger();
LOG.debugv("clear NSRouteClients success, key:{0}, count:{1}", key, count);
}

public void clearNSRouteAppTokens(List<String> boxUUIDs) {
if (boxUUIDs.isEmpty()) {
LOG.debugv("boxUUIDs is empty");
return;
}
List<String> keys = boxUUIDs.stream().map(boxUUID -> GTR_PREV_APP_TOKENS + boxUUID).collect(Collectors.toList());
Integer count = redisClient.del(keys).toInteger();
LOG.debugv("clear NSRouteAppTokens success, keys:{0}, count:{1}", keys, count);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2022 Institute of Software Chinese Academy of Sciences (ISCAS)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package xyz.eulix.platform.services.cache;

import lombok.Getter;
import lombok.Setter;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

import java.util.Set;

/**
* 以 `Redis sets` 的形式在 redis 存储
* key - 表名:AppDomains, 字段: boxUUID
* value - `sets` 中存储的元素为空间应用的域名,`app-token`
*/
@Setter
@Getter
public class GTRouteAppTokens {
@Schema(description = "boxUUID")
private String boxUUID;

@Schema(description = "appTokens")
private Set<String> appTokens;

public GTRouteAppTokens(Set<String> appTokens) {
this.appTokens = appTokens;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2022 Institute of Software Chinese Academy of Sciences (ISCAS)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package xyz.eulix.platform.services.cache;

import lombok.Getter;
import lombok.Setter;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

/**
* key - 表名: GTR, 字段:空间的用户 subdomain
* value - json 格式,包括主要字段如下:
* - gtSvrNode - 目标域名, 即 network server (GT)node,如 `eulixnetwork-server.bp-cicada-rc.svc.cluster.local:80`
* - gtCliId - network-client 配对的id, 如 `fae1e91d8ccf464daa1b3d0d9e3509fa`
* - redirectDomain - 重定向的用户域名, 如 `b5r9qd8h.myself.com`
* - redirectDomainStatus - 重定向域名状态,1表示正常,2表示已过期
* - boxUUID - 盒子设备的 boxUUID
* - userId - 盒子上用户对外的 id
*/
@Setter
@Getter
public class GTRouteBasic {
@Schema(description = "用户subdomain")
private String subdomain;

@Schema(description = "network server 地址 & network client id等基础数据")
private NetworkBasic networkBasic;

public GTRouteBasic(String subdomain, NetworkBasic networkBasic) {
this.subdomain = subdomain;
this.networkBasic = networkBasic;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2022 Institute of Software Chinese Academy of Sciences (ISCAS)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package xyz.eulix.platform.services.cache;

import lombok.Getter;
import lombok.Setter;
import org.eclipse.microprofile.openapi.annotations.media.Schema;

import java.util.Set;

/**
* 以 `Redis sets` 的形式在 redis 存储
* key - 表名:SpaceUUIDs, 字段: boxUUID+userId
* value - `sets` 中存储的元素为 client-uuid
*/
@Setter
@Getter
public class GTRouteClients {
@Schema(description = "boxUUID")
private String boxUUID;

@Schema(description = "userId")
private String userId;

@Schema(description = "clientUUIDs")
private Set<String> clientUUIDs;

public GTRouteClients(String boxUUID, String userId, Set<String> clientUUIDs) {
this.boxUUID = boxUUID;
this.userId = userId;
this.clientUUIDs = clientUUIDs;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2022 Institute of Software Chinese Academy of Sciences (ISCAS)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package xyz.eulix.platform.services.cache;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

/**
* key - 表名: GTR, 字段:空间的用户 subdomain
* value - json 格式,包括主要字段如下:
* - gtSvrNode - 目标域名, 即 network server (GT)node,如 `eulixnetwork-server.bp-cicada-rc.svc.cluster.local:80`
* - gtCliId - network-client 配对的id, 如 `fae1e91d8ccf464daa1b3d0d9e3509fa`
* - redirectDomain - 重定向的用户域名, 如 `b5r9qd8h.myself.com`
* - redirectDomainStatus - 重定向域名状态,1表示正常,2表示已过期
* - boxUUID - 盒子设备的 boxUUID
* - userId - 盒子上用户对外的 id
*/
@Data
public class NetworkBasic {
String gtSvrNode;
String gtCliId;
String redirectDomain;
Integer redirectDomainStatus;
String boxUUID;
String userId;

public NetworkBasic(String gtSvrNode, String gtCliId, String boxUUID, String userId) {
this.gtSvrNode = gtSvrNode;
this.gtCliId = gtCliId;
this.boxUUID = boxUUID;
this.userId = userId;
}

@JsonCreator
public NetworkBasic(@JsonProperty("gtSvrNode") String gtSvrNode,
@JsonProperty("gtCliId") String gtCliId,
@JsonProperty("redirectDomain") String redirectDomain,
@JsonProperty("redirectDomainStatus") Integer redirectDomainStatus,
@JsonProperty("boxUUID") String boxUUID,
@JsonProperty("userId") String userId) {
this.gtSvrNode = gtSvrNode;
this.gtCliId = gtCliId;
this.redirectDomain = redirectDomain;
this.redirectDomainStatus = redirectDomainStatus;
this.boxUUID = boxUUID;
this.userId = userId;
}
}
Loading

0 comments on commit ea3273c

Please sign in to comment.