2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.sdc.toscaparser.api;
23 import java.util.ArrayList;
24 import java.util.LinkedHashMap;
27 import org.onap.sdc.toscaparser.api.elements.CapabilityTypeDef;
28 import org.onap.sdc.toscaparser.api.elements.PropertyDef;
30 public class CapabilityAssignment {
33 private LinkedHashMap<String, Object> _properties;
34 private CapabilityTypeDef _definition;
35 private LinkedHashMap<String, Object> _customDef;
37 public CapabilityAssignment(String cname,
38 LinkedHashMap<String, Object> cproperties,
39 CapabilityTypeDef cdefinition, LinkedHashMap<String, Object> customDef) {
41 _properties = cproperties;
42 _definition = cdefinition;
43 _customDef = customDef;
47 * Get the properties list for capability
49 * @return list of property objects for capability
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;
56 for (Map.Entry<String, Object> me : props.entrySet()) {
57 String pname = me.getKey();
58 Object pvalue = me.getValue();
60 LinkedHashMap<String, PropertyDef> propsDef = _definition.getPropertiesDef();
61 if (propsDef != null) {
62 PropertyDef pd = (PropertyDef) propsDef.get(pname);
64 properties.add(new Property(pname, pvalue, pd.getSchema(), _customDef));
73 * Get the map of properties
75 * @return map of all properties contains dictionary of property name and property object
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);
87 * Get the property value by name
89 * @param pname - the property name for capability
90 * @return the property value for this name
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();
102 * Get the name for capability
104 * @return the name for capability
106 public String getName() {
111 * Get the definition for capability
113 * @return CapabilityTypeDef - contain definition for capability
115 public CapabilityTypeDef getDefinition() {
120 * Set the property for capability
122 * @param pname - the property name for capability to set
123 * @param pvalue - the property valiue for capability to set
125 public void setProperty(String pname, Object pvalue) {
126 _properties.put(pname, pvalue);
130 public String toString() {
131 return "CapabilityAssignment{" +
132 "name='" + name + '\'' +
133 ", _properties=" + _properties +
134 ", _definition=" + _definition +
141 from toscaparser.properties import Property
144 class CapabilityAssignment(object):
145 '''TOSCA built-in capabilities type.'''
147 def __init__(self, name, properties, definition):
149 self._properties = properties
150 self.definition = definition
152 def get_properties_objects(self):
153 '''Return a list of property objects.'''
155 props = self._properties
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))
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()}
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