use of org.eclipse.winery.model.tosca.TServiceTemplate in project winery by eclipse.
the class ServiceTemplateComplianceRuleRuleChecker method getRuleIds.
public List<ComplianceRuleId> getRuleIds(TServiceTemplate serviceTemplate) {
List<ComplianceRuleId> complianceRules = Lists.newArrayList();
Namespace namespace = new Namespace(serviceTemplate.getTargetNamespace(), false);
Collection<Namespace> componentsNamespaces = RepositoryFactory.getRepository().getComponentsNamespaces(ComplianceRuleId.class);
List<Namespace> relevantNamespaces = componentsNamespaces.stream().filter(ns -> namespace.getDecoded().startsWith(ns.getDecoded())).collect(Collectors.toList());
for (Namespace space : relevantNamespaces) {
complianceRules.addAll((Collection<? extends ComplianceRuleId>) ((FilebasedRepository) RepositoryFactory.getRepository()).getAllIdsInNamespace(ComplianceRuleId.class, space));
}
return complianceRules;
}
use of org.eclipse.winery.model.tosca.TServiceTemplate 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());
}
use of org.eclipse.winery.model.tosca.TServiceTemplate 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.model.tosca.TServiceTemplate in project winery by eclipse.
the class BackendUtils method injectArtifactTemplateIntoDeploymentArtifact.
public static boolean injectArtifactTemplateIntoDeploymentArtifact(ServiceTemplateId serviceTemplate, String nodeTemplateId, String deploymentArtifactId, ArtifactTemplateId artifactTemplate) throws IOException {
TServiceTemplate element = RepositoryFactory.getRepository().getElement(serviceTemplate);
element.getTopologyTemplate().getNodeTemplate(nodeTemplateId).getDeploymentArtifacts().getDeploymentArtifact(deploymentArtifactId).setArtifactRef(artifactTemplate.getQName());
RepositoryFactory.getRepository().setElement(serviceTemplate, element);
return true;
}
use of org.eclipse.winery.model.tosca.TServiceTemplate 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