/** * Create instance of <code>CtlSchema</code>. * * @param dto data transfer object */ public CtlSchema(CTLSchemaDto dto) { this.id = getLongId(dto.getId()); this.metaInfo = new CtlSchemaMetaInfo(dto.getMetaInfo()); this.version = dto.getVersion(); this.createdUsername = dto.getCreatedUsername(); this.createdTime = dto.getCreatedTime(); update(dto); }
@Override public CTLSchemaDto getOrCreateEmptySystemSchema(String createdUsername) { CTLSchemaDto ctlSchema = findCtlSchemaByFqnAndVerAndTenantIdAndApplicationId( DEFAULT_SYSTEM_EMPTY_SCHEMA_FQN, DEFAULT_SYSTEM_EMPTY_SCHEMA_VERSION, null, null); if (ctlSchema == null) { ctlSchema = new CTLSchemaDto(); CtlSchemaMetaInfoDto metaInfo = new CtlSchemaMetaInfoDto(DEFAULT_SYSTEM_EMPTY_SCHEMA_FQN); ctlSchema.setMetaInfo(metaInfo); ctlSchema.setVersion(DEFAULT_SYSTEM_EMPTY_SCHEMA_VERSION); ctlSchema.setCreatedUsername(createdUsername); ctlSchema.setDependencySet(new HashSet<CTLSchemaDto>()); String body = getStringFromFile(DEFAULT_SYSTEM_EMPTY_SCHEMA_FILE, CtlServiceImpl.class); if (!body.isEmpty()) { ctlSchema.setBody(body); } else { throw new RuntimeException("Can't read default system schema."); // NOSONAR } ctlSchema = saveCtlSchema(ctlSchema); } return ctlSchema; }
/** * Update <code>CtlSchema</code> instance, get data from dto. * * @param dto data transfer object */ public void update(CTLSchemaDto dto) { this.body = dto.getBody(); this.defaultRecord = dto.getDefaultRecord(); Set<CTLSchemaDto> dependencies = dto.getDependencySet(); if (dependencies != null && !dependencies.isEmpty()) { for (CTLSchemaDto dependency : dependencies) { dependencySet.add(new CtlSchema(dependency)); } } }
@Override public CTLSchemaDto toDto() { CTLSchemaDto ctlSchemaDto = createDto(); ctlSchemaDto.setId(getStringId()); ctlSchemaDto.setMetaInfo(metaInfo.toDto()); ctlSchemaDto.setVersion(version); ctlSchemaDto.setCreatedTime(createdTime); ctlSchemaDto.setCreatedUsername(createdUsername); ctlSchemaDto.setBody(body); ctlSchemaDto.setDefaultRecord(defaultRecord); ctlSchemaDto.setDependencySet(DaoUtil.convertDtoSet(dependencySet)); return ctlSchemaDto; }
ctlSchema = new CTLSchemaDto(); if (isEmpty(ctlSchema.getId())) { ctlSchema.setCreatedUsername(currentUser.getUsername()); RecordField schemaForm = ctlSchemaForm.getSchema(); ctlSchema.setMetaInfo(ctlSchemaForm.getMetaInfo()); ctlSchema.getMetaInfo().setFqn(schemaForm.getDeclaredFqn().getFqnString()); ctlSchema.getMetaInfo().setTenantId(currentUser.getTenantId()); ctlSchema.setVersion(schemaForm.getVersion()); List<FqnVersion> dependenciesList = schemaForm.getContext().getCtlDependenciesList(); Set<CTLSchemaDto> dependencies = new HashSet<>(); controlService.getAnyCtlSchemaByFqnVersionTenantIdAndApplicationId( fqnVersion.getFqnString(), fqnVersion.getVersion(), ctlSchema.getMetaInfo().getTenantId(), ctlSchema.getMetaInfo().getApplicationId()); if (dependency != null) { dependencies.add(dependency); throw new IllegalArgumentException(message); ctlSchema.setDependencySet(dependencies); SchemaFormAvroConverter converter = getCtlSchemaConverterForScope( ctlSchema.getMetaInfo().getTenantId(), ctlSchema.getMetaInfo().getApplicationId(), converterType); Schema avroSchema = converter.createSchemaFromSchemaForm(schemaForm); String schemaBody = SchemaFormAvroConverter.createSchemaString(avroSchema, true); ctlSchema.setBody(schemaBody);
private void validateCtlSchemaObject(CTLSchemaDto ctlSchema) { validateObject(ctlSchema, "Incorrect ctl schema object"); CtlSchemaMetaInfoDto metaInfo = ctlSchema.getMetaInfo(); if (metaInfo == null) { throw new RuntimeException("Incorrect ctl schema object. CtlSchemaMetaInfoDto is mandatory " + "information."); } else { if (isBlank(metaInfo.getFqn()) || ctlSchema.getVersion() == null) { throw new RuntimeException("Incorrect CTL schema, please add correct version and fqn."); } } }
@Test public void testRemoveCTLSchemaByFqnAndVerAndTenantIdAndApplicationId() { String schemaId = tenantSchema.getId(); ctlService.removeCtlSchemaByFqnAndVerAndTenantIdAndApplicationId(tenantSchema.getMetaInfo().getFqn(), tenantSchema.getVersion(), tenantSchema.getMetaInfo().getTenantId(), tenantSchema.getMetaInfo().getApplicationId()); Assert.assertNull(ctlService.findCtlSchemaById(schemaId)); }
@Test public void testSaveFlatCtlSchema() throws IOException { CTLSchemaDto testCtl = new CTLSchemaDto(); CtlSchemaMetaInfoDto ctlMetaInfo = new CtlSchemaMetaInfoDto("org.kaaproject.kaa.NoDependenciesCtlExample", tenant.getId()); testCtl.setMetaInfo(ctlMetaInfo); testCtl.setVersion(1); testCtl.setBody(readSchemaFileAsString(TEST_CTL_SCHEMA_FLAT)); testCtl = ctlService.saveCtlSchema(testCtl); CTLSchemaDto foundCtl = ctlService.findCtlSchemaById(testCtl.getId()); String defaultRecord = foundCtl.getDefaultRecord(); Assert.assertNotNull(foundCtl); Assert.assertTrue(defaultRecord.contains("\"bytesField\":\"\"")); Assert.assertTrue(defaultRecord.contains("\"booleanField\":false")); Assert.assertTrue(defaultRecord.contains("\"intField\":0")); Assert.assertTrue(defaultRecord.contains("\"longField\":0")); Assert.assertTrue(defaultRecord.contains("\"floatField\":0.0")); Assert.assertTrue(defaultRecord.contains("\"doubleField\":0.0")); Assert.assertTrue(defaultRecord.contains("\"stringField\":\"\"")); Assert.assertTrue(defaultRecord.contains("\"enumField\":\"SPADES\"")); Assert.assertTrue(defaultRecord.contains("\"arrayField\":[]")); Assert.assertTrue(defaultRecord.contains("\"recordsArrayField\":[]")); Assert.assertTrue(defaultRecord.contains("\"recordField\":{\"bytesFieldInRecord\":\"\",\"booleanFieldInRecord\":false,\"intFieldInRecord\":0,\"floatFieldInRecord\":0.0}")); }
private EndpointProfileSchemaDto createDefaultProfileSchema(String appId, String createdUsername) { EndpointProfileSchemaDto profileSchemaDto = new EndpointProfileSchemaDto(); profileSchemaDto.setApplicationId(appId); CTLSchemaDto ctlSchema = ctlService.getOrCreateEmptySystemSchema(createdUsername); profileSchemaDto.setCtlSchemaId(ctlSchema.getId()); profileSchemaDto.setName(DEFAULT_SCHEMA_NAME); profileSchemaDto.setCreatedUsername(createdUsername); profileSchemaDto = profileService.saveProfileSchema(profileSchemaDto); if (profileSchemaDto == null) { throw new RuntimeException("Can't save default profile schema "); //NOSONAR } return profileSchemaDto; }
if (schema.getDependencySet() != null) { for (CTLSchemaDto dependency : schema.getDependencySet()) { try { CTLSchemaDto dependencySchema = controlService.getCtlSchemaByFqnVersionTenantIdAndApplicationId( dependency.getMetaInfo().getFqn(), dependency.getVersion(), dependency.getMetaInfo().getTenantId(), dependency.getMetaInfo().getApplicationId()); if (dependencySchema == null) { String message = "Unable to locate dependency \"" + dependency.getMetaInfo().getFqn() + "\" (version " + dependency.getVersion() + ")"; throw new IllegalArgumentException(message); return parser.parse(schema.getBody()); } catch (Exception cause) { LOG.error("Unable to parse CTL schema \"" + schema.getMetaInfo().getFqn() + "\" (version " + schema.getVersion() + "): ", cause); throw new IllegalArgumentException("Unable to parse CTL schema \"" + schema.getMetaInfo().getFqn() + "\" (version " + schema.getVersion() + "): " + cause.getMessage());
@Override public CtlSchemaReferenceDto getLastCtlSchemaReferenceDto(String ctlSchemaId) throws KaaAdminServiceException { try { if (!isEmpty(ctlSchemaId)) { CTLSchemaDto ctlSchemaDto = controlService.getCtlSchemaById(ctlSchemaId); CtlSchemaReferenceDto ctlSchemaReference = getAvailableApplicationCtlSchemaReferences(null).stream() .filter(ctlSchemaReferenceDto -> ctlSchemaReferenceDto.getMetaInfo() .getId() .equals(ctlSchemaDto.getMetaInfo().getId())) .findFirst() .get(); return ctlSchemaReference; } } catch (Exception ex) { throw Utils.handleException(ex); } return null; }
@Override public FileData shallowExport(CTLSchemaDto schema) { try { FileData result = new FileData(); result.setContentType(JSON); result.setFileName(MessageFormat.format(CTL_EXPORT_TEMPLATE, schema.getMetaInfo().getFqn(), schema.getVersion())); // Format schema body Object json = FORMATTER.readValue(schema.getBody(), Object.class); result.setFileData(FORMATTER.writerWithDefaultPrettyPrinter().writeValueAsString(json) .getBytes()); return result; } catch (Exception cause) { throw new RuntimeException("An unexpected exception occured: " + cause.toString()); } }
@Override public CTLSchemaDto saveCtlSchema(CTLSchemaDto unSavedSchema) { validateCtlSchemaObject(unSavedSchema); if (unSavedSchema.getDefaultRecord() == null) { unSavedSchema = generateDefaultRecord(unSavedSchema); } else { validateDefaultRecord(unSavedSchema); if (isBlank(unSavedSchema.getId())) { CtlSchemaMetaInfoDto metaInfo = unSavedSchema.getMetaInfo(); CTLSchemaDto dto; synchronized (this) {
try { Utils.checkNotNull(schema); checkCtlSchemaVersion(schema.getVersion()); checkCtlSchemaEditScope( schema.getMetaInfo().getTenantId(), schema.getMetaInfo().getApplicationId()); if (schema.getDependencySet() != null) { for (CTLSchemaDto dependency : schema.getDependencySet()) { CTLSchemaDto schemaFound = controlService.getAnyCtlSchemaByFqnVersionTenantIdAndApplicationId( dependency.getMetaInfo().getFqn(), dependency.getVersion(), schema.getMetaInfo().getTenantId(), schema.getMetaInfo().getApplicationId()); if (schemaFound == null) { missingDependencies.add( new FqnVersion(dependency.getMetaInfo().getFqn(), dependency.getVersion())); } else { dependencies.add(schemaFound); controlService, schema.getMetaInfo().getTenantId()); parser.validate(schema);
private List<FileData> recursiveShallowExport(List<FileData> files, CTLSchemaDto parent) throws Exception { files.add(this.shallowExport(parent)); ObjectNode object = new ObjectMapper().readValue(parent.getBody(), ObjectNode.class); ArrayNode dependencies = (ArrayNode) object.get(DEPENDENCIES); if (dependencies != null) { for (JsonNode node : dependencies) { ObjectNode dependency = (ObjectNode) node; String fqn = dependency.get(FQN).getTextValue(); Integer version = dependency.get(VERSION).getIntValue(); CTLSchemaDto child = this.findAnyCtlSchemaByFqnAndVerAndTenantIdAndApplicationId( fqn, version, parent.getMetaInfo().getTenantId(), parent.getMetaInfo().getApplicationId()); Validate.notNull(child, MessageFormat.format("The dependency [{0}] was not found!", fqn)); this.recursiveShallowExport(files, child); } } return files; } }
private void validateDefaultRecord(CTLSchemaDto unSavedSchema) { try { String schemaBody = flatExportAsString(unSavedSchema); GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(schemaBody); converter.decodeJson(unSavedSchema.getDefaultRecord()); } catch (IOException | RuntimeException ex) { LOG.error("Invalid default record for CTL schema with body: {}", unSavedSchema.getBody(), ex); throw new RuntimeException("An unexpected exception occured: " + ex.toString()); } }
@Override public List<Integer> getAllCtlSchemaVersionsByFqnTenantIdAndApplicationId( String fqn, String tenantId, String applicationId) throws ControlServiceException { List<CTLSchemaDto> schemas = ctlService.findAllCtlSchemasByFqnAndTenantIdAndApplicationId(fqn, tenantId, applicationId); List<Integer> versions = new ArrayList<>(schemas.size()); for (CTLSchemaDto schema : schemas) { versions.add(schema.getVersion()); } return versions; }
fqn, versions.get(0), tenantId, applicationId).getMetaInfo(); CTLSchemaDto schema = controlService.getCtlSchemaByFqnVersionTenantIdAndApplicationId( fqn, version, tenantId, applicationId); Set<CTLSchemaDto> schemaDependents = schema.getDependencySet(); dependencies.addAll(schemaDependents.stream() .filter(dep -> dep.getMetaInfo().getScope() == CTLSchemaScopeDto.APPLICATION) .collect(Collectors.toList()));
private Schema parseDependencies(CTLSchemaDto schema, final Schema.Parser parser) throws Exception { if (schema.getDependencySet() != null) { for (CTLSchemaDto dependency : schema.getDependencySet()) { this.parseDependencies(dependency, parser); } } ObjectNode object = new ObjectMapper().readValue(schema.getBody(), ObjectNode.class); object.remove(DEPENDENCIES); String body = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(object); return parser.parse(body); }
@Override protected CTLSchemaDto createDto() { return new CTLSchemaDto(); }