Re-implement model type value for Resource Mapping
[aai/babel.git] / src / main / java / org / onap / aai / babel / parser / ArtifactGeneratorToscaParser.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.aai.babel.parser;
23
24 import com.google.gson.Gson;
25 import com.google.gson.JsonSyntaxException;
26 import java.io.BufferedReader;
27 import java.io.File;
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;
34 import java.util.Map;
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;
54
55 /**
56  * Wrapper for the sdc-tosca parser.
57  *
58  */
59 public class ArtifactGeneratorToscaParser {
60
61     private static Logger log = LogHelper.INSTANCE;
62
63     public static final String PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE = "artifactgenerator.config";
64     public static final String PROPERTY_TOSCA_MAPPING_FILE = "tosca.mappings.config";
65
66     public static final String GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND =
67             "Cannot generate artifacts. System property %s not configured";
68
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";
75
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";
81
82     private static final String VERSION = "version";
83
84     private ISdcCsarHelper csarHelper;
85
86     /**
87      * Constructs using csarHelper
88      *
89      * @param csarHelper
90      *            The csar helper
91      */
92     public ArtifactGeneratorToscaParser(ISdcCsarHelper csarHelper) {
93         this.csarHelper = csarHelper;
94     }
95
96     /**
97      * Initializes the Widget to UUID mapping configuration.
98      * 
99      * @throws IOException
100      *             if an error occurs reading the configuration properties
101      */
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);
107             if (file.exists()) {
108                 Properties properties = new Properties();
109                 properties.load(new FileInputStream(file));
110                 WidgetConfigurationUtil.setConfig(properties);
111             } else {
112                 throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGFILE_NOT_FOUND, configLocation));
113             }
114         } else {
115             throw new IllegalArgumentException(
116                     String.format(GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND, PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE));
117         }
118     }
119
120     /**
121      * Initializes the group filtering and TOSCA to Widget mapping configuration.
122      * 
123      * @param configLocation
124      *            the pathname to the JSON mappings file
125      * @throws IOException
126      *             if the file content could not be read successfully
127      */
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));
133         }
134
135         GroupConfiguration config;
136
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);
141         }
142
143         if (config == null) {
144             throw new IOException("There is no content for the Mappings Configuration " + configLocation);
145         }
146
147         WidgetConfigurationUtil.setSupportedInstanceGroups(config.getInstanceGroupTypes());
148         WidgetConfigurationUtil.setWidgetTypes(config.getWidgetTypes());
149         WidgetConfigurationUtil.setWidgetMappings(config.getWidgetMappings());
150     }
151
152     /**
153      * Process groups for this service node, according to the defined filter.
154      *
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
160      */
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()));
170                 }
171             }
172         }
173         return resources;
174     }
175
176     /**
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.
179      *
180      * @param stringProps
181      *            initial Map of String property values (e.g. from the TOSCA YAML metadata section)
182      * @param toscaProps
183      *            Map of TOSCA Property Type Object values to merge in (or overwrite)
184      * @return a Map of the property values converted to String
185      */
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()));
190         return props;
191     }
192
193     public Resource createInstanceGroupModel(Map<String, String> properties) {
194         Resource groupModel = new Resource(Type.INSTANCE_GROUP, true);
195         groupModel.populateModelIdentificationInformation(properties);
196         return groupModel;
197     }
198
199     /**
200      * Add the resource/widget to the specified model.
201      * 
202      * @param model
203      * @param relation
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
207      */
208     public void addRelatedModel(final Model model, final Resource relation) throws XmlArtifactGenerationException {
209         if (relation.getModelType() == ModelType.RESOURCE) {
210             model.addResource(relation);
211         } else {
212             model.addWidget(Widget.getWidget(relation.getWidgetType()));
213         }
214     }
215
216     public boolean hasAllottedResource(Map<String, String> metadata) {
217         return ALLOTTED_RESOURCE.equals(metadata.get(CATEGORY));
218     }
219
220     public boolean hasSubCategoryTunnelXConnect(Map<String, String> metadata) {
221         return TUNNEL_XCONNECT.equals(metadata.get(SUBCATEGORY));
222     }
223
224     /**
225      * Process TOSCA Group information for VF Modules.
226      *
227      * @param resources
228      * @param model
229      * @param serviceNode
230      * @throws XmlArtifactGenerationException
231      */
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);
237
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);
243             }
244         }
245     }
246
247     /**
248      * @param resourceModel
249      * @param resourceNodeTemplates
250      */
251     public void processResourceModels(Model resourceModel, List<NodeTemplate> resourceNodeTemplates) {
252         boolean foundProvidingService = false;
253
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);
259
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);
266             }
267
268             foundProvidingService |= processModel(resourceModel, metadata, model, resourceNodeTemplate.getProperties());
269         }
270
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));
275         }
276     }
277
278     /**
279      * Create an Instance Group Model and populate it with the supplied data.
280      *
281      * @param resourceModel
282      *            the Resource node template Model
283      * @param memberNodes
284      *            the Resources and Widgets belonging to the Group
285      * @param metaProperties
286      *            the metadata of the Group
287      * @param properties
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
292      */
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());
299
300         if (memberNodes != null && !memberNodes.isEmpty()) {
301             resources.addAll(generateResourcesAndWidgets(memberNodes, groupModel));
302         }
303
304         return resources;
305     }
306
307     /**
308      * @param memberNodes
309      * @param 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
313      *             widget type
314      */
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()));
319
320         List<Resource> resources = new ArrayList<>();
321
322         for (NodeTemplate nodeTemplate : memberNodes) {
323             String nodeTypeName = nodeTemplate.getType();
324             final String metadataType = nodeTemplate.getMetaData().getValue("type");
325
326             log.debug(String.format("Get model for %s (metadata type %s)", nodeTypeName, metadataType));
327             Resource memberModel = Model.getModelFor(nodeTypeName, metadataType);
328
329             if (memberModel != null) {
330                 memberModel.populateModelIdentificationInformation(nodeTemplate.getMetaData().getAllProperties());
331
332                 log.debug(String.format("Generating grouped %s (%s) from TOSCA type %s",
333                         memberModel.getClass().getSuperclass().getSimpleName(), memberModel.getClass(), nodeTypeName));
334
335                 addRelatedModel(groupModel, memberModel);
336                 if (memberModel.getModelType() == ModelType.RESOURCE) {
337                     resources.add(memberModel);
338                 }
339             }
340         }
341         return resources;
342     }
343
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()));
348
349         processVfModuleGroup(groupModel, csarHelper.getMembersOfVfModule(serviceNode, groupDefinition));
350
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);
355         }
356     }
357
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);
366             }
367         }
368     }
369
370     /**
371      * Process the Widget members of a VF Module Group
372      * 
373      * @param group
374      * @param member
375      * @throws XmlArtifactGenerationException
376      */
377     private void processGroupMembers(Resource group, NodeTemplate member) throws XmlArtifactGenerationException {
378         Resource resource = Model.getModelFor(member.getType());
379
380         log.debug(member.getType() + " mapped to " + resource);
381
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);
385         }
386
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);
392         }
393     }
394
395     /**
396      * Create a Map of property name against String property value from the input Map
397      *
398      * @param inputMap
399      *            The input Map
400      * @return Map of property name against String property value
401      */
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()));
405     }
406
407     /**
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.
410      *
411      * @param resourceModel
412      *            parent Resource
413      * @param metaData
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
420      */
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);
425
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());
432             }
433             resourceModel.addResource((Resource) resourceNode);
434         }
435         return foundProvidingService;
436     }
437
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()));
444         }
445         Map<String, String> properties = populateStringProperties(nodeProperties);
446         properties.put(VERSION, "1.0");
447         resourceNode.populateModelIdentificationInformation(properties);
448         resourceModel.addResource(resourceNode);
449     }
450 }