use of org.onap.so.db.catalog.beans.HeatTemplateParam in project so by onap.
the class MsoHeatUtils method convertInputMap.
/**
* New with 1707 - this method will convert all the String *values* of the inputs to their "actual" object type
* (based on the param type: in the db - which comes from the template): (heat variable type) -> java Object type
* string -> String number -> Integer json -> marshal object to json comma_delimited_list -> ArrayList boolean ->
* Boolean if any of the conversions should fail, we will default to adding it to the inputs as a string - see if
* Openstack can handle it. Also, will remove any params that are extra. Any aliases will be converted to their
* appropriate name (anyone use this feature?)
*
* @param inputs - the Map<String, String> of the inputs received on the request
* @param template the HeatTemplate object - this is so we can also verify if the param is valid for this template
* @return HashMap<String, Object> of the inputs, cleaned and converted
*/
public Map<String, Object> convertInputMap(Map<String, Object> inputs, HeatTemplate template) {
HashMap<String, Object> newInputs = new HashMap<>();
HashMap<String, HeatTemplateParam> params = new HashMap<>();
HashMap<String, HeatTemplateParam> paramAliases = new HashMap<>();
if (inputs == null) {
return new HashMap<>();
}
for (HeatTemplateParam htp : template.getParameters()) {
params.put(htp.getParamName(), htp);
if (htp.getParamAlias() != null && !"".equals(htp.getParamAlias())) {
logger.debug("\tFound ALIAS {} -> {}", htp.getParamName(), htp.getParamAlias());
paramAliases.put(htp.getParamAlias(), htp);
}
}
for (String key : inputs.keySet()) {
boolean alias = false;
String realName = null;
if (!params.containsKey(key)) {
// add check here for an alias
if (!paramAliases.containsKey(key)) {
continue;
} else {
alias = true;
realName = paramAliases.get(key).getParamName();
}
}
String type = params.get(key).getParamType();
if (type == null || "".equals(type)) {
logger.debug("**PARAM_TYPE is null/empty for {}, will default to string", key);
type = "string";
}
if ("string".equalsIgnoreCase(type)) {
// Easiest!
String str = inputs.get(key) != null ? inputs.get(key).toString() : null;
if (alias)
newInputs.put(realName, str);
else
newInputs.put(key, str);
} else if ("number".equalsIgnoreCase(type)) {
String integerString = inputs.get(key) != null ? inputs.get(key).toString() : null;
Integer anInteger = null;
try {
anInteger = Integer.parseInt(integerString);
} catch (Exception e) {
logger.debug("Unable to convert {} to an integer!!", integerString, e);
anInteger = null;
}
if (anInteger != null) {
if (alias)
newInputs.put(realName, anInteger);
else
newInputs.put(key, anInteger);
} else {
if (alias)
newInputs.put(realName, integerString);
else
newInputs.put(key, integerString);
}
} else if ("json".equalsIgnoreCase(type)) {
Object jsonObj = inputs.get(key);
Object json;
try {
if (jsonObj instanceof String) {
json = JSON_MAPPER.readTree(jsonObj.toString());
} else {
// will already marshal to json without intervention
json = jsonObj;
}
} catch (IOException e) {
logger.error("failed to map to json, directly converting to string instead", e);
json = jsonObj.toString();
}
if (alias)
newInputs.put(realName, json);
else
newInputs.put(key, json);
} else if ("comma_delimited_list".equalsIgnoreCase(type)) {
String commaSeparated = inputs.get(key) != null ? inputs.get(key).toString() : null;
try {
List<String> anArrayList = this.convertCdlToArrayList(commaSeparated);
if (alias)
newInputs.put(realName, anArrayList);
else
newInputs.put(key, anArrayList);
} catch (Exception e) {
logger.debug("Unable to convert {} to an ArrayList!!", commaSeparated, e);
if (alias)
newInputs.put(realName, commaSeparated);
else
newInputs.put(key, commaSeparated);
}
} else if ("boolean".equalsIgnoreCase(type)) {
String booleanString = inputs.get(key) != null ? inputs.get(key).toString() : null;
Boolean aBool = Boolean.valueOf(booleanString);
if (alias)
newInputs.put(realName, aBool);
else
newInputs.put(key, aBool);
} else {
// it's null or something undefined - just add it back as a String
String str = inputs.get(key).toString();
if (alias)
newInputs.put(realName, str);
else
newInputs.put(key, str);
}
}
return newInputs;
}
use of org.onap.so.db.catalog.beans.HeatTemplateParam in project so by onap.
the class MsoHeatEnvironmentEntry method toFullStringExcludeNonParams.
public StringBuilder toFullStringExcludeNonParams(Set<HeatTemplateParam> params) {
// Basically give back the envt - but exclude the params that aren't in the HeatTemplate
StringBuilder sb = new StringBuilder();
ArrayList<String> paramNameList = new ArrayList<>(params.size());
for (HeatTemplateParam htp : params) {
paramNameList.add(htp.getParamName());
}
if (this.hasParameters()) {
sb.append("parameters:\n");
for (MsoHeatEnvironmentParameter hep : this.parameters) {
String paramName = hep.getName();
if (paramNameList.contains(paramName)) {
// This parameter *is* in the Heat Template - so include it:
sb.append(" " + hep.getName() + ": " + hep.getValue() + "\n");
// New - 1607 - if any of the params mapped badly - JUST RETURN THE ORIGINAL ENVT!
if (hep.getValue().startsWith("_BAD")) {
return this.rawEntry;
}
}
}
sb.append("\n");
}
// if (this.hasResources()) {
// sb.append("resource_registry:\n");
// for (MsoHeatEnvironmentResource her : this.resources) {
// sb.append(" \"" + her.getName() + "\": " + her.getValue() + "\n");
// }
// }
sb.append("\n");
sb.append(this.resourceRegistryEntryRaw);
return sb;
}
use of org.onap.so.db.catalog.beans.HeatTemplateParam in project so by onap.
the class MsoHeatUtilsUnitTest method convertInputMapTest.
@Test
public void convertInputMapTest() throws JsonParseException, JsonMappingException, IOException {
MsoHeatUtils utils = new MsoHeatUtils();
Map<String, Object> input = new HashMap<>();
HeatTemplate template = new HeatTemplate();
template.setArtifactUuid("my-uuid");
Set<HeatTemplateParam> parameters = template.getParameters();
HeatTemplateParam paramNum = new HeatTemplateParam();
paramNum.setParamType("number");
paramNum.setParamName("my-number");
input.put("my-number", "3");
HeatTemplateParam paramString = new HeatTemplateParam();
paramString.setParamType("string");
paramString.setParamName("my-string");
input.put("my-string", "hello");
HeatTemplateParam paramJson = new HeatTemplateParam();
paramJson.setParamType("json");
paramJson.setParamName("my-json");
HeatTemplateParam paramJsonEscaped = new HeatTemplateParam();
paramJsonEscaped.setParamType("json");
paramJsonEscaped.setParamName("my-json-escaped");
Map<String, Object> jsonMap = mapper.readValue(getJson("free-form.json"), new TypeReference<Map<String, Object>>() {
});
input.put("my-json", jsonMap);
input.put("my-json-escaped", getJson("free-form.json"));
parameters.add(paramNum);
parameters.add(paramString);
parameters.add(paramJson);
parameters.add(paramJsonEscaped);
Map<String, Object> output = utils.convertInputMap(input, template);
assertEquals(3, output.get("my-number"));
assertEquals("hello", output.get("my-string"));
assertTrue("expect no change in type", output.get("my-json") instanceof Map);
assertTrue("expect string to become jsonNode", output.get("my-json-escaped") instanceof JsonNode);
JSONAssert.assertEquals(getJson("free-form.json"), mapper.writeValueAsString(output.get("my-json-escaped")), false);
}
use of org.onap.so.db.catalog.beans.HeatTemplateParam in project so by onap.
the class MsoHeatUtilsUnitTest method convertInputMapNullsTest.
@Test
public final void convertInputMapNullsTest() {
MsoHeatUtils utils = new MsoHeatUtils();
Map<String, Object> inputs = new HashMap<>();
Set<HeatTemplateParam> params = new HashSet<>();
HeatTemplate ht = new HeatTemplate();
HeatTemplateParam htp = new HeatTemplateParam();
htp.setParamName("vnf_name");
htp.setParamType("string");
params.add(htp);
inputs.put("vnf_name", null);
htp = new HeatTemplateParam();
htp.setParamName("image_size");
htp.setParamType("number");
params.add(htp);
inputs.put("image_size", null);
htp = new HeatTemplateParam();
htp.setParamName("external");
htp.setParamType("boolean");
params.add(htp);
inputs.put("external", null);
htp = new HeatTemplateParam();
htp.setParamName("oam_ips");
htp.setParamType("comma_delimited_list");
params.add(htp);
inputs.put("oam_ips", null);
htp = new HeatTemplateParam();
htp.setParamName("oam_prefixes");
htp.setParamType("json");
params.add(htp);
inputs.put("oam_prefixes", null);
ht.setParameters(params);
Map<String, Object> output = utils.convertInputMap(inputs, ht);
assertNull(output.get("vnf_name"));
assertNull(output.get("image_size"));
assertEquals(false, output.get("external"));
assertNull(output.get("oam_ips"));
assertNull(output.get("oam_prefixes"));
}
use of org.onap.so.db.catalog.beans.HeatTemplateParam in project so by onap.
the class MsoHeatEnvironmentEntryTest method toFullString_ResourceRegistryNotPresentInRawEntry.
@Test
public void toFullString_ResourceRegistryNotPresentInRawEntry() throws JsonParseException, JsonMappingException, IOException {
StringBuilder sb = new StringBuilder(RAW_ENTRY_WITH_NO_RESOURCE_REGISTRY);
MsoHeatEnvironmentEntry testedObject = new MsoHeatEnvironmentEntry(sb);
HeatTemplateParam heatTemplateParam = mapper.readValue(new File(RESOURCE_PATH + "HeatTemplateParam.json"), HeatTemplateParam.class);
assertThat(testedObject.getRawEntry()).isEqualTo(sb);
assertThat(testedObject.isValid()).isTrue();
assertThat(testedObject.containsParameter(PARAMETER_NAME)).isTrue();
assertThat(testedObject.toString()).doesNotContain(RAW_ENTRY_WITH_RESOURCE_REGISTRY);
assertTrue(testedObject.containsParameter(PARAMETER_NAME, "dummyAlias"));
assertTrue(testedObject.containsParameter("dummyName", PARAMETER_NAME));
assertFalse(testedObject.containsParameter("dummyName", "dummyAlias"));
assertEquals("parameters:\n " + PARAMETER_NAME + ": " + VALUE_NAME + "\n\n\n", testedObject.toFullString().toString());
assertEquals("parameters:\n " + PARAMETER_NAME + ": " + VALUE_NAME + "\n\n\n", testedObject.toFullStringExcludeNonParams(new HashSet<HeatTemplateParam>(Arrays.asList(heatTemplateParam))).toString());
assertEquals(1, testedObject.getNumberOfParameters());
assertFalse(testedObject.hasResources());
MsoHeatEnvironmentResource heatResource = new MsoHeatEnvironmentResource("resourceName", "resourceValue");
MsoHeatEnvironmentParameter heatParameter = new MsoHeatEnvironmentParameter("parameterName", "parameterValue");
testedObject.addResource(heatResource);
testedObject.addParameter(heatParameter);
assertEquals(1, testedObject.getNumberOfResources());
assertEquals(2, testedObject.getNumberOfParameters());
testedObject.setResources(null);
testedObject.setParameters(null);
assertNull(testedObject.getParameters());
assertNull(testedObject.getResources());
}
Aggregations