summaryrefslogtreecommitdiffstats
path: root/openecomp-be/lib/openecomp-sdc-tosca-generator-lib/openecomp-sdc-tosca-generator-core/src/main/java/org/openecomp/sdc/generator/core/utils/GeneratorUtils.java
blob: 9ea8ba752a917b42fe69ff717a4a3e7e146956ab (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
package org.openecomp.sdc.generator.core.utils;

import org.openecomp.sdc.tosca.datatypes.ToscaElementTypes;
import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
import org.onap.sdc.tosca.datatypes.model.CapabilityDefinition;
import org.onap.sdc.tosca.datatypes.model.NodeTemplate;
import org.onap.sdc.tosca.datatypes.model.NodeType;
import org.onap.sdc.tosca.datatypes.model.RequirementAssignment;
import org.onap.sdc.tosca.datatypes.model.RequirementDefinition;
import org.onap.sdc.tosca.datatypes.model.ServiceTemplate;
import org.openecomp.sdc.tosca.services.DataModelUtil;
import org.openecomp.sdc.tosca.services.ToscaAnalyzerService;
import org.onap.sdc.tosca.services.ToscaExtensionYamlUtil;
import org.openecomp.sdc.tosca.services.ToscaUtil;
import org.openecomp.sdc.tosca.services.impl.ToscaAnalyzerServiceImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static org.openecomp.sdc.tosca.services.DataModelUtil.addSubstitutionNodeTypeRequirements;

/**
 * The type Generator utils.
 */
public class GeneratorUtils {

  private GeneratorUtils() {
    // prevent instantiation
  }

  //TODO : Read from configuration
  private static final List<String> SUPPORTED_CAPABILITIES = Arrays.asList("host", "os", "endpoint", "scalable");
  private static final List<String> SUPPORTED_REQUIREMENTS = Collections.singletonList("link");

  /**
   * Add service template to tosca service model.
   *
   * @param toscaServiceModel the tosca service model
   * @param serviceTemplate   the service template
   */
  public static void addServiceTemplateToToscaServiceModel(ToscaServiceModel toscaServiceModel,
                                                           ServiceTemplate serviceTemplate) {

    String serviceTemplateFileName = ToscaUtil.getServiceTemplateFileName(serviceTemplate);
    Map<String, ServiceTemplate> serviceTemplates = toscaServiceModel.getServiceTemplates();
    if (!serviceTemplates.containsKey(serviceTemplateFileName)) {
      ToscaUtil.addServiceTemplateToMapWithKeyFileName(serviceTemplates, serviceTemplate);
    }
    toscaServiceModel.setServiceTemplates(serviceTemplates);
  }

  /**
   * Gets substitution node type exposed connection points.
   *
   * @param substitutionNodeType        the substitution node type
   * @param substitutionServiceTemplate the substitution service template
   * @param toscaServiceModel           the tosca service model
   * @return the substitution node type exposed connection points
   */
  public static Map<String, Map<String, List<String>>>
      getSubstitutionNodeTypeExposedConnectionPoints(NodeType substitutionNodeType,
                                                 ServiceTemplate substitutionServiceTemplate,
                                                 ToscaServiceModel toscaServiceModel) {

    Map<String, NodeTemplate> nodeTemplates =
        substitutionServiceTemplate.getTopology_template().getNode_templates();
    String nodeTemplateId;
    NodeTemplate nodeTemplate;
    String nodeType;
    Map<String, Map<String, List<String>>> substitutionMapping = new HashMap<>();
    if (nodeTemplates == null) {
      return substitutionMapping;
    }

    try {
      Map<String, List<String>> capabilitySubstitutionMapping = new HashMap<>();
      Map<String, List<String>> requirementSubstitutionMapping = new HashMap<>();
      substitutionMapping.put("capability", capabilitySubstitutionMapping);
      substitutionMapping.put("requirement", requirementSubstitutionMapping);
      List<Map<String, RequirementDefinition>> nodeTypeRequirementsDefinition;
      Map<String, RequirementAssignment> nodeTemplateRequirementsAssignment;
      List<Map<String, RequirementDefinition>> exposedRequirementsDefinition;
      Map<String, Map<String, RequirementAssignment>> fullFilledRequirementsDefinition =
          new HashMap<>();
      Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition = new HashMap<>();
      Map<String, CapabilityDefinition> exposedCapabilitiesDefinition;

      ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
      for (Map.Entry<String, NodeTemplate> entry : nodeTemplates.entrySet()) {
        nodeTemplateId = entry.getKey();
        nodeTemplate = entry.getValue();
        nodeType = nodeTemplate.getType();
        NodeType flatNodeType = (NodeType) toscaAnalyzerService
            .getFlatEntity(ToscaElementTypes.NODE_TYPE, nodeType, substitutionServiceTemplate,
                toscaServiceModel);
        // get requirements
        nodeTypeRequirementsDefinition =
            getNodeTypeRequirements(flatNodeType, nodeTemplateId, substitutionServiceTemplate,
                requirementSubstitutionMapping);
        nodeTemplateRequirementsAssignment =
            DataModelUtil.getNodeTemplateRequirements(nodeTemplate);
        fullFilledRequirementsDefinition.put(nodeTemplateId, nodeTemplateRequirementsAssignment);
        //set substitution node type requirements
        exposedRequirementsDefinition =
            toscaAnalyzerService.calculateExposedRequirements(nodeTypeRequirementsDefinition,
                nodeTemplateRequirementsAssignment);


        //Filter unsupported requirements
        Iterator<Map<String, RequirementDefinition>> iterator =
            exposedRequirementsDefinition.iterator();
        while (iterator.hasNext()) {
          Map<String, RequirementDefinition> requirementDefinitionMap = iterator.next();
          for (Map.Entry<String, RequirementDefinition> requirementDefinitionEntry :
              requirementDefinitionMap.entrySet()) {
            String requirementKey = requirementDefinitionEntry.getKey();
            if (!SUPPORTED_REQUIREMENTS.contains(requirementKey)) {
              iterator.remove();
            }
          }
        }
        addSubstitutionNodeTypeRequirements(substitutionNodeType, exposedRequirementsDefinition,
            nodeTemplateId);
        //get capabilities
        addNodeTypeCapabilitiesToSubMapping(nodeTypeCapabilitiesDefinition,
            capabilitySubstitutionMapping, nodeType,
            nodeTemplateId, substitutionServiceTemplate, toscaServiceModel);
      }

      exposedCapabilitiesDefinition =
          toscaAnalyzerService.calculateExposedCapabilities(nodeTypeCapabilitiesDefinition,
              fullFilledRequirementsDefinition);

      //Filter unsupported capabilities
      Iterator<Map.Entry<String, CapabilityDefinition>> iterator = exposedCapabilitiesDefinition
          .entrySet().iterator();
      while (iterator.hasNext()) {
        Map.Entry<String, CapabilityDefinition> capabilityDefinitionEntry = iterator.next();
        //Expected Capability is of the format <capabilityId>_<componentName>
        String capabilityKey = capabilityDefinitionEntry.getKey().split("_")[0];
        if (!SUPPORTED_CAPABILITIES.contains(capabilityKey)) {
          iterator.remove();
        }
      }

      DataModelUtil.addNodeTypeCapabilitiesDef(substitutionNodeType, exposedCapabilitiesDefinition);
    } catch (Exception ex) {
      return null;
    }
    return substitutionMapping;
  }

  /**
   * Gets node type requirements.
   *
   * @param flatNodeType                   the flat node type
   * @param templateName                   the template name
   * @param serviceTemplate                the service template
   * @param requirementSubstitutionMapping the requirement substitution mapping
   * @return the node type requirements
   */
  public static List<Map<String, RequirementDefinition>> getNodeTypeRequirements(
      NodeType flatNodeType,
      String templateName,
      ServiceTemplate serviceTemplate,
      Map<String, List<String>> requirementSubstitutionMapping) {
    List<Map<String, RequirementDefinition>> requirementList = new ArrayList<>();
    List<String> requirementMapping;
    if (flatNodeType.getRequirements() != null) {
      for (Map<String, RequirementDefinition> requirementMap : flatNodeType.getRequirements()) {
        for (Map.Entry<String, RequirementDefinition> requirementNodeEntry : requirementMap
            .entrySet()) {
          ToscaExtensionYamlUtil toscaExtensionYamlUtil = new ToscaExtensionYamlUtil();
          RequirementDefinition requirementNodeEntryValue = toscaExtensionYamlUtil
              .yamlToObject(toscaExtensionYamlUtil.objectToYaml(requirementNodeEntry.getValue()),
                  RequirementDefinition.class);
          if (requirementNodeEntryValue.getOccurrences() == null) {
            requirementNodeEntryValue.setOccurrences(new Object[]{1, 1});
          }
          Map<String, RequirementDefinition> requirementDef = new HashMap<>();
          requirementDef.put(requirementNodeEntry.getKey(), requirementNodeEntryValue);
          DataModelUtil.addRequirementToList(requirementList, requirementDef);
          requirementMapping = new ArrayList<>();
          requirementMapping.add(templateName);
          requirementMapping.add(requirementNodeEntry.getKey());
          requirementSubstitutionMapping
              .put(requirementNodeEntry.getKey() + "_" + templateName, requirementMapping);
          if (requirementNodeEntryValue.getNode() == null) {
            requirementNodeEntryValue.setOccurrences(new Object[]{1, 1});
          }
        }
      }
    }
    return requirementList;
  }

  private static void addNodeTypeCapabilitiesToSubMapping(
      Map<String, CapabilityDefinition> nodeTypeCapabilitiesDefinition,
      Map<String, List<String>> capabilitySubstitutionMapping, String type, String templateName,
      ServiceTemplate substitutionServiceTemplate, ToscaServiceModel toscaServiceModel) {
    ToscaAnalyzerService toscaAnalyzerService = new ToscaAnalyzerServiceImpl();
    NodeType flatNodeType = (NodeType) toscaAnalyzerService
        .getFlatEntity(ToscaElementTypes.NODE_TYPE, type, substitutionServiceTemplate,
            toscaServiceModel);
    String capabilityKey;
    List<String> capabilityMapping;
    if (flatNodeType.getCapabilities() != null) {
      for (Map.Entry<String, CapabilityDefinition> capabilityNodeEntry : flatNodeType
          .getCapabilities()
          .entrySet()) {
        capabilityKey = capabilityNodeEntry.getKey() + "_" + templateName;
        nodeTypeCapabilitiesDefinition.put(capabilityKey, capabilityNodeEntry.getValue().clone());
        capabilityMapping = new ArrayList<>();
        capabilityMapping.add(templateName);
        capabilityMapping.add(capabilityNodeEntry.getKey());
        capabilitySubstitutionMapping.put(capabilityKey, capabilityMapping);
      }
    }
  }

}