71f39617acfd7b9c0c129b42ea6295c8923ca342
[vfc/nfvo/wfengine.git] /
1 /*******************************************************************************
2  * Copyright (c) 2013 Pascal Hirmer.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *    Pascal Hirmer - initial API and implementation
11  *******************************************************************************/
12
13 package org.eclipse.winery.topologymodeler.addons.topologycompleter.analyzer;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.winery.model.tosca.TCapability;
21 import org.eclipse.winery.model.tosca.TNodeTemplate;
22 import org.eclipse.winery.model.tosca.TNodeType;
23 import org.eclipse.winery.model.tosca.TRelationshipTemplate;
24 import org.eclipse.winery.model.tosca.TRequirement;
25 import org.eclipse.winery.model.tosca.TRequirementDefinition;
26 import org.eclipse.winery.model.tosca.TRequirementType;
27 import org.eclipse.winery.model.tosca.TTopologyTemplate;
28 import org.eclipse.winery.topologymodeler.addons.topologycompleter.helper.Utils;
29
30 /**
31  * This class analyzes the occurrence of TOSCA {@link TRequirement}s in a {@link TTopologyTemplate} and checks whether they
32  * are already fulfilled or not.
33  */
34 public class RequirementAnalyzer {
35
36         /**
37          * This method checks if {@link TNodeTemplate}s contain {@link TRequirement}s and adds them to a {@link Map}.
38          *
39          * @param toscaAnalyzer
40          *            the {@link TOSCAAnalyzer} object to access the data model
41          *
42          * @return a map containing {@link TNodeTemplate}s and their {@link TRequirement}s
43          */
44         public static Map<TRequirement, TNodeTemplate> analyzeRequirements(TOSCAAnalyzer toscaAnalyzer) {
45
46                 // map containing entries for a Requirement and its corresponding NodeTemplate
47                 Map<TRequirement, TNodeTemplate> unfulfilledRequirements = new HashMap<TRequirement, TNodeTemplate>();
48
49                 for (TNodeTemplate nodeTemplate : toscaAnalyzer.getNodeTemplates()) {
50
51                         List<TRequirement> requirements = new ArrayList<>();
52
53                         TNodeType nodeType = Utils.getNodeTypeForId(toscaAnalyzer.getNodeTypes(), nodeTemplate.getType());
54
55                         if (nodeType.getRequirementDefinitions() != null && !nodeType.getRequirementDefinitions().getRequirementDefinition().isEmpty()) {
56
57                                 List<TRequirementDefinition> requirementDefinitions = nodeType.getRequirementDefinitions().getRequirementDefinition();
58
59                                 // check the requirements of the type of the used NodeTemplate
60                                 for (TRequirementDefinition requirementDefinition: requirementDefinitions) {
61                                         TRequirement requirement = new TRequirement();
62                                         requirement.setType(requirementDefinition.getRequirementType());
63                                         requirement.setName(requirementDefinition.getName());
64                                         requirement.setId(Utils.createRandomID());
65                                 }
66                         }
67
68                         if (nodeTemplate.getRequirements() != null && !nodeTemplate.getRequirements().getRequirement().isEmpty()) {
69                                 requirements.addAll(nodeTemplate.getRequirements().getRequirement());
70                         }
71
72                         if (!requirements.isEmpty()) {
73                                 // list containing the RelationshipTemplates connecting to the NodeTemplate
74                                 List<TRelationshipTemplate> connectors = new ArrayList<TRelationshipTemplate>();
75
76                                 // add the connected RelationshipTemplates
77                                 for (TRelationshipTemplate connector : toscaAnalyzer.getRelationshipTemplates()) {
78                                         if (connector.getSourceElement().getRef().equals(nodeTemplate)) {
79                                                 connectors.add(connector);
80                                         }
81                                 }
82
83                                 // add requirements of unconnected NodeTemplates to the map because they can't be fulfilled
84                                 if (connectors.size() == 0) {
85                                         for (TRequirement requirement : requirements) {
86                                                 unfulfilledRequirements.put(requirement, nodeTemplate);
87                                         }
88                                 } else {
89                                         boolean fulfilled = false;
90
91                                         // check if one of the connected NodeTemplates already fulfill the requirement
92                                         for (TRequirement requirement : requirements) {
93                                                 for (TRelationshipTemplate connector : connectors) {
94                                                         TNodeTemplate connectedNodeTemplate = (TNodeTemplate) connector.getTargetElement().getRef();
95                                                         if (connectedNodeTemplate.getCapabilities() != null) {
96                                                                 for (TCapability capa : connectedNodeTemplate.getCapabilities().getCapability()) {
97                                                                         for (TRequirementType reqType : toscaAnalyzer.getRequirementTypes()) {
98                                                                                 if (requirement.getType().getLocalPart().equals(reqType.getName())) {
99                                                                                         if (reqType.getRequiredCapabilityType().getLocalPart().equals(capa.getType().getLocalPart())
100                                                                                                         && reqType.getRequiredCapabilityType().getNamespaceURI().equals(capa.getType().getNamespaceURI())) {
101                                                                                                 fulfilled = true;
102                                                                                         }
103                                                                                 }
104                                                                         }
105                                                                 }
106                                                         }
107                                                 }
108                                                 if (!fulfilled) {
109                                                         unfulfilledRequirements.put(requirement, nodeTemplate);
110                                                 }
111                                         }
112                                 }
113                         }
114                 }
115                 return unfulfilledRequirements;
116         }
117 }