diff options
Diffstat (limited to 'src/main/java')
7 files changed, 207 insertions, 33 deletions
diff --git a/src/main/java/org/onap/clamp/clds/client/CdsServices.java b/src/main/java/org/onap/clamp/clds/client/CdsServices.java index f25e8b80f..b8eb41946 100644 --- a/src/main/java/org/onap/clamp/clds/client/CdsServices.java +++ b/src/main/java/org/onap/clamp/clds/client/CdsServices.java @@ -34,6 +34,7 @@ import java.util.Map; import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.builder.ExchangeBuilder;
+import org.onap.clamp.clds.exception.cds.CdsParametersException;
import org.onap.clamp.clds.model.cds.CdsBpWorkFlowListResponse;
import org.onap.clamp.clds.util.JsonUtils;
import org.onap.clamp.clds.util.LoggingUtils;
@@ -117,7 +118,7 @@ public class CdsServices { return null;
}
- private JsonObject parseCdsResponse(String response) {
+ protected JsonObject parseCdsResponse(String response) {
JsonObject root = JsonParser.parseString(response).getAsJsonObject();
JsonObject inputs = root.getAsJsonObject("workFlowData").getAsJsonObject("inputs");
JsonObject dataTypes = root.getAsJsonObject("dataTypes");
@@ -135,6 +136,9 @@ public class CdsServices { String type = inputProperty.get("type").getAsString();
if (isComplexType(type, dataTypes)) {
inputObject.add(key, handleComplexType(type, dataTypes));
+ } else if (type.equalsIgnoreCase("list")) {
+ inputObject.add(key, handleListType(key, inputProperty,
+ dataTypes));
} else {
inputObject.add(key, entry.getValue());
}
@@ -142,6 +146,24 @@ public class CdsServices { return inputObject;
}
+ private JsonObject handleListType(String propertyName,
+ JsonObject inputProperty,
+ JsonObject dataTypes) {
+ if (inputProperty.get("entry_schema") != null) {
+ String type = inputProperty.get("entry_schema").getAsJsonObject().get(
+ "type").getAsString();
+ if (dataTypes.get(type) != null) {
+ JsonObject jsonObject = new JsonObject();
+ jsonObject.addProperty("type", "list");
+ jsonObject.add("properties", handleComplexType(type, dataTypes));
+ return jsonObject;
+ } else {
+ return inputProperty;
+ }
+ }
+ throw new CdsParametersException("Entry schema is null for " + propertyName);
+ }
+
private JsonObject handleComplexType(String key, JsonObject dataTypes) {
JsonObject properties = dataTypes.get(key).getAsJsonObject().get("properties").getAsJsonObject();
return getInputProperties(properties, dataTypes);
diff --git a/src/main/java/org/onap/clamp/clds/exception/cds/CdsParametersException.java b/src/main/java/org/onap/clamp/clds/exception/cds/CdsParametersException.java new file mode 100644 index 000000000..73ce31f89 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/exception/cds/CdsParametersException.java @@ -0,0 +1,53 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 Huawei Technologies Co., Ltd. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * ================================================================================ + * + */ + +package org.onap.clamp.clds.exception.cds; + +/** + * Exception while parsing CDS response. + */ +public class CdsParametersException extends RuntimeException { + + /** + * serialization id. + */ + private static final long serialVersionUID = 8425657297510362736L; + + /** + * This constructor can be used to create a new CdsParametersException. + * + * @param message The message to dump + */ + public CdsParametersException(final String message) { + super(message); + } + + /** + * This constructor can be used to create a new CdsParametersException. + * + * @param message The message to dump + * @param cause The Throwable cause object + */ + public CdsParametersException(final String message, final Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/org/onap/clamp/clds/tosca/JsonEditorSchemaConstants.java b/src/main/java/org/onap/clamp/clds/tosca/JsonEditorSchemaConstants.java index fdc94231f..e3c661698 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/JsonEditorSchemaConstants.java +++ b/src/main/java/org/onap/clamp/clds/tosca/JsonEditorSchemaConstants.java @@ -28,8 +28,10 @@ public class JsonEditorSchemaConstants { //Data types in JSON Schema public static final String TYPE_OBJECT = "object"; public static final String TYPE_ARRAY = "array"; + public static final String TYPE_MAP = "map"; public static final String TYPE_STRING = "string"; public static final String TYPE_INTEGER = "integer"; + public static final String TYPE_DATE_TIME = "datetime"; //Key elements in JSON Schema public static final String TYPE = "type"; @@ -43,6 +45,9 @@ public class JsonEditorSchemaConstants { public static final String ITEMS = "items"; public static final String PROPERTIES = "properties"; public static final String PROPERTY_ORDER = "propertyOrder"; + public static final String VALUES = "values"; + public static final String HEADER_TEMPLATE = "headerTemplate"; + public static final String HEADER_TEMPLATE_VALUE = "{{self.name}}"; public static final String MINIMUM = "minimum"; public static final String MAXIMUM = "maximum"; @@ -50,9 +55,13 @@ public class JsonEditorSchemaConstants { public static final String MAX_LENGTH = "maxLength"; public static final String EXCLUSIVE_MINIMUM = "exclusiveMinimum"; public static final String EXCLUSIVE_MAXIMUM = "exclusiveMaximum"; + public static final String MINITEMS = "minItems"; + public static final String MAXITEMS = "maxItems"; public static final String CUSTOM_KEY_FORMAT = "format"; public static final String CUSTOM_KEY_FORMAT_TABS_TOP = "tabs-top"; + public static final String CUSTOM_KEY_FORMAT_TABS = "tabs"; + public static final String CUSTOM_KEY_FORMAT_INPUT = "input"; public static final String FORMAT_SELECT = "select"; public static final String UNIQUE_ITEMS = "uniqueItems"; public static final String TRUE = "true"; @@ -67,5 +76,11 @@ public class JsonEditorSchemaConstants { public static final String SCHEMA = "schema"; public static final String CURRENT_VALUES = "currentValues"; + public static final String PLUGIN = "plugin"; + public static final String DATE_TIME_PICKER = "datetimepicker"; + public static final String VALIDATION = "validation"; + public static final String DATE_TIME_FORMAT = "YYYY/MM/DD HH:mm:ss"; + public static final String INPUT_EVENT = "input_event"; + public static final String DP_CHANGE = "dp.change"; } diff --git a/src/main/java/org/onap/clamp/clds/tosca/ToscaSchemaConstants.java b/src/main/java/org/onap/clamp/clds/tosca/ToscaSchemaConstants.java index 9601649c9..d00c431c8 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/ToscaSchemaConstants.java +++ b/src/main/java/org/onap/clamp/clds/tosca/ToscaSchemaConstants.java @@ -30,8 +30,11 @@ public class ToscaSchemaConstants { public static final String TYPE_MAP = "map"; public static final String TYPE_STRING = "string"; public static final String TYPE_INTEGER = "integer"; + public static final String TYPE_NUMBER = "number"; + public static final String TYPE_DATE_TIME = "datetime"; public static final String TYPE_FLOAT = "float"; public static final String TYPE_BOOLEAN = "boolean"; + public static final String TYPE_USER_DEFINED = "userDefined"; // Key elements in Tosca public static final String NODE_TYPES = "policy_types"; @@ -46,6 +49,8 @@ public class ToscaSchemaConstants { public static final String METADATA = "metadata"; public static final String METADATA_POLICY_MODEL_TYPE = "policy_model_type"; public static final String METADATA_ACRONYM = "acronym"; + public static final String METADATA_ELEMENT_NAME = "element_name"; + public static final String METADATA_HEADER_TEMPLATE = "header_template"; public static final String METADATA_CLAMP_POSSIBLE_VALUES = "clamp_possible_values"; // Constraints diff --git a/src/main/java/org/onap/clamp/clds/tosca/ToscaYamlToJsonConvertor.java b/src/main/java/org/onap/clamp/clds/tosca/ToscaYamlToJsonConvertor.java index 666ca6702..45bb87ed8 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/ToscaYamlToJsonConvertor.java +++ b/src/main/java/org/onap/clamp/clds/tosca/ToscaYamlToJsonConvertor.java @@ -38,6 +38,7 @@ import java.util.stream.Collectors; import org.json.JSONArray; import org.json.JSONObject; import org.onap.clamp.clds.config.ClampProperties; +import org.onap.clamp.tosca.Dictionary; import org.onap.clamp.tosca.DictionaryElement; import org.onap.clamp.tosca.DictionaryService; import org.springframework.beans.factory.annotation.Autowired; @@ -142,6 +143,13 @@ public class ToscaYamlToJsonConvertor { parseNodeAndDataType(loadedYaml, nodeTypes, dataNodes); populateJsonEditorObject(loadedYaml, nodeTypes, dataNodes, jsonParentObject, jsonTempObject, modelTypeToUse); + + String headerTemplate = getValueFromMetadata(validateAndConvertToJson(yamlString), + ToscaSchemaConstants.METADATA_HEADER_TEMPLATE); + if (headerTemplate != null) { + jsonParentObject.put(JsonEditorSchemaConstants.HEADER_TEMPLATE, + JsonEditorSchemaConstants.HEADER_TEMPLATE_VALUE); + } if (jsonTempObject.length() > 0) { jsonParentObject = jsonTempObject; } @@ -234,7 +242,7 @@ public class ToscaYamlToJsonConvertor { jsonTempObject.put(JsonEditorSchemaConstants.ITEMS, jsonParentObject); jsonTempObject.put(JsonEditorSchemaConstants.FORMAT, - JsonEditorSchemaConstants.CUSTOM_KEY_FORMAT_TABS_TOP); + JsonEditorSchemaConstants.CUSTOM_KEY_FORMAT_TABS); jsonTempObject.put(JsonEditorSchemaConstants.UNIQUE_ITEMS, JsonEditorSchemaConstants.TRUE); } @@ -652,12 +660,43 @@ public class ToscaYamlToJsonConvertor { Optional.ofNullable(cldsDictionaryElements).get().stream().forEach(c -> { JSONObject jsonObject = new JSONObject(); + if (c.getSubDictionary() != null) { + Dictionary subDictionary = + dictionaryService.getDictionary(c.getSubDictionary()); + if (subDictionary != null + && !subDictionary.getDictionaryElements().isEmpty()) { + + jsonObject.put(JsonEditorSchemaConstants.CUSTOM_KEY_FORMAT_INPUT, + JsonEditorSchemaConstants.FORMAT_SELECT); + + List<String> shortNames = new ArrayList<>(); + subDictionary.getDictionaryElements().stream().forEach(c1 -> { + shortNames.add(c1.getShortName()); + }); + jsonObject.put(JsonEditorSchemaConstants.VALUES, shortNames); + } + } jsonObject.put(JsonEditorSchemaConstants.TYPE, getJsonType(c.getType())); + if (c.getType() != null - && c.getType().equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING)) { + && (c.getType().equalsIgnoreCase(ToscaSchemaConstants.TYPE_STRING) + || c.getType().equalsIgnoreCase(ToscaSchemaConstants.TYPE_DATE_TIME) + || c.getType().equalsIgnoreCase(ToscaSchemaConstants.TYPE_MAP))) { jsonObject.put(JsonEditorSchemaConstants.MIN_LENGTH, 1); + if (c.getType().equalsIgnoreCase(ToscaSchemaConstants.TYPE_DATE_TIME)) { + jsonObject.put(JsonEditorSchemaConstants.PLUGIN, + JsonEditorSchemaConstants.DATE_TIME_PICKER); + jsonObject.put(JsonEditorSchemaConstants.INPUT_EVENT, + JsonEditorSchemaConstants.DP_CHANGE); + JSONObject formatJsonObject = new JSONObject(); + formatJsonObject.put(JsonEditorSchemaConstants.FORMAT, + JsonEditorSchemaConstants.DATE_TIME_FORMAT); + jsonObject.put(JsonEditorSchemaConstants.VALIDATION, + formatJsonObject); + } } + jsonObject.put(JsonEditorSchemaConstants.ID, c.getName()); jsonObject.put(JsonEditorSchemaConstants.LABEL, c.getShortName()); jsonObject.put(JsonEditorSchemaConstants.OPERATORS, subCldsDictionaryNames); @@ -678,32 +717,44 @@ public class ToscaYamlToJsonConvertor { String dictionaryKey = dictionaryReference.substring( dictionaryReference.indexOf(ToscaSchemaConstants.DICTIONARY) + 11, dictionaryReference.length()); + if (dictionaryKey != null) { - List<DictionaryElement> cldsDictionaryElements = - dictionaryService.getDictionary(dictionaryKey).getDictionaryElements().stream() - .collect(Collectors.toList()); - if (cldsDictionaryElements != null) { - List<String> cldsDictionaryNames = new ArrayList<>(); - List<String> cldsDictionaryFullNames = new ArrayList<>(); - cldsDictionaryElements.stream().forEach(c -> { - // Json type will be translated before Policy creation - if (c.getType() != null && !c.getType().equalsIgnoreCase("json")) { - cldsDictionaryFullNames.add(c.getName()); + if (dictionaryKey.contains(ToscaSchemaConstants.TYPE_USER_DEFINED)) { + childObject.put(JsonEditorSchemaConstants.ENUM, new ArrayList<>()); + // Add Enum titles for generated translated values during + // JSON instance generation + JSONObject enumTitles = new JSONObject(); + enumTitles.put(JsonEditorSchemaConstants.ENUM_TITLES, new ArrayList<>()); + childObject.put(JsonEditorSchemaConstants.OPTIONS, enumTitles); + } else { + List<DictionaryElement> cldsDictionaryElements = + dictionaryService.getDictionary(dictionaryKey).getDictionaryElements() + .stream().collect(Collectors.toList()); + if (cldsDictionaryElements != null) { + List<String> cldsDictionaryNames = new ArrayList<>(); + List<String> cldsDictionaryFullNames = new ArrayList<>(); + cldsDictionaryElements.stream().forEach(c -> { + // Json type will be translated before Policy creation + if (c.getType() != null && !c.getType().equalsIgnoreCase("json")) { + cldsDictionaryFullNames.add(c.getName()); + } + cldsDictionaryNames.add(c.getShortName()); + }); + + if (!cldsDictionaryFullNames.isEmpty()) { + childObject.put(JsonEditorSchemaConstants.ENUM, + cldsDictionaryFullNames); + // Add Enum titles for generated translated values during JSON instance + // generation + JSONObject enumTitles = new JSONObject(); + enumTitles.put(JsonEditorSchemaConstants.ENUM_TITLES, + cldsDictionaryNames); + childObject.put(JsonEditorSchemaConstants.OPTIONS, enumTitles); + } else { + childObject.put(JsonEditorSchemaConstants.ENUM, cldsDictionaryNames); } - cldsDictionaryNames.add(c.getShortName()); - }); - if (!cldsDictionaryFullNames.isEmpty()) { - childObject.put(JsonEditorSchemaConstants.ENUM, cldsDictionaryFullNames); - // Add Enum titles for generated translated values during JSON instance - // generation - JSONObject enumTitles = new JSONObject(); - enumTitles.put(JsonEditorSchemaConstants.ENUM_TITLES, cldsDictionaryNames); - childObject.put(JsonEditorSchemaConstants.OPTIONS, enumTitles); - } else { - childObject.put(JsonEditorSchemaConstants.ENUM, cldsDictionaryNames); } - } } } @@ -711,10 +762,15 @@ public class ToscaYamlToJsonConvertor { private String getJsonType(String toscaType) { String jsonType = null; - if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER)) { + if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_INTEGER) + || toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_NUMBER)) { jsonType = JsonEditorSchemaConstants.TYPE_INTEGER; + } else if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_DATE_TIME)) { + jsonType = JsonEditorSchemaConstants.TYPE_DATE_TIME; } else if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_LIST)) { jsonType = JsonEditorSchemaConstants.TYPE_ARRAY; + } else if (toscaType.equalsIgnoreCase(ToscaSchemaConstants.TYPE_MAP)) { + jsonType = JsonEditorSchemaConstants.TYPE_MAP; } else { jsonType = JsonEditorSchemaConstants.TYPE_STRING; } diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/execution/cds/ToscaMetadataCdsProcess.java b/src/main/java/org/onap/clamp/clds/tosca/update/execution/cds/ToscaMetadataCdsProcess.java index ce1f94699..c15641413 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/execution/cds/ToscaMetadataCdsProcess.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/execution/cds/ToscaMetadataCdsProcess.java @@ -23,6 +23,10 @@ package org.onap.clamp.clds.tosca.update.execution.cds; +import static org.onap.clamp.clds.tosca.ToscaSchemaConstants.PROPERTIES; +import static org.onap.clamp.clds.tosca.ToscaSchemaConstants.TYPE; +import static org.onap.clamp.clds.tosca.ToscaSchemaConstants.TYPE_LIST; + import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; @@ -173,7 +177,7 @@ public class ToscaMetadataCdsProcess extends ToscaMetadataProcess { jsonObject.add("artifact_version", createAnyOfJsonProperty( "artifact version", artifactVersion)); jsonObject.add("mode", createCdsInputProperty( - "mode", "string", "async")); + "mode", "string", "async", null)); jsonObject.add("data", createDataProperty(inputs)); return jsonObject; @@ -182,7 +186,7 @@ public class ToscaMetadataCdsProcess extends ToscaMetadataProcess { private static JsonObject createDataProperty(JsonObject inputs) { JsonObject data = new JsonObject(); data.addProperty("title", "data"); - data.add("properties", addDataFields(inputs)); + data.add(PROPERTIES, addDataFields(inputs)); return data; } @@ -192,7 +196,7 @@ public class ToscaMetadataCdsProcess extends ToscaMetadataProcess { for (Map.Entry<String, JsonElement> entry : entrySet) { String key = entry.getKey(); JsonObject inputProperty = inputs.getAsJsonObject(key); - if (inputProperty.get("type") == null) { + if (inputProperty.get(TYPE) == null) { jsonObject.add(entry.getKey(), createAnyOfJsonObject(key, addDataFields(entry.getValue().getAsJsonObject()))); @@ -200,7 +204,8 @@ public class ToscaMetadataCdsProcess extends ToscaMetadataProcess { jsonObject.add(entry.getKey(), createCdsInputProperty(key, inputProperty.get("type").getAsString(), - null)); + null, + entry.getValue().getAsJsonObject())); } } return jsonObject; @@ -208,14 +213,26 @@ public class ToscaMetadataCdsProcess extends ToscaMetadataProcess { private static JsonObject createCdsInputProperty(String title, String type, - String defaultValue) { + String defaultValue, + JsonObject cdsProperty) { JsonObject property = new JsonObject(); property.addProperty("title", title); - property.addProperty("type", type); + + if (TYPE_LIST.equalsIgnoreCase(type)) { + property.addProperty(TYPE, "array"); + if (cdsProperty.get(PROPERTIES) != null) { + JsonObject listProperties = new JsonObject(); + listProperties.add(PROPERTIES, + addDataFields(cdsProperty.get(PROPERTIES).getAsJsonObject())); + property.add("items", listProperties); + } + } else { + property.addProperty(TYPE, type); + } + if (defaultValue != null) { property.addProperty("default", defaultValue); } - property.addProperty("format", "textarea"); return property; } } diff --git a/src/main/java/org/onap/clamp/loop/LoopService.java b/src/main/java/org/onap/clamp/loop/LoopService.java index ca3681b62..3f568a331 100644 --- a/src/main/java/org/onap/clamp/loop/LoopService.java +++ b/src/main/java/org/onap/clamp/loop/LoopService.java @@ -118,6 +118,12 @@ public class LoopService { Loop addOperationalPolicy(String loopName, String policyType, String policyVersion) throws IOException { Loop loop = getLoop(loopName); PolicyModel policyModel = policyModelsService.getPolicyModel(policyType, policyVersion); + Set<OperationalPolicy> opPolicySet = loop.getOperationalPolicies(); + for (OperationalPolicy opPolicy : opPolicySet) { + if (opPolicy.getPolicyModel().equals(policyModel)) { + throw new IllegalArgumentException("This type of Operational Policy is already added to the loop. Please choose another one."); + } + } if (policyModel == null) { return null; } |