aboutsummaryrefslogtreecommitdiffstats
path: root/models/src/main
diff options
context:
space:
mode:
authorFrancescoFioraEst <francesco.fiora@est.tech>2021-09-29 12:16:06 +0100
committerFrancescoFioraEst <francesco.fiora@est.tech>2021-10-05 15:03:31 +0100
commitf84a61a944d6561550d96880a54c410ca03deabe (patch)
tree70ddc9091d5b3c86ba2225324b5a32b51c3914f5 /models/src/main
parent680b87d278673b1768be1c13fca90d6f7dfef3af (diff)
Handle ParticipantRegister
Send ParticipantUpdate to participant after registration if commissioning has already been created Issue-ID: POLICY-3689 Change-Id: Iccc4fc1c0b95e1f270b6810c5b5f130726f24650 Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
Diffstat (limited to 'models/src/main')
-rw-r--r--models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ServiceTemplateProvider.java226
1 files changed, 226 insertions, 0 deletions
diff --git a/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ServiceTemplateProvider.java b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ServiceTemplateProvider.java
new file mode 100644
index 000000000..6a6d20fe7
--- /dev/null
+++ b/models/src/main/java/org/onap/policy/clamp/controlloop/models/controlloop/persistence/provider/ServiceTemplateProvider.java
@@ -0,0 +1,226 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2021 Nordix Foundation.
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.clamp.controlloop.models.controlloop.persistence.provider;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import javax.ws.rs.core.Response.Status;
+import lombok.RequiredArgsConstructor;
+import org.onap.policy.models.base.PfModelException;
+import org.onap.policy.models.provider.PolicyModelsProvider;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeType;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.springframework.stereotype.Component;
+
+@Component
+@RequiredArgsConstructor
+public class ServiceTemplateProvider {
+
+ private final PolicyModelsProvider modelsProvider;
+
+ /**
+ * Create service template.
+ *
+ * @param serviceTemplate the service template to be created
+ * @return the created service template
+ * @throws PfModelException on errors creating the service template
+ */
+ public ToscaServiceTemplate createServiceTemplate(final ToscaServiceTemplate serviceTemplate)
+ throws PfModelException {
+ return modelsProvider.createServiceTemplate(serviceTemplate);
+ }
+
+ /**
+ * Delete service template.
+ *
+ * @param name the name of the service template to delete.
+ * @param version the version of the service template to delete.
+ * @return the TOSCA service template that was deleted
+ * @throws PfModelException on errors deleting policy types
+ */
+ public ToscaServiceTemplate deleteServiceTemplate(final String name, final String version) throws PfModelException {
+ return modelsProvider.deleteServiceTemplate(name, version);
+ }
+
+ /**
+ * Get the requested control loop definitions.
+ *
+ * @param name the name of the definition to get, null for all definitions
+ * @param version the version of the definition to get, null for all definitions
+ * @return the control loop definitions
+ * @throws PfModelException on errors getting control loop definitions
+ */
+ public ToscaServiceTemplate getToscaServiceTemplate(String name, String version) throws PfModelException {
+ var serviceTemplates = modelsProvider.getServiceTemplateList(name, version);
+ if (serviceTemplates.isEmpty()) {
+ throw new PfModelException(Status.NOT_FOUND, "Control Loop definitions not found");
+ }
+ return serviceTemplates.get(0);
+ }
+
+ /**
+ * Get service templates.
+ *
+ * @param name the name of the topology template to get, set to null to get all service templates
+ * @param version the version of the service template to get, set to null to get all service templates
+ * @return the topology templates found
+ * @throws PfModelException on errors getting service templates
+ */
+ public List<ToscaServiceTemplate> getServiceTemplateList(final String name, final String version)
+ throws PfModelException {
+ return modelsProvider.getServiceTemplateList(name, version);
+ }
+
+ /**
+ * Get the initial node types with common or instance properties.
+ *
+ * @param fullNodeTypes map of all the node types in the specified template
+ * @param common boolean to indicate whether common or instance properties are required
+ * @return node types map that only has common properties
+ * @throws PfModelException on errors getting node type with common properties
+ */
+ private Map<String, ToscaNodeType> getInitialNodeTypesMap(Map<String, ToscaNodeType> fullNodeTypes,
+ boolean common) {
+
+ var tempNodeTypesMap = new HashMap<String, ToscaNodeType>();
+
+ fullNodeTypes.forEach((key, nodeType) -> {
+ var tempToscaNodeType = new ToscaNodeType();
+ tempToscaNodeType.setName(key);
+
+ var resultantPropertyMap = findCommonOrInstancePropsInNodeTypes(nodeType, common);
+
+ if (!resultantPropertyMap.isEmpty()) {
+ tempToscaNodeType.setProperties(resultantPropertyMap);
+ tempNodeTypesMap.put(key, tempToscaNodeType);
+ }
+ });
+ return tempNodeTypesMap;
+ }
+
+ private Map<String, ToscaProperty> findCommonOrInstancePropsInNodeTypes(ToscaNodeType nodeType, boolean common) {
+
+ var tempCommonPropertyMap = new HashMap<String, ToscaProperty>();
+ var tempInstancePropertyMap = new HashMap<String, ToscaProperty>();
+
+ nodeType.getProperties().forEach((propKey, prop) -> {
+
+ if (prop.getMetadata() != null) {
+ prop.getMetadata().forEach((k, v) -> {
+ if (k.equals("common") && v.equals("true") && common) {
+ tempCommonPropertyMap.put(propKey, prop);
+ } else if (k.equals("common") && v.equals("false") && !common) {
+ tempInstancePropertyMap.put(propKey, prop);
+ }
+
+ });
+ } else {
+ tempInstancePropertyMap.put(propKey, prop);
+ }
+ });
+
+ if (tempCommonPropertyMap.isEmpty() && !common) {
+ return tempInstancePropertyMap;
+ } else {
+ return tempCommonPropertyMap;
+ }
+ }
+
+ /**
+ * Get the node types derived from those that have common properties.
+ *
+ * @param initialNodeTypes map of all the node types in the specified template
+ * @param filteredNodeTypes map of all the node types that have common or instance properties
+ * @return all node types that have common properties including their children
+ * @throws PfModelException on errors getting node type with common properties
+ */
+ private Map<String, ToscaNodeType> getFinalNodeTypesMap(Map<String, ToscaNodeType> initialNodeTypes,
+ Map<String, ToscaNodeType> filteredNodeTypes) {
+ for (var i = 0; i < initialNodeTypes.size(); i++) {
+ initialNodeTypes.forEach((key, nodeType) -> {
+ var tempToscaNodeType = new ToscaNodeType();
+ tempToscaNodeType.setName(key);
+
+ if (filteredNodeTypes.get(nodeType.getDerivedFrom()) != null) {
+ tempToscaNodeType.setName(key);
+
+ var finalProps = new HashMap<String, ToscaProperty>(
+ filteredNodeTypes.get(nodeType.getDerivedFrom()).getProperties());
+
+ tempToscaNodeType.setProperties(finalProps);
+ } else {
+ return;
+ }
+ filteredNodeTypes.putIfAbsent(key, tempToscaNodeType);
+
+ });
+ }
+ return filteredNodeTypes;
+ }
+
+ /**
+ * Get the requested node types with common or instance properties.
+ *
+ * @param common boolean indicating common or instance properties
+ * @param serviceTemplate the ToscaServiceTemplate
+ * @return the node types with common or instance properties
+ * @throws PfModelException on errors getting node type properties
+ */
+ public Map<String, ToscaNodeType> getCommonOrInstancePropertiesFromNodeTypes(boolean common,
+ ToscaServiceTemplate serviceTemplate) throws PfModelException {
+ var tempNodeTypesMap = this.getInitialNodeTypesMap(serviceTemplate.getNodeTypes(), common);
+
+ return this.getFinalNodeTypesMap(serviceTemplate.getNodeTypes(), tempNodeTypesMap);
+
+ }
+
+ /**
+ * Get node templates with appropriate common or instance properties added.
+ *
+ * @param initialNodeTemplates map of all the node templates in the specified template
+ * @param nodeTypeProps map of all the node types that have common or instance properties including children
+ * @return all node templates with appropriate common or instance properties added
+ * @throws PfModelException on errors getting map of node templates with common or instance properties added
+ */
+ public Map<String, ToscaNodeTemplate> getDerivedCommonOrInstanceNodeTemplates(
+ Map<String, ToscaNodeTemplate> initialNodeTemplates, Map<String, ToscaNodeType> nodeTypeProps) {
+
+ var finalNodeTemplatesMap = new HashMap<String, ToscaNodeTemplate>();
+
+ initialNodeTemplates.forEach((templateKey, template) -> {
+ if (nodeTypeProps.containsKey(template.getType())) {
+ var finalMergedProps = new HashMap<String, Object>();
+
+ nodeTypeProps.get(template.getType()).getProperties().forEach(finalMergedProps::putIfAbsent);
+
+ template.setProperties(finalMergedProps);
+
+ finalNodeTemplatesMap.put(templateKey, template);
+ } else {
+ return;
+ }
+ });
+ return finalNodeTemplatesMap;
+ }
+}