+ logInfoMsg(getType().toString() + " " + getUniqueIdentifier() + " successfully ingested.");
+ } else {
+ logErrorMsg(
+ FAILURE_MSG_PREFIX + getType().toString() + " " + getUniqueIdentifier() + ROLLBACK_MSG_SUFFIX);
+ }
+ }
+
+ return success;
+ }
+
+ /**
+ * @param aaiClient
+ * @param config
+ * @param distId
+ * @param completedArtifacts
+ * @return
+ */
+ private boolean pushModelVersion(AaiRestClient aaiClient, ModelLoaderConfig config, String distId,
+ List<Artifact> completedArtifacts) {
+ if (xmlResourceCanBeFetched(aaiClient, distId, getModelVerUrl(config))) {
+ logInfoMsg(getType().toString() + " " + getUniqueIdentifier() + " already exists. Skipping ingestion.");
+ return true;
+ }
+
+ // Load the model version
+ boolean success = true;
+ try {
+ success = putXmlResource(aaiClient, distId, getModelVerUrl(config), nodeToString(getModelVer()));
+ if (success) {
+ completedArtifacts.add(this);
+ logInfoMsg(getType().toString() + " " + getUniqueIdentifier() + " successfully ingested.");
+ } else {
+ logErrorMsg(
+ FAILURE_MSG_PREFIX + getType().toString() + " " + getUniqueIdentifier() + ROLLBACK_MSG_SUFFIX);
+ }
+ } catch (TransformerException e) {
+ logErrorMsg(FAILURE_MSG_PREFIX + getType().toString() + " " + getUniqueIdentifier() + ": " + e.getMessage()
+ + ROLLBACK_MSG_SUFFIX);
+ success = false;
+ }
+
+ return success;
+ }
+
+
+ @Override
+ public void rollbackModel(AaiRestClient aaiClient, ModelLoaderConfig config, String distId) {
+ // Gizmo is resilient and doesn't require a rollback. A redistribution will work fine even if
+ // the model is partially loaded.
+ if (config.useGizmo()) {
+ return;
+ }
+
+ String url = getModelVerUrl(config);
+ if (firstVersionOfModel) {
+ // If this was the first version of the model which was added, we want to remove the entire
+ // model rather than just the version.
+ url = getModelUrl(config);
+ }
+
+ // Best effort to delete. Nothing we can do in the event this fails.
+ aaiClient.getAndDeleteResource(url, distId);
+ }
+
+ private String getModelUrl(ModelLoaderConfig config) {
+ String baseURL = config.getAaiBaseUrl().trim();
+ String subURL = config.getAaiModelUrl(getModelNamespaceVersion()).trim();
+ String instance = getModelInvariantId();
+
+ if (!baseURL.endsWith("/") && !subURL.startsWith("/")) {
+ baseURL = baseURL + "/";
+ }
+
+ if (baseURL.endsWith("/") && subURL.startsWith("/")) {
+ baseURL = baseURL.substring(0, baseURL.length() - 1);
+ }
+
+ if (!subURL.endsWith("/")) {
+ subURL = subURL + "/";
+ }
+
+ return baseURL + subURL + instance;
+ }
+
+ private String getModelVerUrl(ModelLoaderConfig config) {
+ String baseURL = config.getAaiBaseUrl().trim();
+ String subURL = config.getAaiModelUrl(getModelNamespaceVersion()).trim() + getModelInvariantId()
+ + AAI_MODEL_VER_SUB_URL;
+ String instance = getModelVerId();
+
+ if (!baseURL.endsWith("/") && !subURL.startsWith("/")) {
+ baseURL = baseURL + "/";
+ }
+
+ if (baseURL.endsWith("/") && subURL.startsWith("/")) {
+ baseURL = baseURL.substring(0, baseURL.length() - 1);
+ }
+
+ if (!subURL.endsWith("/")) {
+ subURL = subURL + "/";
+ }
+
+ return baseURL + subURL + instance;
+ }
+
+ private String nodeToString(Node node) throws TransformerException {
+ StringWriter sw = new StringWriter();
+ TransformerFactory transFact = TransformerFactory.newInstance();
+ transFact.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
+ Transformer t = transFact.newTransformer();
+ t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+ t.transform(new DOMSource(node), new StreamResult(sw));
+ return sw.toString();
+ }