use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class TOSCAGraphIsomorphismTest method testTOSCAComplianceRuleMatcher.
@Test
public void testTOSCAComplianceRuleMatcher() throws IOException {
// TNodeTemplateMatching
// by TNodeType
HashMap<DefinitionsChildId, TExtensibleElements> allEntities = new HashMap<>();
NodeTypeId nodeTypeIdAbstractA = TOSCAModelHelper.createNodeTypeId("AbstractA");
allEntities.put(nodeTypeIdAbstractA, TOSCAModelHelper.createTNodeType(nodeTypeIdAbstractA));
NodeTypeId nodeTypeIdAbstractB = TOSCAModelHelper.createNodeTypeId("AbstractB");
allEntities.put(nodeTypeIdAbstractB, TOSCAModelHelper.createTNodeType(nodeTypeIdAbstractB));
NodeTypeId nodeTypeIdActualA = TOSCAModelHelper.createNodeTypeId("ActualA");
TNodeType actualA = TOSCAModelHelper.createTNodeType(nodeTypeIdActualA);
TOSCAModelHelper.setDerivedFrom(nodeTypeIdAbstractA, actualA);
allEntities.put(nodeTypeIdActualA, actualA);
NodeTypeId nodeTypeIdActualB = TOSCAModelHelper.createNodeTypeId("ActualB");
TNodeType actualB = TOSCAModelHelper.createTNodeType(nodeTypeIdActualB);
TOSCAModelHelper.setDerivedFrom(nodeTypeIdAbstractB, actualB);
allEntities.put(nodeTypeIdActualB, actualB);
NodeTypeId nodeTypeIdActualB2 = TOSCAModelHelper.createNodeTypeId("ActualB2");
TNodeType actualB2 = TOSCAModelHelper.createTNodeType(nodeTypeIdActualB2);
TOSCAModelHelper.setDerivedFrom(nodeTypeIdAbstractB, actualB2);
allEntities.put(nodeTypeIdActualB2, actualB2);
persist(allEntities);
TNodeTemplate nodeTemplate1 = TOSCAModelHelper.createTNodeTemplate("01", nodeTypeIdAbstractA);
TNodeTemplate nodeTemplate2 = TOSCAModelHelper.createTNodeTemplate("02", nodeTypeIdAbstractB);
TNodeTemplate nodeTemplate3 = TOSCAModelHelper.createTNodeTemplate("03", nodeTypeIdActualA);
TNodeTemplate nodeTemplate4 = TOSCAModelHelper.createTNodeTemplate("04", nodeTypeIdActualB);
TNodeTemplate nodeTemplate5 = TOSCAModelHelper.createTNodeTemplate("05", nodeTypeIdActualB);
TNodeTemplate nodeTemplate6 = TOSCAModelHelper.createTNodeTemplate("06", nodeTypeIdActualB2);
TOSCAComplianceRuleMatcher matcher = new TOSCAComplianceRuleMatcher();
// same type
assertTrue(matcher.isTEntityTypesCompatible(TOSCAModelHelper.createTOSCANodeOnlyTypes(nodeTemplate1), TOSCAModelHelper.createTOSCANodeOnlyTypes(nodeTemplate1)));
// "01" is supertype of "03" -> match
assertTrue(matcher.isTEntityTypesCompatible(TOSCAModelHelper.createTOSCANodeOnlyTypes(nodeTemplate1), TOSCAModelHelper.createTOSCANodeOnlyTypes(nodeTemplate3)));
// "03" is subtype of "01" -> match
assertTrue(matcher.isTEntityTypesCompatible(TOSCAModelHelper.createTOSCANodeOnlyTypes(nodeTemplate3), TOSCAModelHelper.createTOSCANodeOnlyTypes(nodeTemplate1)));
// different types
assertFalse(matcher.isTEntityTypesCompatible(TOSCAModelHelper.createTOSCANodeOnlyTypes(nodeTemplate4), TOSCAModelHelper.createTOSCANodeOnlyTypes(nodeTemplate3)));
// different types
assertFalse(matcher.isTEntityTypesCompatible(TOSCAModelHelper.createTOSCANodeOnlyTypes(nodeTemplate3), TOSCAModelHelper.createTOSCANodeOnlyTypes(nodeTemplate2)));
// same type
assertTrue(matcher.isTEntityTypesCompatible(TOSCAModelHelper.createTOSCANodeOnlyTypes(nodeTemplate5), TOSCAModelHelper.createTOSCANodeOnlyTypes(nodeTemplate4)));
// different types derived from same type
assertFalse(matcher.isTEntityTypesCompatible(TOSCAModelHelper.createTOSCANodeOnlyTypes(nodeTemplate5), TOSCAModelHelper.createTOSCANodeOnlyTypes(nodeTemplate6)));
// by TEntityTemplate.Properties
TOSCAModelPropertiesBuilder bldrLeft = new TOSCAModelPropertiesBuilder(TEST_TARGET_NAMESPACE, "TestProperties");
TOSCAModelPropertiesBuilder bldrRight = new TOSCAModelPropertiesBuilder(TEST_TARGET_NAMESPACE, "TestProperties");
// test by values
// test simple matching
bldrLeft.addProperty("A", "Something");
bldrRight.addProperty("A", "Something");
assertTrue(matcher.isPropertiesCompatible(TOSCAModelHelper.createTOSCANodeOnlyProperties(bldrLeft), TOSCAModelHelper.createTOSCANodeOnlyProperties(bldrRight)));
bldrRight.addProperty("A", "SomeOtherThing");
assertFalse(matcher.isPropertiesCompatible(TOSCAModelHelper.createTOSCANodeOnlyProperties(bldrLeft), TOSCAModelHelper.createTOSCANodeOnlyProperties(bldrRight)));
// testRegex
bldrLeft.addProperty("A", ".*");
assertTrue(matcher.isPropertiesCompatible(TOSCAModelHelper.createTOSCANodeOnlyProperties(bldrLeft), TOSCAModelHelper.createTOSCANodeOnlyProperties(bldrRight)));
// test by property set
// left subset of right
bldrLeft.addProperty("B", ".*");
bldrRight.addProperty("B", ".*");
bldrRight.addProperty("C", ".*");
assertTrue(matcher.isPropertiesCompatible(TOSCAModelHelper.createTOSCANodeOnlyProperties(bldrLeft), TOSCAModelHelper.createTOSCANodeOnlyProperties(bldrRight)));
// other way round
assertFalse(matcher.isPropertiesCompatible(TOSCAModelHelper.createTOSCANodeOnlyProperties(bldrRight), TOSCAModelHelper.createTOSCANodeOnlyProperties(bldrLeft)));
// String repositoryURI = "http://localhost:8080/winery";
//
// boolean USE_PROXY = false;
//
// IWineryRepositoryClient client = new WineryRepositoryClient(USE_PROXY);
//
// client.addRepository(repositoryURI);
//
// TTopologyTemplate testTemplate = client.getTopologyTemplate(new QName( "http://opentosca.org/compliancerules","TestTemplate"));
//
// TNodeTemplate.Policies leftPolicies;
// TPolicy policy;
}
use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class TOSCAGraphIsomorphismTest method testServiceTemplateComplianceRuleChecker.
@Test
public void testServiceTemplateComplianceRuleChecker() throws IOException {
HashMap<DefinitionsChildId, TExtensibleElements> allEntities = new HashMap<>();
TServiceTemplate tServiceTemplate = createTServiceTemplate("ServiceTemplateTestId", TEST_TARGET_NAMESPACE);
ComplianceRuleId crId1 = new ComplianceRuleId(new QName(TEST_TARGET_NAMESPACE, "test1"));
ComplianceRuleId crId2 = new ComplianceRuleId(new QName(TEST_TARGET_NAMESPACE, "test2"));
ComplianceRuleId crId3 = new ComplianceRuleId(new QName(TEST_TARGET_NAMESPACE, "test3"));
// create NodeType
NodeTypeId nodeTypeIdA = createNodeTypeId("IDA");
NodeTypeId nodeTypeIdB = createNodeTypeId("IDB");
NodeTypeId nodeTypeIdC = createNodeTypeId("IDC");
NodeTypeId nodeTypeIdD = createNodeTypeId("IDD");
TNodeType nodeTypeA = createTNodeType(nodeTypeIdA);
allEntities.put(nodeTypeIdA, nodeTypeA);
TNodeType nodeTypeB = createTNodeType(nodeTypeIdB);
setDerivedFrom(nodeTypeIdA, nodeTypeB);
allEntities.put(nodeTypeIdB, nodeTypeB);
TNodeType nodeTypeC = createTNodeType(nodeTypeIdC);
setDerivedFrom(nodeTypeIdA, nodeTypeC);
allEntities.put(nodeTypeIdC, nodeTypeC);
TNodeType nodeTypeD = createTNodeType(nodeTypeIdD);
allEntities.put(nodeTypeIdD, nodeTypeD);
TNodeTemplate ruleOneIdentifier = createTNodeTemplate("R1_I", nodeTypeIdA);
TNodeTemplate ruleOneRequiredStructure = createTNodeTemplate("R1_RS", nodeTypeIdB);
TNodeTemplate ruleTwoIdentifier = createTNodeTemplate("R2_I", nodeTypeIdA);
TNodeTemplate ruleTwoRequiredStructure = createTNodeTemplate("R2_RS", nodeTypeIdC);
TNodeTemplate ruleThreeIdentifier = createTNodeTemplate("R3_I", nodeTypeIdA);
TNodeTemplate ruleThreeRequiredStructure = createTNodeTemplate("R3_RS", nodeTypeIdD);
TNodeTemplate serviceTemplateTopology = createTNodeTemplate("ST", nodeTypeIdB);
// satisfied
TComplianceRule ruleOne = createTComplianceRule(crId1);
ruleOne.setIdentifier(createTTopologyTemplate(Arrays.asList(ruleOneIdentifier), newArrayList()));
ruleOne.setRequiredStructure(createTTopologyTemplate(Arrays.asList(ruleOneRequiredStructure), newArrayList()));
allEntities.put(crId1, ruleOne);
// unsatisfied
TComplianceRule ruleTwo = createTComplianceRule(crId2);
ruleTwo.setIdentifier(createTTopologyTemplate(Arrays.asList(ruleTwoIdentifier), newArrayList()));
ruleTwo.setRequiredStructure(createTTopologyTemplate(Arrays.asList(ruleTwoRequiredStructure), newArrayList()));
allEntities.put(crId2, ruleTwo);
// invalid rule
TComplianceRule ruleThree = createTComplianceRule(crId3);
ruleThree.setIdentifier(createTTopologyTemplate(Arrays.asList(ruleThreeIdentifier), newArrayList()));
ruleThree.setRequiredStructure(createTTopologyTemplate(Arrays.asList(ruleThreeRequiredStructure), newArrayList()));
allEntities.put(crId3, ruleThree);
tServiceTemplate.setTopologyTemplate(createTTopologyTemplate(Arrays.asList(serviceTemplateTopology), newArrayList()));
persist(allEntities);
ServiceTemplateComplianceRuleRuleChecker checker = new ServiceTemplateComplianceRuleRuleChecker(tServiceTemplate);
assertEquals(3, checker.getRuleIds(tServiceTemplate).stream().count());
assertEquals(3, checker.getRuleIds(tServiceTemplate).stream().filter(id -> id.getQName().getLocalPart().matches("test1|test2|test3")).count());
ServiceTemplateCheckingResult serviceTemplateCheckingResult = checker.checkComplianceRules();
assertEquals(1, serviceTemplateCheckingResult.getSatisfied().size());
assertEquals(1, serviceTemplateCheckingResult.getUnsatisfied().size());
assertEquals(1, serviceTemplateCheckingResult.getException().size());
JAXBContext jaxbContext = null;
try {
jaxbContext = JAXBContext.newInstance(ServiceTemplateCheckingResult.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(serviceTemplateCheckingResult, sw);
System.out.println(sw.toString());
} catch (JAXBException e) {
e.printStackTrace();
}
}
use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class WriterUtils method storeDefinitions.
public static void storeDefinitions(Definitions definitions, boolean overwrite, Path dir) {
Path path = null;
try {
path = Files.createTempDirectory("winery");
} catch (IOException e) {
e.printStackTrace();
}
LOGGER.debug("Store definition: {}", definitions.getId());
saveDefinitions(definitions, path, definitions.getTargetNamespace(), definitions.getId());
Definitions cleanDefinitions = loadDefinitions(path, definitions.getTargetNamespace(), definitions.getId());
CsarImporter csarImporter = new CsarImporter();
List<Exception> exceptions = new ArrayList<>();
cleanDefinitions.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().forEach(entry -> {
String namespace = csarImporter.getNamespace(entry, definitions.getTargetNamespace());
csarImporter.setNamespace(entry, namespace);
String id = ModelUtilities.getId(entry);
Class<? extends DefinitionsChildId> widClazz = Util.getComponentIdClassForTExtensibleElements(entry.getClass());
final DefinitionsChildId wid = BackendUtils.getDefinitionsChildId(widClazz, namespace, id, false);
if (RepositoryFactory.getRepository().exists(wid)) {
if (overwrite) {
try {
RepositoryFactory.getRepository().forceDelete(wid);
} catch (IOException e) {
exceptions.add(e);
}
} else {
return;
}
}
if (entry instanceof TArtifactTemplate) {
TArtifactTemplate.ArtifactReferences artifactReferences = ((TArtifactTemplate) entry).getArtifactReferences();
Stream.of(artifactReferences).filter(Objects::nonNull).flatMap(ref -> ref.getArtifactReference().stream()).filter(Objects::nonNull).forEach(ref -> {
String reference = ref.getReference();
URI refURI;
try {
refURI = new URI(reference);
} catch (URISyntaxException e) {
LOGGER.error("Invalid URI {}", reference);
return;
}
if (refURI.isAbsolute()) {
return;
}
Path artifactPath = dir.resolve(reference);
if (!Files.exists(artifactPath)) {
LOGGER.error("File not found {}", artifactPath);
return;
}
ArtifactTemplateFilesDirectoryId aDir = new ArtifactTemplateFilesDirectoryId((ArtifactTemplateId) wid);
RepositoryFileReference aFile = new RepositoryFileReference(aDir, artifactPath.getFileName().toString());
MediaType mediaType = null;
try (InputStream is = Files.newInputStream(artifactPath);
BufferedInputStream bis = new BufferedInputStream(is)) {
mediaType = BackendUtils.getMimeType(bis, artifactPath.getFileName().toString());
RepositoryFactory.getRepository().putContentToFile(aFile, bis, mediaType);
} catch (IOException e) {
LOGGER.error("Could not read artifact template file: {}", artifactPath);
return;
}
});
}
final Definitions part = BackendUtils.createWrapperDefinitions(wid);
part.getServiceTemplateOrNodeTypeOrNodeTypeImplementation().add(entry);
RepositoryFileReference ref = BackendUtils.getRefOfDefinitions(wid);
String content = BackendUtils.getXMLAsString(part, true);
try {
RepositoryFactory.getRepository().putContentToFile(ref, content, MediaTypes.MEDIATYPE_TOSCA_DEFINITIONS);
} catch (Exception e) {
exceptions.add(e);
}
});
}
use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class IGenericRepository method getReferencedDefinitionsChildIds.
default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(RelationshipTypeImplementationId id) {
// We have to use a HashSet to ensure that no duplicate ids are added
// There may be multiple IAs referencing the same type
Collection<DefinitionsChildId> ids = new HashSet<>();
final TRelationshipTypeImplementation element = this.getElement(id);
// IAs
return this.getReferencedTOSCAComponentImplementationArtifactIds(ids, element.getImplementationArtifacts(), id);
}
use of org.eclipse.winery.common.ids.definitions.DefinitionsChildId in project winery by eclipse.
the class IGenericRepository method getReferencedDefinitionsChildIds.
default Collection<DefinitionsChildId> getReferencedDefinitionsChildIds(ServiceTemplateId id) {
// We have to use a HashSet to ensure that no duplicate ids are added
// E.g., there may be multiple relationship templates having the same type
Collection<DefinitionsChildId> ids = new HashSet<>();
TServiceTemplate serviceTemplate = this.getElement(id);
// add included things to export queue
TBoundaryDefinitions boundaryDefs;
if ((boundaryDefs = serviceTemplate.getBoundaryDefinitions()) != null) {
TBoundaryDefinitions.Policies policies = boundaryDefs.getPolicies();
if (policies != null) {
for (TPolicy policy : policies.getPolicy()) {
PolicyTypeId policyTypeId = new PolicyTypeId(policy.getPolicyType());
ids.add(policyTypeId);
PolicyTemplateId policyTemplateId = new PolicyTemplateId(policy.getPolicyRef());
ids.add(policyTemplateId);
}
}
// reqs and caps don't have to be exported here as they are references to existing reqs/caps (of nested node templates)
}
if (serviceTemplate.getTopologyTemplate() != null) {
for (TEntityTemplate entityTemplate : serviceTemplate.getTopologyTemplate().getNodeTemplateOrRelationshipTemplate()) {
QName qname = entityTemplate.getType();
if (entityTemplate instanceof TNodeTemplate) {
ids.add(new NodeTypeId(qname));
TNodeTemplate n = (TNodeTemplate) entityTemplate;
// crawl through deployment artifacts
TDeploymentArtifacts deploymentArtifacts = n.getDeploymentArtifacts();
if (deploymentArtifacts != null) {
List<TDeploymentArtifact> das = deploymentArtifacts.getDeploymentArtifact();
for (TDeploymentArtifact da : das) {
ids.add(new ArtifactTypeId(da.getArtifactType()));
if ((qname = da.getArtifactRef()) != null) {
ids.add(new ArtifactTemplateId(qname));
}
}
}
// crawl through reqs/caps
TNodeTemplate.Requirements requirements = n.getRequirements();
if (requirements != null) {
for (TRequirement req : requirements.getRequirement()) {
QName type = req.getType();
RequirementTypeId rtId = new RequirementTypeId(type);
ids.add(rtId);
}
}
TNodeTemplate.Capabilities capabilities = n.getCapabilities();
if (capabilities != null) {
for (TCapability cap : capabilities.getCapability()) {
QName type = cap.getType();
CapabilityTypeId ctId = new CapabilityTypeId(type);
ids.add(ctId);
}
}
// crawl through policies
org.eclipse.winery.model.tosca.TNodeTemplate.Policies policies = n.getPolicies();
if (policies != null) {
for (TPolicy pol : policies.getPolicy()) {
QName type = pol.getPolicyType();
PolicyTypeId ctId = new PolicyTypeId(type);
ids.add(ctId);
QName template = pol.getPolicyRef();
if (template != null) {
PolicyTemplateId policyTemplateId = new PolicyTemplateId(template);
ids.add(policyTemplateId);
}
}
}
} else {
assert (entityTemplate instanceof TRelationshipTemplate);
ids.add(new RelationshipTypeId(qname));
}
}
}
return ids;
}
Aggregations