|
16 | 16 |
|
17 | 17 | import java.io.UnsupportedEncodingException;
|
18 | 18 | import java.net.URLDecoder;
|
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.HashMap; |
19 | 21 | import java.util.List;
|
20 | 22 | import java.util.Map;
|
21 | 23 | import java.util.Objects;
|
|
39 | 41 | import org.eclipse.winery.model.tosca.TRelationshipType;
|
40 | 42 | import org.eclipse.winery.model.tosca.TRelationshipTypeImplementation;
|
41 | 43 | import org.eclipse.winery.model.tosca.TServiceTemplate;
|
| 44 | +import org.eclipse.winery.model.tosca.extensions.OTParticipant; |
42 | 45 | import org.eclipse.winery.model.tosca.utils.ModelUtilities;
|
43 | 46 |
|
44 | 47 | import io.github.edmm.core.parser.Entity;
|
|
49 | 52 | import io.github.edmm.core.parser.SequenceEntity;
|
50 | 53 | import io.github.edmm.core.parser.support.DefaultKeys;
|
51 | 54 |
|
| 55 | +import static org.eclipse.winery.model.tosca.constants.Namespaces.TOSCA_WINERY_EXTENSIONS_NAMESPACE; |
| 56 | + |
52 | 57 | public class EdmmConverter {
|
53 | 58 |
|
54 | 59 | private final Map<QName, TNodeType> nodeTypes;
|
@@ -85,20 +90,82 @@ public EdmmConverter(Map<QName, TNodeType> nodeTypes, Map<QName, TRelationshipTy
|
85 | 90 | }
|
86 | 91 |
|
87 | 92 | public EntityGraph transform(TServiceTemplate serviceTemplate) {
|
| 93 | + |
88 | 94 | EntityGraph entityGraph = new EntityGraph();
|
89 | 95 |
|
| 96 | + setMetadata(entityGraph); |
| 97 | + |
90 | 98 | List<TNodeTemplate> nodeTemplates = serviceTemplate.getTopologyTemplate().getNodeTemplates();
|
91 | 99 | List<TRelationshipTemplate> relationshipTemplates = serviceTemplate.getTopologyTemplate().getRelationshipTemplates();
|
92 | 100 | if (!nodeTemplates.isEmpty()) {
|
93 | 101 | entityGraph.addEntity(new MappingEntity(EntityGraph.COMPONENTS, entityGraph));
|
94 | 102 | }
|
95 |
| - |
96 | 103 | nodeTemplates.forEach(nodeTemplate -> createNode(nodeTemplate, entityGraph));
|
97 | 104 | relationshipTemplates.forEach(relationship -> createRelation(relationship, entityGraph));
|
98 | 105 |
|
| 106 | + List<OTParticipant> participants = serviceTemplate.getTopologyTemplate().getParticipants(); |
| 107 | + if (participants != null && !participants.isEmpty()) { |
| 108 | + entityGraph.addEntity(new MappingEntity(EntityGraph.PARTICIPANTS, entityGraph)); |
| 109 | + participants.forEach(participant -> createParticipant(participant, nodeTemplates, entityGraph)); |
| 110 | + } |
| 111 | + |
| 112 | + createTechnologyMapping(nodeTemplates, entityGraph); |
| 113 | + |
99 | 114 | return entityGraph;
|
100 | 115 | }
|
101 | 116 |
|
| 117 | + private void setMetadata(EntityGraph entityGraph) { |
| 118 | + entityGraph.addEntity(new ScalarEntity("edm_1_0", EntityGraph.VERSION, entityGraph)); |
| 119 | + entityGraph.addEntity(new ScalarEntity("12345", EntityGraph.MULTI_ID, entityGraph)); |
| 120 | + } |
| 121 | + |
| 122 | + private void createTechnologyMapping(List<TNodeTemplate> nodeTemplates, EntityGraph entityGraph) { |
| 123 | + |
| 124 | + Map<String, List<TNodeTemplate>> deploymentTechnologyMapping = new HashMap<>(); |
| 125 | + for (TNodeTemplate nodeTemplate : nodeTemplates) { |
| 126 | + Map<QName, String> attributes = nodeTemplate.getOtherAttributes(); |
| 127 | + String key = attributes.get(new QName(TOSCA_WINERY_EXTENSIONS_NAMESPACE, "deployment-technology")); |
| 128 | + if (key != null) { |
| 129 | + deploymentTechnologyMapping.computeIfAbsent(key, k -> new ArrayList<>()); |
| 130 | + deploymentTechnologyMapping.get(key).add(nodeTemplate); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if (!deploymentTechnologyMapping.isEmpty()) { |
| 135 | + entityGraph.addEntity(new MappingEntity(EntityGraph.ORCHESTRATION_TECHNOLOGY, entityGraph)); |
| 136 | + |
| 137 | + deploymentTechnologyMapping.forEach((key, nodes) -> { |
| 138 | + EntityId entity = EntityGraph.ORCHESTRATION_TECHNOLOGY.extend(key); |
| 139 | + entityGraph.addEntity(new SequenceEntity(entity, entityGraph)); |
| 140 | + for (TNodeTemplate nodeTemplate : nodes) { |
| 141 | + EntityId valueEntity = entity.extend(nodeTemplate.getId()); |
| 142 | + entityGraph.addEntity(new ScalarEntity(nodeTemplate.getId(), valueEntity, entityGraph)); |
| 143 | + } |
| 144 | + }); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private void createParticipant(OTParticipant participant, List<TNodeTemplate> nodeTemplates, EntityGraph entityGraph) { |
| 149 | + |
| 150 | + EntityId participantEntity = EntityGraph.PARTICIPANTS.extend(participant.getName()); |
| 151 | + entityGraph.addEntity(new MappingEntity(participantEntity, entityGraph)); |
| 152 | + |
| 153 | + EntityId endpointEntityId = participantEntity.extend(DefaultKeys.ENDPOINT); |
| 154 | + entityGraph.addEntity(new ScalarEntity(participant.getUrl(), endpointEntityId, entityGraph)); |
| 155 | + |
| 156 | + EntityId componentsEntityId = participantEntity.extend(DefaultKeys.COMPONENTS); |
| 157 | + entityGraph.addEntity(new SequenceEntity(componentsEntityId, entityGraph)); |
| 158 | + |
| 159 | + for (TNodeTemplate nodeTemplate : nodeTemplates) { |
| 160 | + Map<QName, String> attributes = nodeTemplate.getOtherAttributes(); |
| 161 | + String name = attributes.get(new QName(TOSCA_WINERY_EXTENSIONS_NAMESPACE, "participant")); |
| 162 | + if (participant.getName().equals(name)) { |
| 163 | + EntityId valueEntity = componentsEntityId.extend(nodeTemplate.getId()); |
| 164 | + entityGraph.addEntity(new ScalarEntity(nodeTemplate.getId(), valueEntity, entityGraph)); |
| 165 | + } |
| 166 | + } |
| 167 | + } |
| 168 | + |
102 | 169 | private void createRelation(TRelationshipTemplate relationship, EntityGraph entityGraph) {
|
103 | 170 | EntityId sourceComponentEntityId = EntityGraph.COMPONENTS.extend(relationship.getSourceElement().getRef().getId());
|
104 | 171 | // the entity will always be in the graph since we first transform the NodeTemplates
|
|
0 commit comments