Fix NPE in toJsonString()
[ccsdk/sli/core.git] / sli / common / src / main / java / org / onap / ccsdk / sli / core / sli / SvcLogicGraph.java
1 /*-
2 x * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Modifications copyright (C) 2017 AT&T Intellectual Property. All rights
9  *                                              reserved.
10  * ================================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.ccsdk.sli.core.sli;
26
27 import java.io.PrintStream;
28 import java.io.Serializable;
29 import java.util.HashMap;
30 import java.util.Map;
31
32 public class SvcLogicGraph implements Serializable {
33         
34         /**
35          * 
36          */
37         private static final long serialVersionUID = 1L;
38         
39         private String module = null;
40         private String rpc = null;
41         private String mode = null;
42         private String version = null;
43
44   private String md5sum = null;
45
46         private Map<String, Serializable> attributes;
47         private Map<String, SvcLogicNode> namedNodes;
48         private SvcLogicNode rootNode;
49         
50         public SvcLogicGraph()
51         {
52                 attributes = new HashMap<>();
53                 namedNodes = new HashMap<>();
54                 rootNode = null;
55         }
56
57   public String getMd5sum() {
58     return md5sum;
59   }
60
61
62   public void setMd5sum(String md5sum) {
63     this.md5sum = md5sum;
64   }
65
66         
67         
68         public String getModule() {
69                 return module;
70         }
71
72
73         public void setModule(String module) {
74                 this.module = module;
75         }
76
77
78         public String getRpc() {
79                 return rpc;
80         }
81
82
83         public void setRpc(String rpc) {
84                 this.rpc = rpc;
85         }
86
87         
88
89
90         public String getMode() {
91                 return mode;
92         }
93
94
95         public void setMode(String mode) {
96                 this.mode = mode;
97         }
98
99
100         public String getVersion() {
101                 return version;
102         }
103
104
105         public void setVersion(String version) {
106                 this.version = version;
107         }
108
109
110         public void setRootNode(SvcLogicNode rootNode)
111         {
112                 this.rootNode = rootNode;
113         }
114         
115         public SvcLogicNode getRootNode()
116         {
117                 return(rootNode);
118         }
119         
120         public Serializable getAttribute(String name)
121         {
122                 if (attributes.containsKey(name))
123                 {
124                         return(attributes.get(name));
125                 }
126                 else
127                 {
128                         return(null);
129                 }
130                         
131         }
132         
133         public void setAttribute(String name, Serializable value) throws DuplicateValueException
134         {
135                 if (attributes.containsKey(name))
136                 {
137                         throw new DuplicateValueException("Duplicate attribute "+name);
138                 }
139                 
140                 attributes.put(name, value);
141         }
142         
143         public SvcLogicNode getNamedNode(String nodeName)
144         {
145                 if (namedNodes.containsKey(nodeName))
146                 {
147                         return(namedNodes.get(nodeName));
148                 }
149                 else
150                 {
151                         return(null);
152                 }
153         }
154
155         public void setNamedNode(String nodeName, SvcLogicNode node) throws DuplicateValueException
156         {
157                 if (namedNodes.containsKey(nodeName))
158                 {
159                         throw new DuplicateValueException("Duplicate node name "+nodeName);
160                 }
161                 
162                 namedNodes.put(nodeName, node);
163         }
164         
165         
166         
167         public void printAsGv(PrintStream pstr)
168         {
169                 pstr.println("digraph g {");
170                 pstr.println("START [label=\"START\\n"+module+":"+rpc+"\"];");
171                 
172                 if (rootNode != null)
173                 {
174                         pstr.println("START -> node"+rootNode.getNodeId()+";");
175                         rootNode.setVisited(false, true);
176                         rootNode.printAsGv(pstr);
177                 }
178                 pstr.println("}");
179         }
180         
181         public void printAsXml(PrintStream pstr)
182         {
183                 pstr.println("<service-logic module='"+getModule()+"' version='"+getVersion()+"'>");
184                 pstr.println("  <method rpc='"+getRpc()+"' mode='"+getMode()+"'>");
185                 if (rootNode != null)
186                 {
187                         rootNode.setVisited(false, true);
188                         rootNode.printAsXml(pstr, 2);
189                 }
190                 pstr.println("  </method>");
191                 pstr.println("</service-logic>");
192         }
193         
194         @Override
195         public String toString() {
196             return "SvcLogicGraph [module=" + module + ", rpc=" + rpc + ", mode=" + mode + ", version=" + version + ", md5sum=" + md5sum + "]";
197         }
198         
199 }