aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/babel/parser/ArtifactGeneratorToscaParser.java
blob: 684324a81f665aaa8c5aff0d997c54c275d189b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
 * Copyright (c) 2017-2019 European Software Marketing Ltd.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.aai.babel.parser;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.onap.aai.babel.logging.LogHelper;
import org.onap.aai.babel.xml.generator.XmlArtifactGenerationException;
import org.onap.aai.babel.xml.generator.data.GroupConfiguration;
import org.onap.aai.babel.xml.generator.data.WidgetConfigurationUtil;
import org.onap.aai.babel.xml.generator.model.Model;
import org.onap.aai.babel.xml.generator.model.Resource;
import org.onap.aai.babel.xml.generator.model.Widget;
import org.onap.aai.babel.xml.generator.model.WidgetType;
import org.onap.aai.babel.xml.generator.types.ModelType;
import org.onap.aai.cl.api.Logger;
import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
import org.onap.sdc.toscaparser.api.Group;
import org.onap.sdc.toscaparser.api.NodeTemplate;
import org.onap.sdc.toscaparser.api.Property;
import org.onap.sdc.toscaparser.api.elements.Metadata;

/**
 * Wrapper for the sdc-tosca parser.
 *
 */
public class ArtifactGeneratorToscaParser {

    private static Logger log = LogHelper.INSTANCE;

    public static final String PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE = "artifactgenerator.config";
    public static final String PROPERTY_TOSCA_MAPPING_FILE = "tosca.mappings.config";

    public static final String GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND =
            "Cannot generate artifacts. System property %s not configured";

    private static final String GENERATOR_AAI_CONFIGFILE_NOT_FOUND =
            "Cannot generate artifacts. Artifact Generator Configuration file not found at %s";
    private static final String GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING =
            "Cannot generate artifacts. Providing Service Metadata is missing for allotted resource %s";
    private static final String GENERATOR_AAI_PROVIDING_SERVICE_MISSING =
            "Cannot generate artifacts. Providing Service is missing for allotted resource %s";

    // Metadata properties
    private static final String CATEGORY = "category";
    private static final String ALLOTTED_RESOURCE = "Allotted Resource";
    private static final String SUBCATEGORY = "subcategory";
    private static final String TUNNEL_XCONNECT = "Tunnel XConnect";

    private static final String VERSION = "version";

    private ISdcCsarHelper csarHelper;

    /**
     * Constructs using csarHelper
     *
     * @param csarHelper
     *            The csar helper
     */
    public ArtifactGeneratorToscaParser(ISdcCsarHelper csarHelper) {
        this.csarHelper = csarHelper;
    }

    /**
     * Initializes the Widget to UUID mapping configuration.
     *
     * @throws IOException
     *             if an error occurs reading the configuration properties
     */
    public static void initWidgetConfiguration() throws IOException {
        log.debug("Getting Widget Configuration");
        String configLocation = System.getProperty(PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE);
        if (configLocation != null) {
            File file = new File(configLocation);
            if (file.exists()) {
                Properties properties = new Properties();
                properties.load(new FileInputStream(file));
                WidgetConfigurationUtil.setConfig(properties);
            } else {
                throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGFILE_NOT_FOUND, configLocation));
            }
        } else {
            throw new IllegalArgumentException(
                    String.format(GENERATOR_AAI_CONFIGLOCATION_NOT_FOUND, PROPERTY_ARTIFACT_GENERATOR_CONFIG_FILE));
        }
    }

    /**
     * Initializes the group filtering and TOSCA to Widget mapping configuration.
     *
     * @param configLocation
     *            the pathname to the JSON mappings file
     * @throws IOException
     *             if the file content could not be read successfully
     */
    public static void initToscaMappingsConfiguration(String configLocation) throws IOException {
        log.debug("Getting TOSCA Mappings Configuration");
        File file = new File(configLocation);
        if (!file.exists()) {
            throw new IllegalArgumentException(String.format(GENERATOR_AAI_CONFIGFILE_NOT_FOUND, configLocation));
        }

        GroupConfiguration config;

        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(configLocation))) {
            config = new Gson().fromJson(bufferedReader, GroupConfiguration.class);
        } catch (JsonSyntaxException e) {
            throw new IOException("Invalid Mappings Configuration " + configLocation, e);
        }

        if (config == null) {
            throw new IOException("There is no content for the Mappings Configuration " + configLocation);
        }

        WidgetConfigurationUtil.setSupportedInstanceGroups(config.getInstanceGroupTypes());
        WidgetConfigurationUtil.setWidgetTypes(config.getWidgetTypes());
        WidgetConfigurationUtil.setWidgetMappings(config.getWidgetMappings());
    }

    /**
     * Process groups for this service node, according to the defined filter.
     *
     * @param resourceModel
     * @param serviceNodeTemplate
     * @return resources for which XML Models should be generated
     * @throws XmlArtifactGenerationException
     *             if there is no configuration defined for a member Widget of an instance group
     */
    public List<Resource> processInstanceGroups(Model resourceModel, NodeTemplate serviceNodeTemplate)
            throws XmlArtifactGenerationException {
        List<Resource> resources = new ArrayList<>();
        if (serviceNodeTemplate.getSubMappingToscaTemplate() != null) {
            List<Group> serviceGroups = csarHelper.getGroupsOfOriginOfNodeTemplate(serviceNodeTemplate);
            for (Group group : serviceGroups) {
                if (WidgetConfigurationUtil.isSupportedInstanceGroup(group.getType())) {
                    resources.addAll(processInstanceGroup(resourceModel, group.getMemberNodes(),
                            group.getMetadata().getAllProperties(), group.getProperties()));
                }
            }
        }
        return resources;
    }

    /**
     * Merge a Map of String values with a Map of TOSCA Property Objects to create a combined Map. If there are
     * duplicate keys then the TOSCA Property value takes precedence.
     *
     * @param stringProps
     *            initial Map of String property values (e.g. from the TOSCA YAML metadata section)
     * @param toscaProps
     *            Map of TOSCA Property Type Object values to merge in (or overwrite)
     * @return a Map of the property values converted to String
     */
    public Map<String, String> mergeProperties(Map<String, String> stringProps, Map<String, Property> toscaProps) {
        Map<String, String> props = new HashMap<>(stringProps);
        toscaProps.forEach((key, toscaProp) -> props.put(key,
                toscaProp.getValue() == null ? "" : toscaProp.getValue().toString()));
        return props;
    }

    public Resource createInstanceGroupModel(Map<String, String> properties) {
        Resource groupModel = new Resource(WidgetType.valueOf("INSTANCE_GROUP"), true);
        groupModel.populateModelIdentificationInformation(properties);
        return groupModel;
    }

    /**
     * Add the resource/widget to the specified model.
     *
     * @param model
     * @param relation
     *            resource or widget model to add
     * @throws XmlArtifactGenerationException
     *             if the relation is a widget and there is no configuration defined for the relation's widget type
     */
    public void addRelatedModel(final Model model, final Resource relation) throws XmlArtifactGenerationException {
        if (relation.getModelType() == ModelType.RESOURCE) {
            model.addResource(relation);
        } else {
            model.addWidget(Widget.getWidget(relation.getWidgetType()));
        }
    }

    public boolean hasAllottedResource(Map<String, String> metadata) {
        return ALLOTTED_RESOURCE.equals(metadata.get(CATEGORY));
    }

    public boolean hasSubCategoryTunnelXConnect(Map<String, String> metadata) {
        return TUNNEL_XCONNECT.equals(metadata.get(SUBCATEGORY));
    }

    /**
     * Process TOSCA Group information for VF Modules.
     *
     * @param resources
     * @param model
     * @param serviceNode
     * @throws XmlArtifactGenerationException
     */
    public void processVfModules(List<Resource> resources, Model resourceModel, NodeTemplate serviceNode)
            throws XmlArtifactGenerationException {
        // Get the customisation UUID for each VF node and use it to get its Groups
        String uuid = csarHelper.getNodeTemplateCustomizationUuid(serviceNode);
        List<Group> serviceGroups = csarHelper.getVfModulesByVf(uuid);

        // Process each VF Group
        for (Group serviceGroup : serviceGroups) {
            Model groupModel = Model.getModelFor(serviceGroup.getType());
            if (groupModel.hasWidgetType("VFMODULE")) {
                processVfModule(resources, resourceModel, serviceGroup, serviceNode, (Resource) groupModel);
            }
        }
    }

    /**
     * @param resourceModel
     * @param resourceNodeTemplates
     */
    public void processResourceModels(Model resourceModel, List<NodeTemplate> resourceNodeTemplates) {
        boolean foundProvidingService = false;

        for (NodeTemplate resourceNodeTemplate : resourceNodeTemplates) {
            String nodeTypeName = resourceNodeTemplate.getType();
            Metadata metadata = resourceNodeTemplate.getMetaData();
            String metaDataType = Optional.ofNullable(metadata).map(m -> m.getValue("type")).orElse(nodeTypeName);
            Resource model = Model.getModelFor(nodeTypeName, metaDataType);

            if (metadata != null && hasAllottedResource(metadata.getAllProperties())
                    && model.hasWidgetType("VSERVER")) {
                model = new Resource(WidgetType.valueOf("ALLOTTED_RESOURCE"), false);
                Map<String, Object> props = new HashMap<>();
                props.put("providingService", true);
                model.setProperties(props);
            }

            foundProvidingService |= processModel(resourceModel, metadata, model, resourceNodeTemplate.getProperties());
        }

        if (resourceModel.hasWidgetType("ALLOTTED_RESOURCE") && !foundProvidingService) {
            final String modelInvariantId = resourceModel.getModelId();
            throw new IllegalArgumentException(String.format(GENERATOR_AAI_PROVIDING_SERVICE_MISSING,
                    modelInvariantId == null ? "<null ID>" : modelInvariantId));
        }
    }

    /**
     * Create an Instance Group Model and populate it with the supplied data.
     *
     * @param resourceModel
     *            the Resource node template Model
     * @param memberNodes
     *            the Resources and Widgets belonging to the Group
     * @param metaProperties
     *            the metadata of the Group
     * @param properties
     *            the properties of the Group
     * @return the Instance Group and Member resource models
     * @throws XmlArtifactGenerationException
     *             if there is no configuration defined for one of the member Widgets
     */
    private List<Resource> processInstanceGroup(Model resourceModel, ArrayList<NodeTemplate> memberNodes,
            Map<String, String> metaProperties, Map<String, Property> properties)
            throws XmlArtifactGenerationException {
        Resource groupModel = createInstanceGroupModel(mergeProperties(metaProperties, properties));
        resourceModel.addResource(groupModel);
        List<Resource> resources = Stream.of(groupModel).collect(Collectors.toList());

        if (memberNodes != null && !memberNodes.isEmpty()) {
            resources.addAll(generateResourcesAndWidgets(memberNodes, groupModel));
        }

        return resources;
    }

    /**
     * @param memberNodes
     * @param groupModel
     * @return a list of Resources
     * @throws XmlArtifactGenerationException
     *             if a member node template is a widget and there is no configuration defined for that relation's
     *             widget type
     */
    private List<Resource> generateResourcesAndWidgets(final ArrayList<NodeTemplate> memberNodes,
            final Resource groupModel) throws XmlArtifactGenerationException {
        log.debug(String.format("Processing member nodes for Group %s (invariant UUID %s)", //
                groupModel.getModelName(), groupModel.getModelId()));

        List<Resource> resources = new ArrayList<>();

        for (NodeTemplate nodeTemplate : memberNodes) {
            String nodeTypeName = nodeTemplate.getType();
            final String metadataType = nodeTemplate.getMetaData().getValue("type");

            log.debug(String.format("Get model for %s (metadata type %s)", nodeTypeName, metadataType));
            Resource memberModel = Model.getModelFor(nodeTypeName, metadataType);

            if (memberModel != null) {
                memberModel.populateModelIdentificationInformation(nodeTemplate.getMetaData().getAllProperties());

                log.debug(String.format("Generating grouped %s (%s) from TOSCA type %s",
                        memberModel.getClass().getSuperclass().getSimpleName(), memberModel.getClass(), nodeTypeName));

                addRelatedModel(groupModel, memberModel);
                if (memberModel.getModelType() == ModelType.RESOURCE) {
                    resources.add(memberModel);
                }
            }
        }
        return resources;
    }

    private void processVfModule(List<Resource> resources, Model vfModel, Group groupDefinition,
            NodeTemplate serviceNode, Resource groupModel) throws XmlArtifactGenerationException {
        groupModel.populateModelIdentificationInformation(
                mergeProperties(groupDefinition.getMetadata().getAllProperties(), groupDefinition.getProperties()));

        processVfModuleGroup(groupModel, csarHelper.getMembersOfVfModule(serviceNode, groupDefinition));

        vfModel.addResource(groupModel); // Add group (VfModule) to the (VF) model
        // Check if we have already encountered the same VfModule across all the artifacts
        if (!resources.contains(groupModel)) {
            resources.add(groupModel);
        }
    }

    private void processVfModuleGroup(Resource groupModel, List<NodeTemplate> members)
            throws XmlArtifactGenerationException {
        if (members != null && !members.isEmpty()) {
            // Get names of the members of the service group
            List<String> memberNames = members.stream().map(NodeTemplate::getName).collect(Collectors.toList());
            groupModel.setMembers(memberNames);
            for (NodeTemplate member : members) {
                processGroupMembers(groupModel, member);
            }
        }
    }

    /**
     * Process the Widget members of a VF Module Group
     *
     * @param group
     * @param member
     * @throws XmlArtifactGenerationException
     */
    private void processGroupMembers(Resource group, NodeTemplate member) throws XmlArtifactGenerationException {
        Resource resource = Model.getModelFor(member.getType());

        log.debug(member.getType() + " mapped to " + resource);

        if (resource.hasWidgetType("L3_NET")) {
            // An l3-network inside a vf-module is treated as a Widget
            resource.setModelType(ModelType.WIDGET);
        }

        if (resource.getModelType() == ModelType.WIDGET) {
            Widget widget = Widget.getWidget(resource.getWidgetType());
            widget.addKey(member.getName());
            // Add the widget element encountered to the Group model
            group.addWidget(widget);
        }
    }

    /**
     * Create a Map of property name against String property value from the input Map
     *
     * @param inputMap
     *            The input Map
     * @return Map of property name against String property value
     */
    private Map<String, String> populateStringProperties(Map<String, Property> inputMap) {
        return inputMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
                e -> e.getValue().getValue() == null ? "" : e.getValue().getValue().toString()));
    }

    /**
     * If the specified resourceNode is a type of Resource, add it to the specified resourceModel. If the Resource type
     * is ProvidingService then return true, otherwise return false.
     *
     * @param resourceModel
     *            parent Resource
     * @param metaData
     *            for populating the Resource IDs
     * @param resourceNode
     *            any Model (will be ignored if not a Resource)
     * @param nodeProperties
     *            the node properties
     * @return whether or not a ProvidingService was processed
     */
    private boolean processModel(Model resourceModel, Metadata metaData, Resource resourceNode,
            Map<String, Property> nodeProperties) {
        boolean foundProvidingService = resourceNode != null
                && (boolean) Optional.ofNullable(resourceNode.getProperties().get("providingService")).orElse(false);

        if (foundProvidingService) {
            processProvidingService(resourceModel, resourceNode, nodeProperties);
        } else if (resourceNode != null && resourceNode.getModelType() == ModelType.RESOURCE
                && !resourceNode.hasWidgetType("L3_NET")) {
            if (metaData != null) {
                resourceNode.populateModelIdentificationInformation(metaData.getAllProperties());
            }
            resourceModel.addResource(resourceNode);
        }
        return foundProvidingService;
    }

    private void processProvidingService(Model resourceModel, Resource resourceNode,
            Map<String, Property> nodeProperties) {
        if (nodeProperties == null || nodeProperties.get("providing_service_uuid") == null
                || nodeProperties.get("providing_service_invariant_uuid") == null) {
            throw new IllegalArgumentException(
                    String.format(GENERATOR_AAI_PROVIDING_SERVICE_METADATA_MISSING, resourceModel.getModelId()));
        }
        Map<String, String> properties = populateStringProperties(nodeProperties);
        properties.put(VERSION, "1.0");
        resourceNode.populateModelIdentificationInformation(properties);
        resourceModel.addResource(resourceNode);
    }
}