2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\r
5 * Licensed under the Apache License, Version 2.0 (the "License");
\r
6 * you may not use this file except in compliance with the License.
\r
7 * You may obtain a copy of the License at
\r
9 * http://www.apache.org/licenses/LICENSE-2.0
\r
11 * Unless required by applicable law or agreed to in writing, software
\r
12 * distributed under the License is distributed on an "AS IS" BASIS,
\r
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 * See the License for the specific language governing permissions and
\r
15 * limitations under the License.
\r
18 package org.onap.ccsdk.apps.controllerblueprints.service;
\r
20 import com.fasterxml.jackson.databind.JsonNode;
\r
21 import com.google.common.base.Preconditions;
\r
22 import org.apache.commons.collections.MapUtils;
\r
23 import org.apache.commons.lang3.StringUtils;
\r
24 import org.jetbrains.annotations.NotNull;
\r
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;
\r
26 import org.onap.ccsdk.apps.controllerblueprints.core.ConfigModelConstant;
\r
27 import org.onap.ccsdk.apps.controllerblueprints.core.data.*;
\r
28 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintEnhancerDefaultService;
\r
29 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRepoService;
\r
30 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;
\r
31 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment;
\r
32 import com.att.eelf.configuration.EELFLogger;
\r
33 import com.att.eelf.configuration.EELFManager;
\r
34 import org.springframework.stereotype.Service;
\r
36 import java.util.HashMap;
\r
37 import java.util.List;
\r
38 import java.util.Map;
\r
41 * BluePrintEnhancerService
\r
43 * @author Brinda Santh DATE : 8/8/2018
\r
47 public class BluePrintEnhancerService extends BluePrintEnhancerDefaultService {
\r
49 private static EELFLogger log = EELFManager.getInstance().getLogger(BluePrintEnhancerService.class);
\r
51 private Map<String, DataType> recipeDataTypes = new HashMap<>();
\r
53 public BluePrintEnhancerService(BluePrintRepoService bluePrintEnhancerRepoDBService) {
\r
54 super(bluePrintEnhancerRepoDBService);
\r
58 public void enrichTopologyTemplate(@NotNull ServiceTemplate serviceTemplate) throws BluePrintException{
\r
59 super.enrichTopologyTemplate(serviceTemplate);
\r
61 // Update the Recipe Inputs and DataTypes
\r
62 populateRecipeInputs(serviceTemplate);
\r
67 public void enrichNodeTemplate(@NotNull String nodeTemplateName, @NotNull NodeTemplate nodeTemplate) throws BluePrintException {
\r
68 super.enrichNodeTemplate(nodeTemplateName, nodeTemplate);
\r
70 String nodeTypeName = nodeTemplate.getType();
\r
71 log.info("*** Enriching NodeType: {}", nodeTypeName);
\r
72 // Get NodeType from Repo and Update Service Template
\r
73 NodeType nodeType = super.populateNodeType(nodeTypeName);
\r
76 super.enrichNodeType(nodeTypeName, nodeType);
\r
78 // Custom for Artifact Population
\r
79 if (StringUtils.isNotBlank(nodeType.getDerivedFrom())
\r
80 && ConfigModelConstant.MODEL_TYPE_NODE_ARTIFACT.equalsIgnoreCase(nodeType.getDerivedFrom())) {
\r
81 populateArtifactTemplateMappingDataType(nodeTemplateName, nodeTemplate);
\r
84 //Enrich Node Template Artifacts
\r
85 super.enrichNodeTemplateArtifactDefinition(nodeTemplateName, nodeTemplate);
\r
90 private void populateArtifactTemplateMappingDataType(@NotNull String nodeTemplateName, @NotNull NodeTemplate nodeTemplate)
\r
91 throws BluePrintException {
\r
92 log.info("****** Processing Artifact Node Template : {}", nodeTemplateName);
\r
94 if (nodeTemplate.getProperties() != null) {
\r
96 if (!nodeTemplate.getProperties().containsKey(ConfigModelConstant.PROPERTY_RECIPE_NAMES)) {
\r
97 throw new BluePrintException("Node Template (" + nodeTemplateName + ") doesn't have "
\r
98 + ConfigModelConstant.PROPERTY_RECIPE_NAMES + " property.");
\r
101 // Modified for ONAP converted Object to JsonNode
\r
102 JsonNode recipeNames = nodeTemplate.getProperties().get(ConfigModelConstant.PROPERTY_RECIPE_NAMES);
\r
104 log.info("Processing Receipe Names : {} ", recipeNames);
\r
106 if (recipeNames != null && recipeNames.isArray() && recipeNames.size() > 0) {
\r
108 Map<String, PropertyDefinition> mappingProperties =
\r
109 getCapabilityMappingProperties(nodeTemplateName, nodeTemplate);
\r
111 for (JsonNode recipeNameNode : recipeNames) {
\r
112 String recipeName = recipeNameNode.textValue();
\r
113 processRecipe(nodeTemplateName, mappingProperties, recipeName);
\r
119 private void processRecipe(@NotNull String nodeTemplateName, Map<String, PropertyDefinition> mappingProperties, String recipeName) {
\r
120 if (StringUtils.isNotBlank(recipeName)) {
\r
121 DataType recipeDataType = this.recipeDataTypes.get(recipeName);
\r
122 if (recipeDataType == null) {
\r
123 log.info("DataType not present for the recipe({})", recipeName);
\r
124 recipeDataType = new DataType();
\r
125 recipeDataType.setVersion("1.0.0");
\r
126 recipeDataType.setDescription(
\r
127 "This is Dynamic Data type definition generated from resource mapping for the config template name "
\r
128 + nodeTemplateName + ".");
\r
129 recipeDataType.setDerivedFrom(ConfigModelConstant.MODEL_TYPE_DATA_TYPE_DYNAMIC);
\r
130 Map<String, PropertyDefinition> dataTypeProperties = new HashMap<>();
\r
131 recipeDataType.setProperties(dataTypeProperties);
\r
133 log.info("DataType Already present for the recipe({})", recipeName);
\r
136 // Merge all the Recipe Properties
\r
137 mergeDataTypeProperties(recipeDataType, mappingProperties);
\r
139 // Overwrite Recipe DataType
\r
140 this.recipeDataTypes.put(recipeName, recipeDataType);
\r
145 private Map<String, PropertyDefinition> getCapabilityMappingProperties(String nodeTemplateName,
\r
146 NodeTemplate nodeTemplate) throws BluePrintException {
\r
148 Map<String, PropertyDefinition> dataTypeProperties = null;
\r
149 if (nodeTemplate != null && MapUtils.isNotEmpty(nodeTemplate.getCapabilities())) {
\r
150 CapabilityAssignment capability =
\r
151 nodeTemplate.getCapabilities().get(ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING);
\r
153 if (capability != null && capability.getProperties() != null) {
\r
155 String resourceAssignmentContent = JacksonUtils
\r
156 .getJson(capability.getProperties().get(ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING));
\r
158 List<ResourceAssignment> resourceAssignments =
\r
159 JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment.class);
\r
161 Preconditions.checkNotNull(resourceAssignments, "Failed to Processing Resource Mapping " + resourceAssignmentContent);
\r
162 dataTypeProperties = new HashMap<>();
\r
164 for (ResourceAssignment resourceAssignment : resourceAssignments) {
\r
165 if (resourceAssignment != null
\r
166 // && Boolean.valueOf(resourceAssignment.getInputParameter())
\r
167 && resourceAssignment.getProperty() != null
\r
168 && StringUtils.isNotBlank(resourceAssignment.getName())) {
\r
170 // Enrich the Property Definition
\r
171 super.enrichPropertyDefinition(resourceAssignment.getName(), resourceAssignment.getProperty());
\r
173 dataTypeProperties.put(resourceAssignment.getName(), resourceAssignment.getProperty());
\r
180 return dataTypeProperties;
\r
183 private void mergeDataTypeProperties(DataType dataType, Map<String, PropertyDefinition> mergeProperties) {
\r
184 if (dataType != null && dataType.getProperties() != null && mergeProperties != null) {
\r
185 // Add the Other Template Properties
\r
186 mergeProperties.forEach((mappingKey, propertyDefinition) -> dataType.getProperties().put(mappingKey, propertyDefinition));
\r
190 private void populateRecipeInputs(ServiceTemplate serviceTemplate) {
\r
191 if (serviceTemplate.getTopologyTemplate() != null
\r
192 && MapUtils.isNotEmpty(serviceTemplate.getTopologyTemplate().getInputs())
\r
193 && MapUtils.isNotEmpty(this.recipeDataTypes)
\r
194 && MapUtils.isNotEmpty(serviceTemplate.getDataTypes())) {
\r
195 this.recipeDataTypes.forEach((recipeName, recipeDataType) -> {
\r
196 String dataTypePrefix = recipeName.replace("-action", "") + "-request";
\r
197 String dataTypeName = "dt-" + dataTypePrefix;
\r
199 serviceTemplate.getDataTypes().put(dataTypeName, recipeDataType);
\r
201 PropertyDefinition customInputProperty = new PropertyDefinition();
\r
202 customInputProperty.setDescription("This is Dynamic Data type for the receipe " + recipeName + ".");
\r
203 customInputProperty.setRequired(Boolean.FALSE);
\r
204 customInputProperty.setType(dataTypeName);
\r
205 serviceTemplate.getTopologyTemplate().getInputs().put(dataTypePrefix, customInputProperty);
\r