930c88d8dcaa6655ffc96153f61b81289a3e1cca
[ccsdk/cds.git] /
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  *\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
8  *\r
9  *     http://www.apache.org/licenses/LICENSE-2.0\r
10  *\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
16  */\r
17 \r
18 package org.onap.ccsdk.apps.controllerblueprints.service;\r
19 \r
20 import com.att.eelf.configuration.EELFLogger;\r
21 import com.att.eelf.configuration.EELFManager;\r
22 import com.fasterxml.jackson.databind.JsonNode;\r
23 import com.google.common.base.Preconditions;\r
24 import org.apache.commons.collections.MapUtils;\r
25 import org.apache.commons.lang3.StringUtils;\r
26 import org.jetbrains.annotations.NotNull;\r
27 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;\r
28 import org.onap.ccsdk.apps.controllerblueprints.core.ConfigModelConstant;\r
29 import org.onap.ccsdk.apps.controllerblueprints.core.data.*;\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 org.onap.ccsdk.apps.controllerblueprints.service.enhancer.ResourceAssignmentEnhancerService;\r
33 \r
34 import java.util.HashMap;\r
35 import java.util.List;\r
36 import java.util.Map;\r
37 \r
38 /**\r
39  * BluePrintEnhancerService\r
40  *\r
41  * @author Brinda Santh DATE : 8/8/2018\r
42  */\r
43 \r
44 @Deprecated\r
45 public class BluePrintEnhancerService {\r
46 \r
47     private static EELFLogger log = EELFManager.getInstance().getLogger(BluePrintEnhancerService.class);\r
48 \r
49     private ResourceAssignmentEnhancerService resourceAssignmentEnhancerService;\r
50 \r
51     private Map<String, DataType> recipeDataTypes = new HashMap<>();\r
52 \r
53 \r
54     private void populateArtifactTemplateMappingDataType(@NotNull String nodeTemplateName, @NotNull NodeTemplate nodeTemplate)\r
55             throws BluePrintException {\r
56         log.info("****** Processing Artifact Node Template : {}", nodeTemplateName);\r
57 \r
58         if (nodeTemplate.getProperties() != null) {\r
59 \r
60             if (!nodeTemplate.getProperties().containsKey(ConfigModelConstant.PROPERTY_RECIPE_NAMES)) {\r
61                 throw new BluePrintException("Node Template (" + nodeTemplateName + ") doesn't have "\r
62                         + ConfigModelConstant.PROPERTY_RECIPE_NAMES + " property.");\r
63             }\r
64 \r
65             // Modified for ONAP converted Object to JsonNode\r
66             JsonNode recipeNames = nodeTemplate.getProperties().get(ConfigModelConstant.PROPERTY_RECIPE_NAMES);\r
67 \r
68             log.info("Processing Recipe Names : {} ", recipeNames);\r
69 \r
70             if (recipeNames != null && recipeNames.isArray() && recipeNames.size() > 0) {\r
71 \r
72                 Map<String, PropertyDefinition> mappingProperties =\r
73                         getCapabilityMappingProperties(nodeTemplateName, nodeTemplate);\r
74 \r
75                 for (JsonNode recipeNameNode : recipeNames) {\r
76                     String recipeName = recipeNameNode.textValue();\r
77                     processRecipe(nodeTemplateName, mappingProperties, recipeName);\r
78                 }\r
79             }\r
80         }\r
81     }\r
82 \r
83     private void processRecipe(@NotNull String nodeTemplateName, Map<String, PropertyDefinition> mappingProperties, String recipeName) {\r
84         if (StringUtils.isNotBlank(recipeName)) {\r
85             DataType recipeDataType = this.recipeDataTypes.get(recipeName);\r
86             if (recipeDataType == null) {\r
87                 log.info("DataType not present for the recipe({})", recipeName);\r
88                 recipeDataType = new DataType();\r
89                 recipeDataType.setVersion("1.0.0");\r
90                 recipeDataType.setDescription(\r
91                         "This is Dynamic Data type definition generated from resource mapping for the config template name "\r
92                                 + nodeTemplateName + ".");\r
93                 recipeDataType.setDerivedFrom(ConfigModelConstant.MODEL_TYPE_DATA_TYPE_DYNAMIC);\r
94                 Map<String, PropertyDefinition> dataTypeProperties = new HashMap<>();\r
95                 recipeDataType.setProperties(dataTypeProperties);\r
96             } else {\r
97                 log.info("DataType Already present for the recipe({})", recipeName);\r
98             }\r
99 \r
100             // Merge all the Recipe Properties\r
101             mergeDataTypeProperties(recipeDataType, mappingProperties);\r
102 \r
103             // Overwrite Recipe DataType\r
104             this.recipeDataTypes.put(recipeName, recipeDataType);\r
105 \r
106         }\r
107     }\r
108 \r
109     private Map<String, PropertyDefinition> getCapabilityMappingProperties(String nodeTemplateName,\r
110                                                                            NodeTemplate nodeTemplate) throws BluePrintException {\r
111 \r
112         Map<String, PropertyDefinition> dataTypeProperties = null;\r
113         if (nodeTemplate != null && MapUtils.isNotEmpty(nodeTemplate.getCapabilities())) {\r
114             CapabilityAssignment capability =\r
115                     nodeTemplate.getCapabilities().get(ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING);\r
116 \r
117             if (capability != null && capability.getProperties() != null) {\r
118 \r
119                 String resourceAssignmentContent = JacksonUtils\r
120                         .getJson(capability.getProperties().get(ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING));\r
121 \r
122                 List<ResourceAssignment> resourceAssignments =\r
123                         JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment.class);\r
124 \r
125                 Preconditions.checkNotNull(resourceAssignments, "Failed to Processing Resource Mapping " + resourceAssignmentContent);\r
126                 // Enhance Resource Assignment TODO("Plug Resource Assignment Enhancer Service")\r
127                 //resourceAssignmentEnhancerService.enhanceBluePrint(this, resourceAssignments);\r
128 \r
129                 dataTypeProperties = new HashMap<>();\r
130 \r
131                 for (ResourceAssignment resourceAssignment : resourceAssignments) {\r
132                     if (resourceAssignment != null\r
133                             // && Boolean.valueOf(resourceAssignment.getInputParameter())\r
134                             && resourceAssignment.getProperty() != null\r
135                             && StringUtils.isNotBlank(resourceAssignment.getName())) {\r
136 \r
137                         dataTypeProperties.put(resourceAssignment.getName(), resourceAssignment.getProperty());\r
138 \r
139                     }\r
140                 }\r
141 \r
142             }\r
143         }\r
144         return dataTypeProperties;\r
145     }\r
146 \r
147     private void mergeDataTypeProperties(DataType dataType, Map<String, PropertyDefinition> mergeProperties) {\r
148         if (dataType != null && dataType.getProperties() != null && mergeProperties != null) {\r
149             // Add the Other Template Properties\r
150             mergeProperties.forEach((mappingKey, propertyDefinition) -> dataType.getProperties().put(mappingKey, propertyDefinition));\r
151         }\r
152     }\r
153 \r
154     private void populateRecipeInputs(ServiceTemplate serviceTemplate) {\r
155         if (serviceTemplate.getTopologyTemplate() != null\r
156                 && MapUtils.isNotEmpty(serviceTemplate.getTopologyTemplate().getInputs())\r
157                 && MapUtils.isNotEmpty(this.recipeDataTypes)\r
158                 && MapUtils.isNotEmpty(serviceTemplate.getDataTypes())) {\r
159             this.recipeDataTypes.forEach((recipeName, recipeDataType) -> {\r
160                 String dataTypePrefix = recipeName.replace("-action", "") + "-request";\r
161                 String dataTypeName = "dt-" + dataTypePrefix;\r
162 \r
163                 serviceTemplate.getDataTypes().put(dataTypeName, recipeDataType);\r
164 \r
165                 PropertyDefinition customInputProperty = new PropertyDefinition();\r
166                 customInputProperty.setDescription("This is Dynamic Data type for the receipe " + recipeName + ".");\r
167                 customInputProperty.setRequired(Boolean.FALSE);\r
168                 customInputProperty.setType(dataTypeName);\r
169                 serviceTemplate.getTopologyTemplate().getInputs().put(dataTypePrefix, customInputProperty);\r
170 \r
171             });\r
172         }\r
173     }\r
174 }\r