[SDC-242] jtosca resolve get_input
[sdc/sdc-tosca.git] / src / main / java / org / openecomp / sdc / toscaparser / api / Capability.java
1 package org.openecomp.sdc.toscaparser.api;
2
3 import java.util.ArrayList;
4 import java.util.LinkedHashMap;
5 import java.util.Map;
6
7 import org.openecomp.sdc.toscaparser.api.elements.CapabilityTypeDef;
8 import org.openecomp.sdc.toscaparser.api.elements.PropertyDef;
9
10 public class Capability {
11         
12         private String name;
13         private LinkedHashMap<String,Object> _properties;
14         private CapabilityTypeDef _definition;
15
16         public Capability(String cname, 
17                                           LinkedHashMap<String,Object> cproperties,
18                                          CapabilityTypeDef cdefinition) {
19                 name = cname;
20                 _properties = cproperties;
21                 _definition = cdefinition;
22         }
23         
24         public ArrayList<Property> getPropertiesObjects() {
25                 // Return a list of property objects
26                 ArrayList<Property> properties = new ArrayList<Property>();
27                 LinkedHashMap<String,Object> props = _properties;
28                 if(props != null) {
29                         for(Map.Entry<String,Object> me: props.entrySet()) {
30                                 String pname = me.getKey();
31                                 Object pvalue = me.getValue();
32                                 
33                                 LinkedHashMap<String,PropertyDef> propsDef = _definition.getPropertiesDef();
34                                 if(propsDef != null) {
35                                         PropertyDef pd = (PropertyDef)propsDef.get(pname);
36                                         if(pd != null) {
37                                                 properties.add(new Property(pname,pvalue,pd.getSchema(),null));
38                                         }
39                                 }
40                         }
41                 }
42                 return properties;
43         }
44         
45         public LinkedHashMap<String,Property> getProperties() {
46         // Return a dictionary of property name-object pairs
47                 LinkedHashMap<String,Property> npps = new LinkedHashMap<>();
48                 for(Property p: getPropertiesObjects()) {
49                         npps.put(p.getName(),p);
50                 }
51                 return npps;
52         }
53
54         public Object getPropertyValue(String pname) {
55         // Return the value of a given property name
56                 LinkedHashMap<String,Property> props = getProperties();
57         if(props != null && props.get(pname) != null) {
58             return props.get(name).getValue();
59         }
60         return null;
61         }
62
63          public String getName() {
64                  return name;
65          }
66          
67          public CapabilityTypeDef getDefinition() {
68                  return _definition;
69          }
70          
71          // setter
72          public void setProperty(String pname,Object pvalue) {
73                  _properties.put(pname,pvalue);
74          }
75
76     @Override
77     public String toString() {
78         return "Capability{" +
79                 "name='" + name + '\'' +
80                 ", _properties=" + _properties +
81                 ", _definition=" + _definition +
82                 '}';
83     }
84 }
85
86 /*python
87
88 from toscaparser.properties import Property
89
90
91 class Capability(object):
92     '''TOSCA built-in capabilities type.'''
93
94     def __init__(self, name, properties, definition):
95         self.name = name
96         self._properties = properties
97         self.definition = definition
98
99     def get_properties_objects(self):
100         '''Return a list of property objects.'''
101         properties = []
102         props = self._properties
103         if props:
104             for name, value in props.items():
105                 props_def = self.definition.get_properties_def()
106                 if props_def and name in props_def:
107                     properties.append(Property(name, value,
108                                                props_def[name].schema))
109         return properties
110
111     def get_properties(self):
112         '''Return a dictionary of property name-object pairs.'''
113         return {prop.name: prop
114                 for prop in self.get_properties_objects()}
115
116     def get_property_value(self, name):
117         '''Return the value of a given property name.'''
118         props = self.get_properties()
119         if props and name in props:
120             return props[name].value
121 */