Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / CapabilityAssignment.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
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.sdc.toscaparser.api;
22
23 import java.util.ArrayList;
24 import java.util.LinkedHashMap;
25 import java.util.Map;
26
27 import org.onap.sdc.toscaparser.api.elements.CapabilityTypeDef;
28 import org.onap.sdc.toscaparser.api.elements.PropertyDef;
29
30 public class CapabilityAssignment {
31
32     private String name;
33     private LinkedHashMap<String, Object> _properties;
34     private CapabilityTypeDef _definition;
35     private LinkedHashMap<String, Object> _customDef;
36
37     public CapabilityAssignment(String cname,
38                                 LinkedHashMap<String, Object> cproperties,
39                                 CapabilityTypeDef cdefinition, LinkedHashMap<String, Object> customDef) {
40         name = cname;
41         _properties = cproperties;
42         _definition = cdefinition;
43         _customDef = customDef;
44     }
45
46     /**
47      * Get the properties list for capability
48      *
49      * @return list of property objects for capability
50      */
51     public ArrayList<Property> getPropertiesObjects() {
52         // Return a list of property objects
53         ArrayList<Property> properties = new ArrayList<Property>();
54         LinkedHashMap<String, Object> props = _properties;
55         if (props != null) {
56             for (Map.Entry<String, Object> me : props.entrySet()) {
57                 String pname = me.getKey();
58                 Object pvalue = me.getValue();
59
60                 LinkedHashMap<String, PropertyDef> propsDef = _definition.getPropertiesDef();
61                 if (propsDef != null) {
62                     PropertyDef pd = (PropertyDef) propsDef.get(pname);
63                     if (pd != null) {
64                         properties.add(new Property(pname, pvalue, pd.getSchema(), _customDef));
65                     }
66                 }
67             }
68         }
69         return properties;
70     }
71
72     /**
73      * Get the map of properties
74      *
75      * @return map of all properties contains dictionary of property name and property object
76      */
77     public LinkedHashMap<String, Property> getProperties() {
78         // Return a dictionary of property name-object pairs
79         LinkedHashMap<String, Property> npps = new LinkedHashMap<>();
80         for (Property p : getPropertiesObjects()) {
81             npps.put(p.getName(), p);
82         }
83         return npps;
84     }
85
86     /**
87      * Get the property value by name
88      *
89      * @param pname - the property name for capability
90      * @return the property value for this name
91      */
92     public Object getPropertyValue(String pname) {
93         // Return the value of a given property name
94         LinkedHashMap<String, Property> props = getProperties();
95         if (props != null && props.get(pname) != null) {
96             return props.get(name).getValue();
97         }
98         return null;
99     }
100
101     /**
102      * Get the name for capability
103      *
104      * @return the name for capability
105      */
106     public String getName() {
107         return name;
108     }
109
110     /**
111      * Get the definition for capability
112      *
113      * @return CapabilityTypeDef - contain definition for capability
114      */
115     public CapabilityTypeDef getDefinition() {
116         return _definition;
117     }
118
119     /**
120      * Set the property for capability
121      *
122      * @param pname  - the property name for capability to set
123      * @param pvalue - the property valiue for capability to set
124      */
125     public void setProperty(String pname, Object pvalue) {
126         _properties.put(pname, pvalue);
127     }
128
129     @Override
130     public String toString() {
131         return "CapabilityAssignment{" +
132                 "name='" + name + '\'' +
133                 ", _properties=" + _properties +
134                 ", _definition=" + _definition +
135                 '}';
136     }
137 }
138
139 /*python
140
141 from toscaparser.properties import Property
142
143
144 class CapabilityAssignment(object):
145     '''TOSCA built-in capabilities type.'''
146
147     def __init__(self, name, properties, definition):
148         self.name = name
149         self._properties = properties
150         self.definition = definition
151
152     def get_properties_objects(self):
153         '''Return a list of property objects.'''
154         properties = []
155         props = self._properties
156         if props:
157             for name, value in props.items():
158                 props_def = self.definition.get_properties_def()
159                 if props_def and name in props_def:
160                     properties.append(Property(name, value,
161                                                props_def[name].schema))
162         return properties
163
164     def get_properties(self):
165         '''Return a dictionary of property name-object pairs.'''
166         return {prop.name: prop
167                 for prop in self.get_properties_objects()}
168
169     def get_property_value(self, name):
170         '''Return the value of a given property name.'''
171         props = self.get_properties()
172         if props and name in props:
173             return props[name].value
174 */