2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.sdc.toscaparser.api;
23 import org.onap.sdc.toscaparser.api.elements.RelationshipType;
25 import java.util.ArrayList;
26 import java.util.LinkedHashMap;
28 //import java.util.Iterator;
30 public class ToscaGraph {
31 // Graph of Tosca Node Templates
33 private ArrayList<NodeTemplate> nodeTemplates;
34 private LinkedHashMap<String, NodeTemplate> vertices;
36 public ToscaGraph(ArrayList<NodeTemplate> inodeTemplates) {
37 nodeTemplates = inodeTemplates;
38 vertices = new LinkedHashMap<String, NodeTemplate>();
42 private void createVertex(NodeTemplate node) {
43 if (vertices.get(node.getName()) == null) {
44 vertices.put(node.getName(), node);
48 private void createEdge(NodeTemplate node1,
50 RelationshipType relation) {
51 if (vertices.get(node1.getName()) == null) {
53 vertices.get(node1.name)._addNext(node2, relation);
57 public NodeTemplate vertex(String name) {
58 if (vertices.get(name) != null) {
59 return vertices.get(name);
64 // public Iterator getIter() {
65 // return vertices.values().iterator();
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);
86 public String toString() {
88 + "nodeTemplates=" + nodeTemplates
89 + ", vertices=" + vertices
96 class ToscaGraph(object):
97 '''Graph of Tosca Node Templates.'''
98 def __init__(self, nodetemplates):
99 self.nodetemplates = nodetemplates
103 def _create_vertex(self, node):
104 if node not in self.vertices:
105 self.vertices[node.name] = node
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,
113 def vertex(self, node):
114 if node in self.vertices:
115 return self.vertices[node]
118 return iter(self.vertices.values())
121 for node in self.nodetemplates:
122 relation = node.relationships
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)