f1d4b6b19d6156479cb5cbb82b0195fc27cd4f43
[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.helper;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import org.eclipse.winery.model.tosca.TCapability;
19 import org.eclipse.winery.model.tosca.TNodeTemplate;
20 import org.eclipse.winery.model.tosca.TRelationshipType;
21 import org.eclipse.winery.model.tosca.TRequirement;
22 import org.eclipse.winery.topologymodeler.addons.topologycompleter.analyzer.TOSCAAnalyzer;
23
24 /**
25  * This class searches a {@link TRelationshipType} which is able to connect two given {@link TNodeTemplate}s.
26  *
27  */
28 public class NodeTemplateConnector {
29
30         /**
31          * Searches a compatible {@link TRelationshipType} to connect two {@link TNodeTemplate}s.
32          *
33          * @param source
34          *            the source {@link TNodeTemplate}
35          * @param target
36          *            the target {@link TNodeTemplate}
37          * @param toscaAnalyzer
38          *            the {@link TOSCAAnalyzer} object to access the data model
39          * @param requirement
40          *            the {@link TRequirement} of the source {@link TNodeTemplate}
41          *
42          * @return a list of suitable {@link TRelationshipType}s
43          */
44         public static List<TRelationshipType> findRelationshipType(TNodeTemplate source, TNodeTemplate target, TOSCAAnalyzer toscaAnalyzer, TRequirement requirement) {
45
46                 List<TRelationshipType> suitableRelationshipTypes = new ArrayList<TRelationshipType>();
47                 List<TRelationshipType> allRelationshipTypes = toscaAnalyzer.getRelationshipTypes();
48
49                 // in case the connection to a placeholder is searched, no requirement exists
50                 if (requirement != null) {
51
52                         List<TCapability> capabilities = target.getCapabilities().getCapability();
53
54                         // check if a RelationshipType can connect a requirement of the source NodeTemplate to a capability of the target NodeTemplate
55                         for (TRelationshipType relationshipType : allRelationshipTypes) {
56                                 if (relationshipType.getValidSource() != null && relationshipType.getValidTarget() != null) {
57                                         for (TCapability capability : capabilities) {
58                                                 if ((relationshipType.getValidSource().getTypeRef().equals(requirement.getType()) && relationshipType.getValidTarget().getTypeRef().equals(capability.getType()))) {
59                                                         suitableRelationshipTypes.add(relationshipType);
60                                                 }
61                                         }
62                                 }
63                         }
64                 }
65
66                 // to extend the selection check if a RelationshipType can connect the type of the source NodeTemplate to the type of the target NodeTemplate
67                 for (TRelationshipType rt : allRelationshipTypes) {
68                         if (rt.getValidSource() != null && rt.getValidTarget() != null) {
69                                 if ((rt.getValidSource().getTypeRef().equals(source.getType()) && rt.getValidTarget().getTypeRef().equals(target.getType()))) {
70                                         suitableRelationshipTypes.add(rt);
71                                 }
72                         }
73                 }
74
75                 // in case no suitable relationship type could be found, search for generic types without the optional ValidSource / ValidTarget elements.
76                 if (suitableRelationshipTypes.isEmpty()) {
77                         for (TRelationshipType rt : allRelationshipTypes) {
78                                 if (rt.getValidSource() == null && rt.getValidTarget() == null) {
79                                         suitableRelationshipTypes.add(rt);
80                                 }
81                         }
82                 }
83
84                 return suitableRelationshipTypes;
85         }
86 }