use of org.eclipse.winery.model.tosca.TEntityTemplate in project container by OpenTOSCA.
the class ToscaEngine method getEntityTemplateProperties.
@Nullable
public static Document getEntityTemplateProperties(TEntityTemplate template) {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
Document doc = null;
try {
doc = documentBuilderFactory.newDocumentBuilder().newDocument();
} catch (ParserConfigurationException e) {
LOG.error("Couldn't create parser", e);
return null;
}
if (template.getProperties() instanceof TEntityTemplate.WineryKVProperties) {
TEntityTemplate.WineryKVProperties props = (TEntityTemplate.WineryKVProperties) template.getProperties();
Map<String, String> propMap = props.getKVProperties();
// So just that people understand:
/*
<Properties>
<Properties xmlns="http://opentosca.org/nodetypes/propertiesdefinition/winery">
<ContainerPort>80</ContainerPort>
<Port>get_input: ApplicationPort</Port>
<ContainerID/>
<ContainerIP/>
</Properties>
</Properties>
<Properties>
<DockerEngine_Properties xmlns="http://opentosca.org/nodetypes/properties">
<DockerEngineURL>get_input: DockerEngineURL</DockerEngineURL>
<DockerEngineCertificate/>
<State>Running</State>
</DockerEngine_Properties>
</Properties>
In case of the DockerEngine Properties props.getElementName() returns DockerEngine_Properties
But
in case of the MyTinyToDoDocker Container Properties props.getElementName() returns NULL!!
TODO: FIXME in winery!
*/
Element rootElement = doc.createElementNS(props.getNamespace(), props.getElementName() != null ? props.getElementName() : "Properties");
doc.appendChild(rootElement);
for (String propName : propMap.keySet()) {
Element propElement = doc.createElementNS(props.getNamespace(), propName);
propElement.setTextContent(propMap.get(propName));
rootElement.appendChild(propElement);
}
}
return doc;
}
use of org.eclipse.winery.model.tosca.TEntityTemplate in project container by OpenTOSCA.
the class RulesChecker method checkRules.
private boolean checkRules(final List<TServiceTemplate> stRuleList, final String ruleType, final TServiceTemplate serviceTemplate, final List<TParameterDTO> inputParameters) throws ParserConfigurationException {
for (final TServiceTemplate stRule : stRuleList) {
LOG.debug("Checking Rule: " + stRule.getName() + " RuleType: " + ruleType);
final TTopologyTemplate rule = stRule.getTopologyTemplate();
final List<TEntityTemplate> templateRuleList = rule.getNodeTemplateOrRelationshipTemplate();
for (final TEntityTemplate templateRule : templateRuleList) {
if (!(templateRule instanceof TRelationshipTemplate)) {
continue;
}
final TRelationshipTemplate relationshipRule = (TRelationshipTemplate) templateRule;
final TNodeTemplate sourceRuleNTemplate = (TNodeTemplate) relationshipRule.getSourceElement().getRef();
final TNodeTemplate targetRuleNTemplate = (TNodeTemplate) relationshipRule.getTargetElement().getRef();
boolean ruleCanBeApplied = false;
// check for types
if (sourceRuleNTemplate.getId().equals("*")) {
final List<TNodeTemplate> nodeTemplates = serviceTemplate.getTopologyTemplate().getNodeTemplates();
for (final TNodeTemplate nodeTemplate : nodeTemplates) {
final QName nodeType = nodeTemplate.getType();
// found matching nodetemplate
if (nodeType.equals(sourceRuleNTemplate.getType())) {
LOG.debug("Rule " + stRule.getName() + " can be applied to Service Template: " + serviceTemplate + ". Reason: Matching Source NodeTypes.");
ruleCanBeApplied = true;
}
}
// check for identical IDs
} else {
// check source
if (ToscaEngine.getNodeTemplate(serviceTemplate, sourceRuleNTemplate.getId()).isPresent()) {
LOG.debug("Rule " + stRule.getName() + " can be applied to Service Template: " + serviceTemplate + ". Reason: Matching Source NodeTemplateIDs.");
ruleCanBeApplied = true;
}
}
if (!ruleCanBeApplied) {
LOG.debug("Rule " + stRule.getName() + " can not be applied to Service Template: " + serviceTemplate + ".Thus, rule is ignored.");
continue;
}
if (targetRuleNTemplate.getId().equals("*")) {
targetRuleNTemplate.getType();
boolean found = false;
while (!found) {
TNodeTemplate targetNT = ToscaEngine.getRelatedNodeTemplate(serviceTemplate, sourceRuleNTemplate, relationshipRule.getType());
if (targetNT == null) {
switch(ruleType) {
case "white":
LOG.warn("Target Node Template not found. Rule not fulfilled.");
return false;
case "black":
LOG.debug("Nodes are not matching. Rule is fulfilled.");
break;
}
} else {
final QName relatedNodeType = targetNT.getType();
if (relatedNodeType.equals(targetRuleNTemplate.getType())) {
found = true;
LOG.debug("Matching Target Node Type found. Node Template: " + targetNT);
// comparing properties
if (arePropertiesMatching(sourceRuleNTemplate, inputParameters, targetRuleNTemplate)) {
switch(ruleType) {
case "white":
LOG.debug("Properties are matching. Rule is fulfilled.");
break;
case "black":
LOG.warn("Rule is not fulfilled. Aborting the Provisioning. Reason: Properties are matching.");
return false;
}
} else {
switch(ruleType) {
case "white":
LOG.warn("Rule is not fulfilled. Aborting the Provisioning. Reason: Properties are not matching.");
return false;
case "black":
LOG.debug("Properties are not matching. Rule is fulfilled.");
break;
}
}
}
}
}
// check target nodetemplateID
} else {
Optional<TNodeTemplate> nodeTemplate = ToscaEngine.getNodeTemplate(serviceTemplate, targetRuleNTemplate.getId());
if (nodeTemplate.isPresent()) {
// comparing properties
if (arePropertiesMatching(sourceRuleNTemplate, inputParameters, targetRuleNTemplate)) {
switch(ruleType) {
case "white":
LOG.debug("Properties are matching. Rule is fulfilled.");
break;
case "black":
LOG.warn("Rule is not fulfilled. Aborting the Provisioning. Reason: Properties are matching.");
return false;
}
} else {
switch(ruleType) {
case "white":
LOG.warn("Rule is not fulfilled. Aborting the Provisioning. Reason: Properties are not matching.");
return false;
case "black":
LOG.debug("Properties are not matching. Rule is fulfilled.");
break;
}
}
// if source is matching but target isn't, abort
} else {
switch(ruleType) {
case "white":
LOG.warn("Rule is not fulfilled. Aborting the Provisioning. Reason: Source is matching, but target isn't.");
return false;
case "black":
LOG.debug("Nodes are not matching. Rule is fulfilled.");
break;
}
}
}
}
}
return true;
}
Aggregations