2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
6 * Copyright (c) 2017-2019 European Software Marketing Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.aai.babel.parser;
24 import com.google.gson.Gson;
25 import com.google.gson.JsonSyntaxException;
26 import java.io.BufferedReader;
28 import java.io.FileReader;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
34 import java.util.Optional;
35 import java.util.stream.Collectors;
36 import java.util.stream.Stream;
37 import org.onap.aai.babel.logging.LogHelper;
38 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
39 import org.onap.aai.babel.xml.generator.data.GroupConfiguration;
40 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
41 import org.onap.aai.babel.xml.generator.model.Model;
42 import org.onap.aai.babel.xml.generator.model.Resource;
43 import org.onap.aai.babel.xml.generator.model.Widget;
44 import org.onap.aai.babel.xml.generator.model.WidgetType;
45 import org.onap.aai.babel.xml.generator.types.ModelType;
46 import org.onap.aai.cl.api.Logger;
47 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
48 import org.onap.sdc.toscaparser.api.Group;
49 import org.onap.sdc.toscaparser.api.NodeTemplate;
50 import org.onap.sdc.toscaparser.api.Property;
51 import org.onap.sdc.toscaparser.api.elements.Metadata;
54 * Wrapper for the sdc-tosca parser.
57 public class ArtifactGeneratorToscaParser {
59 private static Logger log = LogHelper.INSTANCE;
61 public static final String PROPERTY_TOSCA_MAPPING_FILE = "tosca.mappings.config";
63 public static final String GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND =
64 "Cannot generate artifacts. System property %s not configured";
66 private static final String GENERATOR_AAI_CONFIGFILE_NOT_FOUND =
67 "Cannot generate artifacts. Artifact Generator Configuration file not found at %s";
68 private static final String GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING =
69 "Cannot generate artifacts. Providing Service Metadata is missing for allotted resource %s";
70 private static final String GENERATOR_AAI_PROVIDING_SERVICE_MISSING =
71 "Cannot generate artifacts. Providing Service is missing for allotted resource %s";
73 // Metadata properties
74 private static final String CATEGORY = "category";
75 private static final String ALLOTTED_RESOURCE = "Allotted Resource";
76 private static final String SUBCATEGORY = "subcategory";
77 private static final String TUNNEL_XCONNECT = "Tunnel XConnect";
79 private static final String VERSION = "version";
81 private ISdcCsarHelper csarHelper;
84 * Constructs using csarHelper
89 public ArtifactGeneratorToscaParser(ISdcCsarHelper csarHelper) {
90 this.csarHelper = csarHelper;
94 * Initializes the group filtering and TOSCA to Widget mapping configuration.
96 * @param configLocation
97 * the pathname to the JSON mappings file
99 * if the file content could not be read successfully
101 public static void initToscaMappingsConfiguration(String configLocation) throws IOException {
102 log.debug("Getting TOSCA Mappings Configuration");
103 File file = new File(configLocation);
104 if (!file.exists()) {
105 throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGFILE_NOT_FOUND, configLocation));
108 GroupConfiguration config;
110 try (BufferedReader bufferedReader = new BufferedReader(new FileReader(configLocation))) {
111 config = new Gson().fromJson(bufferedReader, GroupConfiguration.class);
112 } catch (JsonSyntaxException e) {
113 throw new IOException("Invalid Mappings Configuration " + configLocation, e);
116 if (config == null) {
117 throw new IOException("There is no content for the Mappings Configuration " + configLocation);
120 WidgetConfigurationUtil.setSupportedInstanceGroups(config.getInstanceGroupTypes());
121 WidgetConfigurationUtil.setWidgetTypes(config.getWidgetTypes());
122 WidgetConfigurationUtil.setWidgetMappings(config.getWidgetMappings());
126 * Process groups for this service node, according to the defined filter.
128 * @param resourceModel
129 * @param serviceNodeTemplate
130 * @return resources for which XML Models should be generated
131 * @throws XmlArtifactGenerationException
132 * if there is no configuration defined for a member Widget of an instance group
134 public List<Resource> processInstanceGroups(Model resourceModel, NodeTemplate serviceNodeTemplate)
135 throws XmlArtifactGenerationException {
136 List<Resource> resources = new ArrayList<>();
137 if (serviceNodeTemplate.getSubMappingToscaTemplate() != null) {
138 List<Group> serviceGroups = csarHelper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate);
139 for (Group group : serviceGroups) {
140 if (WidgetConfigurationUtil.isSupportedInstanceGroup(group.getType())) {
141 resources.addAll(processInstanceGroup(resourceModel, group.getMemberNodes(),
142 group.getMetadata().getAllProperties(), group.getProperties()));
150 * Merge a Map of String values with a Map of TOSCA Property Objects to create a combined Map. If there are
151 * duplicate keys then the TOSCA Property value takes precedence.
154 * initial Map of String property values (e.g. from the TOSCA YAML metadata section)
156 * Map of TOSCA Property Type Object values to merge in (or overwrite)
157 * @return a Map of the property values converted to String
159 public Map<String, String> mergeProperties(Map<String, String> stringProps, Map<String, Property> toscaProps) {
160 Map<String, String> props = new HashMap<>(stringProps);
161 toscaProps.forEach((key, toscaProp) -> props.put(key,
162 toscaProp.getValue() == null ? "" : toscaProp.getValue().toString()));
166 public Resource createInstanceGroupModel(Map<String, String> properties) {
167 Resource groupModel = new Resource(WidgetType.valueOf("INSTANCE_GROUP"), true);
168 groupModel.populateModelIdentificationInformation(properties);
173 * Add the resource/widget to the specified model.
177 * resource or widget model to add
178 * @throws XmlArtifactGenerationException
179 * if the relation is a widget and there is no configuration defined for the relation's widget type
181 public void addRelatedModel(final Model model, final Resource relation) throws XmlArtifactGenerationException {
182 if (relation.getModelType() == ModelType.RESOURCE) {
183 model.addResource(relation);
185 model.addWidget(Widget.createWidget(relation.getWidgetType()));
189 public boolean hasAllottedResource(Map<String, String> metadata) {
190 return ALLOTTED_RESOURCE.equals(metadata.get(CATEGORY));
193 public boolean hasSubCategoryTunnelXConnect(Map<String, String> metadata) {
194 return TUNNEL_XCONNECT.equals(metadata.get(SUBCATEGORY));
198 * Process TOSCA Group information for VF Modules.
203 * @throws XmlArtifactGenerationException
204 * if the configured widget mappings do not support the widget type of a VF Module
206 public void processVfModules(List<Resource> resources, Model resourceModel, NodeTemplate serviceNode)
207 throws XmlArtifactGenerationException {
208 // Get the customization UUID for each VF node and use it to get its Groups
209 String uuid = csarHelper.getNodeTemplateCustomizationUuid(serviceNode);
210 List<Group> serviceGroups = csarHelper.getVfModulesByVf(uuid);
212 // Process each VF Group
213 for (Group serviceGroup : serviceGroups) {
214 Model groupModel = Model.getModelFor(serviceGroup.getType());
215 if (groupModel.hasWidgetType("VFMODULE")) {
216 processVfModule(resources, resourceModel, serviceGroup, serviceNode, (Resource) groupModel);
222 * @param resourceModel
223 * @param resourceNodeTemplates
225 public void processResourceModels(Model resourceModel, List<NodeTemplate> resourceNodeTemplates) {
226 boolean foundProvidingService = false;
228 for (NodeTemplate resourceNodeTemplate : resourceNodeTemplates) {
229 String nodeTypeName = resourceNodeTemplate.getType();
230 Metadata metadata = resourceNodeTemplate.getMetaData();
231 String metaDataType = Optional.ofNullable(metadata).map(m -> m.getValue("type")).orElse(nodeTypeName);
232 Resource model = Model.getModelFor(nodeTypeName, metaDataType);
234 if (metadata != null && hasAllottedResource(metadata.getAllProperties())
235 && model.hasWidgetType("VSERVER")) {
236 model = new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), false);
237 Map<String, Object> props = new HashMap<>();
238 props.put("providingService", true);
239 model.setProperties(props);
242 foundProvidingService |= processModel(resourceModel, metadata, model, resourceNodeTemplate.getProperties());
245 if (resourceModel.hasWidgetType("ALLOTTED_RESOURCE") && !foundProvidingService) {
246 final String modelInvariantId = resourceModel.getModelId();
247 throw new IllegalArgumentException(String.format(GENERATOR_AAI_PROVIDING_SERVICE_MISSING,
248 modelInvariantId == null ? "<null ID>" : modelInvariantId));
253 * Create an Instance Group Model and populate it with the supplied data.
255 * @param resourceModel
256 * the Resource node template Model
258 * the Resources and Widgets belonging to the Group
259 * @param metaProperties
260 * the metadata of the Group
262 * the properties of the Group
263 * @return the Instance Group and Member resource models
264 * @throws XmlArtifactGenerationException
265 * if there is no configuration defined for one of the member Widgets
267 private List<Resource> processInstanceGroup(Model resourceModel, ArrayList<NodeTemplate> memberNodes,
268 Map<String, String> metaProperties, Map<String, Property> properties)
269 throws XmlArtifactGenerationException {
270 Resource groupModel = createInstanceGroupModel(mergeProperties(metaProperties, properties));
271 resourceModel.addResource(groupModel);
272 List<Resource> resources = Stream.of(groupModel).collect(Collectors.toList());
274 if (memberNodes != null && !memberNodes.isEmpty()) {
275 resources.addAll(generateResourcesAndWidgets(memberNodes, groupModel));
284 * @return a list of Resources
285 * @throws XmlArtifactGenerationException
286 * if a member node template is a widget and there is no configuration defined for that relation's
289 private List<Resource> generateResourcesAndWidgets(final ArrayList<NodeTemplate> memberNodes,
290 final Resource groupModel) throws XmlArtifactGenerationException {
291 log.debug(String.format("Processing member nodes for Group %s (invariant UUID %s)", //
292 groupModel.getModelName(), groupModel.getModelId()));
294 List<Resource> resources = new ArrayList<>();
296 for (NodeTemplate nodeTemplate : memberNodes) {
297 String nodeTypeName = nodeTemplate.getType();
298 final String metadataType = nodeTemplate.getMetaData().getValue("type");
300 log.debug(String.format("Get model for %s (metadata type %s)", nodeTypeName, metadataType));
301 Resource memberModel = Model.getModelFor(nodeTypeName, metadataType);
303 if (memberModel != null) {
304 memberModel.populateModelIdentificationInformation(nodeTemplate.getMetaData().getAllProperties());
306 log.debug(String.format("Generating grouped %s (%s) from TOSCA type %s",
307 memberModel.getClass().getSuperclass().getSimpleName(), memberModel.getClass(), nodeTypeName));
309 addRelatedModel(groupModel, memberModel);
310 if (memberModel.getModelType() == ModelType.RESOURCE) {
311 resources.add(memberModel);
321 * @param groupDefinition
324 * @throws XmlArtifactGenerationException
325 * if the configured widget mappings do not support the widget type of a VF Module
327 private void processVfModule(List<Resource> resources, Model vfModel, Group groupDefinition,
328 NodeTemplate serviceNode, Resource groupModel) throws XmlArtifactGenerationException {
329 groupModel.populateModelIdentificationInformation(
330 mergeProperties(groupDefinition.getMetadata().getAllProperties(), groupDefinition.getProperties()));
332 processVfModuleGroup(groupModel, csarHelper.getMembersOfVfModule(serviceNode, groupDefinition));
334 vfModel.addResource(groupModel); // Add group (VfModule) to the (VF) model
335 // Check if we have already encountered the same VfModule across all the artifacts
336 if (!resources.contains(groupModel)) {
337 resources.add(groupModel);
344 * @throws XmlArtifactGenerationException
345 * if the configured widget mappings do not support the widget type of a member
347 private void processVfModuleGroup(Resource groupModel, List<NodeTemplate> members)
348 throws XmlArtifactGenerationException {
349 if (members != null && !members.isEmpty()) {
350 // Get names of the members of the service group
351 List<String> memberNames = members.stream().map(NodeTemplate::getName).collect(Collectors.toList());
352 groupModel.setMembers(memberNames);
353 for (NodeTemplate member : members) {
354 processGroupMembers(groupModel, member);
360 * Process the Widget members of a VF Module Group
363 * the group resource model
365 * the group member to process
366 * @throws XmlArtifactGenerationException
367 * if the configured widget mappings do not support the widget type of the member
369 private void processGroupMembers(Resource group, NodeTemplate member) throws XmlArtifactGenerationException {
370 Resource resource = Model.getModelFor(member.getType());
372 log.debug(member.getType() + " mapped to " + resource);
374 if (resource.hasWidgetType("L3_NET")) {
375 // An l3-network inside a vf-module is treated as a Widget
376 resource.setModelType(ModelType.WIDGET);
379 if (resource.getModelType() == ModelType.WIDGET) {
380 Widget widget = Widget.createWidget(resource.getWidgetType());
381 widget.addKey(member.getName());
382 // Add the widget element encountered to the Group model
383 group.addWidget(widget);
388 * Create a Map of property name against String property value from the input Map
392 * @return Map of property name against String property value
394 private Map<String, String> populateStringProperties(Map<String, Property> inputMap) {
395 return inputMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
396 e -> e.getValue().getValue() == null ? "" : e.getValue().getValue().toString()));
400 * If the specified resourceNode is a type of Resource, add it to the specified resourceModel. If the Resource type
401 * is ProvidingService then return true, otherwise return false.
403 * @param resourceModel
406 * for populating the Resource IDs
407 * @param resourceNode
408 * any Model (will be ignored if not a Resource)
409 * @param nodeProperties
410 * the node properties
411 * @return whether or not a ProvidingService was processed
413 private boolean processModel(Model resourceModel, Metadata metaData, Resource resourceNode,
414 Map<String, Property> nodeProperties) {
415 boolean foundProvidingService = resourceNode != null
416 && (boolean) Optional.ofNullable(resourceNode.getProperties().get("providingService")).orElse(false);
418 if (foundProvidingService) {
419 processProvidingService(resourceModel, resourceNode, nodeProperties);
420 } else if (resourceNode != null && resourceNode.getModelType() == ModelType.RESOURCE
421 && !resourceNode.hasWidgetType("L3_NET")) {
422 if (metaData != null) {
423 resourceNode.populateModelIdentificationInformation(metaData.getAllProperties());
425 resourceModel.addResource(resourceNode);
427 return foundProvidingService;
430 private void processProvidingService(Model resourceModel, Resource resourceNode,
431 Map<String, Property> nodeProperties) {
432 if (nodeProperties == null || nodeProperties.get("providing_service_uuid") == null
433 || nodeProperties.get("providing_service_invariant_uuid") == null) {
434 throw new IllegalArgumentException(
435 String.format(GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING, resourceModel.getModelId()));
437 Map<String, String> properties = populateStringProperties(nodeProperties);
438 properties.put(VERSION, "1.0");
439 resourceNode.populateModelIdentificationInformation(properties);
440 resourceModel.addResource(resourceNode);