diff options
author | sebdet <sebastien.determe@intl.att.com> | 2020-03-19 10:29:30 -0700 |
---|---|---|
committer | Sébastien Determe <sebastien.determe@intl.att.com> | 2020-03-23 22:34:41 +0000 |
commit | afac3121bfacc4d81903b1d8492613b0b77d3298 (patch) | |
tree | b5f7bee2b2cc4ff1668c87a980e89b52d300ce53 /src/main/java | |
parent | 3c327c1fe313cb31dcc914dcaee7f38255240099 (diff) |
Fix the CDS calls
Fix the CDS calls for generic tosca policies, the new tosca parser must be interrupted so that clamp can do json schema enrichment
Issue-ID: CLAMP-786
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
Change-Id: Ib7e85a6cc3849f614a3025779d29e8024f7a2ef2
Diffstat (limited to 'src/main/java')
18 files changed, 417 insertions, 85 deletions
diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/ToscaConverterWithDictionarySupport.java b/src/main/java/org/onap/clamp/clds/tosca/update/ToscaConverterWithDictionarySupport.java index c1bf1ad80..5832986a1 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/ToscaConverterWithDictionarySupport.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/ToscaConverterWithDictionarySupport.java @@ -32,6 +32,7 @@ import org.onap.clamp.clds.tosca.update.parser.metadata.ToscaMetadataParser; import org.onap.clamp.clds.tosca.update.parser.metadata.ToscaMetadataParserWithDictionarySupport; import org.onap.clamp.clds.tosca.update.templates.JsonTemplateManager; import org.onap.clamp.clds.util.JsonUtils; +import org.onap.clamp.loop.service.Service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -63,15 +64,18 @@ public class ToscaConverterWithDictionarySupport { * * @param toscaFile The tosca file as String * @param policyTypeToDecode The policy type to decode + * @param serviceModel The service model associated so that the clamp enrichment could be done if required by + * the tosca model * @return A json object being a json schema */ - public JsonObject convertToscaToJsonSchemaObject(String toscaFile, String policyTypeToDecode) { + public JsonObject convertToscaToJsonSchemaObject(String toscaFile, String policyTypeToDecode, + Service serviceModel) { try { return new JsonTemplateManager(toscaFile, clampProperties.getFileContent("tosca.converter.default.datatypes"), clampProperties.getFileContent("tosca.converter.json.schema.templates")) .getJsonSchemaForPolicyType(policyTypeToDecode, Boolean.parseBoolean(clampProperties.getStringValue( - "tosca.converter.dictionary.support.enabled")) ? metadataParser : null); + "tosca.converter.dictionary.support.enabled")) ? metadataParser : null, serviceModel); } catch (IOException | UnknownComponentException e) { logger.error("Unable to convert the tosca properly, exception caught during the decoding", e); @@ -85,9 +89,10 @@ public class ToscaConverterWithDictionarySupport { * * @param toscaFile The tosca file as String * @param policyTypeToDecode The policy type to decode + * @param serviceModel The service Model so that clamp enrichment could be done if required by tosca model * @return A String containing the json schema */ - public String convertToscaToJsonSchemaString(String toscaFile, String policyTypeToDecode) { - return JsonUtils.GSON.toJson(this.convertToscaToJsonSchemaObject(toscaFile, policyTypeToDecode)); + public String convertToscaToJsonSchemaString(String toscaFile, String policyTypeToDecode, Service serviceModel) { + return JsonUtils.GSON.toJson(this.convertToscaToJsonSchemaObject(toscaFile, policyTypeToDecode, serviceModel)); } } diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/execution/ToscaMetadataExecutor.java b/src/main/java/org/onap/clamp/clds/tosca/update/execution/ToscaMetadataExecutor.java new file mode 100644 index 000000000..dbd8ee1bc --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/tosca/update/execution/ToscaMetadataExecutor.java @@ -0,0 +1,110 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * 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.tosca.update.execution; + +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.google.gson.JsonObject; +import java.util.HashMap; +import java.util.Map; +import javax.annotation.PostConstruct; +import org.onap.clamp.clds.tosca.update.execution.cds.ToscaMetadataCdsProcess; +import org.onap.clamp.clds.tosca.update.execution.target.ToscaMetadataTargetProcess; +import org.onap.clamp.loop.service.Service; +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; +import org.springframework.stereotype.Component; + +/** + * This class is used to execute a code based on a key found in the metadata section. + */ +@Component +public class ToscaMetadataExecutor { + + private static final EELFLogger logger = + EELFManager.getInstance().getLogger(ToscaMetadataExecutor.class); + + @Autowired + private DictionaryService dictionaryService; + + private Map<String, ToscaMetadataProcess> mapOfProcesses = new HashMap<>(); + + /** + * This method executes the required process specified in processInfo + * + * @param processInfo A String containing the process to execute, like "cds/param1:value1/param2:value2" + * @param childObject The jsonObject + * @param serviceModel The service model associated to do clamp enrichment + */ + public void executeTheProcess(String processInfo, JsonObject childObject, Service serviceModel) { + String[] processParameters = (processInfo + "/ ").split("/"); + logger.info("Executing the Tosca clamp process " + processParameters[0] + " with parameters " + + processParameters[1].trim()); + mapOfProcesses.get(processParameters[0]).executeProcess(processParameters[1].trim(), childObject, serviceModel); + } + + /** + * Init method. + */ + @PostConstruct + public void init() { + mapOfProcesses.put("CDS", new ToscaMetadataCdsProcess()); + mapOfProcesses.put("CSAR_RESOURCES", new ToscaMetadataTargetProcess()); + + preProvisionDictionaryTable(); + } + + private void preProvisionDictionaryTable() { + // Set up dictionary elements + Dictionary actorDictionary = new Dictionary(); + actorDictionary.setName("DefaultActor"); + actorDictionary.setSecondLevelDictionary(0); + actorDictionary.setSubDictionaryType(""); + + DictionaryElement elementSo = new DictionaryElement(); + elementSo.setName("SO"); + elementSo.setShortName("SO"); + elementSo.setType("string"); + elementSo.setDescription("SO component"); + actorDictionary.addDictionaryElements(elementSo); + + DictionaryElement elementSdnc = new DictionaryElement(); + elementSdnc.setName("SDNC"); + elementSdnc.setShortName("SDNC"); + elementSdnc.setType("string"); + elementSdnc.setDescription("SDNC component"); + actorDictionary.addDictionaryElements(elementSdnc); + + DictionaryElement elementCds = new DictionaryElement(); + elementCds.setName("CDS"); + elementCds.setShortName("CDS"); + elementCds.setType("string"); + elementCds.setDescription("CDS component"); + actorDictionary.addDictionaryElements(elementCds); + + dictionaryService.saveOrUpdateDictionary(actorDictionary); + } +} diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/execution/ToscaMetadataProcess.java b/src/main/java/org/onap/clamp/clds/tosca/update/execution/ToscaMetadataProcess.java new file mode 100644 index 000000000..214eadb03 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/tosca/update/execution/ToscaMetadataProcess.java @@ -0,0 +1,43 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * 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.tosca.update.execution; + +import com.google.gson.JsonObject; +import org.onap.clamp.loop.service.Service; + +/** + * This code is the interface that must be implemented to have a tosca process. + */ +public abstract class ToscaMetadataProcess { + + /** + * This method add some elements to the JsonObject childObject passed in argument. + * The process can take multiple parameters in arguments. + * + * @param parameters The parameters required by the process + * @param childObject The Json Object modified by the current process + * @param serviceModel The service model associated to do clamp enrichment + */ + public abstract void executeProcess(String parameters, JsonObject childObject, Service serviceModel); +} 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 new file mode 100644 index 000000000..cf9e010b3 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/tosca/update/execution/cds/ToscaMetadataCdsProcess.java @@ -0,0 +1,113 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * 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.tosca.update.execution.cds; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import java.util.Map; +import org.onap.clamp.clds.tosca.update.execution.ToscaMetadataProcess; +import org.onap.clamp.loop.service.Service; +import org.onap.clamp.tosca.DictionaryService; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * This class is there to add the JsonObject for CDS in the json Schema according to what is found in the Tosca model. + */ +public class ToscaMetadataCdsProcess extends ToscaMetadataProcess { + + @Autowired + private DictionaryService dictionaryService; + + @Override + public void executeProcess(String parameters, JsonObject childObject, Service serviceModel) { + switch (parameters) { + case "payload": + childObject.add("anyOf", generatePayload(serviceModel)); + break; + case "operation": + childObject.add("enum", generateOperation(serviceModel)); + break; + } + } + + private static JsonArray generatePayload(Service serviceModel) { + JsonArray schemaAnyOf = new JsonArray(); + schemaAnyOf.addAll(generatePayloadPerResource("VF", serviceModel)); + schemaAnyOf.addAll(generatePayloadPerResource("PNF", serviceModel)); + return schemaAnyOf; + } + + private static JsonArray generateOperation(Service serviceModel) { + JsonArray schemaEnum = new JsonArray(); + schemaEnum.addAll(generateOperationPerResource("VF", serviceModel)); + schemaEnum.addAll(generateOperationPerResource("PNF", serviceModel)); + return schemaEnum; + } + + private static JsonArray generateOperationPerResource(String resourceName, Service serviceModel) { + JsonArray schemaEnum = new JsonArray(); + for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName) + .entrySet()) { + JsonObject controllerProperties = entry.getValue().getAsJsonObject() + .getAsJsonObject("controllerProperties"); + if (controllerProperties != null) { + for (String workflowsEntry : controllerProperties.getAsJsonObject("workflows").keySet()) { + schemaEnum.add(workflowsEntry); + } + } + } + return schemaEnum; + } + + private static JsonArray generatePayloadPerResource(String resourceName, Service serviceModel) { + JsonArray schemaAnyOf = new JsonArray(); + for (Map.Entry<String, JsonElement> entry : serviceModel.getResourceDetails().getAsJsonObject(resourceName) + .entrySet()) { + JsonObject controllerProperties = entry.getValue().getAsJsonObject() + .getAsJsonObject("controllerProperties"); + if (controllerProperties != null) { + for (Map.Entry<String, JsonElement> workflowsEntry : controllerProperties.getAsJsonObject("workflows") + .entrySet()) { + JsonObject obj = new JsonObject(); + obj.addProperty("title", workflowsEntry.getKey()); + obj.add("properties", createPayloadProperty(workflowsEntry.getValue().getAsJsonObject(), + controllerProperties)); + schemaAnyOf.add(obj); + } + } + } + return schemaAnyOf; + } + + private static JsonObject createPayloadProperty(JsonObject workFlow, JsonObject controllerProperties) { + JsonObject payloadResult = new JsonObject(); + + payloadResult.addProperty("artifact_name", controllerProperties.get("sdnc_model_name").getAsString()); + payloadResult.addProperty("artifact_version", controllerProperties.get("sdnc_model_version").getAsString()); + payloadResult.addProperty("mode", "async"); + payloadResult.add("data", workFlow.getAsJsonObject("inputs")); + return payloadResult; + } +} diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/execution/target/ToscaMetadataTargetProcess.java b/src/main/java/org/onap/clamp/clds/tosca/update/execution/target/ToscaMetadataTargetProcess.java new file mode 100644 index 000000000..8b8fa1784 --- /dev/null +++ b/src/main/java/org/onap/clamp/clds/tosca/update/execution/target/ToscaMetadataTargetProcess.java @@ -0,0 +1,40 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP CLAMP + * ================================================================================ + * Copyright (C) 2020 AT&T Intellectual Property. All rights + * reserved. + * ================================================================================ + * 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.tosca.update.execution.target; + +import com.google.gson.JsonObject; +import org.onap.clamp.clds.tosca.update.execution.ToscaMetadataProcess; +import org.onap.clamp.loop.service.Service; +import org.onap.clamp.policy.operational.OperationalPolicyRepresentationBuilder; + +/** + * This class is there to add the JsonObject for CDS in the json Schema according to what is found in the Tosca model. + */ +public class ToscaMetadataTargetProcess extends ToscaMetadataProcess { + + @Override + public void executeProcess(String parameters, JsonObject childObject, Service serviceModel) { + childObject.add("anyOf", OperationalPolicyRepresentationBuilder.createAnyOfArray(serviceModel, false)); + } +} diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/parser/ToscaConverterToJsonSchema.java b/src/main/java/org/onap/clamp/clds/tosca/update/parser/ToscaConverterToJsonSchema.java index cfc0e42a8..e1d493a35 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/parser/ToscaConverterToJsonSchema.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/parser/ToscaConverterToJsonSchema.java @@ -33,6 +33,7 @@ import org.onap.clamp.clds.tosca.update.elements.ToscaElement; import org.onap.clamp.clds.tosca.update.elements.ToscaElementProperty; import org.onap.clamp.clds.tosca.update.parser.metadata.ToscaMetadataParser; import org.onap.clamp.clds.tosca.update.templates.JsonTemplate; +import org.onap.clamp.loop.service.Service; public class ToscaConverterToJsonSchema { private LinkedHashMap<String, ToscaElement> components; @@ -40,19 +41,23 @@ public class ToscaConverterToJsonSchema { private ToscaMetadataParser metadataParser; + private Service serviceModel; + /** * Constructor. * * @param toscaElementsMap All the tosca elements found (policy type + data types + native tosca datatypes) * @param jsonSchemaTemplates All Json schema templates to use * @param metadataParser The metadata parser to use for metadata section + * @param serviceModel The service model for clamp enrichment */ public ToscaConverterToJsonSchema(LinkedHashMap<String, ToscaElement> toscaElementsMap, LinkedHashMap<String, JsonTemplate> jsonSchemaTemplates, - ToscaMetadataParser metadataParser) { + ToscaMetadataParser metadataParser, Service serviceModel) { this.components = toscaElementsMap; this.templates = jsonSchemaTemplates; this.metadataParser = metadataParser; + this.serviceModel = serviceModel; } /** @@ -220,7 +225,7 @@ public class ToscaConverterToJsonSchema { break; case "metadata": if (metadataParser != null) { - metadataParser.processAllMetadataElement(toscaElementProperty).entrySet() + metadataParser.processAllMetadataElement(toscaElementProperty, serviceModel).entrySet() .forEach((jsonEntry) -> { propertiesInJson.add(jsonEntry.getKey(), jsonEntry.getValue()); @@ -238,9 +243,8 @@ public class ToscaConverterToJsonSchema { if (getToscaElement(this.extractSpecificFieldFromMap(toscaElementProperty, "entry_schema")) != null) { String nameComponent = this.extractSpecificFieldFromMap(toscaElementProperty, "entry_schema"); - ToscaConverterToJsonSchema - child = new ToscaConverterToJsonSchema(components, templates, - metadataParser); + ToscaConverterToJsonSchema child = new ToscaConverterToJsonSchema(components, templates, + metadataParser, serviceModel); JsonObject propertiesContainer = new JsonObject(); switch ((String) toscaElementProperty.getItems().get("type")) { diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/parser/metadata/ToscaMetadataParser.java b/src/main/java/org/onap/clamp/clds/tosca/update/parser/metadata/ToscaMetadataParser.java index a51818e2e..f4a48139e 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/parser/metadata/ToscaMetadataParser.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/parser/metadata/ToscaMetadataParser.java @@ -25,7 +25,8 @@ package org.onap.clamp.clds.tosca.update.parser.metadata; import com.google.gson.JsonObject; import org.onap.clamp.clds.tosca.update.elements.ToscaElementProperty; +import org.onap.clamp.loop.service.Service; public interface ToscaMetadataParser { - JsonObject processAllMetadataElement(ToscaElementProperty toscaElementProperty); + JsonObject processAllMetadataElement(ToscaElementProperty toscaElementProperty, Service serviceModel); } diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/parser/metadata/ToscaMetadataParserWithDictionarySupport.java b/src/main/java/org/onap/clamp/clds/tosca/update/parser/metadata/ToscaMetadataParserWithDictionarySupport.java index 349ccee9e..8aff9668f 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/parser/metadata/ToscaMetadataParserWithDictionarySupport.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/parser/metadata/ToscaMetadataParserWithDictionarySupport.java @@ -29,10 +29,11 @@ import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Optional; -import org.json.JSONArray; import org.onap.clamp.clds.tosca.JsonEditorSchemaConstants; import org.onap.clamp.clds.tosca.ToscaSchemaConstants; import org.onap.clamp.clds.tosca.update.elements.ToscaElementProperty; +import org.onap.clamp.clds.tosca.update.execution.ToscaMetadataExecutor; +import org.onap.clamp.loop.service.Service; import org.onap.clamp.tosca.DictionaryElement; import org.onap.clamp.tosca.DictionaryService; import org.springframework.beans.factory.annotation.Autowired; @@ -42,6 +43,9 @@ import org.springframework.stereotype.Component; public class ToscaMetadataParserWithDictionarySupport implements ToscaMetadataParser { @Autowired + private ToscaMetadataExecutor toscaMetadataExecutor; + + @Autowired private DictionaryService dictionaryService; /** @@ -50,9 +54,10 @@ public class ToscaMetadataParserWithDictionarySupport implements ToscaMetadataPa * @param toscaElementProperty The property metadata as Json Object * @return The jsonObject structure that must be added to the json schema */ - public JsonObject processAllMetadataElement(ToscaElementProperty toscaElementProperty) { + public JsonObject processAllMetadataElement(ToscaElementProperty toscaElementProperty, Service serviceModel) { if (dictionaryService != null) { - return parseMetadataPossibleValues(toscaElementProperty.getItems(), dictionaryService); + return parseMetadataPossibleValues(toscaElementProperty.getItems(), dictionaryService, serviceModel, + toscaMetadataExecutor); } else { return null; @@ -60,38 +65,32 @@ public class ToscaMetadataParserWithDictionarySupport implements ToscaMetadataPa } private static JsonObject parseMetadataPossibleValues(LinkedHashMap<String, Object> childNodeMap, - DictionaryService dictionaryService) { + DictionaryService dictionaryService, Service serviceModel, + ToscaMetadataExecutor toscaMetadataExecutor) { JsonObject childObject = new JsonObject(); if (childNodeMap.containsKey(ToscaSchemaConstants.METADATA) && childNodeMap.get(ToscaSchemaConstants.METADATA) != null) { - LinkedHashMap<String, Object> metadataMap = - (LinkedHashMap<String, Object>) childNodeMap.get(ToscaSchemaConstants.METADATA); - if (metadataMap != null) { - metadataMap.entrySet().stream().forEach(constraint -> { - if (constraint.getKey() - .equalsIgnoreCase(ToscaSchemaConstants.METADATA_CLAMP_POSSIBLE_VALUES)) { - JSONArray validValuesArray = new JSONArray(); - if (constraint.getValue() instanceof ArrayList<?>) { - boolean processDictionary = ((ArrayList<?>) constraint.getValue()) - .stream().anyMatch(value -> (value instanceof String - && ((String) value).contains(ToscaSchemaConstants.DICTIONARY))); - if (processDictionary) { - ((ArrayList<?>) constraint.getValue()).stream().forEach(value -> { - if ((value instanceof String && ((String) value) - .contains(ToscaSchemaConstants.DICTIONARY))) { - processDictionaryElements((String) value, childObject, dictionaryService); - } - - }); - } - } + ((LinkedHashMap<String, Object>) childNodeMap.get(ToscaSchemaConstants.METADATA)).forEach((key, + value) -> { + if (key.equalsIgnoreCase(ToscaSchemaConstants.METADATA_CLAMP_POSSIBLE_VALUES)) { + if (((String) value).contains(ToscaSchemaConstants.DICTIONARY)) { + processDictionaryElements((String) value, childObject, dictionaryService); } - }); - } + if (((String) value).contains("ClampExecution:")) { + executeClampProcess(((String) value).replaceAll("ClampExecution:", ""), childObject, + serviceModel, toscaMetadataExecutor); + } + } + }); } return childObject; } + private static void executeClampProcess(String processInfo, JsonObject childObject, Service serviceModel, + ToscaMetadataExecutor toscaMetadataExecutor) { + toscaMetadataExecutor.executeTheProcess(processInfo, childObject, serviceModel); + } + private static void processDictionaryElements(String dictionaryReference, JsonObject childObject, DictionaryService dictionaryService) { String[] dictionaryKeyArray = diff --git a/src/main/java/org/onap/clamp/clds/tosca/update/templates/JsonTemplateManager.java b/src/main/java/org/onap/clamp/clds/tosca/update/templates/JsonTemplateManager.java index 5e4b66009..0b399ba0b 100644 --- a/src/main/java/org/onap/clamp/clds/tosca/update/templates/JsonTemplateManager.java +++ b/src/main/java/org/onap/clamp/clds/tosca/update/templates/JsonTemplateManager.java @@ -35,6 +35,7 @@ import org.onap.clamp.clds.tosca.update.parser.ToscaConverterToJsonSchema; import org.onap.clamp.clds.tosca.update.parser.ToscaElementParser; import org.onap.clamp.clds.tosca.update.parser.metadata.ToscaMetadataParser; import org.onap.clamp.clds.util.JsonUtils; +import org.onap.clamp.loop.service.Service; public class JsonTemplateManager { private LinkedHashMap<String, JsonTemplate> jsonSchemaTemplates; @@ -45,7 +46,7 @@ public class JsonTemplateManager { * * @param toscaYamlContent Policy Tosca Yaml content as string * @param nativeToscaDatatypes The tosca yaml with tosca native datatypes - * @param jsonSchemaTemplates template properties as string + * @param jsonSchemaTemplates template properties as string */ public JsonTemplateManager(String toscaYamlContent, String nativeToscaDatatypes, String jsonSchemaTemplates) { if (toscaYamlContent != null && !toscaYamlContent.isEmpty()) { @@ -107,11 +108,13 @@ public class JsonTemplateManager { */ public void updateTemplate(String nameTemplate, JsonTemplateField jsonTemplateField, Boolean operation) { // Operation = true && field is not present => add Field - if (operation && !this.jsonSchemaTemplates.get(nameTemplate).getJsonTemplateFields().contains(jsonTemplateField)) { + if (operation + && !this.jsonSchemaTemplates.get(nameTemplate).getJsonTemplateFields().contains(jsonTemplateField)) { this.jsonSchemaTemplates.get(nameTemplate).addField(jsonTemplateField); } // Operation = false && field is present => remove Field - else if (!operation && this.jsonSchemaTemplates.get(nameTemplate).getJsonTemplateFields().contains(jsonTemplateField)) { + else if (!operation + && this.jsonSchemaTemplates.get(nameTemplate).getJsonTemplateFields().contains(jsonTemplateField)) { this.jsonSchemaTemplates.get(nameTemplate).removeField(jsonTemplateField); } } @@ -135,16 +138,17 @@ public class JsonTemplateManager { /** * For a given policy type, get a corresponding JsonObject from the tosca model. * - * @param policyType The policy type in the tosca + * @param policyType The policy type in the tosca * @param toscaMetadataParser The MetadataParser class that must be used if metadata section are encountered, if null - * they will be skipped + * they will be skipped * @return an json object defining the equivalent json schema from the tosca for a given policy type */ - public JsonObject getJsonSchemaForPolicyType(String policyType, ToscaMetadataParser toscaMetadataParser) + public JsonObject getJsonSchemaForPolicyType(String policyType, ToscaMetadataParser toscaMetadataParser, + Service serviceModel) throws UnknownComponentException { ToscaConverterToJsonSchema toscaConverterToJsonSchema = new ToscaConverterToJsonSchema(toscaElements, jsonSchemaTemplates, - toscaMetadataParser); + toscaMetadataParser, serviceModel); if (toscaConverterToJsonSchema.getToscaElement(policyType) == null) { throw new UnknownComponentException(policyType); } @@ -180,5 +184,4 @@ public class JsonTemplateManager { } return generatedTemplates; } - -} +}
\ No newline at end of file diff --git a/src/main/java/org/onap/clamp/loop/LoopController.java b/src/main/java/org/onap/clamp/loop/LoopController.java index 9c2c71f5f..a2a44631b 100644 --- a/src/main/java/org/onap/clamp/loop/LoopController.java +++ b/src/main/java/org/onap/clamp/loop/LoopController.java @@ -207,7 +207,7 @@ public class LoopController { for (MicroServicePolicy microServicePolicy : loop.getMicroServicePolicies()) { if (microServicePolicy.getName().equals(microServicePolicyName)) { this.microServicePolicyService - .refreshMicroServicePolicyJsonRepresentation(microServicePolicy, toscaConverter); + .refreshMicroServicePolicyJsonRepresentation(microServicePolicy, toscaConverter, loop); } } return loop; diff --git a/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java b/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java index ae66c54b8..98b928916 100644 --- a/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java +++ b/src/main/java/org/onap/clamp/loop/template/PolicyModelsService.java @@ -148,7 +148,7 @@ public class PolicyModelsService { public void createPolicyInDbIfNeeded(PolicyModel policyModel) { if (!policyModelsRepository.existsById( new PolicyModelId(policyModel.getPolicyModelType(), policyModel.getVersion()))) { - policyModelsRepository.save(policyModel); + policyModelsRepository.saveAndFlush(policyModel); } } @@ -173,7 +173,7 @@ public class PolicyModelsService { JsonObject supportedPdpJson = new JsonObject(); supportedPdpJson.add("supportedPdpGroups", supportedPdpGroups); policyModel.setPolicyPdpGroup(supportedPdpJson); - policyModelsRepository.save(policyModel); + policyModelsRepository.saveAndFlush(policyModel); } } } diff --git a/src/main/java/org/onap/clamp/policy/Policy.java b/src/main/java/org/onap/clamp/policy/Policy.java index abb16d73f..d84f2c8a0 100644 --- a/src/main/java/org/onap/clamp/policy/Policy.java +++ b/src/main/java/org/onap/clamp/policy/Policy.java @@ -46,6 +46,7 @@ import org.json.JSONObject; import org.onap.clamp.clds.tosca.update.ToscaConverterWithDictionarySupport; import org.onap.clamp.dao.model.jsontype.StringJsonUserType; import org.onap.clamp.loop.common.AuditEntity; +import org.onap.clamp.loop.service.Service; import org.onap.clamp.loop.template.LoopElementModel; import org.onap.clamp.loop.template.PolicyModel; import org.yaml.snakeyaml.Yaml; @@ -171,8 +172,10 @@ public abstract class Policy extends AuditEntity { * Regenerate the Policy Json Representation. * * @param toscaConverter The tosca converter required to regenerate the json schema + * @param serviceModel The service model associated */ - public abstract void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter); + public abstract void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter, + Service serviceModel); /** * policyModel getter. diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java index b3bd7e88a..e997b4309 100644 --- a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicy.java @@ -129,10 +129,8 @@ public class MicroServicePolicy extends Policy implements Serializable { ToscaConverterWithDictionarySupport toscaConverter) { this(Policy.generatePolicyName("MICROSERVICE", service.getName(), service.getVersion(), RandomStringUtils.randomAlphanumeric(3), RandomStringUtils.randomAlphanumeric(3)), - loopElementModel.getPolicyModels().first(), false, - new JsonObject(), - loopElementModel, null, null); - this.updateJsonRepresentation(toscaConverter); + loopElementModel.getPolicyModels().first(), false, new JsonObject(), loopElementModel, null, null); + this.updateJsonRepresentation(toscaConverter, service); } @Override @@ -151,9 +149,10 @@ public class MicroServicePolicy extends Policy implements Serializable { } @Override - public void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter) { - this.setJsonRepresentation(toscaConverter.convertToscaToJsonSchemaObject(this.getPolicyModel().getPolicyModelTosca(), - this.getPolicyModel().getPolicyModelType())); + public void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter, Service serviceModel) { + this.setJsonRepresentation( + toscaConverter.convertToscaToJsonSchemaObject(this.getPolicyModel().getPolicyModelTosca(), + this.getPolicyModel().getPolicyModelType(), serviceModel)); } public Boolean getShared() { diff --git a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java index 0631380f5..37533c1f6 100644 --- a/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java +++ b/src/main/java/org/onap/clamp/policy/microservice/MicroServicePolicyService.java @@ -99,11 +99,13 @@ public class MicroServicePolicyService implements PolicyService<MicroServicePoli * Api to refresh the MicroService Policy UI window. * * @param microServicePolicy The micro Service policy object - * @param toscaConverter the tosca converter required to convert the tosca model to json schema + * @param toscaConverter The tosca converter required to convert the tosca model to json schema + * @param loop As a microservice object can belong to multiple loops, we need it here */ public void refreshMicroServicePolicyJsonRepresentation(MicroServicePolicy microServicePolicy, - ToscaConverterWithDictionarySupport toscaConverter) { - microServicePolicy.updateJsonRepresentation(toscaConverter); + ToscaConverterWithDictionarySupport toscaConverter, + Loop loop) { + microServicePolicy.updateJsonRepresentation(toscaConverter, loop.getModelService()); this.microServiceRepository.saveAndFlush(microServicePolicy); } diff --git a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java index 492c9b9ef..b5a88b39d 100644 --- a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicy.java @@ -126,16 +126,16 @@ public class OperationalPolicy extends Policy implements Serializable { RandomStringUtils.randomAlphanumeric(3), RandomStringUtils.randomAlphanumeric(3)), new JsonObject(), new JsonObject(), loopElementModel.getPolicyModels().first(), loopElementModel, null, null); this.setLoop(loop); - this.updateJsonRepresentation(toscaConverter); + this.updateJsonRepresentation(toscaConverter, service); } /** * Create an operational policy from a policy model. * - * @param loop The parent loop - * @param service The loop service - * @param policyModel The policy model - * @param toscaConverter The tosca converter that must be used to create the Json representation + * @param loop The parent loop + * @param service The loop service + * @param policyModel The policy model + * @param toscaConverter The tosca converter that must be used to create the Json representation * @throws IOException In case of issues with the legacy files (generated from resource files */ public OperationalPolicy(Loop loop, Service service, PolicyModel policyModel, @@ -144,7 +144,7 @@ public class OperationalPolicy extends Policy implements Serializable { RandomStringUtils.randomAlphanumeric(3), RandomStringUtils.randomAlphanumeric(3)), new JsonObject(), new JsonObject(), policyModel, null, null, null); this.setLoop(loop); - this.updateJsonRepresentation(toscaConverter); + this.updateJsonRepresentation(toscaConverter, service); } public void setLoop(Loop loopName) { @@ -171,7 +171,7 @@ public class OperationalPolicy extends Policy implements Serializable { } @Override - public void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter) { + public void updateJsonRepresentation(ToscaConverterWithDictionarySupport toscaConverter, Service serviceModel) { { this.setJsonRepresentation(new JsonObject()); if (this.getPolicyModel() == null) { @@ -187,7 +187,7 @@ public class OperationalPolicy extends Policy implements Serializable { // Generic Case this.setJsonRepresentation(toscaConverter.convertToscaToJsonSchemaObject( this.getPolicyModel().getPolicyModelTosca(), - this.getPolicyModel().getPolicyModelType())); + this.getPolicyModel().getPolicyModelType(), serviceModel)); } } } diff --git a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java index 755985191..6577069ec 100644 --- a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyRepresentationBuilder.java @@ -60,7 +60,7 @@ public class OperationalPolicyRepresentationBuilder { jsonSchema.get("properties").getAsJsonObject() .get("operational_policy").getAsJsonObject().get("properties").getAsJsonObject().get("policies") .getAsJsonObject().get("items").getAsJsonObject().get("properties").getAsJsonObject().get("target") - .getAsJsonObject().get("anyOf").getAsJsonArray().addAll(createAnyOfArray(modelJson)); + .getAsJsonObject().get("anyOf").getAsJsonArray().addAll(createAnyOfArray(modelJson, true)); // update CDS recipe and payload information to schema JsonArray actors = jsonSchema.get("properties").getAsJsonObject() @@ -72,12 +72,12 @@ public class OperationalPolicyRepresentationBuilder { if ("CDS".equalsIgnoreCase(actor.getAsJsonObject().get("title").getAsString())) { actor.getAsJsonObject().get("properties").getAsJsonObject().get("type").getAsJsonObject() .get("anyOf").getAsJsonArray() - .addAll(createAnyOfArrayForCdsRecipe(modelJson.getResourceDetails())); + .addAll(createAnyOfArrayForCdsRecipe(modelJson)); } } return jsonSchema; } catch (IOException e) { - logger.error("Unable to generate the json schema because of an exception",e); + logger.error("Unable to generate the json schema because of an exception", e); return new JsonObject(); } } @@ -100,7 +100,7 @@ public class OperationalPolicyRepresentationBuilder { return property; } - private static JsonArray createVnfSchema(Service modelService) { + private static JsonArray createVnfSchema(Service modelService, boolean generateType) { JsonArray vnfSchemaArray = new JsonArray(); JsonObject modelVnfs = modelService.getResourceByType("VF"); @@ -108,7 +108,9 @@ public class OperationalPolicyRepresentationBuilder { JsonObject vnfOneOfSchema = new JsonObject(); vnfOneOfSchema.addProperty("title", "VNF" + "-" + entry.getKey()); JsonObject properties = new JsonObject(); - properties.add("type", createSchemaProperty("Type", "string", "VNF", "True", null)); + if (generateType) { + properties.add("type", createSchemaProperty("Type", "string", "VNF", "True", null)); + } properties.add("resourceID", createSchemaProperty("Resource ID", "string", modelVnfs.get(entry.getKey()).getAsJsonObject().get("name").getAsString(), "True", null)); @@ -118,7 +120,7 @@ public class OperationalPolicyRepresentationBuilder { return vnfSchemaArray; } - private static JsonArray createVfModuleSchema(Service modelService) { + private static JsonArray createVfModuleSchema(Service modelService, boolean generateType) { JsonArray vfModuleOneOfSchemaArray = new JsonArray(); JsonObject modelVfModules = modelService.getResourceByType("VFModule"); @@ -126,7 +128,9 @@ public class OperationalPolicyRepresentationBuilder { JsonObject vfModuleOneOfSchema = new JsonObject(); vfModuleOneOfSchema.addProperty("title", "VFMODULE" + "-" + entry.getKey()); JsonObject properties = new JsonObject(); - properties.add("type", createSchemaProperty("Type", "string", "VFMODULE", "True", null)); + if (generateType) { + properties.add("type", createSchemaProperty("Type", "string", "VFMODULE", "True", null)); + } properties.add("resourceID", createSchemaProperty("Resource ID", "string", modelVfModules.get(entry.getKey()).getAsJsonObject().get("vfModuleModelName").getAsString(), @@ -160,17 +164,23 @@ public class OperationalPolicyRepresentationBuilder { return vfModuleOneOfSchemaArray; } - private static JsonArray createAnyOfArray(Service modelJson) { + /** + * Create an anyOf array of possible structure we may have for Target + * + * @param modelJson The service object + * @return A JsonArray with everything inside + */ + public static JsonArray createAnyOfArray(Service modelJson, boolean generateType) { JsonArray targetOneOfStructure = new JsonArray(); - targetOneOfStructure.addAll(createVnfSchema(modelJson)); - targetOneOfStructure.addAll(createVfModuleSchema(modelJson)); + targetOneOfStructure.addAll(createVnfSchema(modelJson, generateType)); + targetOneOfStructure.addAll(createVfModuleSchema(modelJson, generateType)); return targetOneOfStructure; } - private static JsonArray createAnyOfArrayForCdsRecipe(JsonObject resourceDetails) { + public static JsonArray createAnyOfArrayForCdsRecipe(Service modelJson) { JsonArray anyOfStructure = new JsonArray(); - anyOfStructure.addAll(createAnyOfCdsRecipe(resourceDetails.getAsJsonObject("VF"))); - anyOfStructure.addAll(createAnyOfCdsRecipe(resourceDetails.getAsJsonObject("PNF"))); + anyOfStructure.addAll(createAnyOfCdsRecipe(modelJson.getResourceDetails().getAsJsonObject("VF"))); + anyOfStructure.addAll(createAnyOfCdsRecipe(modelJson.getResourceDetails().getAsJsonObject("PNF"))); return anyOfStructure; } diff --git a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyService.java b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyService.java index 9c0cbe99c..d5442e145 100644 --- a/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyService.java +++ b/src/main/java/org/onap/clamp/policy/operational/OperationalPolicyService.java @@ -88,7 +88,7 @@ public class OperationalPolicyService implements PolicyService<OperationalPolicy */ public void refreshOperationalPolicyJsonRepresentation(OperationalPolicy operationalPolicy, ToscaConverterWithDictionarySupport toscaConverter) { - operationalPolicy.updateJsonRepresentation(toscaConverter); + operationalPolicy.updateJsonRepresentation(toscaConverter, operationalPolicy.getLoop().getModelService()); this.operationalPolicyRepository.saveAndFlush(operationalPolicy); } } diff --git a/src/main/java/org/onap/clamp/tosca/DictionaryService.java b/src/main/java/org/onap/clamp/tosca/DictionaryService.java index 98e3516fa..6172641a7 100644 --- a/src/main/java/org/onap/clamp/tosca/DictionaryService.java +++ b/src/main/java/org/onap/clamp/tosca/DictionaryService.java @@ -49,7 +49,7 @@ public class DictionaryService { } public Dictionary saveOrUpdateDictionary(Dictionary dictionary) { - return dictionaryRepository.save(dictionary); + return dictionaryRepository.saveAndFlush(dictionary); } /** @@ -76,7 +76,7 @@ public class DictionaryService { }); dict.setDictionaryElements(updatedDictionaryElements); } - return dictionaryRepository.save(dict); + return dictionaryRepository.saveAndFlush(dict); } @@ -124,7 +124,7 @@ public class DictionaryService { if (element != null) { Dictionary dict = getDictionary(dictionaryName); dict.removeDictionaryElement(element); - dictionaryRepository.save(dict); + dictionaryRepository.saveAndFlush(dict); } } } |