/** * Exports a CTL schema and all of its dependencies depending on the export method specified. */ public FileData downloadCtlSchemaByAppToken(CTLSchemaDto ctlSchemaDto, CTLSchemaExportMethod method, String appToken) { final FileDataResponseExtractor extractor = new FileDataResponseExtractor(); MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>(); parameters.add("fqn", ctlSchemaDto.getMetaInfo().getFqn()); parameters.add("version", Integer.toString(ctlSchemaDto.getVersion())); if (ctlSchemaDto.getMetaInfo().getApplicationId() != null) { parameters.add("applicationToken", appToken); } parameters.add("method", method.name()); RequestCallback request = new DataRequestCallback<>(parameters); return restTemplate.execute(restTemplate.getUrl() + "CTL/exportSchema", HttpMethod.POST, request, extractor); }
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; } }
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); } }
/** * Create new instance of CTL schema meta info. * * @param dto data transfer object that used for creating new instance, * it hold id, full qualified name, tenant id and application id * of new instance */ public CtlSchemaMetaInfo(CtlSchemaMetaInfoDto dto) { this.id = ModelUtils.getLongId(dto.getId()); this.fqn = dto.getFqn(); Long tenantId = getLongId(dto.getTenantId()); this.tenant = tenantId != null ? new Tenant(tenantId) : null; Long appId = getLongId(dto.getApplicationId()); this.application = appId != null ? new Application(appId) : null; }
@Override public NotificationSchemaViewDto createNotificationSchemaFormCtlSchema( CtlSchemaFormDto ctlSchemaForm) throws KaaAdminServiceException { LOG.error("createNotificationSchemaFormCtlSchema [{}]", ctlSchemaForm.getSchema().getDisplayString()); checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { checkApplicationId(ctlSchemaForm.getMetaInfo().getApplicationId()); NotificationSchemaDto notificationSchema = new NotificationSchemaDto(); notificationSchema.setApplicationId(ctlSchemaForm.getMetaInfo().getApplicationId()); notificationSchema.setName(ctlSchemaForm.getSchema().getDisplayNameFieldValue()); notificationSchema.setDescription(ctlSchemaForm.getSchema().getDescriptionFieldValue()); CtlSchemaFormDto savedCtlSchemaForm = ctlService.saveCtlSchemaForm( ctlSchemaForm, ConverterType.FORM_AVRO_CONVERTER); notificationSchema.setCtlSchemaId(savedCtlSchemaForm.getId()); NotificationSchemaDto savedNotificationSchema = saveNotificationSchema(notificationSchema); return getNotificationSchemaView(savedNotificationSchema.getId()); } catch (Exception ex) { throw Utils.handleException(ex); } } }
@Override public CTLSchemaDto getCtlSchemaById(String schemaId) throws KaaAdminServiceException { this.checkAuthority(KaaAuthorityDto.values()); try { this.checkCtlSchemaId(schemaId); CTLSchemaDto schemaFound = controlService.getCtlSchemaById(schemaId); Utils.checkNotNull(schemaFound); checkCtlSchemaReadScope( schemaFound.getMetaInfo().getTenantId(), schemaFound.getMetaInfo().getApplicationId()); return schemaFound; } catch (Exception cause) { throw Utils.handleException(cause); } }
@Override public LogSchemaViewDto createLogSchemaFormCtlSchema(CtlSchemaFormDto ctlSchemaForm) throws KaaAdminServiceException { LOG.error("createLogSchemaFormCtlSchema [{}]", ctlSchemaForm.getSchema().getDisplayString()); checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { checkApplicationId(ctlSchemaForm.getMetaInfo().getApplicationId()); LogSchemaDto logSchema = new LogSchemaDto(); logSchema.setApplicationId(ctlSchemaForm.getMetaInfo().getApplicationId()); logSchema.setName(ctlSchemaForm.getSchema().getDisplayNameFieldValue()); logSchema.setDescription(ctlSchemaForm.getSchema().getDescriptionFieldValue()); CtlSchemaFormDto savedCtlSchemaForm = ctlService.saveCtlSchemaForm( ctlSchemaForm, ConverterType.FORM_AVRO_CONVERTER); logSchema.setCtlSchemaId(savedCtlSchemaForm.getId()); LogSchemaDto savedLogSchema = saveLogSchema(logSchema); return getLogSchemaView(savedLogSchema.getId()); } catch (Exception ex) { throw Utils.handleException(ex); } }
@Override public ProfileSchemaViewDto createProfileSchemaFormCtlSchema(CtlSchemaFormDto ctlSchemaForm) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { checkApplicationId(ctlSchemaForm.getMetaInfo().getApplicationId()); EndpointProfileSchemaDto profileSchema = new EndpointProfileSchemaDto(); profileSchema.setApplicationId(ctlSchemaForm.getMetaInfo().getApplicationId()); profileSchema.setName(ctlSchemaForm.getSchema().getDisplayNameFieldValue()); profileSchema.setDescription(ctlSchemaForm.getSchema().getDescriptionFieldValue()); CtlSchemaFormDto savedCtlSchemaForm = ctlService.saveCtlSchemaForm( ctlSchemaForm, ConverterType.FORM_AVRO_CONVERTER); profileSchema.setCtlSchemaId(savedCtlSchemaForm.getId()); EndpointProfileSchemaDto savedProfileSchema = saveProfileSchema(profileSchema); return getProfileSchemaView(savedProfileSchema.getId()); } catch (Exception ex) { throw Utils.handleException(ex); } }
@Override public CtlSchemaFormDto getLatestCtlSchemaForm(String metaInfoId) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.values()); try { this.checkCtlSchemaMetaInfoId(metaInfoId); CTLSchemaDto ctlSchema = controlService.getLatestCtlSchemaByMetaInfoId(metaInfoId); Utils.checkNotNull(ctlSchema); checkCtlSchemaReadScope( ctlSchema.getMetaInfo().getTenantId(), ctlSchema.getMetaInfo().getApplicationId()); return toCtlSchemaForm(ctlSchema, ConverterType.FORM_AVRO_CONVERTER); } catch (Exception cause) { throw Utils.handleException(cause); } }
@Override public ServerProfileSchemaViewDto createServerProfileSchemaFormCtlSchema( CtlSchemaFormDto ctlSchemaForm) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { checkApplicationId(ctlSchemaForm.getMetaInfo().getApplicationId()); ServerProfileSchemaDto serverProfileSchema = new ServerProfileSchemaDto(); serverProfileSchema.setApplicationId(ctlSchemaForm.getMetaInfo().getApplicationId()); serverProfileSchema.setName(ctlSchemaForm.getSchema().getDisplayNameFieldValue()); serverProfileSchema.setDescription(ctlSchemaForm.getSchema().getDescriptionFieldValue()); CtlSchemaFormDto savedCtlSchemaForm = ctlService.saveCtlSchemaForm( ctlSchemaForm, ConverterType.FORM_AVRO_CONVERTER); serverProfileSchema.setCtlSchemaId(savedCtlSchemaForm.getId()); ServerProfileSchemaDto savedServerProfileSchema = saveServerProfileSchema( serverProfileSchema); return getServerProfileSchemaView(savedServerProfileSchema.getId()); } catch (Exception ex) { throw Utils.handleException(ex); } }
@Override public String prepareCtlSchemaExport(String ctlSchemaId, CTLSchemaExportMethod method) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.values()); try { CTLSchemaDto schemaFound = controlService.getCtlSchemaById(ctlSchemaId); Utils.checkNotNull(schemaFound); checkCtlSchemaReadScope( schemaFound.getMetaInfo().getTenantId(), schemaFound.getMetaInfo().getApplicationId()); CtlSchemaExportKey key = new CtlSchemaExportKey(ctlSchemaId, method); return Base64.encodeObject(key, Base64.URL_SAFE); } catch (Exception ex) { throw Utils.handleException(ex); } }
@Test public void testFindSiblingsFqns() { List<CtlSchemaMetaInfoDto> siblingSchemas = ctlService.findSiblingsByFqnTenantIdAndApplicationId(appSchema.getMetaInfo().getFqn(), appSchema.getMetaInfo().getTenantId(), appSchema.getMetaInfo().getApplicationId()); Assert.assertNotNull(siblingSchemas); Assert.assertEquals(1, siblingSchemas.size()); Assert.assertEquals(app2Schema.getMetaInfo(), siblingSchemas.get(0)); }
@Override public ConfigurationSchemaViewDto createConfigurationSchemaFormCtlSchema( CtlSchemaFormDto ctlSchemaForm) throws KaaAdminServiceException { checkAuthority( KaaAuthorityDto.TENANT_ADMIN, KaaAuthorityDto.TENANT_DEVELOPER, KaaAuthorityDto.TENANT_USER); try { checkApplicationId(ctlSchemaForm.getMetaInfo().getApplicationId()); ConfigurationSchemaDto confSchema = new ConfigurationSchemaDto(); confSchema.setApplicationId(ctlSchemaForm.getMetaInfo().getApplicationId()); confSchema.setName(ctlSchemaForm.getSchema().getDisplayNameFieldValue()); confSchema.setDescription(ctlSchemaForm.getSchema().getDescriptionFieldValue()); CtlSchemaFormDto savedCtlSchemaForm = ctlService.saveCtlSchemaForm( ctlSchemaForm, ConverterType.CONFIGURATION_FORM_AVRO_CONVERTER); confSchema.setCtlSchemaId(savedCtlSchemaForm.getId()); ConfigurationSchemaDto savedConfSchema = saveConfigurationSchema(confSchema); return getConfigurationSchemaView(savedConfSchema.getId()); } catch (Exception ex) { throw Utils.handleException(ex); } }
@Override public boolean checkFqnExists(CtlSchemaFormDto ctlSchemaForm) throws KaaAdminServiceException { checkAuthority(KaaAuthorityDto.values()); try { if (isEmpty(ctlSchemaForm.getId())) { AuthUserDto currentUser = getCurrentUser(); RecordField schemaForm = ctlSchemaForm.getSchema(); String fqn = schemaForm.getDeclaredFqn().getFqnString(); String tenantId = currentUser.getTenantId(); return checkFqnExists(fqn, tenantId, ctlSchemaForm.getMetaInfo().getApplicationId()); } } catch (Exception cause) { throw Utils.handleException(cause); } return false; }
@Override public CtlSchemaFormDto getCtlSchemaFormByMetaInfoIdAndVer(String metaInfoId, int version) throws KaaAdminServiceException { this.checkAuthority(KaaAuthorityDto.values()); try { this.checkCtlSchemaMetaInfoId(metaInfoId); this.checkCtlSchemaVersion(version); CTLSchemaDto schemaFound = controlService.getCtlSchemaByMetaInfoIdAndVer( metaInfoId, version); Utils.checkNotNull(schemaFound); checkCtlSchemaReadScope( schemaFound.getMetaInfo().getTenantId(), schemaFound.getMetaInfo().getApplicationId()); return toCtlSchemaForm(schemaFound, ConverterType.FORM_AVRO_CONVERTER); } catch (Exception cause) { throw Utils.handleException(cause); } }
@Test public void testFindCTLSchemaByFqnAndVerAndTenantIdAndApplicationId() { CtlSchemaMetaInfoDto metaInfo = firstSchema.getMetaInfo(); CTLSchemaDto found = ctlService.findCtlSchemaByFqnAndVerAndTenantIdAndApplicationId(metaInfo.getFqn(), firstSchema.getVersion(), metaInfo.getTenantId(), metaInfo.getApplicationId()); Assert.assertEquals(firstSchema, found); }
@Override public CTLSchemaDto getCtlSchemaByFqnVersionTenantIdAndApplicationId(String fqn, Integer version, String tenantId, String applicationId) throws KaaAdminServiceException { this.checkAuthority(KaaAuthorityDto.values()); try { this.checkCtlSchemaFqn(fqn); this.checkCtlSchemaVersion(version); if (!isEmpty(applicationId)) { this.checkApplicationId(applicationId); } CTLSchemaDto schemaFound = controlService.getCtlSchemaByFqnVersionTenantIdAndApplicationId( fqn, version, tenantId, applicationId); Utils.checkNotNull(schemaFound); checkCtlSchemaReadScope( schemaFound.getMetaInfo().getTenantId(), schemaFound.getMetaInfo().getApplicationId()); return schemaFound; } catch (Exception cause) { throw Utils.handleException(cause); } }
@Test public void testFindByFqnAndVerAndTenantIdAndApplicationId() { CtlSchema found = ctlSchemaDao.findByFqnAndVerAndTenantIdAndApplicationId(firstSchema.getMetaInfo().getFqn(), firstSchema.getVersion(), firstSchema.getMetaInfo().getTenantId(), firstSchema.getMetaInfo().getApplicationId()); Assert.assertEquals(firstSchema, found.toDto()); }
@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 testRemoveCTLSchemaByFqnAndVerAndWithoutTenantId() { String schemaId = systemSchema.getId(); ctlService.removeCtlSchemaByFqnAndVerAndTenantIdAndApplicationId(systemSchema.getMetaInfo().getFqn(), systemSchema.getVersion(), systemSchema.getMetaInfo().getTenantId(), systemSchema.getMetaInfo().getApplicationId()); Assert.assertNull(ctlService.findCtlSchemaById(schemaId)); }