package org.onap.clamp.clds.model;
+import com.google.gson.annotations.Expose;
+
import java.util.List;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.clamp.clds.client.req.policy.PolicyClient;
+import org.onap.clamp.clds.config.ClampProperties;
import org.onap.clamp.clds.dao.CldsDao;
+import org.onap.clamp.clds.tosca.ToscaYamlToJsonConvertor;
public class CldsToscaModel extends CldsToscaModelRevision {
+ @Expose
private String id;
+ @Expose
private String policyType;
+ @Expose
private String toscaModelName;
- private String toscaModelYaml;
/**
- * Creates or updates Tosca Model to DB
+ * Construct.
+ */
+ public CldsToscaModel() {
+ }
+
+ /**
+ * Creates or updates Tosca Model to DB.
*
- * @param cldsDao
- * @param userId
+ * @param cldsDao The cldsDao
+ * @param userId The user Id
*/
- public CldsToscaModel save(CldsDao cldsDao, String userId) {
+ public CldsToscaModel save(CldsDao cldsDao, ClampProperties refProp, PolicyClient policyClient, String userId) {
CldsToscaModel cldsToscaModel = null;
- // TODO tosca parsing logic
- this.setToscaModelJson("{}");
- this.setPolicyType("Aging");// TODO update with subString or node_type from the model name
+ refProp.getStringList("tosca.policyTypes", ",").stream().forEach(policyType -> {
+ if (StringUtils.containsIgnoreCase(this.getToscaModelName(), policyType)) {
+ this.setPolicyType(policyType);
+ }
+ });
+
+ ToscaYamlToJsonConvertor convertor = new ToscaYamlToJsonConvertor(cldsDao);
+ this.setToscaModelJson(convertor.parseToscaYaml(this.getToscaModelYaml()));
List<CldsToscaModel> toscaModels = cldsDao.getToscaModelByName(this.getToscaModelName());
if (toscaModels != null && !toscaModels.isEmpty()) {
CldsToscaModel toscaModel = toscaModels.stream().findFirst().get();
- // CldsToscaModelRevision modelRevision =
- // revisions.stream().max(Comparator.comparingDouble(CldsToscaModelRevision::getVersion)).get();
this.setVersion(incrementVersion(toscaModel.getVersion()));
this.setId(toscaModel.getId());
this.setUserId(userId);
+ if (refProp.getStringValue("import.tosca.model").equalsIgnoreCase("true")) {
+ policyClient.importToscaModel(this);
+ }
cldsToscaModel = cldsDao.updateToscaModelWithNewVersion(this, userId);
} else {
this.setVersion(1);
+ if (refProp.getStringValue("import.tosca.model").equalsIgnoreCase("true")) {
+ policyClient.importToscaModel(this);
+ }
cldsToscaModel = cldsDao.insToscaModel(this, userId);
}
return cldsToscaModel;
}
/**
+ * Get the Id.
+ *
* @return the id
*/
public String getId() {
}
/**
- * @param id
- * the id to set
+ * Set the id.
+ *
+ * @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
+ * Get the policy type.
+ *
* @return the policyType
*/
public String getPolicyType() {
}
/**
- * @param policyType
- * the policyType to set
+ * Set the policy type.
+ *
+ * @param policyType the policyType to set
*/
public void setPolicyType(String policyType) {
this.policyType = policyType;
}
/**
+ * Get the tosca model name.
+ *
* @return the toscaModelName
*/
public String getToscaModelName() {
}
/**
- * @param toscaModelName
- * the toscaModelName to set
+ * Set the tosca model name.
+ *
+ * @param toscaModelName the toscaModelName to set
*/
public void setToscaModelName(String toscaModelName) {
this.toscaModelName = toscaModelName;
}
- /**
- * @return the toscaModelYaml
- */
- @Override
- public String getToscaModelYaml() {
- return toscaModelYaml;
- }
-
- /**
- * @param toscaModelYaml
- * the toscaModelYaml to set
- */
- @Override
- public void setToscaModelYaml(String toscaModelYaml) {
- this.toscaModelYaml = toscaModelYaml;
- }
-
}