diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/control/service/DefaultControlService.java b/server/node/src/main/java/org/kaaproject/kaa/server/control/service/DefaultControlService.java index 8d3a9bb625..2043797bbf 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/control/service/DefaultControlService.java +++ b/server/node/src/main/java/org/kaaproject/kaa/server/control/service/DefaultControlService.java @@ -1059,20 +1059,25 @@ public void deleteProfileFilterRecord(String endpointProfileSchemaId, String ser */ @Override public FileData generateSdk(SdkProfileDto sdkProfile, SdkPlatform platform) throws ControlServiceException { + EndpointProfileSchemaDto profileSchema = profileService.findProfileSchemaByAppIdAndVersion(sdkProfile.getApplicationId(), sdkProfile.getProfileSchemaVersion()); if (profileSchema == null) { throw new NotFoundException("Profile schema not found!"); } + ConfigurationSchemaDto configurationSchema = configurationService.findConfSchemaByAppIdAndVersion(sdkProfile.getApplicationId(), sdkProfile.getConfigurationSchemaVersion()); if (configurationSchema == null) { throw new NotFoundException("Configuration schema not found!"); } + ConfigurationDto defaultConfiguration = configurationService.findDefaultConfigurationBySchemaId(configurationSchema.getId()); if (defaultConfiguration == null) { throw new NotFoundException("Default configuration not found!"); } + + NotificationSchemaDto notificationSchema = notificationService.findNotificationSchemaByAppIdAndTypeAndVersion( sdkProfile.getApplicationId(), NotificationTypeDto.USER, sdkProfile.getNotificationSchemaVersion()); if (notificationSchema == null) { @@ -1085,18 +1090,30 @@ public FileData generateSdk(SdkProfileDto sdkProfile, SdkPlatform platform) thro throw new NotFoundException("Log schema not found!"); } + CTLSchemaDto profileCtlSchema = ctlService.findCTLSchemaById(profileSchema.getCtlSchemaId()); if (profileCtlSchema == null) { throw new NotFoundException("Profile CTL schema not found!"); } + + CTLSchemaDto confCtlSchema = ctlService.findCTLSchemaById(configurationSchema.getCtlSchemaId()); + if (confCtlSchema == null) { + throw new NotFoundException("Configuration CTL schema not found!"); + } + + String profileSchemaBodyString = ctlService.flatExportAsString(profileCtlSchema); + String confSchemaBodyString = ctlService.flatExportAsString(confCtlSchema); DataSchema profileDataSchema = new DataSchema(profileSchemaBodyString); + DataSchema confDataSchema = new DataSchema(confSchemaBodyString); DataSchema notificationDataSchema = new DataSchema(notificationSchema.getSchema()); ProtocolSchema protocolSchema = new ProtocolSchema(configurationSchema.getProtocolSchema()); DataSchema logDataSchema = new DataSchema(logSchema.getSchema()); String profileSchemaBody = profileDataSchema.getRawSchema(); + String confSchemaBody = confDataSchema.getRawSchema(); + byte[] defaultConfigurationData = GenericAvroConverter.toRawData(defaultConfiguration.getBody(), configurationSchema.getBaseSchema()); @@ -1131,7 +1148,7 @@ public FileData generateSdk(SdkProfileDto sdkProfile, SdkPlatform platform) thro try { sdkFile = generator.generateSdk(Version.PROJECT_VERSION, controlZKService.getCurrentBootstrapNodes(), sdkProfile, profileSchemaBody, notificationDataSchema.getRawSchema(), protocolSchema.getRawSchema(), - configurationSchema.getBaseSchema(), defaultConfigurationData, eventFamilies, logDataSchema.getRawSchema()); + confSchemaBody, defaultConfigurationData, eventFamilies, logDataSchema.getRawSchema()); } catch (Exception e) { LOG.error("Unable to generate SDK", e); throw new ControlServiceException(e); diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/control/service/sdk/JavaSdkGenerator.java b/server/node/src/main/java/org/kaaproject/kaa/server/control/service/sdk/JavaSdkGenerator.java index 19cd574472..4cfad8adaf 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/control/service/sdk/JavaSdkGenerator.java +++ b/server/node/src/main/java/org/kaaproject/kaa/server/control/service/sdk/JavaSdkGenerator.java @@ -57,284 +57,171 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** - * The Class JavaSdkGenerator. - */ + public class JavaSdkGenerator extends SdkGenerator { - /** - * The Constant LOG. - */ + private static final Logger LOG = LoggerFactory.getLogger(JavaSdkGenerator.class); - /** - * The Constant JAVA_SDK_DIR. - */ + private static final String JAVA_SDK_DIR = "sdk/java"; - /** - * The Constant JAVA_SDK_PREFIX. - */ + private static final String JAVA_SDK_PREFIX = "kaa-java-ep-sdk-"; - /** - * The Constant ANDROID_SDK_DIR. - */ + private static final String ANDROID_SDK_DIR = "sdk/android"; - /** - * The Constant ANDROID_SDK_PREFIX. - */ + private static final String ANDROID_SDK_PREFIX = "kaa-android-ep-sdk-"; - /** - * The Constant CLIENT_PROPERTIES. - */ + private static final String CLIENT_PROPERTIES = "client.properties"; - /** - * The Constant BUILD_VERSION. - */ + private static final String BUILD_VERSION = "build.version"; - /** - * The Constant BUILD_COMMIT_HASH. - */ + private static final String BUILD_COMMIT_HASH = "build.commit_hash"; - /** - * The Constant BOOTSTRAP_SERVERS_PROPERTY. - */ + private static final String BOOTSTRAP_SERVERS_PROPERTY = "transport.bootstrap.servers"; - /** - * The Constant SDK_TOKEN_PROPERTY - */ + private static final String SDK_TOKEN_PROPERTY = "sdk_token"; - /** - * The Constant CONFIG_DATA_DEFAULT_PROPERTY. - */ + private static final String CONFIG_DATA_DEFAULT_PROPERTY = "config.data.default"; - /** - * The Constant CONFIG_SCHEMA_DEFAULT_PROPERTY. - */ + private static final String CONFIG_SCHEMA_DEFAULT_PROPERTY = "config.schema.default"; - /** - * The Constant KAA_CLIENT_SOURCE_TEMPLATE. - */ + private static final String KAA_CLIENT_SOURCE_TEMPLATE = "sdk/java/KaaClient.java.template"; - /** - * The Constant BASE_KAA_CLIENT_SOURCE_TEMPLATE. - */ + private static final String BASE_KAA_CLIENT_SOURCE_TEMPLATE = "sdk/java/BaseKaaClient.java.template"; - /** - * The Constant CONFIGURATION_MANAGER_SOURCE_TEMPLATE. - */ + private static final String CONFIGURATION_MANAGER_SOURCE_TEMPLATE = "sdk/java/cf/ConfigurationManager.java.template"; - /** - * The Constant CONFIGURATION_MANAGER_IMPL_SOURCE_TEMPLATE. - */ + private static final String CONFIGURATION_MANAGER_IMPL_SOURCE_TEMPLATE = "sdk/java/cf/ResyncConfigurationManager.java.template"; - /** - * The Constant CONFIGURATION_LISTENER_SOURCE_TEMPLATE. - */ + private static final String CONFIGURATION_LISTENER_SOURCE_TEMPLATE = "sdk/java/cf/ConfigurationListener.java.template"; - /** - * The Constant CONFIGURATION_DESERIALIZER_SOURCE_TEMPLATE. - */ + private static final String CONFIGURATION_DESERIALIZER_SOURCE_TEMPLATE = "sdk/java/cf/ConfigurationDeserializer.java.template"; - /** - * The Constant NOTIFICATION_LISTENER_SOURCE_TEMPLATE. - */ + private static final String NOTIFICATION_LISTENER_SOURCE_TEMPLATE = "sdk/java/nf/NotificationListener.java.template"; - /** - * The Constant NOTIFICATION_DESERIALIZER_SOURCE_TEMPLATE. - */ + private static final String NOTIFICATION_DESERIALIZER_SOURCE_TEMPLATE = "sdk/java/nf/NotificationDeserializer.java.template"; - /** - * The Constant PROFILE_CONTAINER_SOURCE_TEMPLATE. - */ + private static final String PROFILE_CONTAINER_SOURCE_TEMPLATE = "sdk/java/profile/ProfileContainer.java.template"; - /** - * The Constant PROFILE_SERIALIZER_SOURCE_TEMPLATE. - */ + private static final String PROFILE_SERIALIZER_SOURCE_TEMPLATE = "sdk/java/profile/ProfileSerializer.java.template"; - /** - * The Constant DEFAULT_PROFILE_SERIALIZER_SOURCE_TEMPLATE. - */ + private static final String DEFAULT_PROFILE_SERIALIZER_SOURCE_TEMPLATE = "sdk/java/profile/DefaultProfileSerializer.java.template"; - /** - * The Constant LOG_RECORD_SOURCE_TEMPLATE. - */ + private static final String LOG_RECORD_SOURCE_TEMPLATE = "sdk/java/log/LogRecord.java.template"; - /** - * The Constant LOG_COLLECTOR_INTERFACE_TEMPLATE. - */ + private static final String LOG_COLLECTOR_INTERFACE_TEMPLATE = "sdk/java/log/LogCollector.java.template"; - /** - * The Constant LOG_COLLECTOR_SOURCE_TEMPLATE. - */ + private static final String LOG_COLLECTOR_SOURCE_TEMPLATE = "sdk/java/log/DefaultLogCollector.java.template"; - /** - * The Constant USER_VERIFIER_CONSTANTS_SOURCE_TEMPLATE. - */ + private static final String USER_VERIFIER_CONSTANTS_SOURCE_TEMPLATE = "sdk/java/event/UserVerifierConstants.java.template"; - /** - * The Constant ABSTRACT_PROFILE_CONTAINER. - */ + private static final String PROFILE_CONTAINER = "ProfileContainer"; - /** - * The Constant ABSTRACT_PROFILE_CONTAINER. - */ + private static final String PROFILE_SERIALIZER = "ProfileSerializer"; - /** - * The Constant CONFIGURATION_MANAGER. - */ + private static final String CONFIGURATION_MANAGER = "ConfigurationManager"; - /** - * The Constant CONFIGURATION_MANAGER. - */ + private static final String CONFIGURATION_MANAGER_IMPL = "ResyncConfigurationManager"; - /** - * The Constant CONFIGURATION_LISTENER. - */ + private static final String CONFIGURATION_LISTENER = "ConfigurationListener"; - /** - * The Constant CONFIGURATION_DESERIALIZER. - */ + private static final String CONFIGURATION_DESERIALIZER = "ConfigurationDeserializer"; - /** - * The Constant NOTIFICATION_LISTENER. - */ + private static final String NOTIFICATION_LISTENER = "NotificationListener"; - /** - * The Constant NOTIFICATION_DESERIALIZER. - */ + private static final String NOTIFICATION_DESERIALIZER = "NotificationDeserializer"; - /** - * The Constant USER_VERIFIER_CONSTANTS. - */ + private static final String USER_VERIFIER_CONSTANTS = "UserVerifierConstants"; - /** - * The Constant DEFAULT_SCHEMA_VERSION. - */ + private static final int DEFAULT_SCHEMA_VERSION = 1; - /** - * The Constant DEFAULT_PROFILE_SCHEMA_VERSION. - */ + private static final int DEFAULT_PROFILE_SCHEMA_VERSION = 0; - /** - * The Constant KAA_CLIENT. - */ + private static final String KAA_CLIENT = "KaaClient"; - /** - * The Constant BASE_KAA_CLIENT. - */ + private static final String BASE_KAA_CLIENT = "BaseKaaClient"; - /** - * The Constant LOG_RECORD. - */ + private static final String LOG_RECORD = "LogRecord"; - /** - * The Constant LOG_COLLECTOR_INTERFACE. - */ + private static final String LOG_COLLECTOR_INTERFACE = "LogCollector"; - /** - * The Constant DEFAULT_LOG_COLLECTOR. - */ + private static final String LOG_COLLECTOR_SOURCE = "DefaultLogCollector"; - /** - * The Constant PROFILE_CLASS_PACKAGE_VAR. - */ + private static final String PROFILE_CLASS_PACKAGE_VAR = "\\$\\{profile_class_package\\}"; - /** - * The Constant PROFILE_CLASS_VAR. - */ + private static final String PROFILE_CLASS_VAR = "\\$\\{profile_class\\}"; - /** - * The Constant CONFIGURATION_CLASS_PACKAGE_VAR. - */ + private static final String CONFIGURATION_CLASS_PACKAGE_VAR = "\\$\\{configuration_class_package\\}"; - /** - * The Constant CONFIGURATION_CLASS_VAR. - */ + private static final String CONFIGURATION_CLASS_VAR = "\\$\\{configuration_class\\}"; - /** - * The Constant NOTIFICATION_CLASS_PACKAGE_VAR. - */ + private static final String NOTIFICATION_CLASS_PACKAGE_VAR = "\\$\\{notification_class_package\\}"; - /** - * The Constant NOTIFICATION_CLASS_VAR. - */ + private static final String NOTIFICATION_CLASS_VAR = "\\$\\{notification_class\\}"; - /** - * The Constant LOG_RECORD_CLASS_PACKAGE_VAR. - */ + private static final String LOG_RECORD_CLASS_PACKAGE_VAR = "\\$\\{log_record_class_package\\}"; - /** - * The Constant LOG_RECORD_CLASS_VAR. - */ + private static final String LOG_RECORD_CLASS_VAR = "\\$\\{log_record_class\\}"; - /** - * The Constant DEFAULT_USER_VERIFIER_TOKEN_VAR. - */ + private static final String DEFAULT_USER_VERIFIER_TOKEN_VAR = "\\$\\{default_user_verifier_token\\}"; - /** - * The Constant JAVA_SOURCE_COMPILER_RELEASE. - */ + private static final String JAVA_SOURCE_COMPILER_RELEASE = "7"; - - /** - * The Constant JAVA_TARGET_COMPILER_RELEASE. - */ + private static final String JAVA_TARGET_COMPILER_RELEASE = "7"; - /** - * The Constant random. - */ + private static final SecureRandom RANDOM = new SecureRandom(); private final SdkPlatform sdkPlatform; diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/operations/pojo/GetDeltaRequest.java b/server/node/src/main/java/org/kaaproject/kaa/server/operations/pojo/GetDeltaRequest.java index 659940c219..1c1472c35b 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/operations/pojo/GetDeltaRequest.java +++ b/server/node/src/main/java/org/kaaproject/kaa/server/operations/pojo/GetDeltaRequest.java @@ -28,50 +28,29 @@ */ public class GetDeltaRequest { - /** The application token. */ + private final String applicationToken; /** Supports only configuration resync delta encoded using base schema. */ private final boolean resyncOnly; - /** The configuration hash. */ + private final EndpointObjectHash configurationHash; - /** The endpoint profile. */ + private EndpointProfileDto endpointProfile; - - /** - * Instantiates a new delta request. - * - * @param applicationToken - * the application token - */ + + public GetDeltaRequest(String applicationToken) { this(applicationToken, null, true); } - - /** - * Instantiates a new delta request. - * - * @param applicationToken - * the application token - * @param configurationHash - * the configuration hash - */ + + public GetDeltaRequest(String applicationToken, EndpointObjectHash configurationHash){ this(applicationToken, configurationHash, true); } - /** - * Instantiates a new delta request. - * - * @param applicationToken - * the application token - * @param configurationHash - * the configuration hash - * @param resyncOnly - * the resync only - */ + public GetDeltaRequest(String applicationToken, EndpointObjectHash configurationHash, boolean resyncOnly) { super(); this.applicationToken = applicationToken; @@ -79,29 +58,17 @@ public GetDeltaRequest(String applicationToken, EndpointObjectHash configuration this.resyncOnly = resyncOnly; } - /** - * Gets the application token. - * - * @return the application token - */ + public String getApplicationToken() { return applicationToken; } - /** - * Gets the configuration hash. - * - * @return the configuration hash - */ + public EndpointObjectHash getConfigurationHash() { return configurationHash; } - /** - * Gets the endpoint profile. - * - * @return the endpoint profile - */ + public EndpointProfileDto getEndpointProfile() { return endpointProfile; } @@ -116,21 +83,12 @@ public boolean isResyncOnly() { return resyncOnly; } - /** - * Sets the endpoint profile. - * - * @param endpointProfile - * the new endpoint profile - */ + public void setEndpointProfile(EndpointProfileDto endpointProfile) { this.endpointProfile = endpointProfile; } - /** - * Checks if is first request. - * - * @return true, if is first request - */ + public boolean isFirstRequest() { return getConfigurationHash() == null || getConfigurationHash().getData() == null || getConfigurationHash().getData().length == 0; } diff --git a/server/node/src/main/java/org/kaaproject/kaa/server/operations/service/delta/DefaultDeltaService.java b/server/node/src/main/java/org/kaaproject/kaa/server/operations/service/delta/DefaultDeltaService.java index 385cc4f465..cae00bbb45 100644 --- a/server/node/src/main/java/org/kaaproject/kaa/server/operations/service/delta/DefaultDeltaService.java +++ b/server/node/src/main/java/org/kaaproject/kaa/server/operations/service/delta/DefaultDeltaService.java @@ -32,6 +32,7 @@ import org.kaaproject.kaa.common.dto.EndpointGroupStateDto; import org.kaaproject.kaa.common.dto.EndpointProfileDto; import org.kaaproject.kaa.common.dto.EndpointUserConfigurationDto; +import org.kaaproject.kaa.common.dto.ctl.CTLSchemaDto; import org.kaaproject.kaa.common.endpoint.security.MessageEncoderDecoder; import org.kaaproject.kaa.common.hash.EndpointObjectHash; import org.kaaproject.kaa.server.common.Base64Util; @@ -41,8 +42,10 @@ import org.kaaproject.kaa.server.common.core.algorithms.override.OverrideException; import org.kaaproject.kaa.server.common.core.configuration.BaseData; import org.kaaproject.kaa.server.common.core.configuration.OverrideData; +import org.kaaproject.kaa.server.common.core.configuration.RawData; import org.kaaproject.kaa.server.common.core.schema.BaseSchema; import org.kaaproject.kaa.server.common.core.schema.OverrideSchema; +import org.kaaproject.kaa.server.common.core.schema.RawSchema; import org.kaaproject.kaa.server.common.dao.ConfigurationService; import org.kaaproject.kaa.server.common.dao.EndpointService; import org.kaaproject.kaa.server.common.dao.UserConfigurationService; @@ -71,19 +74,17 @@ @Service public class DefaultDeltaService implements DeltaService { - /** The Constant LOG. */ private static final Logger LOG = LoggerFactory.getLogger(DefaultDeltaService.class); - /** The cache service. */ @Autowired private CacheService cacheService; - /** The configuration service. */ + @Autowired private ConfigurationService configurationService; - /** The user configuration service. */ + @Autowired private UserConfigurationService userConfigurationService; - /** The profile service. */ + @Autowired private EndpointService endpointService; @@ -91,7 +92,7 @@ public class DefaultDeltaService implements DeltaService { @Autowired private OverrideAlgorithmFactory configurationOverrideFactory; - /** The Constant ENDPOINT_GROUP_COMPARATOR. */ + private static final Comparator ENDPOINT_GROUP_COMPARATOR = new Comparator() { @Override @@ -211,9 +212,13 @@ public ConfigurationCacheEntry compute(DeltaCacheKey deltaKey) { BaseData mergedConfiguration = getMergedConfiguration(endpointId, userConfiguration, deltaKey, latestConfigurationSchema); + // converting merged base schema to raw schema + String ctlSchema = cacheService.getFlatCtlSchemaById(latestConfigurationSchema.getCtlSchemaId()); + RawData rawData = new RawData(new RawSchema(ctlSchema), mergedConfiguration.getRawData()); + LOG.trace("[{}] Merged configuration {}", endpointId, mergedConfiguration.getRawData()); - deltaCache = buildBaseResyncDelta(endpointId, mergedConfiguration, userConfigurationHash); + deltaCache = buildBaseResyncDelta(endpointId, rawData, userConfigurationHash); if (cacheService.getConfByHash(deltaCache.getHash()) == null) { EndpointConfigurationDto newConfiguration = new EndpointConfigurationDto(); @@ -351,10 +356,11 @@ public BaseData compute(List key) { return mergedConfiguration; } - private ConfigurationCacheEntry buildBaseResyncDelta(String endpointId, BaseData mergedConfiguration, EndpointObjectHash userConfigurationHash) { + private ConfigurationCacheEntry buildBaseResyncDelta(String endpointId, RawData mergedConfiguration, EndpointObjectHash userConfigurationHash) { byte[] configuration = GenericAvroConverter.toRawData(mergedConfiguration.getRawData(), mergedConfiguration.getSchema() .getRawSchema()); return new ConfigurationCacheEntry(configuration, new BaseBinaryDelta(configuration), EndpointObjectHash.fromSHA1(configuration), userConfigurationHash); } + }