24 changed files with 467 additions and 208 deletions
@ -1,102 +0,0 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.sync.vc; |
|||
|
|||
import lombok.RequiredArgsConstructor; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.cache.TbTransactionalCache; |
|||
import org.thingsboard.server.common.data.AdminSettings; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.sync.vc.EntitiesVersionControlSettings; |
|||
import org.thingsboard.server.common.data.sync.vc.VersionControlAuthMethod; |
|||
import org.thingsboard.server.dao.settings.AdminSettingsService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
@RequiredArgsConstructor |
|||
public class DefaultTbVersionControlSettingsService implements TbVersionControlSettingsService { |
|||
|
|||
public static final String SETTINGS_KEY = "entitiesVersionControl"; |
|||
private final AdminSettingsService adminSettingsService; |
|||
private final TbTransactionalCache<TenantId, EntitiesVersionControlSettings> cache; |
|||
|
|||
@Override |
|||
public EntitiesVersionControlSettings restore(TenantId tenantId, EntitiesVersionControlSettings settings) { |
|||
EntitiesVersionControlSettings storedSettings = get(tenantId); |
|||
if (storedSettings != null) { |
|||
VersionControlAuthMethod authMethod = settings.getAuthMethod(); |
|||
if (VersionControlAuthMethod.USERNAME_PASSWORD.equals(authMethod) && settings.getPassword() == null) { |
|||
settings.setPassword(storedSettings.getPassword()); |
|||
} else if (VersionControlAuthMethod.PRIVATE_KEY.equals(authMethod) && settings.getPrivateKey() == null) { |
|||
settings.setPrivateKey(storedSettings.getPrivateKey()); |
|||
if (settings.getPrivateKeyPassword() == null) { |
|||
settings.setPrivateKeyPassword(storedSettings.getPrivateKeyPassword()); |
|||
} |
|||
} |
|||
} |
|||
return settings; |
|||
} |
|||
|
|||
@Override |
|||
public EntitiesVersionControlSettings get(TenantId tenantId) { |
|||
EntitiesVersionControlSettings settings = cache.getAndPutInTransaction(tenantId, () -> { |
|||
AdminSettings adminSettings = adminSettingsService.findAdminSettingsByKey(tenantId, SETTINGS_KEY); |
|||
if (adminSettings != null) { |
|||
try { |
|||
return JacksonUtil.convertValue(adminSettings.getJsonValue(), EntitiesVersionControlSettings.class); |
|||
} catch (Exception e) { |
|||
throw new RuntimeException("Failed to load version control settings!", e); |
|||
} |
|||
} |
|||
return null; |
|||
}, true); |
|||
if (settings != null) { |
|||
settings = new EntitiesVersionControlSettings(settings); |
|||
} |
|||
return settings; |
|||
} |
|||
|
|||
@Override |
|||
public EntitiesVersionControlSettings save(TenantId tenantId, EntitiesVersionControlSettings versionControlSettings) { |
|||
AdminSettings adminSettings = adminSettingsService.findAdminSettingsByKey(tenantId, SETTINGS_KEY); |
|||
if (adminSettings == null) { |
|||
adminSettings = new AdminSettings(); |
|||
adminSettings.setKey(SETTINGS_KEY); |
|||
adminSettings.setTenantId(tenantId); |
|||
} |
|||
adminSettings.setJsonValue(JacksonUtil.valueToTree(versionControlSettings)); |
|||
AdminSettings savedAdminSettings = adminSettingsService.saveAdminSettings(tenantId, adminSettings); |
|||
EntitiesVersionControlSettings savedVersionControlSettings; |
|||
try { |
|||
savedVersionControlSettings = JacksonUtil.convertValue(savedAdminSettings.getJsonValue(), EntitiesVersionControlSettings.class); |
|||
} catch (Exception e) { |
|||
throw new RuntimeException("Failed to load version control settings!", e); |
|||
} |
|||
//API calls to adminSettingsService are not in transaction, so we can simply evict the cache.
|
|||
cache.evict(tenantId); |
|||
return savedVersionControlSettings; |
|||
} |
|||
|
|||
@Override |
|||
public boolean delete(TenantId tenantId) { |
|||
boolean result = adminSettingsService.deleteAdminSettings(tenantId, SETTINGS_KEY); |
|||
cache.evict(tenantId); |
|||
return result; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,80 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.sync.vc; |
|||
|
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.cache.TbTransactionalCache; |
|||
import org.thingsboard.server.common.data.AdminSettings; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.dao.settings.AdminSettingsService; |
|||
|
|||
import java.io.Serializable; |
|||
|
|||
public abstract class TbAbstractVersionControlSettingsService<T extends Serializable> { |
|||
|
|||
private final String settingsKey; |
|||
private final AdminSettingsService adminSettingsService; |
|||
private final TbTransactionalCache<TenantId, T> cache; |
|||
private final Class<T> clazz; |
|||
|
|||
public TbAbstractVersionControlSettingsService(AdminSettingsService adminSettingsService, TbTransactionalCache<TenantId, T> cache, Class<T> clazz, String settingsKey) { |
|||
this.adminSettingsService = adminSettingsService; |
|||
this.cache = cache; |
|||
this.clazz = clazz; |
|||
this.settingsKey = settingsKey; |
|||
} |
|||
|
|||
public T get(TenantId tenantId) { |
|||
return cache.getAndPutInTransaction(tenantId, () -> { |
|||
AdminSettings adminSettings = adminSettingsService.findAdminSettingsByKey(tenantId, settingsKey); |
|||
if (adminSettings != null) { |
|||
try { |
|||
return JacksonUtil.convertValue(adminSettings.getJsonValue(), clazz); |
|||
} catch (Exception e) { |
|||
throw new RuntimeException("Failed to load " + settingsKey + " settings!", e); |
|||
} |
|||
} |
|||
return null; |
|||
}, true); |
|||
} |
|||
|
|||
public T save(TenantId tenantId, T settings) { |
|||
AdminSettings adminSettings = adminSettingsService.findAdminSettingsByKey(tenantId, settingsKey); |
|||
if (adminSettings == null) { |
|||
adminSettings = new AdminSettings(); |
|||
adminSettings.setKey(settingsKey); |
|||
adminSettings.setTenantId(tenantId); |
|||
} |
|||
adminSettings.setJsonValue(JacksonUtil.valueToTree(settings)); |
|||
AdminSettings savedAdminSettings = adminSettingsService.saveAdminSettings(tenantId, adminSettings); |
|||
T savedSettings; |
|||
try { |
|||
savedSettings = JacksonUtil.convertValue(savedAdminSettings.getJsonValue(), clazz); |
|||
} catch (Exception e) { |
|||
throw new RuntimeException("Failed to load auto commit settings!", e); |
|||
} |
|||
//API calls to adminSettingsService are not in transaction, so we can simply evict the cache.
|
|||
cache.evict(tenantId); |
|||
return savedSettings; |
|||
} |
|||
|
|||
public boolean delete(TenantId tenantId) { |
|||
boolean result = adminSettingsService.deleteAdminSettings(tenantId, settingsKey); |
|||
cache.evict(tenantId); |
|||
return result; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.sync.vc.autocommit; |
|||
|
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.cache.TbTransactionalCache; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.sync.vc.AutoCommitSettings; |
|||
import org.thingsboard.server.dao.settings.AdminSettingsService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.sync.vc.TbAbstractVersionControlSettingsService; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
public class DefaultTbAutoCommitSettingsService extends TbAbstractVersionControlSettingsService<AutoCommitSettings> implements TbAutoCommitSettingsService { |
|||
|
|||
public static final String SETTINGS_KEY = "autoCommitSettings"; |
|||
|
|||
public DefaultTbAutoCommitSettingsService(AdminSettingsService adminSettingsService, TbTransactionalCache<TenantId, AutoCommitSettings> cache) { |
|||
super(adminSettingsService, cache, AutoCommitSettings.class, SETTINGS_KEY); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.sync.vc.autocommit; |
|||
|
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.sync.vc.AutoCommitSettings; |
|||
import org.thingsboard.server.common.data.sync.vc.RepositorySettings; |
|||
|
|||
public interface TbAutoCommitSettingsService { |
|||
|
|||
AutoCommitSettings get(TenantId tenantId); |
|||
|
|||
AutoCommitSettings save(TenantId tenantId, AutoCommitSettings settings); |
|||
|
|||
boolean delete(TenantId tenantId); |
|||
|
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.sync.vc.repository; |
|||
|
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.cache.TbTransactionalCache; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.sync.vc.RepositorySettings; |
|||
import org.thingsboard.server.common.data.sync.vc.VersionControlAuthMethod; |
|||
import org.thingsboard.server.dao.settings.AdminSettingsService; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.sync.vc.TbAbstractVersionControlSettingsService; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
public class DefaultTbRepositorySettingsService extends TbAbstractVersionControlSettingsService<RepositorySettings> implements TbRepositorySettingsService { |
|||
|
|||
public static final String SETTINGS_KEY = "entitiesVersionControl"; |
|||
|
|||
public DefaultTbRepositorySettingsService(AdminSettingsService adminSettingsService, TbTransactionalCache<TenantId, RepositorySettings> cache) { |
|||
super(adminSettingsService, cache, RepositorySettings.class, SETTINGS_KEY); |
|||
} |
|||
|
|||
@Override |
|||
public RepositorySettings restore(TenantId tenantId, RepositorySettings settings) { |
|||
RepositorySettings storedSettings = get(tenantId); |
|||
if (storedSettings != null) { |
|||
VersionControlAuthMethod authMethod = settings.getAuthMethod(); |
|||
if (VersionControlAuthMethod.USERNAME_PASSWORD.equals(authMethod) && settings.getPassword() == null) { |
|||
settings.setPassword(storedSettings.getPassword()); |
|||
} else if (VersionControlAuthMethod.PRIVATE_KEY.equals(authMethod) && settings.getPrivateKey() == null) { |
|||
settings.setPrivateKey(storedSettings.getPrivateKey()); |
|||
if (settings.getPrivateKeyPassword() == null) { |
|||
settings.setPrivateKeyPassword(storedSettings.getPrivateKeyPassword()); |
|||
} |
|||
} |
|||
} |
|||
return settings; |
|||
} |
|||
|
|||
@Override |
|||
public RepositorySettings get(TenantId tenantId) { |
|||
RepositorySettings settings = super.get(tenantId); |
|||
if (settings != null) { |
|||
settings = new RepositorySettings(settings); |
|||
} |
|||
return settings; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.sync.vc.repository; |
|||
|
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|||
import org.springframework.cache.CacheManager; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.cache.CaffeineTbTransactionalCache; |
|||
import org.thingsboard.server.common.data.CacheConstants; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.sync.vc.RepositorySettings; |
|||
|
|||
@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "caffeine", matchIfMissing = true) |
|||
@Service("RepositorySettingsCache") |
|||
public class RepositorySettingsCaffeineCache extends CaffeineTbTransactionalCache<TenantId, RepositorySettings> { |
|||
|
|||
public RepositorySettingsCaffeineCache(CacheManager cacheManager) { |
|||
super(cacheManager, CacheConstants.REPOSITORY_SETTINGS_CACHE); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.sync.vc.repository; |
|||
|
|||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
|||
import org.springframework.data.redis.connection.RedisConnectionFactory; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.server.cache.CacheSpecsMap; |
|||
import org.thingsboard.server.cache.RedisTbTransactionalCache; |
|||
import org.thingsboard.server.cache.TBRedisCacheConfiguration; |
|||
import org.thingsboard.server.cache.TbRedisSerializer; |
|||
import org.thingsboard.server.common.data.CacheConstants; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.sync.vc.RepositorySettings; |
|||
|
|||
@ConditionalOnProperty(prefix = "cache", value = "type", havingValue = "redis") |
|||
@Service("RepositorySettingsCache") |
|||
public class RepositorySettingsRedisCache extends RedisTbTransactionalCache<TenantId, RepositorySettings> { |
|||
|
|||
public RepositorySettingsRedisCache(TBRedisCacheConfiguration configuration, CacheSpecsMap cacheSpecsMap, RedisConnectionFactory connectionFactory) { |
|||
super(CacheConstants.REPOSITORY_SETTINGS_CACHE, cacheSpecsMap, connectionFactory, configuration, new TbRedisSerializer<>()); |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.common.data.sync.vc; |
|||
|
|||
import org.thingsboard.server.common.data.EntityType; |
|||
import org.thingsboard.server.common.data.sync.vc.request.create.AutoVersionCreateConfig; |
|||
|
|||
import java.util.HashMap; |
|||
|
|||
public class AutoCommitSettings extends HashMap<EntityType, AutoVersionCreateConfig> { |
|||
|
|||
private static final long serialVersionUID = -5757067601838792059L; |
|||
|
|||
} |
|||
Loading…
Reference in new issue