Skip to content

Commit

Permalink
fix handling of properties of artifactTemplates (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
zimmerml authored Mar 23, 2021
1 parent 6f0388a commit f746b00
Showing 1 changed file with 66 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,70 +17,74 @@

public class Util {

private final static Logger LOG = LoggerFactory.getLogger(Util.class);
private final static Logger LOG = LoggerFactory.getLogger(Util.class);

/**
* Determine the ServiceTemplateInstanceId long from the ServiceInstanceId QName.
*
* @return the ServiceTemplateInstanceId if the retrieval is successful, <code>Long.MIN_VALUE</code> otherwise
*/
public static long determineServiceTemplateInstanceId(final URI serviceInstanceID) {
if (Objects.nonNull(serviceInstanceID)) {
try {
if (serviceInstanceID.toString().contains("/")) {
return Long.parseLong(StringUtils.substringAfterLast(serviceInstanceID.toString(), "/"));
} else {
return Long.parseLong(serviceInstanceID.toString());
}
} catch (final NumberFormatException e) {
LOG.error("Unable to parse ServiceTemplateInstance ID out of serviceInstanceID: {}", serviceInstanceID);
}
} else {
LOG.error("Unable to parse ServiceTemplateInstance ID out of serviceInstanceID because it is null!");
}
return Long.MIN_VALUE;
}
/**
* Determine the ServiceTemplateInstanceId long from the ServiceInstanceId
* QName.
*
* @return the ServiceTemplateInstanceId if the retrieval is successful,
* <code>Long.MIN_VALUE</code> otherwise
*/
public static long determineServiceTemplateInstanceId(final URI serviceInstanceID) {
if (Objects.nonNull(serviceInstanceID)) {
try {
if (serviceInstanceID.toString().contains("/")) {
return Long.parseLong(StringUtils.substringAfterLast(serviceInstanceID.toString(), "/"));
} else {
return Long.parseLong(serviceInstanceID.toString());
}
} catch (final NumberFormatException e) {
LOG.error("Unable to parse ServiceTemplateInstance ID out of serviceInstanceID: {}", serviceInstanceID);
}
} else {
LOG.error("Unable to parse ServiceTemplateInstance ID out of serviceInstanceID because it is null!");
}
return Long.MIN_VALUE;
}

/**
* Checks if a certain property was specified in the Tosca.xml of the ArtifactTemplate and returns it if so.
*
* @param artifactTemplate the ID of the ArtifactTemplate
* @param propertyName the name of the property
* @return the property value if specified, null otherwise
*/
public static String getProperty(final TArtifactTemplate artifactTemplate, final String propertyName) {
final Document properties = ToscaEngine.getEntityTemplateProperties(artifactTemplate);
// check if there are specified properties at all
if (properties == null) {
return null;
}
/**
* Checks if a certain property was specified in the Tosca.xml of the
* ArtifactTemplate and returns it if so.
*
* @param artifactTemplate the ID of the ArtifactTemplate
* @param propertyName the name of the property
* @return the property value if specified, null otherwise
*/
public static String getProperty(final TArtifactTemplate artifactTemplate, final String propertyName) {
final Document properties = ToscaEngine.getEntityTemplateProperties(artifactTemplate);
// check if there are specified properties at all
if (properties == null || !properties.hasChildNodes()) {
return null;
}

final NodeList list = properties.getFirstChild().getChildNodes();
// iterate through properties and check name
for (int i = 0; i < list.getLength(); i++) {
final Node propNode = list.item(i);
final String localName = propNode.getLocalName();
if (localName != null && localName.equals(propertyName)) {
return propNode.getTextContent().trim();
}
}
return null;
}
final NodeList list = properties.getFirstChild().getChildNodes();
// iterate through properties and check name
for (int i = 0; i < list.getLength(); i++) {
final Node propNode = list.item(i);
final String localName = propNode.getLocalName();
if (localName != null && localName.equals(propertyName)) {
return propNode.getTextContent().trim();
}
}
return null;
}

/**
* Checks if a PortType property was specified in the Tosca.xml of the ArtifactTemplate and returns it if so.
*
* @param artifactTemplate the ArtifactTemplate
* @return the PortType property value as QName if specified, null otherwise
*/
public static QName getPortTypeQName(final TArtifactTemplate artifactTemplate) {
try {
QName portType = QName.valueOf(getProperty(artifactTemplate, "PortType"));
LOG.debug("PortType property: {}", portType.toString());
return portType;
} catch (final IllegalArgumentException e) {
LOG.warn("PortType property can not be parsed to QName.");
}
return null;
}
/**
* Checks if a PortType property was specified in the Tosca.xml of the
* ArtifactTemplate and returns it if so.
*
* @param artifactTemplate the ArtifactTemplate
* @return the PortType property value as QName if specified, null otherwise
*/
public static QName getPortTypeQName(final TArtifactTemplate artifactTemplate) {
try {
QName portType = QName.valueOf(getProperty(artifactTemplate, "PortType"));
LOG.debug("PortType property: {}", portType.toString());
return portType;
} catch (final IllegalArgumentException e) {
LOG.warn("PortType property can not be parsed to QName.");
}
return null;
}
}

0 comments on commit f746b00

Please sign in to comment.