Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / ToscaGraph.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.sdc.toscaparser.api;
22
23 import org.onap.sdc.toscaparser.api.elements.RelationshipType;
24
25 import java.util.ArrayList;
26 import java.util.LinkedHashMap;
27
28 //import java.util.Iterator;
29
30 public class ToscaGraph {
31     // Graph of Tosca Node Templates
32
33     private ArrayList<NodeTemplate> nodeTemplates;
34     private LinkedHashMap<String, NodeTemplate> vertices;
35
36     public ToscaGraph(ArrayList<NodeTemplate> inodeTemplates) {
37         nodeTemplates = inodeTemplates;
38         vertices = new LinkedHashMap<String, NodeTemplate>();
39         create();
40     }
41
42     private void createVertex(NodeTemplate node) {
43         if (vertices.get(node.getName()) == null) {
44             vertices.put(node.getName(), node);
45         }
46     }
47
48     private void createEdge(NodeTemplate node1,
49                             NodeTemplate node2,
50                             RelationshipType relation) {
51         if (vertices.get(node1.getName()) == null) {
52             createVertex(node1);
53             vertices.get(node1.name)._addNext(node2, relation);
54         }
55     }
56
57     public NodeTemplate vertex(String name) {
58         if (vertices.get(name) != null) {
59             return vertices.get(name);
60         }
61         return null;
62     }
63
64 //  public Iterator getIter() {
65 //              return vertices.values().iterator();
66 //      }
67
68     private void create() {
69         for (NodeTemplate node : nodeTemplates) {
70             LinkedHashMap<RelationshipType, NodeTemplate> relation = node.getRelationships();
71             if (relation != null) {
72                 for (RelationshipType rel : relation.keySet()) {
73                     NodeTemplate nodeTpls = relation.get(rel);
74                     for (NodeTemplate tpl : nodeTemplates) {
75                         if (tpl.getName().equals(nodeTpls.getName())) {
76                             createEdge(node, tpl, rel);
77                         }
78                     }
79                 }
80             }
81             createVertex(node);
82         }
83     }
84
85     @Override
86     public String toString() {
87         return "ToscaGraph{"
88                 + "nodeTemplates=" + nodeTemplates
89                 + ", vertices=" + vertices
90                 + '}';
91     }
92 }
93
94 /*python
95
96 class ToscaGraph(object):
97     '''Graph of Tosca Node Templates.'''
98     def __init__(self, nodetemplates):
99         self.nodetemplates = nodetemplates
100         self.vertices = {}
101         self._create()
102
103     def _create_vertex(self, node):
104         if node not in self.vertices:
105             self.vertices[node.name] = node
106
107     def _create_edge(self, node1, node2, relationship):
108         if node1 not in self.vertices:
109             self._create_vertex(node1)
110         self.vertices[node1.name]._add_next(node2,
111                                             relationship)
112
113     def vertex(self, node):
114         if node in self.vertices:
115             return self.vertices[node]
116
117     def __iter__(self):
118         return iter(self.vertices.values())
119
120     def _create(self):
121         for node in self.nodetemplates:
122             relation = node.relationships
123             if relation:
124                 for rel, nodetpls in relation.items():
125                     for tpl in self.nodetemplates:
126                         if tpl.name == nodetpls.name:
127                             self._create_edge(node, tpl, rel)
128             self._create_vertex(node)
129 */