use of org.eclipse.winery.common.ids.definitions.NodeTypeId 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.NodeTypeId 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.NodeTypeId 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;
}
use of org.eclipse.winery.common.ids.definitions.NodeTypeId in project winery by eclipse.
the class GenericArtifactsResource method generateImplementationArtifact.
/**
* Generates the implementation artifact using the implementation artifact generator. Also sets the proeprties
* according to the requirements of OpenTOSCA.
* <p>
* DOES NOT WORK FOR RELATION SHIP TYPE IMPLEMENTATIONS
*
* @param artifactTemplateResource the resource associated with the artifactTemplateId. If null, the object is
* created in this method
*/
private Response generateImplementationArtifact(String interfaceNameStr, String javapackage, UriInfo uriInfo, ArtifactTemplateId artifactTemplateId) {
TInterface iface;
assert (this instanceof ImplementationArtifactsResource);
QName type = RestUtils.getType(this.res);
NodeTypeId typeId;
// required for IA Generation
typeId = new NodeTypeId(type);
final IRepository repository = RepositoryFactory.getRepository();
TNodeType nodeType = repository.getElement(typeId);
List<TInterface> interfaces = nodeType.getInterfaces().getInterface();
Iterator<TInterface> it = interfaces.iterator();
do {
iface = it.next();
if (iface.getName().equals(interfaceNameStr)) {
break;
}
} while (it.hasNext());
// iface now contains the right interface
ArtifactTemplateSourceDirectoryId sourceDirectoryId = new ArtifactTemplateSourceDirectoryId(artifactTemplateId);
Path workingDir;
try {
workingDir = Files.createTempDirectory("winery");
} catch (IOException e2) {
GenericArtifactsResource.LOGGER.debug("Could not create temporary directory", e2);
return Response.serverError().entity("Could not create temporary directory").build();
}
URI artifactTemplateFilesUri = uriInfo.getBaseUri().resolve(RestUtils.getAbsoluteURL(artifactTemplateId)).resolve("files/");
URL artifactTemplateFilesUrl;
try {
artifactTemplateFilesUrl = artifactTemplateFilesUri.toURL();
} catch (MalformedURLException e2) {
GenericArtifactsResource.LOGGER.debug("Could not convert URI to URL", e2);
return Response.serverError().entity("Could not convert URI to URL").build();
}
String name = this.generateName(typeId, interfaceNameStr);
Generator gen = new Generator(iface, javapackage, artifactTemplateFilesUrl, name, workingDir.toFile());
Path targetPath;
try {
targetPath = gen.generateProject();
} catch (Exception e) {
LOGGER.debug("IA generator failed", e);
return Response.serverError().entity("IA generator failed").build();
}
DirectoryId fileDir = new ArtifactTemplateSourceDirectoryId(artifactTemplateId);
try {
BackendUtils.importDirectory(targetPath, repository, fileDir);
} catch (IOException e) {
throw new WebApplicationException(e);
}
// cleanup dir
FileUtils.forceDelete(workingDir);
this.storeProperties(artifactTemplateId, typeId, name);
URI url = uriInfo.getBaseUri().resolve(Util.getUrlPath(artifactTemplateId));
return Response.created(url).build();
}
Aggregations