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