Improve handling 'empty'/null string in Service fields
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / tosca / ToscaExportRelationshipTemplatesHandler.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19 package org.openecomp.sdc.be.tosca;
20
21 import static org.apache.commons.collections.CollectionUtils.isEmpty;
22
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.concurrent.atomic.AtomicInteger;
28 import org.apache.commons.collections.MapUtils;
29 import org.openecomp.sdc.be.tosca.model.ToscaNodeTemplate;
30 import org.openecomp.sdc.be.tosca.model.ToscaRelationship;
31 import org.openecomp.sdc.be.tosca.model.ToscaRelationshipTemplate;
32 import org.openecomp.sdc.be.tosca.model.ToscaTemplateRequirement;
33
34 /**
35  * Handles the relationship_templates in the TOSCA export
36  */
37 public class ToscaExportRelationshipTemplatesHandler {
38
39     /**
40      * Creates the relationship_templates map based on the node_templates requirements.
41      *
42      * @param nodeTemplateMap the node template map
43      * @return the relationship_templates map
44      */
45     public Map<String, ToscaRelationshipTemplate> createFrom(final Map<String, ToscaNodeTemplate> nodeTemplateMap) {
46         if (MapUtils.isEmpty(nodeTemplateMap)) {
47             return Collections.emptyMap();
48         }
49         final Map<String, ToscaRelationshipTemplate> relationshipTemplates = new HashMap<>();
50         for (final Entry<String, ToscaNodeTemplate> nodeEntry : nodeTemplateMap.entrySet()) {
51             final ToscaNodeTemplate nodeTemplate = nodeEntry.getValue();
52             if (isEmpty(nodeTemplate.getRequirements())) {
53                 continue;
54             }
55             final AtomicInteger relationshipTemplateCount = new AtomicInteger(1);
56             for (final Map<String, ToscaTemplateRequirement> requirementMap : nodeTemplate.getRequirements()) {
57                 requirementMap.entrySet().stream().filter(entry -> entry.getValue().isRelationshipComplexNotation()).forEach(requirementEntry -> {
58                     final ToscaTemplateRequirement requirement = requirementEntry.getValue();
59                     final ToscaRelationship relationship = requirement.getRelationshipAsComplexType();
60                     final ToscaRelationshipTemplate relationshipTemplate = new ToscaRelationshipTemplate();
61                     relationshipTemplate.setType(relationship.getType());
62                     relationshipTemplate.setInterfaces(relationship.getInterfaces());
63                     final String relationshipName = String
64                         .format("%s.%s", ToscaRelationshipTemplate.createRelationshipName(nodeEntry.getKey(), requirementEntry.getKey()),
65                             relationshipTemplateCount);
66                     requirement.setRelationship(relationshipName);
67                     relationshipTemplate.setName(relationshipName);
68                     relationshipTemplates.put(relationshipName, relationshipTemplate);
69                     relationshipTemplateCount.incrementAndGet();
70                 });
71             }
72         }
73         return relationshipTemplates;
74     }
75 }