3b7abd39bdde5651a5ba31d92e9368d58063175a
[vid.git] / vid-app-common / src / main / java / org / onap / vid / model / Node.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 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.vid.model;
22
23 import java.text.DateFormat;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.Map;
28 import java.util.Map.Entry;
29
30 import org.onap.vid.asdc.beans.tosca.Input;
31 import org.onap.vid.asdc.beans.tosca.NodeTemplate;
32
33 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
34 /**
35  * The Class Node.
36  */
37 public class Node {
38         /** The Constant LOG. */
39         private static final EELFLoggerDelegate LOG = EELFLoggerDelegate.getLogger(Node.class);
40         
41         /** The Constant dateFormat. */
42         final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
43
44         /** The uuid. */
45         private String uuid;
46         
47         /** The invariant uuid. */
48         private String invariantUuid;
49         
50         /** The description. */
51         private String description;
52         
53         /** The name. */
54         private String name;
55         
56         /** The version. */
57         private String version;
58         
59         /** The model customization uuid. */
60         private String customizationUuid;
61         
62         /** The inputs. */
63         private Map<String, Input> inputs = new HashMap<>();
64         
65         /** The get_input or other constructs from node template properties. */
66         private Map<String, CommandProperty> commands;
67         
68         /** The get_input or other constructs from node template properties. */
69         private Map<String, String> properties;
70
71         /** Type from Metadata */
72         private String type;
73         /**
74          * Instantiates a new node.
75          */
76         public Node() {
77                 this.commands = new HashMap<String, CommandProperty>();
78                 this.properties = new HashMap<String, String>();
79         }
80         
81         /**
82          * Gets the uuid.
83          *
84          * @return the uuid
85          */
86         public String getUuid() {
87                 return uuid;
88         }
89
90         /**
91          * Gets the invariant uuid.
92          *
93          * @return the invariant uuid
94          */
95         public String getInvariantUuid() {
96                 return invariantUuid;
97         }
98
99         /**
100          * Gets the description.
101          *
102          * @return the description
103          */
104         public String getDescription() {
105                 return description;
106         }
107
108         /**
109          * Gets the name.
110          *
111          * @return the name
112          */
113         public String getName() {
114                 return name;
115         }
116
117         /**
118          * Gets the version.
119          *
120          * @return the version
121          */
122         public String getVersion() {
123                 return version;
124         }
125         
126         /**
127          * Gets the customization uuid.
128          *
129          * @return the model customization uuid
130          */
131         public String getCustomizationUuid() {
132                 return customizationUuid;
133         }
134         /**
135          * Gets the inputs.
136          *
137          * @return the inputs
138          */
139         public Map<String, Input> getInputs() {
140                 return inputs;
141         }
142         /**
143          * Gets the commands.
144          *
145          * @return the commands
146          */
147         public Map<String, CommandProperty> getCommands() {
148                 return commands;
149         }
150         /**
151          * Gets the properties.
152          *
153          * @return the properties
154          */
155         public Map<String, String> getProperties() {
156                 return properties;
157         }
158         /**
159          * Sets the uuid.
160          *
161          * @param uuid the new uuid
162          */
163         public void setUuid(String uuid) {
164                 this.uuid = uuid;
165         }
166
167         /**
168          * Sets the invariant uuid.
169          *
170          * @param invariantUuid the new invariant uuid
171          */
172         public void setInvariantUuid(String invariantUuid) {
173                 this.invariantUuid = invariantUuid;
174         }
175
176         /**
177          * Sets the description.
178          *
179          * @param description the new description
180          */
181         public void setDescription(String description) {
182                 this.description = description;
183         }
184
185         /**
186          * Sets the name.
187          *
188          * @param name the new name
189          */
190         public void setName(String name) {
191                 this.name = name;
192         }
193
194         /**
195          * Sets the version.
196          *
197          * @param version the new version
198          */
199         public void setVersion(String version) {
200                 this.version = version;
201         }
202         /**
203          * Sets the customization uuid.
204          *
205          * @param u the new customization uuid
206          */
207         public void setCustomizationUuid(String u) {
208                 this.customizationUuid = u;
209         }
210
211         /**
212          * Sets the inputs.
213          *
214          * @param inputs the inputs
215          */
216         public void setInputs(Map<String, Input> inputs) {
217                 this.inputs = inputs;
218         }
219         /**
220          * Sets the commands.
221          *
222          * @param m the commands
223          */
224         public void setCommands( Map<String, CommandProperty>m ) {
225                 commands = m;
226         }
227         /**
228          * Sets the properties.
229          *
230          * @param p the properties
231          */
232         public void setProperties( Map<String, String>p) {
233                 properties = p;
234         }
235
236
237         /**
238          * @return metadata type
239          */
240         public String getType() {
241                 return type;
242         }
243
244         /**
245          * Set metadata type
246          *
247          * @param type e.g. VF/CP/SERVICE_PROXY
248          */
249         public void setType(String type) {
250                 this.type = type;
251         }
252
253         /**
254          * Extract node.
255          *
256          * @param modelCustomizationName the model customization name
257          * @param nodeTemplate the node template
258          * @return the node
259          */
260         public void extractNode (NodeTemplate nodeTemplate) {
261                 
262                 String methodName = "extractNode";
263                 
264                 setUuid(nodeTemplate.getMetadata().getUUID());
265                 setInvariantUuid(nodeTemplate.getMetadata().getInvariantUUID());
266                 setDescription(nodeTemplate.getMetadata().getDescription());
267                 setName(nodeTemplate.getMetadata().getName());
268                 setVersion(nodeTemplate.getMetadata().getVersion());
269                 // add customizationUUID
270                 setCustomizationUuid(nodeTemplate.getMetadata().getCustomizationUUID());
271
272                 try {
273                         // nodeTemplate.getProperties() map of String->Object
274                         for (Entry<String, Object> entrySet : nodeTemplate.getProperties().entrySet()) {
275                                 LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " node template property: " + entrySet.getKey());
276                                 if ( entrySet.getValue() != null ) {
277                                         readStringAndCommandsProperties(entrySet);
278                                 }
279                         }
280                 }
281                 catch ( Exception e ) {
282                         LOG.error(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + methodName + " Unable to parse node properties: e=" + 
283                                         e.toString());
284                 }
285         }
286
287         private void readStringAndCommandsProperties(Entry<String, Object> entrySet) {
288                 String key = entrySet.getKey();
289                 String methodName = "readStringAndCommandsProperties";
290                 LOG.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + "  property: " +
291                                 key + "=" + entrySet.getValue());
292                 Class<?> c = entrySet.getValue().getClass();
293                 if ( c.getName().equalsIgnoreCase(String.class.getName())) {
294             getProperties().put (key, (String) entrySet.getValue());
295         }
296         else {
297             Class<?>[] interfaces = entrySet.getValue().getClass().getInterfaces();
298
299             for(Class<?> ifc: interfaces ) {
300                 if ( ifc.getName().equalsIgnoreCase(Map.class.getName()) ) {
301                     readGetInputAsCommands(entrySet, key);
302
303                 }
304             }
305
306         }
307         }
308
309     private void readGetInputAsCommands(Entry<String, Object> entrySet, String key) {
310         // only extract get_input for now
311         @SuppressWarnings("unchecked")
312         HashMap<String,String> v = (HashMap<String,String>) entrySet.getValue();
313         for (Entry<String, String> entry : v.entrySet()) {
314             // only include get_input for now
315             if ( ModelConstants.GET_INPUT_TAG.equalsIgnoreCase ( entry.getKey() ) ) {
316                 CommandProperty cp = new CommandProperty();
317                 cp.setCommand(entry.getKey());
318                 cp.setInputName(entry.getValue());
319                 cp.setDisplayName(key);
320                 getCommands().put(key,cp);
321             }
322         }
323     }
324
325 }