use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class Converter method convertDefinitionsChildToYaml.
public String convertDefinitionsChildToYaml(DefinitionsChildId id) throws MultiException {
Path path = Utils.getTmpDir(Paths.get(id.getQName().getLocalPart()));
convertX2Y(repository.getDefinitions(id), path);
// convention: single file in root contains the YAML support
// TODO: Links in the YAML should be changed to real links into Winery
Optional<Path> rootYamlFile;
try {
return Files.find(path, 1, (filePath, basicFileAttributes) -> filePath.getFileName().toString().endsWith(".yml")).findAny().map(p -> {
try {
return new String(Files.readAllBytes(p), StandardCharsets.UTF_8);
} catch (IOException e) {
LOGGER.debug("Could not read root file", e);
return "Could not read root file";
}
}).orElseThrow(() -> {
MultiException multiException = new MultiException();
multiException.add(new WineryRepositoryException("Root YAML file not found."));
return multiException;
});
} catch (IOException e) {
MultiException multiException = new MultiException();
multiException.add(new WineryRepositoryException("Root YAML file not found.", e));
throw multiException;
}
}
use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class IWineryRepositoryCommon method getTypeForTemplate.
/**
* Returns the stored type for the given template
*
* @param template the template to determine the type for
* @throws NullPointerException if template.getType() returns null
*/
// we suppress "unchecked" as we use Class.forName
@SuppressWarnings("unchecked")
default TEntityType getTypeForTemplate(TEntityTemplate template) {
QName type = template.getType();
Objects.requireNonNull(type);
// Possibilities:
// a) try all possibly types whether an appropriate QName exists
// b) derive type class from template class. Determine appropriate template element afterwards.
// We go for b)
String instanceResourceClassName = template.getClass().toString();
int idx = instanceResourceClassName.lastIndexOf('.');
// get everything from ".T", where "." is the last dot
instanceResourceClassName = instanceResourceClassName.substring(idx + 2);
// strip off "Template"
instanceResourceClassName = instanceResourceClassName.substring(0, instanceResourceClassName.length() - "Template".length());
// add "Type"
instanceResourceClassName += "Type";
// an id is required to instantiate the resource
String idClassName = "org.eclipse.winery.common.ids.definitions." + instanceResourceClassName + "Id";
org.slf4j.Logger LOGGER = LoggerFactory.getLogger(this.getClass());
LOGGER.debug("idClassName: {}", idClassName);
// Get instance of id class having "type" as id
Class<? extends DefinitionsChildId> idClass;
try {
idClass = (Class<? extends DefinitionsChildId>) Class.forName(idClassName);
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Could not determine id class", e);
}
Constructor<? extends DefinitionsChildId> idConstructor;
try {
idConstructor = idClass.getConstructor(QName.class);
} catch (NoSuchMethodException | SecurityException e) {
throw new IllegalStateException("Could not get QName id constructor", e);
}
DefinitionsChildId typeId;
try {
typeId = idConstructor.newInstance(type);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
throw new IllegalStateException("Could not instantiate type", e);
}
final Definitions definitions = this.getDefinitions(typeId);
return (TEntityType) definitions.getElement();
}
use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class IdUtil method getPathFragment.
/**
* Executes the real conversion to a path fragment
*
* @param id the id to transform to a path
* @param doubleEncode true if each sub fragment should be double encoded,
* false if it should be encoded only once
*/
private static String getPathFragment(final GenericId id, final boolean doubleEncode) {
String toInsert;
if (id instanceof DefinitionsChildId) {
// @return "[ComponentName]s/{namespace}/{id}/"
DefinitionsChildId tId = (DefinitionsChildId) id;
String res = Util.getRootPathFragment(tId.getClass());
toInsert = tId.getNamespace().getEncoded();
if (doubleEncode) {
toInsert = Util.URLencode(toInsert);
}
res = res + toInsert + "/";
toInsert = tId.getXmlId().getEncoded();
if (doubleEncode) {
toInsert = Util.URLencode(toInsert);
}
res = res + toInsert + "/";
return res;
} else if (id instanceof ToscaElementId) {
toInsert = id.getXmlId().getEncoded();
if (doubleEncode) {
toInsert = Util.URLencode(toInsert);
}
return IdUtil.getPathFragment(id.getParent()) + toInsert + "/";
} else {
throw new IllegalStateException("Unknown subclass of GenericId " + id.getClass());
}
}
use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class TOSCAGraphIsomorphismTest method testTOSCAComplianceRuleChecker.
@Test
public void testTOSCAComplianceRuleChecker() throws IOException, ComplianceCheckingException {
HashMap<DefinitionsChildId, TExtensibleElements> allEntities = new HashMap<>();
TOSCAModelPropertiesBuilder bldr = new TOSCAModelPropertiesBuilder(TEST_TARGET_NAMESPACE, "MyProperties");
bldr.addProperty("key1", "value1");
bldr.addProperty("key2", "value2");
// create NodeTypes A B
String idNodeTypeA = "idA";
TNodeType nodeTypeA = TOSCAModelHelper.createTNodeType(idNodeTypeA, TEST_TARGET_NAMESPACE);
String idNodeTypeB = "idB";
TNodeType nodeTypeB = TOSCAModelHelper.createTNodeType(idNodeTypeB, TEST_TARGET_NAMESPACE);
String idNodeTypeC = "idC";
TNodeType nodeTypeC = TOSCAModelHelper.createTNodeType(idNodeTypeC, TEST_TARGET_NAMESPACE);
NodeTypeId idA = TOSCAModelHelper.createNodeTypeId(idNodeTypeA);
NodeTypeId idB = TOSCAModelHelper.createNodeTypeId(idNodeTypeB);
NodeTypeId idC = TOSCAModelHelper.createNodeTypeId(idNodeTypeC);
allEntities.put(idA, nodeTypeA);
allEntities.put(idB, nodeTypeB);
allEntities.put(idC, nodeTypeC);
// createRelationshipTypes
String relTypeIdAString = "adRelA";
RelationshipTypeId relTypeIdA = new RelationshipTypeId(new QName(TEST_TARGET_NAMESPACE, relTypeIdAString));
TRelationshipType relTypeA = TOSCAModelHelper.createTRelationshipType(relTypeIdAString, TEST_TARGET_NAMESPACE);
allEntities.put(relTypeIdA, relTypeA);
// createNodeTemplates
TNodeTemplate nodeTemplate1 = TOSCAModelHelper.createTNodeTemplate("01");
nodeTemplate1.setType(idA.getQName());
TNodeTemplate nodeTemplate2 = TOSCAModelHelper.createTNodeTemplate("02");
nodeTemplate2.setType(idB.getQName());
TNodeTemplate nodeTemplate3 = TOSCAModelHelper.createTNodeTemplate("03");
nodeTemplate3.setType(idA.getQName());
nodeTemplate3.setProperties(bldr.build());
TNodeTemplate nodeTemplate4 = TOSCAModelHelper.createTNodeTemplate("04");
nodeTemplate4.setType(idB.getQName());
TNodeTemplate nodeTemplate5 = TOSCAModelHelper.createTNodeTemplate("05");
nodeTemplate5.setType(idA.getQName());
TNodeTemplate nodeTemplate6 = TOSCAModelHelper.createTNodeTemplate("06");
nodeTemplate6.setType(idB.getQName());
TNodeTemplate nodeTemplate7 = TOSCAModelHelper.createTNodeTemplate("07");
nodeTemplate7.setType(idA.getQName());
nodeTemplate7.setProperties(bldr.build());
TNodeTemplate nodeTemplate8 = TOSCAModelHelper.createTNodeTemplate("08");
nodeTemplate8.setType(idB.getQName());
TNodeTemplate nodeTemplate9 = TOSCAModelHelper.createTNodeTemplate("CompletelyUnrelated");
nodeTemplate9.setType(idC.getQName());
// create RelationshipTemplates
TRelationshipTemplate relTemplate1 = TOSCAModelHelper.createTRelationshipTemplate("1");
relTemplate1.setSourceNodeTemplate(nodeTemplate1);
relTemplate1.setTargetNodeTemplate(nodeTemplate2);
relTemplate1.setType(relTypeIdA.getQName());
TRelationshipTemplate relTemplate2 = TOSCAModelHelper.createTRelationshipTemplate("2");
relTemplate2.setSourceNodeTemplate(nodeTemplate3);
relTemplate2.setTargetNodeTemplate(nodeTemplate4);
relTemplate2.setType(relTypeIdA.getQName());
TRelationshipTemplate relTemplate3 = TOSCAModelHelper.createTRelationshipTemplate("3");
relTemplate3.setSourceNodeTemplate(nodeTemplate5);
relTemplate3.setTargetNodeTemplate(nodeTemplate6);
relTemplate3.setType(relTypeIdA.getQName());
TRelationshipTemplate relTemplate4 = TOSCAModelHelper.createTRelationshipTemplate("4");
relTemplate4.setSourceNodeTemplate(nodeTemplate7);
relTemplate4.setTargetNodeTemplate(nodeTemplate8);
relTemplate4.setType(relTypeIdA.getQName());
// create TopologyTemplates
List<TNodeTemplate> nodeTemplates = new ArrayList<>();
List<TRelationshipTemplate> relationshipTemplates = new ArrayList<>();
// create identifier
nodeTemplates.add(nodeTemplate1);
nodeTemplates.add(nodeTemplate2);
relationshipTemplates.add(relTemplate1);
TTopologyTemplate identifier = TOSCAModelHelper.createTTopologyTemplate(nodeTemplates, relationshipTemplates);
identifier.setNodeTemplates(nodeTemplates);
identifier.setRelationshipTemplates(relationshipTemplates);
nodeTemplates.clear();
relationshipTemplates.clear();
// create required structure
nodeTemplates.add(nodeTemplate3);
nodeTemplates.add(nodeTemplate4);
relationshipTemplates.add(relTemplate2);
TTopologyTemplate requiredStructure = TOSCAModelHelper.createTTopologyTemplate(nodeTemplates, relationshipTemplates);
requiredStructure.setNodeTemplates(nodeTemplates);
requiredStructure.setRelationshipTemplates(relationshipTemplates);
nodeTemplates.clear();
relationshipTemplates.clear();
// create topologyToSearchIn
nodeTemplates.add(nodeTemplate5);
nodeTemplates.add(nodeTemplate6);
nodeTemplates.add(nodeTemplate7);
nodeTemplates.add(nodeTemplate8);
relationshipTemplates.add(relTemplate3);
relationshipTemplates.add(relTemplate4);
TTopologyTemplate topologyTemplateToSearchIn = TOSCAModelHelper.createTTopologyTemplate(nodeTemplates, relationshipTemplates);
topologyTemplateToSearchIn.setNodeTemplates(nodeTemplates);
topologyTemplateToSearchIn.setRelationshipTemplates(relationshipTemplates);
nodeTemplates.clear();
relationshipTemplates.clear();
// create unrelated topology
nodeTemplates.add(nodeTemplate9);
TTopologyTemplate unrelatedTopology = TOSCAModelHelper.createTTopologyTemplate(nodeTemplates, relationshipTemplates);
unrelatedTopology.setNodeTemplates(nodeTemplates);
unrelatedTopology.setRelationshipTemplates(relationshipTemplates);
nodeTemplates.clear();
relationshipTemplates.clear();
persist(allEntities);
// **************** //
// Test starts here //
// **************** //
ComplianceRuleChecker checker;
// test null topologyTemplateToCheck
checker = new ComplianceRuleChecker(null, null, null);
ComplianceCheckingException expected = null;
try {
checker.checkComplianceRule();
} catch (ComplianceCheckingException e) {
expected = e;
}
assertNotNull(expected);
assertTrue(StringUtils.equals(ComplianceCheckingException.NO_TEMPLATE_TO_CHECK, expected.getMessage()));
checker.setToCheckTemplate(topologyTemplateToSearchIn);
// test empty rule
expected = null;
try {
checker.checkComplianceRule();
} catch (ComplianceCheckingException e) {
expected = e;
}
assertNotNull(expected);
assertTrue(StringUtils.equals(ComplianceCheckingException.EMPTY_COMPLIANCE_RULE, expected.getMessage()));
// test Whitelist
checker.setRequiredStructureTemplate(requiredStructure);
expected = null;
try {
checker.checkComplianceRule();
} catch (ComplianceCheckingException e) {
expected = e;
}
assertNotNull(expected);
assertTrue(StringUtils.equals(ComplianceCheckingException.WHITELISTING_NOT_YET_IMPLEMENTED, expected.getMessage()));
// test blacklist
checker.setRequiredStructureTemplate(null);
checker.setIdentifierTemplate(identifier);
List<GraphMapping> blacklistResult = checker.checkComplianceRule();
assertEquals(2, blacklistResult.size());
// test completeRule
// invalid Rule: identifier and required structure have no mapping
checker.setIdentifierTemplate(unrelatedTopology);
checker.setRequiredStructureTemplate(requiredStructure);
expected = null;
try {
checker.checkComplianceRule();
} catch (ComplianceCheckingException e) {
expected = e;
}
assertNotNull(expected);
assertTrue(StringUtils.equals(ComplianceCheckingException.IDENTIFIER_NOT_IN_REQUIREDSTRUCTURE, expected.getMessage()));
// valid Rule: identifier in required structure
// finds one violation
checker.setIdentifierTemplate(identifier);
checker.setRequiredStructureTemplate(requiredStructure);
List<GraphMapping> violatingMappings = checker.checkComplianceRule();
assertEquals(1, violatingMappings.size());
// check the mapping.
// must contain nodesTemplates 5 & 6 and relTemplate3
TOSCANode identifierNode1 = checker.getIdentifierGraph().getNode("01");
TOSCANode identifierNode2 = checker.getIdentifierGraph().getNode("02");
TOSCAEdge identifierEdge = checker.getIdentifierGraph().getEdge(identifierNode1, identifierNode2);
GraphMapping violatingMapping = violatingMappings.stream().findFirst().get();
assertNotNull(violatingMapping);
Map<TOSCANode, TOSCANode> resultMap = checker.getSubGraphMappingAsMap(violatingMapping, checker.getIdentifierGraph());
assertTrue(resultMap.get(identifierNode1).getId().matches("05"));
assertTrue(resultMap.get(identifierNode2).getId().matches("06"));
assertTrue(((TOSCAEdge) violatingMapping.getEdgeCorrespondence(identifierEdge, false)).getId().matches("3"));
// finds no violation
checker.setRequiredStructureTemplate(identifier);
assertEquals(0, checker.checkComplianceRule().size());
TComplianceRule rule = new TComplianceRule();
rule.setName("test");
rule.setTargetNamespace(TEST_TARGET_NAMESPACE);
rule.setIdentifier(identifier);
rule.setRequiredStructure(requiredStructure);
}
use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class TOSCAGraphIsomorphismTest method testServiceTemplateAndComplianceRuleAssociation.
@Test
public void testServiceTemplateAndComplianceRuleAssociation() throws IOException {
HashMap<DefinitionsChildId, TExtensibleElements> allEntities = new HashMap<>();
String dirABC = "/ABC";
String dirBCD = "/BCD";
ServiceTemplateId stId = new ServiceTemplateId(new QName(TEST_TARGET_NAMESPACE + dirABC + dirBCD, "test1"));
TServiceTemplate tServiceTemplate = createTServiceTemplate("ServiceTemplateTestId", TEST_TARGET_NAMESPACE + dirABC + dirBCD);
allEntities.put(stId, tServiceTemplate);
ComplianceRuleId crId1 = new ComplianceRuleId(new QName(TEST_TARGET_NAMESPACE, "test1"));
ComplianceRuleId crId2 = new ComplianceRuleId(new QName(TEST_TARGET_NAMESPACE + dirABC, "test2"));
ComplianceRuleId crId3 = new ComplianceRuleId(new QName(TEST_TARGET_NAMESPACE + dirABC, "test3"));
ComplianceRuleId crId4 = new ComplianceRuleId(new QName(TEST_TARGET_NAMESPACE + dirBCD, "test4"));
TComplianceRule ruleOne = createTComplianceRule(crId1);
allEntities.put(crId1, ruleOne);
TComplianceRule ruleTwo = createTComplianceRule(crId2);
allEntities.put(crId2, ruleTwo);
TComplianceRule ruleThree = createTComplianceRule(crId3);
allEntities.put(crId3, ruleThree);
TComplianceRule ruleFour = createTComplianceRule(crId4);
allEntities.put(crId4, ruleFour);
persist(allEntities);
ServiceTemplateComplianceRuleRuleChecker checker = new ServiceTemplateComplianceRuleRuleChecker(tServiceTemplate);
List<ComplianceRuleId> ruleIds = checker.getRuleIds(tServiceTemplate);
assertTrue(ruleIds.stream().filter(id -> id.equals(crId1)).findFirst().isPresent());
assertTrue(ruleIds.stream().filter(id -> id.equals(crId2)).findFirst().isPresent());
assertTrue(ruleIds.stream().filter(id -> id.equals(crId3)).findFirst().isPresent());
assertFalse(ruleIds.stream().filter(id -> id.equals(crId4)).findFirst().isPresent());
}
Aggregations