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()); } }
/** * 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)); } } }
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); }
private CTLSchemaDto generateDefaultRecord(CTLSchemaDto unSavedSchema) { try { String schemaBody = flatExportAsString(unSavedSchema); LOG.debug("Generating default record for flat schema: {}", schemaBody); RawSchema dataSchema = new RawSchema(schemaBody); DefaultRecordGenerationAlgorithm<RawData> dataProcessor = new DefaultRecordGenerationAlgorithmImpl<RawSchema, RawData>( dataSchema, new RawDataFactory()); unSavedSchema.setDefaultRecord(dataProcessor.getRootData().getRawData()); return unSavedSchema; } catch (StackOverflowError ex) { LOG.error("Failed to generate default record. An endless recursion is detected. CTL schema " + "body: {}", unSavedSchema.getBody(), ex); throw new RuntimeException("Unable to generate default record. An endless recursion is " + "detected! " + "Please check non-optional references to nested types."); } catch (ConfigurationGenerationException | IOException | RuntimeException ex) { LOG.error("Failed to generate default record for CTL schema with body: {}", unSavedSchema .getBody(), ex); throw new RuntimeException("An unexpected exception occured: " + ex.toString()); } }
@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()); } }
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; } }
@Override public FileData getRecordStructureSchema(String applicationId, int logSchemaVersion) throws ControlServiceException { ApplicationDto application = applicationService.findAppById(applicationId); if (application == null) { throw new NotFoundException("Application not found!"); } LogSchemaDto logSchema = logSchemaService .findLogSchemaByAppIdAndVersion(applicationId, logSchemaVersion); if (logSchema == null) { throw new NotFoundException("Log schema not found!"); } Schema recordWrapperSchema = null; try { CTLSchemaDto logCtlSchema = getCtlSchemaById(logSchema.getCtlSchemaId()); recordWrapperSchema = RecordWrapperSchemaGenerator .generateRecordWrapperSchema(logCtlSchema.getBody()); } catch (IOException ex) { LOG.error("Unable to get Record Structure Schema", ex); throw new ControlServiceException(ex); } String libraryFileName = MessageFormatter .arrayFormat(SCHEMA_NAME_PATTERN, new Object[] {logSchemaVersion}).getMessage(); String schemaInJson = recordWrapperSchema.toString(true); byte[] schemaData = schemaInJson.getBytes(StandardCharsets.UTF_8); FileData schema = new FileData(); schema.setFileName(libraryFileName); schema.setFileData(schemaData); return schema; }
return parser.parse(schema.getBody()); } catch (Exception cause) { LOG.error("Unable to parse CTL schema \""
CtlSchemaFormDto toCtlSchemaForm(CTLSchemaDto ctlSchema, ConverterType converterType) throws KaaAdminServiceException { try { CtlSchemaFormDto ctlSchemaForm = new CtlSchemaFormDto(ctlSchema); SchemaFormAvroConverter converter = getCtlSchemaConverterForScope( ctlSchemaForm.getMetaInfo().getTenantId(), ctlSchemaForm.getMetaInfo().getApplicationId(), converterType); RecordField form = converter.createSchemaFormFromSchema(ctlSchema.getBody()); ctlSchemaForm.setSchema(form); List<Integer> availableVersions = controlService.getAllCtlSchemaVersionsByFqnTenantIdAndApplicationId( ctlSchema.getMetaInfo().getFqn(), ctlSchema.getMetaInfo().getTenantId(), ctlSchema.getMetaInfo().getApplicationId()); availableVersions = availableVersions == null ? Collections.<Integer>emptyList() : availableVersions; Collections.sort(availableVersions); ctlSchemaForm.getMetaInfo().setVersions(availableVersions); return ctlSchemaForm; } catch (Exception cause) { throw Utils.handleException(cause); } }
LOG.debug("Set latest server profile schema [{}] and default record {} for endpoint with " + "key [{}]", serverProfileSchemaDto.getVersion(), schemaDto.getBody(), keyHash); endpointProfileDto.setServerProfileVersion(serverProfileSchemaDto.getVersion()); endpointProfileDto.setServerProfileBody(schemaDto.getDefaultRecord());