From cf788377d9e017eaabaf01675f0a0b397bc04e6a Mon Sep 17 00:00:00 2001 From: eye-gu <734164350@qq.com> Date: Tue, 24 Dec 2024 11:22:41 +0800 Subject: [PATCH] [type:fix] configs import and export refactor --- .../ConfigsExportImportController.java | 8 +- .../shenyu/admin/mapper/MetaDataMapper.java | 3 +- .../admin/service/DiscoveryService.java | 4 +- .../service/DiscoveryUpstreamService.java | 5 +- .../admin/service/NamespacePluginService.java | 4 +- .../admin/service/PluginHandleService.java | 6 +- .../shenyu/admin/service/PluginService.java | 13 +- .../admin/service/ProxySelectorService.java | 4 +- .../shenyu/admin/service/RuleService.java | 4 +- .../shenyu/admin/service/SelectorService.java | 4 +- .../AuthConfigsExportImportHandler.java | 68 +++++ .../configs/ConfigsExportImportEnum.java | 101 ++++++++ .../ConfigsExportImportHandler.java} | 30 +-- .../service/configs/ConfigsImportContext.java | 70 ++++++ .../DictDataConfigsExportImportHandler.java | 69 ++++++ ...scoveryDataConfigsExportImportHandler.java | 67 +++++ ...pstreamDataConfigsExportImportHandler.java | 66 +++++ .../MetadataConfigsExportImportHandler.java | 67 +++++ ...ePluginDataConfigsExportImportHandler.java | 67 +++++ ...nHandleDataConfigsExportImportHandler.java | 66 +++++ ...emplateDataConfigsExportImportHandler.java | 66 +++++ ...electorDataConfigsExportImportHandler.java | 66 +++++ .../RuleDataConfigsExportImportHandler.java | 66 +++++ ...electorDataConfigsExportImportHandler.java | 67 +++++ .../service/impl/AppAuthServiceImpl.java | 14 +- .../service/impl/ConfigsServiceImpl.java | 233 +++--------------- .../service/impl/DiscoveryServiceImpl.java | 16 +- .../impl/DiscoveryUpstreamServiceImpl.java | 6 +- .../service/impl/MetaDataServiceImpl.java | 10 +- .../impl/NamespacePluginServiceImpl.java | 10 +- .../service/impl/PluginHandleServiceImpl.java | 18 +- .../admin/service/impl/PluginServiceImpl.java | 64 +---- .../impl/ProxySelectorServiceImpl.java | 21 +- .../admin/service/impl/RuleServiceImpl.java | 26 +- .../service/impl/SelectorServiceImpl.java | 29 ++- .../service/impl/SyncDataServiceImpl.java | 19 +- .../resources/mappers/meta-data-sqlmap.xml | 1 + .../admin/service/AppAuthServiceTest.java | 4 +- .../admin/service/ConfigsServiceTest.java | 7 +- .../admin/service/MetaDataServiceTest.java | 6 +- .../admin/service/PluginServiceTest.java | 2 +- .../admin/service/SyncDataServiceTest.java | 2 + .../constant/ExportImportConstants.java | 22 +- 43 files changed, 1149 insertions(+), 352 deletions(-) create mode 100644 shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/AuthConfigsExportImportHandler.java create mode 100644 shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/ConfigsExportImportEnum.java rename shenyu-admin/src/main/java/org/apache/shenyu/admin/service/{provider/MetaDataPathProvider.java => configs/ConfigsExportImportHandler.java} (55%) create mode 100644 shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/ConfigsImportContext.java create mode 100644 shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/DictDataConfigsExportImportHandler.java create mode 100644 shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/DiscoveryDataConfigsExportImportHandler.java create mode 100644 shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/DiscoveryUpstreamDataConfigsExportImportHandler.java create mode 100644 shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/MetadataConfigsExportImportHandler.java create mode 100644 shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/NamespacePluginDataConfigsExportImportHandler.java create mode 100644 shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/PluginHandleDataConfigsExportImportHandler.java create mode 100644 shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/PluginTemplateDataConfigsExportImportHandler.java create mode 100644 shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/ProxySelectorDataConfigsExportImportHandler.java create mode 100644 shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/RuleDataConfigsExportImportHandler.java create mode 100644 shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/SelectorDataConfigsExportImportHandler.java diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ConfigsExportImportController.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ConfigsExportImportController.java index 0967be80be6b..752ba5a5bc36 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ConfigsExportImportController.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/ConfigsExportImportController.java @@ -99,7 +99,7 @@ public ResponseEntity exportConfigsByNamespace(final String namespace, f throw new ShenyuException(result.getMessage()); } HttpHeaders headers = new HttpHeaders(); - String fileName = generateFileName(); + String fileName = generateFileName(namespace); response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); headers.add("Content-Disposition", "attachment;filename=" + fileName); return new ResponseEntity<>((byte[]) result.getData(), headers, HttpStatus.OK); @@ -115,6 +115,12 @@ private String generateFileName() { + ExportImportConstants.EXPORT_CONFIG_FILE_NAME_EXT; } + private String generateFileName(final String namespace) { + return ExportImportConstants.EXPORT_CONFIG_FILE_NAME + namespace + "_" + DateFormatUtils.format(new Date(), ExportImportConstants.EXPORT_CONFIG_FILE_NAME_DATE_FORMAT) + + ExportImportConstants.EXPORT_CONFIG_FILE_NAME_EXT; + } + + /** * Import configs. * diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/MetaDataMapper.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/MetaDataMapper.java index e4f33fa26640..f5b63d4fbfec 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/MetaDataMapper.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/mapper/MetaDataMapper.java @@ -177,9 +177,10 @@ public interface MetaDataMapper extends ExistProvider { * the path is existed. * * @param path path + * @param namespaceId namespaceId * @return existed */ - Boolean pathExisted(@Param("path") Serializable path); + Boolean pathExisted(@Param("path") Serializable path, @Param("namespaceId") String namespaceId); /** * the path is existed. diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/DiscoveryService.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/DiscoveryService.java index 1ffec289e115..b57571e1ed40 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/DiscoveryService.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/DiscoveryService.java @@ -21,6 +21,7 @@ import org.apache.shenyu.admin.model.dto.DiscoveryHandlerDTO; import org.apache.shenyu.admin.model.result.ConfigImportResult; import org.apache.shenyu.admin.model.vo.DiscoveryVO; +import org.apache.shenyu.admin.service.configs.ConfigsImportContext; import org.apache.shenyu.register.common.dto.DiscoveryConfigRegisterDTO; import java.util.List; @@ -131,7 +132,8 @@ public interface DiscoveryService { * * @param namespace the namespace * @param discoveryList the discovery data + * @param context import context * @return config import result */ - ConfigImportResult importData(String namespace, List discoveryList); + ConfigImportResult importData(String namespace, List discoveryList, ConfigsImportContext context); } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/DiscoveryUpstreamService.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/DiscoveryUpstreamService.java index c9828c9627b2..d808c194b6d5 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/DiscoveryUpstreamService.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/DiscoveryUpstreamService.java @@ -20,6 +20,7 @@ import org.apache.shenyu.admin.model.dto.DiscoveryUpstreamDTO; import org.apache.shenyu.admin.model.result.ConfigImportResult; import org.apache.shenyu.admin.model.vo.DiscoveryUpstreamVO; +import org.apache.shenyu.admin.service.configs.ConfigsImportContext; import org.apache.shenyu.common.dto.DiscoverySyncData; import org.apache.shenyu.common.dto.DiscoveryUpstreamData; @@ -130,7 +131,9 @@ public interface DiscoveryUpstreamService { * * @param namespace the namespace * @param discoveryUpstreamList the discoveryUpstream data + * @param context import context * @return config import result */ - ConfigImportResult importData(String namespace, List discoveryUpstreamList); + ConfigImportResult importData(String namespace, List discoveryUpstreamList, + ConfigsImportContext context); } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/NamespacePluginService.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/NamespacePluginService.java index 09b2164d763e..2af693d294e9 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/NamespacePluginService.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/NamespacePluginService.java @@ -25,6 +25,7 @@ import org.apache.shenyu.admin.model.vo.NamespacePluginVO; import org.apache.shenyu.admin.model.vo.PluginSnapshotVO; import org.apache.shenyu.admin.model.vo.PluginVO; +import org.apache.shenyu.admin.service.configs.ConfigsImportContext; import org.apache.shenyu.common.dto.PluginData; import java.util.List; @@ -149,9 +150,10 @@ public interface NamespacePluginService extends PageService pluginList); + ConfigImportResult importData(String namespace, List pluginList, ConfigsImportContext context); /** * List by namespace. diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/PluginHandleService.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/PluginHandleService.java index 731809ee03b7..c7fae887bc22 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/PluginHandleService.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/PluginHandleService.java @@ -21,8 +21,9 @@ import org.apache.shenyu.admin.model.dto.PluginHandleDTO; import org.apache.shenyu.admin.model.page.CommonPager; import org.apache.shenyu.admin.model.query.PluginHandleQuery; -import org.apache.shenyu.admin.model.result.ShenyuAdminResult; +import org.apache.shenyu.admin.model.result.ConfigImportResult; import org.apache.shenyu.admin.model.vo.PluginHandleVO; +import org.apache.shenyu.admin.service.configs.ConfigsImportContext; import java.util.Collection; import java.util.List; @@ -108,7 +109,8 @@ default Integer createOrUpdate(PluginHandleDTO pluginHandleDTO) { /** * import plugin handle list. * @param pluginHandleList the plugin handle list + * @param context the import context * @return shenyu admin result */ - ShenyuAdminResult importData(List pluginHandleList); + ConfigImportResult importData(List pluginHandleList, ConfigsImportContext context); } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/PluginService.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/PluginService.java index ba2df965b806..13105e1e7067 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/PluginService.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/PluginService.java @@ -25,6 +25,7 @@ import org.apache.shenyu.admin.model.result.ConfigImportResult; import org.apache.shenyu.admin.model.vo.PluginSnapshotVO; import org.apache.shenyu.admin.model.vo.PluginVO; +import org.apache.shenyu.admin.service.configs.ConfigsImportContext; import org.apache.shenyu.common.dto.PluginData; import java.util.List; @@ -130,16 +131,8 @@ public interface PluginService extends PageService pluginList); - - /** - * import plugin data. - * - * @param namespace the namespace - * @param pluginList the plugin data - * @return config import result - */ - ConfigImportResult importData(String namespace, List pluginList); + ConfigImportResult importData(List pluginList, ConfigsImportContext context); } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/ProxySelectorService.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/ProxySelectorService.java index 3a08798112e4..d8105d235a97 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/ProxySelectorService.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/ProxySelectorService.java @@ -22,6 +22,7 @@ import org.apache.shenyu.admin.model.query.ProxySelectorQuery; import org.apache.shenyu.admin.model.result.ConfigImportResult; import org.apache.shenyu.admin.model.vo.ProxySelectorVO; +import org.apache.shenyu.admin.service.configs.ConfigsImportContext; import org.apache.shenyu.common.dto.ProxySelectorData; import java.util.List; @@ -111,7 +112,8 @@ public interface ProxySelectorService { * * @param namespace namespace * @param proxySelectorList proxy selector data list + * @param context import context * @return config import result */ - ConfigImportResult importData(String namespace, List proxySelectorList); + ConfigImportResult importData(String namespace, List proxySelectorList, ConfigsImportContext context); } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/RuleService.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/RuleService.java index 298db53b0b64..5c759f26e819 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/RuleService.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/RuleService.java @@ -27,6 +27,7 @@ import org.apache.shenyu.admin.model.query.RuleQueryCondition; import org.apache.shenyu.admin.model.result.ConfigImportResult; import org.apache.shenyu.admin.model.vo.RuleVO; +import org.apache.shenyu.admin.service.configs.ConfigsImportContext; import org.apache.shenyu.common.dto.RuleData; import org.apache.shenyu.common.enums.OperatorEnum; import org.apache.shenyu.common.enums.ParamTypeEnum; @@ -188,9 +189,10 @@ default int createOrUpdate(final RuleDTO ruleDTO) { * * @param namespace namespace * @param ruleList rule list + * @param context import context * @return config import result */ - ConfigImportResult importData(String namespace, List ruleList); + ConfigImportResult importData(String namespace, List ruleList, ConfigsImportContext context); /** * Enabled string by ids and namespaceId. diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/SelectorService.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/SelectorService.java index 7ef418676555..a0934f5426d8 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/SelectorService.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/SelectorService.java @@ -25,6 +25,7 @@ import org.apache.shenyu.admin.model.query.SelectorQueryCondition; import org.apache.shenyu.admin.model.result.ConfigImportResult; import org.apache.shenyu.admin.model.vo.SelectorVO; +import org.apache.shenyu.admin.service.configs.ConfigsImportContext; import org.apache.shenyu.admin.utils.Assert; import org.apache.shenyu.common.dto.SelectorData; import org.apache.shenyu.common.enums.SelectorTypeEnum; @@ -260,9 +261,10 @@ default int createOrUpdate(SelectorDTO selectorDTO) { * * @param namespace the namespace * @param selectorList the plugin selector list + * @param context import context * @return config import result */ - ConfigImportResult importData(String namespace, List selectorList); + ConfigImportResult importData(String namespace, List selectorList, ConfigsImportContext context); /** * Enabled by ids and namespaceId. diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/AuthConfigsExportImportHandler.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/AuthConfigsExportImportHandler.java new file mode 100644 index 000000000000..251738c9e071 --- /dev/null +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/AuthConfigsExportImportHandler.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shenyu.admin.service.configs; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.model.dto.AppAuthDTO; +import org.apache.shenyu.admin.model.result.ConfigImportResult; +import org.apache.shenyu.admin.model.vo.AppAuthVO; +import org.apache.shenyu.admin.service.AppAuthService; +import org.apache.shenyu.common.constant.ExportImportConstants; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.common.utils.JsonUtils; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Optional; + + +@Component +public class AuthConfigsExportImportHandler implements ConfigsExportImportHandler { + + private final AppAuthService appAuthService; + + public AuthConfigsExportImportHandler(final AppAuthService appAuthService) { + this.appAuthService = appAuthService; + } + + @Override + public ConfigsExportImportEnum configsEnum() { + return ConfigsExportImportEnum.Auth; + } + + @Override + public Optional configsExport(final String namespaceId) { + List authDataList = appAuthService.listAllDataByNamespace(namespaceId); + if (CollectionUtils.isNotEmpty(authDataList)) { + authDataList.forEach(appAuthVO -> appAuthVO.setNamespaceId(null)); + return Optional.of(JsonUtils.toJson(authDataList)); + } + return Optional.empty(); + } + + @Override + public void configsImport(final String namespaceId, final String data, final ConfigsImportContext context) { + List authDataList = GsonUtils.getInstance().fromList(data, AppAuthDTO.class); + ConfigImportResult configImportResult = appAuthService.importData(namespaceId, authDataList); + context.getResult().put(ExportImportConstants.AUTH_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); + if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { + context.getResult().put(ExportImportConstants.AUTH_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); + } + } +} diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/ConfigsExportImportEnum.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/ConfigsExportImportEnum.java new file mode 100644 index 000000000000..7928c64f6363 --- /dev/null +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/ConfigsExportImportEnum.java @@ -0,0 +1,101 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shenyu.admin.service.configs; + +public enum ConfigsExportImportEnum { + + /** + * auth. + */ + Auth("auth.json", 0), + + /** + * meta. + */ + Meta("meta.json", 1), + + /** + * plugin template. + */ + PluginTemplate("plugin_template.json", 2), + + /** + * plugin handle. + */ + PluginHandle("plugin_handle.json", 3), + + /** + * namespace plugin. + */ + NamespacePlugin("namespace_plugin.json", 4), + + /** + * selector. + */ + Selector("selector.json", 5), + + /** + * rule. + */ + Rule("rule.json", 6), + + /** + * dict. + */ + Dict("dict.json", 7), + + /** + * proxy selector. + */ + ProxySelector("proxy_selector.json", 8), + + /** + * discovery. + */ + Discovery("discovery.json", 9), + + /** + * discovery upstream. + */ + DiscoveryUpstream("discovery_upstream.json", 10); + + /** + * zip item name. + */ + private String configName; + + /** + * the import order. + * + * @return The smaller, the earlier it will be executed + */ + private int importOrder; + + ConfigsExportImportEnum(final String configName, final int importOrder) { + this.configName = configName; + this.importOrder = importOrder; + } + + public String getConfigName() { + return configName; + } + + public int getImportOrder() { + return importOrder; + } +} diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/provider/MetaDataPathProvider.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/ConfigsExportImportHandler.java similarity index 55% rename from shenyu-admin/src/main/java/org/apache/shenyu/admin/service/provider/MetaDataPathProvider.java rename to shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/ConfigsExportImportHandler.java index 910c6783278a..103a0007f553 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/provider/MetaDataPathProvider.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/ConfigsExportImportHandler.java @@ -15,28 +15,16 @@ * limitations under the License. */ -package org.apache.shenyu.admin.service.provider; +package org.apache.shenyu.admin.service.configs; -import org.apache.shenyu.admin.mapper.MetaDataMapper; -import org.apache.shenyu.admin.validation.ExistProvider; -import org.springframework.stereotype.Component; -import java.io.Serializable; +import java.util.Optional; -/** - * MetaDataPathProvider. - */ -@Component -public class MetaDataPathProvider implements ExistProvider { - - private final MetaDataMapper metaDataMapper; - - public MetaDataPathProvider(final MetaDataMapper metaDataMapper) { - this.metaDataMapper = metaDataMapper; - } - - @Override - public Boolean existed(final Serializable key) { - return metaDataMapper.pathExisted(key); - } +public interface ConfigsExportImportHandler { + + ConfigsExportImportEnum configsEnum(); + + Optional configsExport(String namespaceId); + + void configsImport(String namespaceId, String data, ConfigsImportContext context); } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/ConfigsImportContext.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/ConfigsImportContext.java new file mode 100644 index 000000000000..f722ac26ee2f --- /dev/null +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/ConfigsImportContext.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shenyu.admin.service.configs; + +import com.google.common.collect.Maps; + +import java.util.Map; + +public class ConfigsImportContext { + + /** + * the import result. + */ + private final Map result = Maps.newHashMap(); + + /** + * export selector id -> new or exist selector id. + */ + private final Map selectorIdMapping = Maps.newHashMap(); + + /** + * export proxy selector id -> new or exist proxy selector id. + */ + private final Map proxySelectorIdMapping = Maps.newHashMap(); + + /** + * export discovery handler id -> new or exist discovery handler id. + */ + private final Map discoveryHandlerIdMapping = Maps.newHashMap(); + + /** + * export plugin template id -> new or exist plugin template id. + */ + private final Map pluginTemplateIdMapping = Maps.newHashMap(); + + public Map getResult() { + return result; + } + + public Map getSelectorIdMapping() { + return selectorIdMapping; + } + + public Map getProxySelectorIdMapping() { + return proxySelectorIdMapping; + } + + public Map getDiscoveryHandlerIdMapping() { + return discoveryHandlerIdMapping; + } + + public Map getPluginTemplateIdMapping() { + return pluginTemplateIdMapping; + } +} diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/DictDataConfigsExportImportHandler.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/DictDataConfigsExportImportHandler.java new file mode 100644 index 000000000000..bc901f81f02b --- /dev/null +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/DictDataConfigsExportImportHandler.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shenyu.admin.service.configs; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.model.dto.ShenyuDictDTO; +import org.apache.shenyu.admin.model.result.ConfigImportResult; +import org.apache.shenyu.admin.model.vo.ShenyuDictVO; +import org.apache.shenyu.admin.service.ShenyuDictService; +import org.apache.shenyu.common.constant.ExportImportConstants; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.common.utils.JsonUtils; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Optional; + +@Component +public class DictDataConfigsExportImportHandler implements ConfigsExportImportHandler { + + /** + * The Dict service. + */ + private final ShenyuDictService shenyuDictService; + + public DictDataConfigsExportImportHandler(final ShenyuDictService shenyuDictService) { + this.shenyuDictService = shenyuDictService; + } + + @Override + public ConfigsExportImportEnum configsEnum() { + return ConfigsExportImportEnum.Dict; + } + + @Override + public Optional configsExport(final String namespaceId) { + List dictDataList = shenyuDictService.listAllData(); + if (CollectionUtils.isNotEmpty(dictDataList)) { + return Optional.of(JsonUtils.toJson(dictDataList)); + } + return Optional.empty(); + } + + @Override + public void configsImport(final String namespaceId, final String data, final ConfigsImportContext context) { + List dictList = GsonUtils.getInstance().fromList(data, ShenyuDictDTO.class); + ConfigImportResult configImportResult = shenyuDictService.importData(dictList); + context.getResult().put(ExportImportConstants.DICT_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); + if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { + context.getResult().put(ExportImportConstants.DICT_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); + } + } +} diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/DiscoveryDataConfigsExportImportHandler.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/DiscoveryDataConfigsExportImportHandler.java new file mode 100644 index 000000000000..254aeac5f5f5 --- /dev/null +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/DiscoveryDataConfigsExportImportHandler.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shenyu.admin.service.configs; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.model.dto.DiscoveryDTO; +import org.apache.shenyu.admin.model.result.ConfigImportResult; +import org.apache.shenyu.admin.model.vo.DiscoveryVO; +import org.apache.shenyu.admin.service.DiscoveryService; +import org.apache.shenyu.common.constant.ExportImportConstants; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.common.utils.JsonUtils; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Optional; + +@Component +public class DiscoveryDataConfigsExportImportHandler implements ConfigsExportImportHandler { + + private final DiscoveryService discoveryService; + + public DiscoveryDataConfigsExportImportHandler(final DiscoveryService discoveryService) { + this.discoveryService = discoveryService; + } + + @Override + public ConfigsExportImportEnum configsEnum() { + return ConfigsExportImportEnum.Discovery; + } + + @Override + public Optional configsExport(final String namespaceId) { + List discoveryList = discoveryService.listAllDataByNamespaceId(namespaceId); + if (CollectionUtils.isNotEmpty(discoveryList)) { + discoveryList.forEach(discoveryVO -> discoveryVO.setNamespaceId(null)); + return Optional.of(JsonUtils.toJson(discoveryList)); + } + return Optional.empty(); + } + + @Override + public void configsImport(final String namespaceId, final String data, final ConfigsImportContext context) { + List discoveryList = GsonUtils.getInstance().fromList(data, DiscoveryDTO.class); + ConfigImportResult configImportResult = discoveryService.importData(namespaceId, discoveryList, context); + context.getResult().put(ExportImportConstants.DISCOVERY_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); + if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { + context.getResult().put(ExportImportConstants.DISCOVERY_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); + } + } +} diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/DiscoveryUpstreamDataConfigsExportImportHandler.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/DiscoveryUpstreamDataConfigsExportImportHandler.java new file mode 100644 index 000000000000..8e6e26034302 --- /dev/null +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/DiscoveryUpstreamDataConfigsExportImportHandler.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shenyu.admin.service.configs; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.model.dto.DiscoveryUpstreamDTO; +import org.apache.shenyu.admin.model.result.ConfigImportResult; +import org.apache.shenyu.admin.model.vo.DiscoveryUpstreamVO; +import org.apache.shenyu.admin.service.DiscoveryUpstreamService; +import org.apache.shenyu.common.constant.ExportImportConstants; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.common.utils.JsonUtils; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Optional; + +@Component +public class DiscoveryUpstreamDataConfigsExportImportHandler implements ConfigsExportImportHandler { + + private final DiscoveryUpstreamService discoveryUpstreamService; + + public DiscoveryUpstreamDataConfigsExportImportHandler(final DiscoveryUpstreamService discoveryUpstreamService) { + this.discoveryUpstreamService = discoveryUpstreamService; + } + + @Override + public ConfigsExportImportEnum configsEnum() { + return ConfigsExportImportEnum.DiscoveryUpstream; + } + + @Override + public Optional configsExport(final String namespaceId) { + List discoveryUpstreamList = discoveryUpstreamService.listAllDataByNamespaceId(namespaceId); + if (CollectionUtils.isNotEmpty(discoveryUpstreamList)) { + return Optional.of(JsonUtils.toJson(discoveryUpstreamList)); + } + return Optional.empty(); + } + + @Override + public void configsImport(final String namespaceId, final String data, final ConfigsImportContext context) { + List discoveryUpstreamList = GsonUtils.getInstance().fromList(data, DiscoveryUpstreamDTO.class); + ConfigImportResult configImportResult = discoveryUpstreamService.importData(namespaceId, discoveryUpstreamList, context); + context.getResult().put(ExportImportConstants.DISCOVERY_UPSTREAM_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); + if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { + context.getResult().put(ExportImportConstants.DISCOVERY_UPSTREAM_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); + } + } +} diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/MetadataConfigsExportImportHandler.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/MetadataConfigsExportImportHandler.java new file mode 100644 index 000000000000..450f979c838c --- /dev/null +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/MetadataConfigsExportImportHandler.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shenyu.admin.service.configs; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.model.dto.MetaDataDTO; +import org.apache.shenyu.admin.model.result.ConfigImportResult; +import org.apache.shenyu.admin.model.vo.MetaDataVO; +import org.apache.shenyu.admin.service.MetaDataService; +import org.apache.shenyu.common.constant.ExportImportConstants; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.common.utils.JsonUtils; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Optional; + +@Component +public class MetadataConfigsExportImportHandler implements ConfigsExportImportHandler { + + private final MetaDataService metaDataService; + + public MetadataConfigsExportImportHandler(final MetaDataService metaDataService) { + this.metaDataService = metaDataService; + } + + @Override + public ConfigsExportImportEnum configsEnum() { + return ConfigsExportImportEnum.Meta; + } + + @Override + public Optional configsExport(final String namespaceId) { + List metaDataList = metaDataService.listAllDataByNamespaceId(namespaceId); + if (CollectionUtils.isNotEmpty(metaDataList)) { + metaDataList.forEach(metaDataVO -> metaDataVO.setNamespaceId(null)); + return Optional.of(JsonUtils.toJson(metaDataList)); + } + return Optional.empty(); + } + + @Override + public void configsImport(final String namespaceId, final String data, final ConfigsImportContext context) { + List metaDataList = GsonUtils.getInstance().fromList(data, MetaDataDTO.class); + ConfigImportResult configImportResult = metaDataService.importData(namespaceId, metaDataList); + context.getResult().put(ExportImportConstants.META_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); + if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { + context.getResult().put(ExportImportConstants.META_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); + } + } +} diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/NamespacePluginDataConfigsExportImportHandler.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/NamespacePluginDataConfigsExportImportHandler.java new file mode 100644 index 000000000000..0b036b642698 --- /dev/null +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/NamespacePluginDataConfigsExportImportHandler.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shenyu.admin.service.configs; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.model.dto.NamespacePluginDTO; +import org.apache.shenyu.admin.model.result.ConfigImportResult; +import org.apache.shenyu.admin.model.vo.NamespacePluginVO; +import org.apache.shenyu.admin.service.NamespacePluginService; +import org.apache.shenyu.common.constant.ExportImportConstants; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.common.utils.JsonUtils; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Optional; + +@Component +public class NamespacePluginDataConfigsExportImportHandler implements ConfigsExportImportHandler { + + private final NamespacePluginService namespacePluginService; + + public NamespacePluginDataConfigsExportImportHandler(final NamespacePluginService namespacePluginService) { + this.namespacePluginService = namespacePluginService; + } + + @Override + public ConfigsExportImportEnum configsEnum() { + return ConfigsExportImportEnum.NamespacePlugin; + } + + @Override + public Optional configsExport(final String namespaceId) { + List namespacePluginVOList = namespacePluginService.listAllData(namespaceId); + if (CollectionUtils.isNotEmpty(namespacePluginVOList)) { + namespacePluginVOList.forEach(plugin -> plugin.setNamespaceId(null)); + return Optional.of(JsonUtils.toJson(namespacePluginVOList)); + } + return Optional.empty(); + } + + @Override + public void configsImport(final String namespaceId, final String data, final ConfigsImportContext context) { + List pluginList = GsonUtils.getInstance().fromList(data, NamespacePluginDTO.class); + ConfigImportResult configImportResult = namespacePluginService.importData(namespaceId, pluginList, context); + context.getResult().put(ExportImportConstants.NAMESPACE_PLUGIN_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); + if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { + context.getResult().put(ExportImportConstants.NAMESPACE_PLUGIN_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); + } + } +} diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/PluginHandleDataConfigsExportImportHandler.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/PluginHandleDataConfigsExportImportHandler.java new file mode 100644 index 000000000000..e47b95574816 --- /dev/null +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/PluginHandleDataConfigsExportImportHandler.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shenyu.admin.service.configs; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.model.dto.PluginHandleDTO; +import org.apache.shenyu.admin.model.result.ConfigImportResult; +import org.apache.shenyu.admin.model.vo.PluginHandleVO; +import org.apache.shenyu.admin.service.PluginHandleService; +import org.apache.shenyu.common.constant.ExportImportConstants; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.common.utils.JsonUtils; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Optional; + +@Component +public class PluginHandleDataConfigsExportImportHandler implements ConfigsExportImportHandler { + + private final PluginHandleService pluginHandleService; + + public PluginHandleDataConfigsExportImportHandler(final PluginHandleService pluginHandleService) { + this.pluginHandleService = pluginHandleService; + } + + @Override + public ConfigsExportImportEnum configsEnum() { + return ConfigsExportImportEnum.PluginHandle; + } + + @Override + public Optional configsExport(final String namespaceId) { + List pluginHandleList = pluginHandleService.listAllData(); + if (CollectionUtils.isNotEmpty(pluginHandleList)) { + return Optional.of(JsonUtils.toJson(pluginHandleList)); + } + return Optional.empty(); + } + + @Override + public void configsImport(final String namespaceId, final String data, final ConfigsImportContext context) { + List pluginHandleList = GsonUtils.getInstance().fromList(data, PluginHandleDTO.class); + ConfigImportResult configImportResult = pluginHandleService.importData(pluginHandleList, context); + context.getResult().put(ExportImportConstants.PLUGIN_HANDLE_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); + if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { + context.getResult().put(ExportImportConstants.PLUGIN_HANDLE_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); + } + } +} diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/PluginTemplateDataConfigsExportImportHandler.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/PluginTemplateDataConfigsExportImportHandler.java new file mode 100644 index 000000000000..459188cdf89f --- /dev/null +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/PluginTemplateDataConfigsExportImportHandler.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shenyu.admin.service.configs; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.model.dto.PluginDTO; +import org.apache.shenyu.admin.model.result.ConfigImportResult; +import org.apache.shenyu.admin.model.vo.PluginVO; +import org.apache.shenyu.admin.service.PluginService; +import org.apache.shenyu.common.constant.ExportImportConstants; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.common.utils.JsonUtils; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Optional; + +@Component +public class PluginTemplateDataConfigsExportImportHandler implements ConfigsExportImportHandler { + + private final PluginService pluginService; + + public PluginTemplateDataConfigsExportImportHandler(final PluginService pluginService) { + this.pluginService = pluginService; + } + + @Override + public ConfigsExportImportEnum configsEnum() { + return ConfigsExportImportEnum.PluginTemplate; + } + + @Override + public Optional configsExport(final String namespaceId) { + List pluginDataList = pluginService.listAllData(); + if (CollectionUtils.isNotEmpty(pluginDataList)) { + return Optional.of(JsonUtils.toJson(pluginDataList)); + } + return Optional.empty(); + } + + @Override + public void configsImport(final String namespaceId, final String data, final ConfigsImportContext context) { + List pluginList = GsonUtils.getInstance().fromList(data, PluginDTO.class); + ConfigImportResult configImportResult = pluginService.importData(pluginList, context); + context.getResult().put(ExportImportConstants.PLUGIN_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); + if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { + context.getResult().put(ExportImportConstants.PLUGIN_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); + } + } +} diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/ProxySelectorDataConfigsExportImportHandler.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/ProxySelectorDataConfigsExportImportHandler.java new file mode 100644 index 000000000000..9eab060b4164 --- /dev/null +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/ProxySelectorDataConfigsExportImportHandler.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shenyu.admin.service.configs; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.model.result.ConfigImportResult; +import org.apache.shenyu.admin.service.ProxySelectorService; +import org.apache.shenyu.common.constant.ExportImportConstants; +import org.apache.shenyu.common.dto.ProxySelectorData; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.common.utils.JsonUtils; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Optional; + +@Component +public class ProxySelectorDataConfigsExportImportHandler implements ConfigsExportImportHandler { + + private final ProxySelectorService proxySelectorService; + + public ProxySelectorDataConfigsExportImportHandler(final ProxySelectorService proxySelectorService) { + this.proxySelectorService = proxySelectorService; + } + + @Override + public ConfigsExportImportEnum configsEnum() { + return ConfigsExportImportEnum.ProxySelector; + } + + @Override + public Optional configsExport(final String namespaceId) { + List proxySelectorDataList = proxySelectorService.listAllByNamespaceId(namespaceId); + if (CollectionUtils.isNotEmpty(proxySelectorDataList)) { + proxySelectorDataList.forEach(proxySelectorData -> proxySelectorData.setNamespaceId(null)); + return Optional.of(JsonUtils.toJson(proxySelectorDataList)); + } + return Optional.empty(); + } + + @Override + public void configsImport(final String namespaceId, final String data, final ConfigsImportContext context) { + List proxySelectorList = GsonUtils.getInstance().fromList(data, ProxySelectorData.class); + ConfigImportResult configImportResult = proxySelectorService.importData(namespaceId, proxySelectorList, context); + context.getResult().put(ExportImportConstants.PROXY_SELECTOR_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); + if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { + context.getResult().put(ExportImportConstants.PROXY_SELECTOR_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); + } + } +} diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/RuleDataConfigsExportImportHandler.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/RuleDataConfigsExportImportHandler.java new file mode 100644 index 000000000000..192982aa4393 --- /dev/null +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/RuleDataConfigsExportImportHandler.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shenyu.admin.service.configs; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.model.dto.RuleDTO; +import org.apache.shenyu.admin.model.result.ConfigImportResult; +import org.apache.shenyu.admin.model.vo.RuleVO; +import org.apache.shenyu.admin.service.RuleService; +import org.apache.shenyu.common.constant.ExportImportConstants; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.common.utils.JsonUtils; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Optional; + +@Component +public class RuleDataConfigsExportImportHandler implements ConfigsExportImportHandler { + + private final RuleService ruleService; + + public RuleDataConfigsExportImportHandler(final RuleService ruleService) { + this.ruleService = ruleService; + } + + @Override + public ConfigsExportImportEnum configsEnum() { + return ConfigsExportImportEnum.Rule; + } + + @Override + public Optional configsExport(final String namespaceId) { + List ruleDataList = ruleService.listAllDataByNamespaceId(namespaceId); + if (CollectionUtils.isNotEmpty(ruleDataList)) { + return Optional.of(JsonUtils.toJson(ruleDataList)); + } + return Optional.empty(); + } + + @Override + public void configsImport(final String namespaceId, final String data, final ConfigsImportContext context) { + List ruleList = GsonUtils.getInstance().fromList(data, RuleDTO.class); + ConfigImportResult configImportResult = ruleService.importData(namespaceId, ruleList, context); + context.getResult().put(ExportImportConstants.RULE_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); + if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { + context.getResult().put(ExportImportConstants.RULE_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); + } + } +} diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/SelectorDataConfigsExportImportHandler.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/SelectorDataConfigsExportImportHandler.java new file mode 100644 index 000000000000..bbf33bec1e75 --- /dev/null +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/configs/SelectorDataConfigsExportImportHandler.java @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 org.apache.shenyu.admin.service.configs; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.shenyu.admin.model.dto.SelectorDTO; +import org.apache.shenyu.admin.model.result.ConfigImportResult; +import org.apache.shenyu.admin.model.vo.SelectorVO; +import org.apache.shenyu.admin.service.SelectorService; +import org.apache.shenyu.common.constant.ExportImportConstants; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.common.utils.JsonUtils; +import org.springframework.stereotype.Component; + +import java.util.List; +import java.util.Optional; + +@Component +public class SelectorDataConfigsExportImportHandler implements ConfigsExportImportHandler { + + private final SelectorService selectorService; + + public SelectorDataConfigsExportImportHandler(final SelectorService selectorService) { + this.selectorService = selectorService; + } + + @Override + public ConfigsExportImportEnum configsEnum() { + return ConfigsExportImportEnum.Selector; + } + + @Override + public Optional configsExport(final String namespaceId) { + List selectorDataList = selectorService.listAllDataByNamespaceId(namespaceId); + if (CollectionUtils.isNotEmpty(selectorDataList)) { + selectorDataList.forEach(selectorVO -> selectorVO.setNamespaceId(null)); + return Optional.of(JsonUtils.toJson(selectorDataList)); + } + return Optional.empty(); + } + + @Override + public void configsImport(final String namespaceId, final String data, final ConfigsImportContext context) { + List selectorList = GsonUtils.getInstance().fromList(data, SelectorDTO.class); + ConfigImportResult configImportResult = selectorService.importData(namespaceId, selectorList, context); + context.getResult().put(ExportImportConstants.SELECTOR_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); + if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { + context.getResult().put(ExportImportConstants.SELECTOR_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); + } + } +} diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java index ee5bcb6e08c3..39f13ee5cd5d 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/AppAuthServiceImpl.java @@ -248,16 +248,18 @@ public ShenyuAdminResult syncData(final List appAuthDOList) { Map> paramMap = this.prepareAuthParamData(idList); Map> pathMap = this.prepareAuthPathData(idList); - List dataList = appAuthDOList.stream() + Map> namespaceDataList = appAuthDOList.stream() .filter(Objects::nonNull) .map(appAuthDO -> { String id = appAuthDO.getId(); return buildByEntityWithParamAndPath(appAuthDO, paramMap.get(id), pathMap.get(id)); }) - .collect(Collectors.toList()); - eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.APP_AUTH, - DataEventTypeEnum.REFRESH, - dataList)); + .collect(Collectors.groupingBy(AppAuthData::getNamespaceId)); + namespaceDataList.values().forEach(dataList -> { + eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.APP_AUTH, + DataEventTypeEnum.REFRESH, + dataList)); + }); return ShenyuAdminResult.success(); } @@ -330,6 +332,7 @@ public ConfigImportResult importData(final List authDataList) { } @Override + @Transactional(rollbackFor = Exception.class) public ConfigImportResult importData(final String namespace, final List authDataList) { if (CollectionUtils.isEmpty(authDataList)) { return ConfigImportResult.success(); @@ -359,6 +362,7 @@ public ConfigImportResult importData(final String namespace, final List 0) { successCount++; diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ConfigsServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ConfigsServiceImpl.java index bc2ea0f90655..99842dc26a2a 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ConfigsServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ConfigsServiceImpl.java @@ -25,7 +25,6 @@ import org.apache.shenyu.admin.model.dto.DiscoveryDTO; import org.apache.shenyu.admin.model.dto.DiscoveryUpstreamDTO; import org.apache.shenyu.admin.model.dto.MetaDataDTO; -import org.apache.shenyu.admin.model.dto.NamespacePluginDTO; import org.apache.shenyu.admin.model.dto.PluginDTO; import org.apache.shenyu.admin.model.dto.RuleDTO; import org.apache.shenyu.admin.model.dto.SelectorDTO; @@ -54,19 +53,23 @@ import org.apache.shenyu.admin.service.RuleService; import org.apache.shenyu.admin.service.SelectorService; import org.apache.shenyu.admin.service.ShenyuDictService; +import org.apache.shenyu.admin.service.configs.ConfigsExportImportHandler; +import org.apache.shenyu.admin.service.configs.ConfigsImportContext; import org.apache.shenyu.admin.utils.ZipUtil; import org.apache.shenyu.admin.utils.ZipUtil.ZipItem; import org.apache.shenyu.common.constant.ExportImportConstants; import org.apache.shenyu.common.dto.ProxySelectorData; import org.apache.shenyu.common.utils.GsonUtils; import org.apache.shenyu.common.utils.JsonUtils; -import org.apache.shenyu.common.utils.UUIDUtils; +import org.apache.shenyu.common.utils.ListUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; +import java.util.Comparator; import java.util.List; import java.util.Map; +import java.util.Objects; /** * Implementation of the {@link org.apache.shenyu.admin.service.ConfigsService}. @@ -131,6 +134,11 @@ public class ConfigsServiceImpl implements ConfigsService { */ private final DiscoveryUpstreamService discoveryUpstreamService; + /** + * The configs export import handlers. + */ + private final List configsExportImportHandlers; + public ConfigsServiceImpl(final AppAuthService appAuthService, final PluginService pluginService, final NamespacePluginService namespacePluginService, @@ -141,7 +149,8 @@ public ConfigsServiceImpl(final AppAuthService appAuthService, final ShenyuDictService shenyuDictService, final ProxySelectorService proxySelectorService, final DiscoveryService discoveryService, - final DiscoveryUpstreamService discoveryUpstreamService) { + final DiscoveryUpstreamService discoveryUpstreamService, + final List configsExportImportHandlers) { this.appAuthService = appAuthService; this.pluginService = pluginService; this.namespacePluginService = namespacePluginService; @@ -153,6 +162,8 @@ public ConfigsServiceImpl(final AppAuthService appAuthService, this.proxySelectorService = proxySelectorService; this.discoveryService = discoveryService; this.discoveryUpstreamService = discoveryUpstreamService; + this.configsExportImportHandlers = configsExportImportHandlers.stream() + .sorted(Comparator.comparingInt(c -> c.configsEnum().getImportOrder())).toList(); } @Override @@ -182,29 +193,12 @@ public ShenyuAdminResult configsExport() { @Override public ShenyuAdminResult configsExport(final String namespace) { List zipItemList = Lists.newArrayList(); - - exportAuthData(namespace, zipItemList); - - exportMetadata(namespace, zipItemList); - - exportNamespacePluginData(namespace, zipItemList); - - exportSelectorData(namespace, zipItemList); - - exportRuleData(namespace, zipItemList); - - exportDictData(zipItemList); - - exportPluginTemplateData(zipItemList); - - exportPluginHandleData(zipItemList); - - exportProxySelectorData(namespace, zipItemList); - - exportDiscoveryData(namespace, zipItemList); - - exportDiscoveryUpstreamData(namespace, zipItemList); - + + for (ConfigsExportImportHandler configsExportImportHandler : configsExportImportHandlers) { + configsExportImportHandler.configsExport(namespace) + .ifPresent(data -> zipItemList.add(new ZipUtil.ZipItem(configsExportImportHandler.configsEnum().getConfigName(), data))); + } + return ShenyuAdminResult.success(ZipUtil.zip(zipItemList)); } @@ -399,44 +393,15 @@ public ShenyuAdminResult configsImport(final String namespace, final byte[] sour LOG.info("import file is empty"); return ShenyuAdminResult.success(); } - Map result = Maps.newHashMap(); - for (ZipUtil.ZipItem zipItem : zipItemList) { - switch (zipItem.getItemName()) { - case ExportImportConstants.AUTH_JSON: - importAuthData(namespace, result, zipItem); - break; - case ExportImportConstants.META_JSON: - importMetaData(namespace, result, zipItem); - break; - case ExportImportConstants.PLUGIN_TEMPLATE_JSON: - importPluginTemplateData(result, zipItem); - break; - case ExportImportConstants.NAMESPACE_PLUGIN_JSON: - importNamespacePluginData(namespace, result, zipItem); - break; - case ExportImportConstants.SELECTOR_JSON: - importSelectorData(namespace, result, zipItem); - break; - case ExportImportConstants.RULE_JSON: - importRuleData(namespace, result, zipItem); - break; - case ExportImportConstants.DICT_JSON: - importDictData(result, zipItem); - break; - case ExportImportConstants.PROXY_SELECTOR_JSON: - importProxySelectorData(namespace, result, zipItem); - break; - case ExportImportConstants.DISCOVERY_JSON: - importDiscoveryData(namespace, result, zipItem); - break; - case ExportImportConstants.DISCOVERY_UPSTREAM_JSON: - importDiscoveryUpstreamData(namespace, result, zipItem); - break; - default: - break; + ConfigsImportContext context = new ConfigsImportContext(); + Map zipItemNameMap = ListUtil.toMap(zipItemList, ZipUtil.ZipItem::getItemName); + for (ConfigsExportImportHandler configsExportImportHandler : configsExportImportHandlers) { + ZipUtil.ZipItem zipItem = zipItemNameMap.get(configsExportImportHandler.configsEnum().getConfigName()); + if (Objects.nonNull(zipItem) && StringUtils.isNoneBlank(zipItem.getItemData())) { + configsExportImportHandler.configsImport(namespace, zipItem.getItemData(), context); } } - return ShenyuAdminResult.success(result); + return ShenyuAdminResult.success(context.getResult()); } private void importDiscoveryUpstreamData(final Map result, final ZipUtil.ZipItem zipItem) { @@ -450,20 +415,6 @@ private void importDiscoveryUpstreamData(final Map result, final } } } - - private void importDiscoveryUpstreamData(final String namespace, final Map result, final ZipUtil.ZipItem zipItem) { - String discoveryUpstreamJson = zipItem.getItemData(); - if (StringUtils.isNotEmpty(discoveryUpstreamJson)) { - List discoveryUpstreamList = GsonUtils.getInstance().fromList(discoveryUpstreamJson, DiscoveryUpstreamDTO.class); - // set namespaceId - discoveryUpstreamList.forEach(discoveryUpstreamDTO -> discoveryUpstreamDTO.setNamespaceId(namespace)); - ConfigImportResult configImportResult = discoveryUpstreamService.importData(namespace, discoveryUpstreamList); - result.put(ExportImportConstants.DISCOVERY_UPSTREAM_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); - if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { - result.put(ExportImportConstants.DISCOVERY_UPSTREAM_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); - } - } - } private void importDiscoveryData(final Map result, final ZipUtil.ZipItem zipItem) { String discoveryJson = zipItem.getItemData(); @@ -477,20 +428,6 @@ private void importDiscoveryData(final Map result, final ZipUtil } } - private void importDiscoveryData(final String namespace, final Map result, final ZipUtil.ZipItem zipItem) { - String discoveryJson = zipItem.getItemData(); - if (StringUtils.isNotEmpty(discoveryJson)) { - List discoveryList = GsonUtils.getInstance().fromList(discoveryJson, DiscoveryDTO.class); - // set namespaceId - discoveryList.forEach(discoveryDTO -> discoveryDTO.setNamespaceId(namespace)); - ConfigImportResult configImportResult = discoveryService.importData(namespace, discoveryList); - result.put(ExportImportConstants.DISCOVERY_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); - if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { - result.put(ExportImportConstants.DISCOVERY_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); - } - } - } - private void importProxySelectorData(final Map result, final ZipUtil.ZipItem zipItem) { String proxySelectorJson = zipItem.getItemData(); if (StringUtils.isNotEmpty(proxySelectorJson)) { @@ -503,20 +440,6 @@ private void importProxySelectorData(final Map result, final Zip } } - private void importProxySelectorData(final String namespace, final Map result, final ZipUtil.ZipItem zipItem) { - String proxySelectorJson = zipItem.getItemData(); - if (StringUtils.isNotEmpty(proxySelectorJson)) { - List proxySelectorList = GsonUtils.getInstance().fromList(proxySelectorJson, ProxySelectorData.class); - // set namespaceId - proxySelectorList.forEach(proxySelectorData -> proxySelectorData.setNamespaceId(namespace)); - ConfigImportResult configImportResult = proxySelectorService.importData(namespace, proxySelectorList); - result.put(ExportImportConstants.PROXY_SELECTOR_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); - if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { - result.put(ExportImportConstants.PROXY_SELECTOR_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); - } - } - } - private void importDictData(final Map result, final ZipUtil.ZipItem zipItem) { String dictJson = zipItem.getItemData(); if (StringUtils.isNotEmpty(dictJson)) { @@ -541,20 +464,6 @@ private void importRuleData(final Map result, final ZipUtil.ZipI } } - private void importRuleData(final String namespace, final Map result, final ZipUtil.ZipItem zipItem) { - String ruleJson = zipItem.getItemData(); - if (StringUtils.isNotEmpty(ruleJson)) { - List ruleList = GsonUtils.getInstance().fromList(ruleJson, RuleDTO.class); - // set namespaceId - ruleList.forEach(ruleDTO -> ruleDTO.setNamespaceId(namespace)); - ConfigImportResult configImportResult = ruleService.importData(namespace, ruleList); - result.put(ExportImportConstants.RULE_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); - if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { - result.put(ExportImportConstants.RULE_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); - } - } - } - private void importSelectorData(final Map result, final ZipUtil.ZipItem zipItem) { String selectorJson = zipItem.getItemData(); if (StringUtils.isNotEmpty(selectorJson)) { @@ -567,69 +476,11 @@ private void importSelectorData(final Map result, final ZipUtil. } } - private void importSelectorData(final String namespace, final Map result, final ZipUtil.ZipItem zipItem) { - String selectorJson = zipItem.getItemData(); - if (StringUtils.isNotEmpty(selectorJson)) { - List selectorList = GsonUtils.getInstance().fromList(selectorJson, SelectorDTO.class); - // set namespaceId - selectorList.forEach(selectorDTO -> selectorDTO.setNamespaceId(namespace)); - ConfigImportResult configImportResult = selectorService.importData(namespace, selectorList); - result.put(ExportImportConstants.SELECTOR_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); - if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { - result.put(ExportImportConstants.SELECTOR_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); - } - } - } - private void importPluginData(final Map result, final ZipUtil.ZipItem zipItem) { String pluginJson = zipItem.getItemData(); if (StringUtils.isNotEmpty(pluginJson)) { List pluginList = GsonUtils.getInstance().fromList(pluginJson, PluginDTO.class); - ConfigImportResult configImportResult = pluginService.importData(pluginList); - result.put(ExportImportConstants.PLUGIN_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); - if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { - result.put(ExportImportConstants.PLUGIN_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); - } - } - } - - private void importPluginData(final String namespace, final Map result, final ZipUtil.ZipItem zipItem) { - String pluginJson = zipItem.getItemData(); - if (StringUtils.isNotEmpty(pluginJson)) { - List pluginList = GsonUtils.getInstance().fromList(pluginJson, PluginDTO.class); - // set namespaceId - pluginList.forEach(pluginDTO -> pluginDTO.setNamespaceId(namespace)); - ConfigImportResult configImportResult = pluginService.importData(pluginList); - result.put(ExportImportConstants.PLUGIN_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); - if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { - result.put(ExportImportConstants.PLUGIN_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); - } - } - } - - private void importNamespacePluginData(final String namespace, final Map result, final ZipUtil.ZipItem zipItem) { - String pluginJson = zipItem.getItemData(); - if (StringUtils.isNotEmpty(pluginJson)) { - List namespacePluginDTOS = GsonUtils.getInstance().fromList(pluginJson, NamespacePluginDTO.class); - // set namespaceId - namespacePluginDTOS.forEach(namespacePluginDTO -> { - namespacePluginDTO.setNamespaceId(namespace); - // change id - namespacePluginDTO.setId(UUIDUtils.getInstance().generateShortUuid()); - }); - ConfigImportResult configImportResult = namespacePluginService.importData(namespace, namespacePluginDTOS); - result.put(ExportImportConstants.PLUGIN_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); - if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { - result.put(ExportImportConstants.PLUGIN_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); - } - } - } - - private void importPluginTemplateData(final Map result, final ZipUtil.ZipItem zipItem) { - String pluginTemplateJson = zipItem.getItemData(); - if (StringUtils.isNotEmpty(pluginTemplateJson)) { - List pluginTemplateList = GsonUtils.getInstance().fromList(pluginTemplateJson, PluginDTO.class); - ConfigImportResult configImportResult = pluginService.importData(pluginTemplateList); + ConfigImportResult configImportResult = pluginService.importData(pluginList, null); result.put(ExportImportConstants.PLUGIN_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { result.put(ExportImportConstants.PLUGIN_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); @@ -649,20 +500,6 @@ private void importMetaData(final Map result, final ZipUtil.ZipI } } - private void importMetaData(final String namespace, final Map result, final ZipUtil.ZipItem zipItem) { - String metaJson = zipItem.getItemData(); - if (StringUtils.isNotEmpty(metaJson)) { - List metaDataList = GsonUtils.getInstance().fromList(metaJson, MetaDataDTO.class); - // set namespaceId - metaDataList.forEach(metaDataDTO -> metaDataDTO.setNamespaceId(namespace)); - ConfigImportResult configImportResult = metaDataService.importData(namespace, metaDataList); - result.put(ExportImportConstants.META_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); - if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { - result.put(ExportImportConstants.META_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); - } - } - } - private void importAuthData(final Map result, final ZipUtil.ZipItem zipItem) { String authJson = zipItem.getItemData(); if (StringUtils.isNotEmpty(authJson)) { @@ -674,18 +511,4 @@ private void importAuthData(final Map result, final ZipUtil.ZipI } } } - - private void importAuthData(final String namespace, final Map result, final ZipUtil.ZipItem zipItem) { - String authJson = zipItem.getItemData(); - if (StringUtils.isNotEmpty(authJson)) { - List authDataList = GsonUtils.getInstance().fromList(authJson, AppAuthDTO.class); - // set namespaceId - authDataList.forEach(appAuthDTO -> appAuthDTO.setNamespaceId(namespace)); - ConfigImportResult configImportResult = appAuthService.importData(namespace, authDataList); - result.put(ExportImportConstants.AUTH_IMPORT_SUCCESS_COUNT, configImportResult.getSuccessCount()); - if (StringUtils.isNotEmpty(configImportResult.getFailMessage())) { - result.put(ExportImportConstants.AUTH_IMPORT_FAIL_MESSAGE, configImportResult.getFailMessage()); - } - } - } } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryServiceImpl.java index 24cc2a7019b8..5f16c1b1edf6 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryServiceImpl.java @@ -44,6 +44,7 @@ import org.apache.shenyu.admin.model.vo.DiscoveryVO; import org.apache.shenyu.admin.service.DiscoveryService; import org.apache.shenyu.admin.service.SelectorService; +import org.apache.shenyu.admin.service.configs.ConfigsImportContext; import org.apache.shenyu.admin.transfer.DiscoveryTransfer; import org.apache.shenyu.admin.utils.ShenyuResultMessage; import org.apache.shenyu.common.exception.ShenyuException; @@ -451,11 +452,11 @@ public ConfigImportResult importData(final List discoveryList) { } @Override - public ConfigImportResult importData(final String namespace, final List discoveryList) { + public ConfigImportResult importData(final String namespace, final List discoveryList, final ConfigsImportContext context) { if (CollectionUtils.isEmpty(discoveryList)) { return ConfigImportResult.success(); } - + Map discoveryHandlerIdMapping = context.getDiscoveryHandlerIdMapping(); Map> pluginDiscoveryMap = discoveryMapper .selectAllByNamespaceId(namespace) .stream() @@ -478,15 +479,20 @@ public ConfigImportResult importData(final String namespace, final List discoveryRelDO.setSelectorId(context.getSelectorIdMapping().get(selectorId))); + Optional.ofNullable(discoveryRelDO.getProxySelectorId()) + .ifPresent(proxySelectorId -> discoveryRelDO.setProxySelectorId(context.getProxySelectorIdMapping().get(proxySelectorId))); + discoveryRelDO.setId(UUIDUtils.getInstance().generateShortUuid()); discoveryRelMapper.insertSelective(discoveryRelDO); } } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryUpstreamServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryUpstreamServiceImpl.java index 73789d900922..c31ae18023c4 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryUpstreamServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DiscoveryUpstreamServiceImpl.java @@ -39,6 +39,7 @@ import org.apache.shenyu.admin.model.result.ConfigImportResult; import org.apache.shenyu.admin.model.vo.DiscoveryUpstreamVO; import org.apache.shenyu.admin.service.DiscoveryUpstreamService; +import org.apache.shenyu.admin.service.configs.ConfigsImportContext; import org.apache.shenyu.admin.transfer.DiscoveryTransfer; import org.apache.shenyu.admin.utils.ShenyuResultMessage; import org.apache.shenyu.common.dto.DiscoverySyncData; @@ -289,10 +290,11 @@ public ConfigImportResult importData(final List discoveryU } @Override - public ConfigImportResult importData(final String namespace, final List discoveryUpstreamList) { + public ConfigImportResult importData(final String namespace, final List discoveryUpstreamList, final ConfigsImportContext context) { if (CollectionUtils.isEmpty(discoveryUpstreamList)) { return ConfigImportResult.success(); } + Map discoveryHandlerIdMapping = context.getDiscoveryHandlerIdMapping(); int successCount = 0; StringBuilder errorMsgBuilder = new StringBuilder(); Map> discoveryHandlerUpstreamMap = discoveryUpstreamMapper @@ -313,7 +315,9 @@ public ConfigImportResult importData(final String namespace, final List ids, final Boolean e public void syncData() { List all = metaDataMapper.findAll(); if (CollectionUtils.isNotEmpty(all)) { - eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.META_DATA, DataEventTypeEnum.REFRESH, MetaDataTransfer.INSTANCE.mapToDataAll(all))); + Map> namespaceMetaDataList = all.stream().collect(Collectors.groupingBy(MetaDataDO::getNamespaceId)); + namespaceMetaDataList.values().forEach(m -> { + eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.META_DATA, DataEventTypeEnum.REFRESH, MetaDataTransfer.INSTANCE.mapToDataAll(m))); + }); } } @@ -241,6 +244,7 @@ public ConfigImportResult importData(final List metaDataList) { } @Override + @Transactional(rollbackFor = Exception.class) public ConfigImportResult importData(final String namespace, final List metaDataList) { if (CollectionUtils.isEmpty(metaDataList)) { return ConfigImportResult.success(); @@ -263,10 +267,10 @@ public ConfigImportResult importData(final String namespace, final List activePluginSnapshot(final String namespaceId) { } @Override - public ConfigImportResult importData(final String namespace, final List namespacePluginList) { + @Transactional(rollbackFor = Exception.class) + public ConfigImportResult importData(final String namespace, final List namespacePluginList, final ConfigsImportContext context) { if (CollectionUtils.isEmpty(namespacePluginList)) { return ConfigImportResult.success(); } @@ -243,13 +246,16 @@ public ConfigImportResult importData(final String namespace, final List 0) { // publish create event. init plugin data diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginHandleServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginHandleServiceImpl.java index d89cc87bec6d..004faf208e9f 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginHandleServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginHandleServiceImpl.java @@ -29,10 +29,11 @@ import org.apache.shenyu.admin.model.page.CommonPager; import org.apache.shenyu.admin.model.page.PageResultUtils; import org.apache.shenyu.admin.model.query.PluginHandleQuery; -import org.apache.shenyu.admin.model.result.ShenyuAdminResult; +import org.apache.shenyu.admin.model.result.ConfigImportResult; import org.apache.shenyu.admin.model.vo.PluginHandleVO; import org.apache.shenyu.admin.model.vo.ShenyuDictVO; import org.apache.shenyu.admin.service.PluginHandleService; +import org.apache.shenyu.admin.service.configs.ConfigsImportContext; import org.apache.shenyu.admin.service.publish.PluginHandleEventPublisher; import org.apache.shenyu.common.utils.ListUtil; import org.slf4j.Logger; @@ -145,10 +146,10 @@ public List listAllDataByPluginIds(final Collection plug } return buildPluginHandleVO(pluginHandleDOList); } - + @Override @Transactional(rollbackFor = Exception.class) - public ShenyuAdminResult importData(final List pluginHandleList) { + public ConfigImportResult importData(final List pluginHandleList, final ConfigsImportContext context) { Map> existHandleMap = listAllData() .stream() .filter(Objects::nonNull) @@ -158,9 +159,10 @@ public ShenyuAdminResult importData(final List pluginHandleList .filter(Objects::nonNull) .collect(Collectors.groupingBy(PluginHandleDTO::getPluginId)); + int successCount = 0; for (Map.Entry> pluginHandleEntry : importHandleMap.entrySet()) { // pluginId - String pluginId = pluginHandleEntry.getKey(); + String pluginId = context.getPluginTemplateIdMapping().get(pluginHandleEntry.getKey()); List handles = pluginHandleEntry.getValue(); if (CollectionUtils.isNotEmpty(handles)) { if (existHandleMap.containsKey(pluginId)) { @@ -171,19 +173,23 @@ public ShenyuAdminResult importData(final List pluginHandleList .collect(Collectors.toSet()); for (PluginHandleDTO handle : handles) { if (!handleFiledMap.contains(handle.getField())) { + handle.setPluginId(pluginId); create(handle); + successCount++; } } } else { for (PluginHandleDTO handle : handles) { + handle.setPluginId(pluginId); create(handle); + successCount++; } } } } - return ShenyuAdminResult.success(); + return ConfigImportResult.success(successCount); } - + /** * The associated Handle needs to be deleted synchronously. * diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java index d11abe454d0e..b8675d3619c3 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/PluginServiceImpl.java @@ -17,16 +17,12 @@ package org.apache.shenyu.admin.service.impl; -import com.google.common.collect.Maps; import org.apache.commons.collections4.CollectionUtils; -import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.shenyu.admin.aspect.annotation.Pageable; import org.apache.shenyu.admin.mapper.NamespacePluginRelMapper; import org.apache.shenyu.admin.mapper.PluginMapper; import org.apache.shenyu.admin.model.dto.PluginDTO; -import org.apache.shenyu.admin.model.dto.PluginHandleDTO; -import org.apache.shenyu.admin.model.entity.NamespacePluginRelDO; import org.apache.shenyu.admin.model.entity.PluginDO; import org.apache.shenyu.admin.model.event.plugin.PluginCreatedEvent; import org.apache.shenyu.admin.model.page.CommonPager; @@ -38,6 +34,7 @@ import org.apache.shenyu.admin.model.vo.PluginVO; import org.apache.shenyu.admin.service.PluginHandleService; import org.apache.shenyu.admin.service.PluginService; +import org.apache.shenyu.admin.service.configs.ConfigsImportContext; import org.apache.shenyu.admin.service.publish.PluginEventPublisher; import org.apache.shenyu.admin.transfer.PluginTransfer; import org.apache.shenyu.admin.utils.Assert; @@ -50,6 +47,7 @@ import org.apache.shenyu.common.utils.JarDependencyUtils; import org.apache.shenyu.common.utils.ListUtil; import org.apache.shenyu.common.utils.LogUtils; +import org.apache.shenyu.common.utils.UUIDUtils; import org.opengauss.util.Base64; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -251,7 +249,7 @@ public List activePluginSnapshot() { @Override @Transactional(rollbackFor = Exception.class) - public ConfigImportResult importData(final List pluginList) { + public ConfigImportResult importData(final List pluginList, final ConfigsImportContext context) { if (CollectionUtils.isEmpty(pluginList)) { return ConfigImportResult.success(); } @@ -268,7 +266,11 @@ public ConfigImportResult importData(final List pluginList) { errorMsgBuilder .append(pluginName) .append(","); + Optional.ofNullable(context).ifPresent(c -> c.getPluginTemplateIdMapping().put(pluginDTO.getId(), existPluginMap.get(pluginName).getId())); } else { + String pluginId = UUIDUtils.getInstance().generateShortUuid(); + Optional.ofNullable(context).ifPresent(c -> c.getPluginTemplateIdMapping().put(pluginDTO.getId(), pluginId)); + pluginDTO.setId(pluginId); PluginDO pluginDO = PluginDO.buildPluginDO(pluginDTO); if (pluginMapper.insertSelective(pluginDO) > 0) { // publish create event. init plugin data @@ -284,58 +286,6 @@ public ConfigImportResult importData(final List pluginList) { return ConfigImportResult.success(successCount); } - @Override - public ConfigImportResult importData(final String namespace, final List pluginList) { - if (CollectionUtils.isEmpty(pluginList)) { - return ConfigImportResult.success(); - } - List pluginRelDOList = namespacePluginRelMapper.listByNamespaceId(namespace); - Map existPluginMap = Maps.newHashMap(); - if (CollectionUtils.isNotEmpty(pluginRelDOList)) { - List pluginIds = pluginRelDOList.stream().map(NamespacePluginRelDO::getPluginId).distinct().collect(Collectors.toList()); - existPluginMap = pluginMapper.selectByIds(pluginIds) - .stream() - .filter(Objects::nonNull) - .collect(Collectors.toMap(PluginDO::getName, x -> x)); - } - StringBuilder errorMsgBuilder = new StringBuilder(); - int successCount = 0; - for (PluginDTO pluginDTO : pluginList) { - String pluginName = pluginDTO.getName(); - String pluginId; - // check plugin base info - if (MapUtils.isNotEmpty(existPluginMap) && existPluginMap.containsKey(pluginName)) { - PluginDO existPlugin = existPluginMap.get(pluginName); - pluginId = existPlugin.getId(); - errorMsgBuilder - .append(pluginName) - .append(","); - } else { - PluginDO pluginDO = PluginDO.buildPluginDO(pluginDTO); - pluginId = pluginDO.getId(); - if (pluginMapper.insertSelective(pluginDO) > 0) { - // publish create event. init plugin data - successCount++; - } - } - // check and import plugin handle - List pluginHandleList = pluginDTO.getPluginHandleList(); - if (CollectionUtils.isNotEmpty(pluginHandleList)) { - pluginHandleService - .importData(pluginHandleList - .stream() - .peek(x -> x.setPluginId(pluginId)) - .collect(Collectors.toList())); - } - } - if (StringUtils.isNotEmpty(errorMsgBuilder)) { - errorMsgBuilder.setLength(errorMsgBuilder.length() - 1); - return ConfigImportResult - .fail(successCount, "import fail plugin: " + errorMsgBuilder); - } - return ConfigImportResult.success(successCount); - } - /** * create plugin.
* insert plugin and insert plugin data. diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ProxySelectorServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ProxySelectorServiceImpl.java index df7e5aaaf469..6272cc1480bb 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ProxySelectorServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/ProxySelectorServiceImpl.java @@ -46,6 +46,7 @@ import org.apache.shenyu.admin.model.vo.DiscoveryUpstreamVO; import org.apache.shenyu.admin.model.vo.ProxySelectorVO; import org.apache.shenyu.admin.service.ProxySelectorService; +import org.apache.shenyu.admin.service.configs.ConfigsImportContext; import org.apache.shenyu.admin.transfer.DiscoveryTransfer; import org.apache.shenyu.admin.utils.ShenyuResultMessage; import org.apache.shenyu.common.dto.ProxySelectorData; @@ -498,13 +499,14 @@ public ConfigImportResult importData(final List proxySelector } @Override - public ConfigImportResult importData(final String namespace, final List proxySelectorList) { + public ConfigImportResult importData(final String namespace, final List proxySelectorList, + final ConfigsImportContext context) { if (CollectionUtils.isEmpty(proxySelectorList)) { return ConfigImportResult.success(); } - // TODO namespace + Map proxySelectorIdMapping = context.getProxySelectorIdMapping(); Map> pluginProxySelectorMap = proxySelectorMapper - .selectAll() + .selectByNamespaceId(namespace) .stream() .collect(Collectors.groupingBy(ProxySelectorDO::getPluginName)); int successCount = 0; @@ -512,20 +514,25 @@ public ConfigImportResult importData(final String namespace, final List existProxySelectorNameSet = pluginProxySelectorMap + Map existProxySelectorNameSet = pluginProxySelectorMap .getOrDefault(pluginName, Lists.newArrayList()) .stream() - .map(ProxySelectorDO::getName) - .collect(Collectors.toSet()); + .collect(Collectors.toMap(ProxySelectorDO::getName, ProxySelectorDO::getId)); - if (existProxySelectorNameSet.contains(proxySelectorName)) { + if (existProxySelectorNameSet.containsKey(proxySelectorName)) { errorMsgBuilder .append(proxySelectorName) .append(","); + proxySelectorIdMapping.put(selectorData.getId(), existProxySelectorNameSet.get(proxySelectorName)); continue; } + String oldProxySelectorId = selectorData.getId(); + String newProxySelectorId = UUIDUtils.getInstance().generateShortUuid(); + selectorData.setId(newProxySelectorId); + selectorData.setNamespaceId(namespace); ProxySelectorDO proxySelectorDO = ProxySelectorDO.buildProxySelectorDO(selectorData); if (proxySelectorMapper.insert(proxySelectorDO) > 0) { + proxySelectorIdMapping.put(oldProxySelectorId, newProxySelectorId); successCount++; } } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/RuleServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/RuleServiceImpl.java index aa012c07e054..7426e17b95c9 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/RuleServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/RuleServiceImpl.java @@ -43,6 +43,7 @@ import org.apache.shenyu.admin.model.vo.RuleConditionVO; import org.apache.shenyu.admin.model.vo.RuleVO; import org.apache.shenyu.admin.service.RuleService; +import org.apache.shenyu.admin.service.configs.ConfigsImportContext; import org.apache.shenyu.admin.service.publish.RuleEventPublisher; import org.apache.shenyu.admin.transfer.ConditionTransfer; import org.apache.shenyu.admin.utils.Assert; @@ -53,6 +54,7 @@ import org.apache.shenyu.common.enums.MatchModeEnum; import org.apache.shenyu.common.utils.JsonUtils; import org.apache.shenyu.common.utils.ListUtil; +import org.apache.shenyu.common.utils.UUIDUtils; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -302,11 +304,12 @@ public ConfigImportResult importData(final List ruleList) { } @Override - public ConfigImportResult importData(final String namespace, final List ruleList) { + @Transactional(rollbackFor = Exception.class) + public ConfigImportResult importData(final String namespace, final List ruleList, final ConfigsImportContext context) { if (CollectionUtils.isEmpty(ruleList)) { return ConfigImportResult.success(); } - + Map selectorIdMapping = context.getSelectorIdMapping(); Map> selectorRuleMap = ruleMapper .selectAllByNamespaceId(namespace) .stream() @@ -317,8 +320,16 @@ public ConfigImportResult importData(final String namespace, final List for (RuleDTO ruleDTO : ruleList) { String selectorId = ruleDTO.getSelectorId(); String ruleName = ruleDTO.getName(); + + String newSelectorId = selectorIdMapping.get(selectorId); + if (Objects.isNull(newSelectorId)) { + errorMsgBuilder + .append(ruleName) + .append(","); + continue; + } Set existRuleNameSet = selectorRuleMap - .getOrDefault(selectorId, Lists.newArrayList()) + .getOrDefault(newSelectorId, Lists.newArrayList()) .stream() .map(RuleDO::getName) .collect(Collectors.toSet()); @@ -329,8 +340,17 @@ public ConfigImportResult importData(final String namespace, final List .append(","); continue; } + ruleDTO.setNamespaceId(namespace); + ruleDTO.setSelectorId(newSelectorId); + String ruleId = UUIDUtils.getInstance().generateShortUuid(); + ruleDTO.setId(ruleId); RuleDO ruleDO = RuleDO.buildRuleDO(ruleDTO); final int ruleCount = ruleMapper.insertSelective(ruleDO); + Optional.ofNullable(ruleDTO.getRuleConditions()) + .orElse(Collections.emptyList()).forEach(c -> { + c.setRuleId(ruleId); + c.setId(null); + }); addCondition(ruleDO, ruleDTO.getRuleConditions()); if (ruleCount > 0) { successCount++; diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SelectorServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SelectorServiceImpl.java index e1a6bae00638..11abc1a77b46 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SelectorServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SelectorServiceImpl.java @@ -58,6 +58,7 @@ import org.apache.shenyu.admin.model.vo.SelectorConditionVO; import org.apache.shenyu.admin.model.vo.SelectorVO; import org.apache.shenyu.admin.service.SelectorService; +import org.apache.shenyu.admin.service.configs.ConfigsImportContext; import org.apache.shenyu.admin.service.publish.SelectorEventPublisher; import org.apache.shenyu.admin.transfer.ConditionTransfer; import org.apache.shenyu.admin.transfer.DiscoveryTransfer; @@ -73,6 +74,7 @@ import org.apache.shenyu.common.utils.ContextPathUtils; import org.apache.shenyu.common.utils.JsonUtils; import org.apache.shenyu.common.utils.ListUtil; +import org.apache.shenyu.common.utils.UUIDUtils; import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO; import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.event.EventListener; @@ -495,10 +497,12 @@ public ConfigImportResult importData(final List selectorList) { } @Override - public ConfigImportResult importData(final String namespace, final List selectorList) { + @Transactional(rollbackFor = Exception.class) + public ConfigImportResult importData(final String namespace, final List selectorList, final ConfigsImportContext context) { if (CollectionUtils.isEmpty(selectorList)) { return ConfigImportResult.success(); } + Map selectorIdMapping = context.getSelectorIdMapping(); StringBuilder errorMsgBuilder = new StringBuilder(); int successCount = 0; Map> pluginSelectorMap = selectorMapper.selectAllByNamespaceId(namespace).stream() @@ -510,26 +514,37 @@ public ConfigImportResult importData(final String namespace, final List> selectorEntry : importSelectorMap.entrySet()) { // the import selector's pluginId - String pluginId = selectorEntry.getKey(); + String pluginId = context.getPluginTemplateIdMapping().get(selectorEntry.getKey()); List selectorDTOList = selectorEntry.getValue(); if (CollectionUtils.isNotEmpty(selectorDTOList)) { - Set existSelectorSet = Optional + Map existSelectorSet = Optional .ofNullable(pluginSelectorMap.get(pluginId)) .orElseGet(Lists::newArrayList) .stream() - .map(SelectorDO::getName) - .collect(Collectors.toSet()); + .collect(Collectors.toMap(SelectorDO::getName, SelectorDO::getId)); for (SelectorDTO selectorDTO : selectorDTOList) { // filter by selectorName String selectorName = selectorDTO.getName(); - if (CollectionUtils.isNotEmpty(existSelectorSet) - && existSelectorSet.contains(selectorName)) { + if (MapUtils.isNotEmpty(existSelectorSet) + && existSelectorSet.containsKey(selectorName)) { errorMsgBuilder .append(selectorName) .append(","); + selectorIdMapping.put(selectorDTO.getId(), existSelectorSet.get(selectorName)); } else { + // gen new id + String selectorId = UUIDUtils.getInstance().generateShortUuid(); + selectorIdMapping.put(selectorDTO.getId(), selectorId); + selectorDTO.setId(selectorId); + selectorDTO.setNamespaceId(namespace); + selectorDTO.setPluginId(pluginId); + Optional.ofNullable(selectorDTO.getSelectorConditions()) + .orElse(Collections.emptyList()).forEach(c -> { + c.setSelectorId(selectorId); + c.setId(null); + }); create(selectorDTO); successCount++; } diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SyncDataServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SyncDataServiceImpl.java index f6d21a785b69..44e7c4750c01 100644 --- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SyncDataServiceImpl.java +++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SyncDataServiceImpl.java @@ -42,6 +42,7 @@ import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; @@ -108,13 +109,25 @@ public boolean syncAll(final DataEventTypeEnum type) { appAuthService.syncData(); List pluginDataList = namespacePluginService.listAll(); - eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.PLUGIN, type, pluginDataList)); + Map> namespacePluginDataList = + pluginDataList.stream().collect(Collectors.groupingBy(PluginData::getNamespaceId)); + namespacePluginDataList.values().forEach(p -> { + eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.PLUGIN, type, p)); + }); List selectorDataList = selectorService.listAll(); - eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.SELECTOR, type, selectorDataList)); + Map> namespaceSelectorDataList = + selectorDataList.stream().collect(Collectors.groupingBy(SelectorData::getNamespaceId)); + namespaceSelectorDataList.values().forEach(s -> { + eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.SELECTOR, type, s)); + }); List ruleDataList = ruleService.listAll(); - eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.RULE, type, ruleDataList)); + Map> namespaceRuleDataList = + ruleDataList.stream().collect(Collectors.groupingBy(RuleData::getNamespaceId)); + namespaceRuleDataList.values().forEach(r -> { + eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.RULE, type, r)); + }); metaDataService.syncData(); discoveryService.syncData(); diff --git a/shenyu-admin/src/main/resources/mappers/meta-data-sqlmap.xml b/shenyu-admin/src/main/resources/mappers/meta-data-sqlmap.xml index 91a2fb6edbc3..6f73e725d742 100644 --- a/shenyu-admin/src/main/resources/mappers/meta-data-sqlmap.xml +++ b/shenyu-admin/src/main/resources/mappers/meta-data-sqlmap.xml @@ -148,6 +148,7 @@ SElECT true FROM meta_data WHERE path = #{path} + AND namespace_id = #{namespaceId} LIMIT 1 diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/AppAuthServiceTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/AppAuthServiceTest.java index a7465afc5484..114126e1c450 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/AppAuthServiceTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/AppAuthServiceTest.java @@ -258,7 +258,9 @@ public void testImportData() { @Test public void testSyncData() { - ArrayList all = Lists.newArrayList(new AppAuthDO()); + AppAuthDO authDO = new AppAuthDO(); + authDO.setNamespaceId("test"); + ArrayList all = Lists.newArrayList(authDO); when(appAuthMapper.selectAll()) .thenReturn(null) .thenReturn(Lists.newArrayList()) diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ConfigsServiceTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ConfigsServiceTest.java index 7348ab7928bc..b8023af730f8 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ConfigsServiceTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/ConfigsServiceTest.java @@ -27,10 +27,10 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import java.util.Collections; import java.util.List; import java.util.Map; @@ -45,7 +45,6 @@ @ExtendWith(MockitoExtension.class) public final class ConfigsServiceTest { - @InjectMocks private ConfigsServiceImpl configsService; @Mock @@ -84,7 +83,7 @@ public final class ConfigsServiceTest { @BeforeEach public void setUp() { configsService = new ConfigsServiceImpl(appAuthService, pluginService, namespacePluginService, pluginHandleService, selectorService, ruleService, - metaDataService, shenyuDictService, proxySelectorService, discoveryService, discoveryUpstreamService); + metaDataService, shenyuDictService, proxySelectorService, discoveryService, discoveryUpstreamService, Collections.emptyList()); } @Test @@ -102,7 +101,7 @@ public void testConfigsImport() { when(this.metaDataService.importData(any())).thenReturn( ConfigImportResult.success(1)); - when(this.pluginService.importData(any())).thenReturn( + when(this.pluginService.importData(any(), any())).thenReturn( ConfigImportResult.success(1)); when(this.selectorService.importData(any())).thenReturn( diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/MetaDataServiceTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/MetaDataServiceTest.java index fe2fe759b213..0860090dff2a 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/MetaDataServiceTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/MetaDataServiceTest.java @@ -158,7 +158,7 @@ public void testEnabled() { */ @Test public void testSyncDate() { - ArrayList all = Lists.newArrayList(MetaDataDO.builder().build()); + ArrayList all = Lists.newArrayList(MetaDataDO.builder().namespaceId("test").build()); when(metaDataMapper.findAll()) .thenReturn(null) .thenReturn(Lists.newArrayList()) @@ -256,7 +256,7 @@ public void testImportData() { final List metaDataDTOList = getMetaDataDTOList(); given(this.metaDataMapper.insert(any())).willReturn(1); - given(this.metaDataMapper.pathExisted(any())).willReturn(null); + given(this.metaDataMapper.pathExisted(any(), any())).willReturn(null); ConfigImportResult configImportResult = this.metaDataService.importData(metaDataDTOList); @@ -324,7 +324,7 @@ private void testSaveOrUpdateMetaDataForUpdate() { private void testCreateOrUpdateForInsert() { when(metaDataDTO.getId()).thenReturn(null); when(metaDataMapper.insert(any())).thenReturn(1); - when(metaDataMapper.pathExisted(any())).thenReturn(null); + when(metaDataMapper.pathExisted(any(), any())).thenReturn(null); String msg = metaDataService.createOrUpdate(metaDataDTO); assertEquals(ShenyuResultMessage.CREATE_SUCCESS, msg); } diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/PluginServiceTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/PluginServiceTest.java index a1b9ec1081ca..531d779b7d3a 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/PluginServiceTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/PluginServiceTest.java @@ -200,7 +200,7 @@ public void testImportData() { when(pluginMapper.nameExisted(pluginDTO.getName())).thenReturn(null); when(pluginMapper.insert(any())).thenReturn(1); - ConfigImportResult configImportResult = this.pluginService.importData(pluginDTOList); + ConfigImportResult configImportResult = this.pluginService.importData(pluginDTOList, null); assertNotNull(configImportResult); Assertions.assertEquals(configImportResult.getSuccessCount(), pluginDTOList.size()); diff --git a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/SyncDataServiceTest.java b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/SyncDataServiceTest.java index 351002c96fd9..60cafb9abe7e 100644 --- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/SyncDataServiceTest.java +++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/SyncDataServiceTest.java @@ -154,6 +154,7 @@ private SelectorData buildSelectorData() { selectorData.setPluginName("divide"); selectorData.setSort(1); selectorData.setType(1); + selectorData.setNamespaceId("test"); ConditionData conditionData = new ConditionData(); conditionData.setParamType(ParamTypeEnum.POST.getName()); conditionData.setOperator(OperatorEnum.EQ.getAlias()); @@ -179,6 +180,7 @@ private RuleData buildRuleData() { ruleData.setPluginName("divide"); ruleData.setSelectorId("1"); ruleData.setSort(1); + ruleData.setNamespaceId("test"); ConditionData conditionData = new ConditionData(); conditionData.setParamType(ParamTypeEnum.POST.getName()); conditionData.setOperator(OperatorEnum.EQ.getAlias()); diff --git a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/ExportImportConstants.java b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/ExportImportConstants.java index 5d8022e93c77..830cf02c4c49 100644 --- a/shenyu-common/src/main/java/org/apache/shenyu/common/constant/ExportImportConstants.java +++ b/shenyu-common/src/main/java/org/apache/shenyu/common/constant/ExportImportConstants.java @@ -54,7 +54,7 @@ public final class ExportImportConstants { /** * plugin handle json config name. */ - public static final String PLUGIN_HANDLE_JSON = "proxy_handle.json"; + public static final String PLUGIN_HANDLE_JSON = "plugin_handle.json"; /** * selector json config name. @@ -101,11 +101,31 @@ public final class ExportImportConstants { */ public static final String PLUGIN_IMPORT_SUCCESS_COUNT = "pluginImportSuccessCount"; + /** + * plugin handle import success count. + */ + public static final String PLUGIN_HANDLE_IMPORT_SUCCESS_COUNT = "pluginHandleImportSuccessCount"; + + /** + * namespace plugin import success count. + */ + public static final String NAMESPACE_PLUGIN_IMPORT_SUCCESS_COUNT = "namespacePluginImportSuccessCount"; + /** * plugin import fail message. */ public static final String PLUGIN_IMPORT_FAIL_MESSAGE = "pluginImportFailMessage"; + /** + * plugin handle import fail message. + */ + public static final String PLUGIN_HANDLE_IMPORT_FAIL_MESSAGE = "pluginHandleImportFailMessage"; + + /** + * namespace plugin import fail message. + */ + public static final String NAMESPACE_PLUGIN_IMPORT_FAIL_MESSAGE = "pluginHandleImportFailMessage"; + /** * selector import success count. */