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.FileInputStream;
29 import java.io.FileReader;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
35 import java.util.Optional;
36 import java.util.Properties;
37 import java.util.stream.Collectors;
38 import java.util.stream.Stream;
39 import org.onap.aai.babel.logging.LogHelper;
40 import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
41 import org.onap.aai.babel.xml.generator.data.GroupConfiguration;
42 import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
43 import org.onap.aai.babel.xml.generator.model.Model;
44 import org.onap.aai.babel.xml.generator.model.Resource;
45 import org.onap.aai.babel.xml.generator.model.Widget;
46 import org.onap.aai.babel.xml.generator.model.Widget.Type;
47 import org.onap.aai.babel.xml.generator.types.ModelType;
48 import org.onap.aai.cl.api.Logger;
49 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
50 import org.onap.sdc.toscaparser.api.Group;
51 import org.onap.sdc.toscaparser.api.NodeTemplate;
52 import org.onap.sdc.toscaparser.api.Property;
53 import org.onap.sdc.toscaparser.api.elements.Metadata;
56 * Wrapper for the sdc-tosca parser.
59 public class ArtifactGeneratorToscaParser {
61 private static Logger log = LogHelper.INSTANCE;
63 public static final String PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE = "artifactgenerator.config";
64 public static final String PROPERTY_TOSCA_MAPPING_FILE = "tosca.mappings.config";
66 public static final String GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND =
67 "Cannot generate artifacts. System property %s not configured";
69 private static final String GENERATOR_AAI_CONFIGFILE_NOT_FOUND =
70 "Cannot generate artifacts. Artifact Generator Configuration file not found at %s";
71 private static final String GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING =
72 "Cannot generate artifacts. Providing Service Metadata is missing for allotted resource %s";
73 private static final String GENERATOR_AAI_PROVIDING_SERVICE_MISSING =
74 "Cannot generate artifacts. Providing Service is missing for allotted resource %s";
76 // Metadata properties
77 private static final String CATEGORY = "category";
78 private static final String ALLOTTED_RESOURCE = "Allotted Resource";
79 private static final String SUBCATEGORY = "subcategory";
80 private static final String TUNNEL_XCONNECT = "Tunnel XConnect";
82 private static final String VERSION = "version";
84 private ISdcCsarHelper csarHelper;
87 * Constructs using csarHelper
92 public ArtifactGeneratorToscaParser(ISdcCsarHelper csarHelper) {
93 this.csarHelper = csarHelper;
97 * Initializes the Widget to UUID mapping configuration.
100 * if an error occurs reading the configuration properties
102 public static void initWidgetConfiguration() throws IOException {
103 log.debug("Getting Widget Configuration");
104 String configLocation = System.getProperty(PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE);
105 if (configLocation != null) {
106 File file = new File(configLocation);
108 Properties properties = new Properties();
109 properties.load(new FileInputStream(file));
110 WidgetConfigurationUtil.setConfig(properties);
112 throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGFILE_NOT_FOUND, configLocation));
115 throw new IllegalArgumentException(
116 String.format(GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND, PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE));
121 * Initializes the group filtering and TOSCA to Widget mapping configuration.
123 * @param configLocation
124 * the pathname to the JSON mappings file
125 * @throws IOException
126 * if the file content could not be read successfully
128 public static void initToscaMappingsConfiguration(String configLocation) throws IOException {
129 log.debug("Getting TOSCA Mappings Configuration");
130 File file = new File(configLocation);
131 if (!file.exists()) {
132 throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGFILE_NOT_FOUND, configLocation));
135 GroupConfiguration config;
137 try (BufferedReader bufferedReader = new BufferedReader(new FileReader(configLocation))) {
138 config = new Gson().fromJson(bufferedReader, GroupConfiguration.class);
139 } catch (JsonSyntaxException e) {
140 throw new IOException("Invalid Mappings Configuration " + configLocation, e);
143 if (config == null) {
144 throw new IOException("There is no content for the Mappings Configuration " + configLocation);
147 WidgetConfigurationUtil.setSupportedInstanceGroups(config.getInstanceGroupTypes());
148 WidgetConfigurationUtil.setWidgetTypes(config.getWidgetTypes());
149 WidgetConfigurationUtil.setWidgetMappings(config.getWidgetMappings());
153 * Process groups for this service node, according to the defined filter.
155 * @param resourceModel
156 * @param serviceNodeTemplate
157 * @return resources for which XML Models should be generated
158 * @throws XmlArtifactGenerationException
159 * if there is no configuration defined for a member Widget of an instance group
161 public List<Resource> processInstanceGroups(Model resourceModel, NodeTemplate serviceNodeTemplate)
162 throws XmlArtifactGenerationException {
163 List<Resource> resources = new ArrayList<>();
164 if (serviceNodeTemplate.getSubMappingToscaTemplate() != null) {
165 List<Group> serviceGroups = csarHelper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate);
166 for (Group group : serviceGroups) {
167 if (WidgetConfigurationUtil.isSupportedInstanceGroup(group.getType())) {
168 resources.addAll(processInstanceGroup(resourceModel, group.getMemberNodes(),
169 group.getMetadata().getAllProperties(), group.getProperties()));
177 * Merge a Map of String values with a Map of TOSCA Property Objects to create a combined Map. If there are
178 * duplicate keys then the TOSCA Property value takes precedence.
181 * initial Map of String property values (e.g. from the TOSCA YAML metadata section)
183 * Map of TOSCA Property Type Object values to merge in (or overwrite)
184 * @return a Map of the property values converted to String
186 public Map<String, String> mergeProperties(Map<String, String> stringProps, Map<String, Property> toscaProps) {
187 Map<String, String> props = new HashMap<>(stringProps);
188 toscaProps.forEach((key, toscaProp) -> props.put(key,
189 toscaProp.getValue() == null ? "" : toscaProp.getValue().toString()));
193 public Resource createInstanceGroupModel(Map<String, String> properties) {
194 Resource groupModel = new Resource(Type.INSTANCE_GROUP, true);
195 groupModel.populateModelIdentificationInformation(properties);
200 * Add the resource/widget to the specified model.
204 * resource or widget model to add
205 * @throws XmlArtifactGenerationException
206 * if the relation is a widget and there is no configuration defined for the relation's widget type
208 public void addRelatedModel(final Model model, final Resource relation) throws XmlArtifactGenerationException {
209 if (relation.getModelType() == ModelType.RESOURCE) {
210 model.addResource(relation);
212 model.addWidget(Widget.getWidget(relation.getWidgetType()));
216 public boolean hasAllottedResource(Map<String, String> metadata) {
217 return ALLOTTED_RESOURCE.equals(metadata.get(CATEGORY));
220 public boolean hasSubCategoryTunnelXConnect(Map<String, String> metadata) {
221 return TUNNEL_XCONNECT.equals(metadata.get(SUBCATEGORY));
225 * Process TOSCA Group information for VF Modules.
230 * @throws XmlArtifactGenerationException
232 public void processVfModules(List<Resource> resources, Model resourceModel, NodeTemplate serviceNode)
233 throws XmlArtifactGenerationException {
234 // Get the customisation UUID for each VF node and use it to get its Groups
235 String uuid = csarHelper.getNodeTemplateCustomizationUuid(serviceNode);
236 List<Group> serviceGroups = csarHelper.getVfModulesByVf(uuid);
238 // Process each VF Group
239 for (Group serviceGroup : serviceGroups) {
240 Model groupModel = Model.getModelFor(serviceGroup.getType());
241 if (groupModel.getWidgetType() == Type.VFMODULE) {
242 processVfModule(resources, resourceModel, serviceGroup, serviceNode, (Resource) groupModel);
248 * @param resourceModel
249 * @param resourceNodeTemplates
251 public void processResourceModels(Model resourceModel, List<NodeTemplate> resourceNodeTemplates) {
252 boolean foundProvidingService = false;
254 for (NodeTemplate resourceNodeTemplate : resourceNodeTemplates) {
255 String nodeTypeName = resourceNodeTemplate.getType();
256 Metadata metadata = resourceNodeTemplate.getMetaData();
257 String metaDataType = Optional.ofNullable(metadata).map(m -> m.getValue("type")).orElse(nodeTypeName);
258 Resource model = Model.getModelFor(nodeTypeName, metaDataType);
260 if (metadata != null && hasAllottedResource(metadata.getAllProperties())
261 && model.getWidgetType() == Type.VSERVER) {
262 model = new Resource(Type.ALLOTTED_RESOURCE, false);
263 Map<String, Object> props = new HashMap<>();
264 props.put("providingService", true);
265 model.setProperties(props);
268 foundProvidingService |= processModel(resourceModel, metadata, model, resourceNodeTemplate.getProperties());
271 if (resourceModel.getWidgetType() == Type.ALLOTTED_RESOURCE && !foundProvidingService) {
272 final String modelInvariantId = resourceModel.getModelId();
273 throw new IllegalArgumentException(String.format(GENERATOR_AAI_PROVIDING_SERVICE_MISSING,
274 modelInvariantId == null ? "<null ID>" : modelInvariantId));
279 * Create an Instance Group Model and populate it with the supplied data.
281 * @param resourceModel
282 * the Resource node template Model
284 * the Resources and Widgets belonging to the Group
285 * @param metaProperties
286 * the metadata of the Group
288 * the properties of the Group
289 * @return the Instance Group and Member resource models
290 * @throws XmlArtifactGenerationException
291 * if there is no configuration defined for one of the member Widgets
293 private List<Resource> processInstanceGroup(Model resourceModel, ArrayList<NodeTemplate> memberNodes,
294 Map<String, String> metaProperties, Map<String, Property> properties)
295 throws XmlArtifactGenerationException {
296 Resource groupModel = createInstanceGroupModel(mergeProperties(metaProperties, properties));
297 resourceModel.addResource(groupModel);
298 List<Resource> resources = Stream.of(groupModel).collect(Collectors.toList());
300 if (memberNodes != null && !memberNodes.isEmpty()) {
301 resources.addAll(generateResourcesAndWidgets(memberNodes, groupModel));
310 * @return a list of Resources
311 * @throws XmlArtifactGenerationException
312 * if a member node template is a widget and there is no configuration defined for that relation's
315 private List<Resource> generateResourcesAndWidgets(final ArrayList<NodeTemplate> memberNodes,
316 final Resource groupModel) throws XmlArtifactGenerationException {
317 log.debug(String.format("Processing member nodes for Group %s (invariant UUID %s)", //
318 groupModel.getModelName(), groupModel.getModelId()));
320 List<Resource> resources = new ArrayList<>();
322 for (NodeTemplate nodeTemplate : memberNodes) {
323 String nodeTypeName = nodeTemplate.getType();
324 final String metadataType = nodeTemplate.getMetaData().getValue("type");
326 log.debug(String.format("Get model for %s (metadata type %s)", nodeTypeName, metadataType));
327 Resource memberModel = Model.getModelFor(nodeTypeName, metadataType);
329 if (memberModel != null) {
330 memberModel.populateModelIdentificationInformation(nodeTemplate.getMetaData().getAllProperties());
332 log.debug(String.format("Generating grouped %s (%s) from TOSCA type %s",
333 memberModel.getClass().getSuperclass().getSimpleName(), memberModel.getClass(), nodeTypeName));
335 addRelatedModel(groupModel, memberModel);
336 if (memberModel.getModelType() == ModelType.RESOURCE) {
337 resources.add(memberModel);
344 private void processVfModule(List<Resource> resources, Model vfModel, Group groupDefinition,
345 NodeTemplate serviceNode, Resource groupModel) throws XmlArtifactGenerationException {
346 groupModel.populateModelIdentificationInformation(
347 mergeProperties(groupDefinition.getMetadata().getAllProperties(), groupDefinition.getProperties()));
349 processVfModuleGroup(groupModel, csarHelper.getMembersOfVfModule(serviceNode, groupDefinition));
351 vfModel.addResource(groupModel); // Add group (VfModule) to the (VF) model
352 // Check if we have already encountered the same VfModule across all the artifacts
353 if (!resources.contains(groupModel)) {
354 resources.add(groupModel);
358 private void processVfModuleGroup(Resource groupModel, List<NodeTemplate> members)
359 throws XmlArtifactGenerationException {
360 if (members != null && !members.isEmpty()) {
361 // Get names of the members of the service group
362 List<String> memberNames = members.stream().map(NodeTemplate::getName).collect(Collectors.toList());
363 groupModel.setMembers(memberNames);
364 for (NodeTemplate member : members) {
365 processGroupMembers(groupModel, member);
371 * Process the Widget members of a VF Module Group
375 * @throws XmlArtifactGenerationException
377 private void processGroupMembers(Resource group, NodeTemplate member) throws XmlArtifactGenerationException {
378 Resource resource = Model.getModelFor(member.getType());
380 log.debug(member.getType() + " mapped to " + resource);
382 if (resource.getWidgetType() == Type.L3_NET) {
383 // An l3-network inside a vf-module is treated as a Widget
384 resource.setModelType(ModelType.WIDGET);
387 if (resource.getModelType() == ModelType.WIDGET) {
388 Widget widget = Widget.getWidget(resource.getWidgetType());
389 widget.addKey(member.getName());
390 // Add the widget element encountered to the Group model
391 group.addWidget(widget);
396 * Create a Map of property name against String property value from the input Map
400 * @return Map of property name against String property value
402 private Map<String, String> populateStringProperties(Map<String, Property> inputMap) {
403 return inputMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
404 e -> e.getValue().getValue() == null ? "" : e.getValue().getValue().toString()));
408 * If the specified resourceNode is a type of Resource, add it to the specified resourceModel. If the Resource type
409 * is ProvidingService then return true, otherwise return false.
411 * @param resourceModel
414 * for populating the Resource IDs
415 * @param resourceNode
416 * any Model (will be ignored if not a Resource)
417 * @param nodeProperties
418 * the node properties
419 * @return whether or not a ProvidingService was processed
421 private boolean processModel(Model resourceModel, Metadata metaData, Resource resourceNode,
422 Map<String, Property> nodeProperties) {
423 boolean foundProvidingService = resourceNode != null
424 && (boolean) Optional.ofNullable(resourceNode.getProperties().get("providingService")).orElse(false);
426 if (foundProvidingService) {
427 processProvidingService(resourceModel, resourceNode, nodeProperties);
428 } else if (resourceNode != null && resourceNode.getModelType() == ModelType.RESOURCE
429 && resourceNode.getWidgetType() != Widget.Type.L3_NET) {
430 if (metaData != null) {
431 resourceNode.populateModelIdentificationInformation(metaData.getAllProperties());
433 resourceModel.addResource((Resource) resourceNode);
435 return foundProvidingService;
438 private void processProvidingService(Model resourceModel, Resource resourceNode,
439 Map<String, Property> nodeProperties) {
440 if (nodeProperties == null || nodeProperties.get("providing_service_uuid") == null
441 || nodeProperties.get("providing_service_invariant_uuid") == null) {
442 throw new IllegalArgumentException(
443 String.format(GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING, resourceModel.getModelId()));
445 Map<String, String> properties = populateStringProperties(nodeProperties);
446 properties.put(VERSION, "1.0");
447 resourceNode.populateModelIdentificationInformation(properties);
448 resourceModel.addResource(resourceNode);