Skip to content

Commit bc46938

Browse files
Resolve "Property Assignment Support"
1 parent 148bae7 commit bc46938

19 files changed

Lines changed: 2562 additions & 35 deletions

docs/usage/usage/design_time.rst

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ You will need the package of the VSP to onboard.
1717

1818
.. code:: Python
1919
20-
from onapsdk.vendor import Vendor
21-
from onapsdk.vsp import Vsp
20+
from onapsdk.sdc.vendor import Vendor
21+
from onapsdk.sdc.vsp import Vsp
2222
2323
# We assume here that the Vendor has been already onboarded
2424
vendor = Vendor(name="myVendor")
@@ -31,34 +31,93 @@ Onboard a VF
3131

3232
.. code:: Python
3333
34-
from onapsdk.vsp import Vsp
35-
from onapsdk.vf import Vf
34+
from onapsdk.sdc.vsp import Vsp
35+
from onapsdk.sdc.vf import Vf
3636
3737
# We assume here that the VSP has been already onboarded
3838
vsp = Vsp(name="myVSP")
3939
vf = Vf(name="myVF", vsp=vsp)
4040
vf.onboard()
4141
42+
Onboard a VF with properties assignement
43+
----------------------------------------
44+
45+
.. code:: Python
46+
47+
from onapsdk.sdc.properties import Property
48+
from onapsdk.sdc.vsp import Vsp
49+
from onapsdk.sdc.vf import Vf
50+
51+
# We assume here that the VSP has been already onboarded
52+
vsp = Vsp(name="myVSP")
53+
property_1 = Property(
54+
name="prop1",
55+
property_type="string",
56+
value="test"
57+
)
58+
property_2 = Property(
59+
name="prop2",
60+
property_type="integer"
61+
)
62+
vf = Vf(name="myVF",
63+
vsp=vsp,
64+
properties=[
65+
property_1,
66+
property_2
67+
],
68+
inputs=[property_1])
69+
vf.onboard()
70+
4271
Onboard a Service
4372
-----------------
4473

4574
.. code:: Python
4675
47-
from onapsdk.vf import Vf
48-
from onapsdk.service import Service
76+
from onapsdk.sdc.vf import Vf
77+
from onapsdk.sdc.service import Service
4978
5079
# We assume here that the VF has been already onboarded
5180
vf = Vf(name="myVF")
5281
service = Service(name="myService", resources=[vf])
5382
service.onboard()
5483
84+
Onboard a Service with properties assignement
85+
---------------------------------------------
86+
87+
.. code:: Python
88+
89+
from onapsdk.sdc.properties import Property
90+
from onapsdk.sdc.vf import Vf
91+
from onapsdk.sdc.service import Service
92+
93+
# We assume here that the VF has been already onboarded
94+
vf = Vf(name="myVF")
95+
property_1 = Property(
96+
name="prop1",
97+
property_type="string",
98+
value="test"
99+
)
100+
property_2 = Property(
101+
name="prop2",
102+
property_type="integer",
103+
declare_input=True
104+
)
105+
service = Service(name="myService",
106+
resources=[vf],
107+
properties=[
108+
property_1,
109+
property_2
110+
],
111+
inputs=[property_1])
112+
service.onboard()
113+
55114
Onboard a Service with VL
56115
-------------------------
57116

58117
.. code:: Python
59118
60-
from onapsdk.vl import VL
61-
from onapsdk.service import Service
119+
from onapsdk.sdc.vl import VL
120+
from onapsdk.sdc.service import Service
62121
63122
# No VF needed, but you need to be sure that Vl with given
64123
# name exists in SDC

integration_tests/test_03_vf.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import requests
99

1010
from onapsdk.sdc import SDC
11+
from onapsdk.sdc.properties import Property
1112
from onapsdk.sdc.vendor import Vendor
1213
from onapsdk.sdc.vsp import Vsp
1314
from onapsdk.sdc.vf import Vf
@@ -54,3 +55,25 @@ def test_vf_onboard_unknown():
5455
vf.onboard()
5556
assert vsp.status == const.CERTIFIED
5657
assert vf.version == "1.0"
58+
59+
@pytest.mark.integration
60+
def test_vf_properties():
61+
"""Integration test to check properties assignment for Vf."""
62+
response = requests.post("{}/reset".format(Vendor.base_front_url))
63+
response.raise_for_status()
64+
vendor = Vendor(name="test")
65+
vendor.onboard()
66+
vsp = Vsp(name="test", package=open("{}/ubuntu16.zip".format(
67+
os.path.dirname(os.path.abspath(__file__))), 'rb'))
68+
vsp.vendor = vendor
69+
vsp.onboard()
70+
prop = Property(name="test1", property_type="string", value="123")
71+
vf = Vf(name="test", vsp=vsp, properties=[
72+
prop,
73+
Property(name="test2", property_type="integer")],
74+
inputs=[prop])
75+
vf.onboard()
76+
vf_properties = list(vf.properties)
77+
vf_inputs = list(vf.inputs)
78+
assert len(vf_properties) == 2
79+
assert len(vf_inputs) == 1

integration_tests/test_04_service.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import requests
99

1010
from onapsdk.sdc import SDC
11+
from onapsdk.sdc.properties import Property
1112
from onapsdk.sdc.vendor import Vendor
1213
from onapsdk.sdc.vsp import Vsp
1314
from onapsdk.sdc.vf import Vf
@@ -84,3 +85,27 @@ def test_service_upload_tca_artifact():
8485
artifact_name="tca_clampnode.yaml",
8586
artifact=data)
8687
payload_file.close()
88+
89+
@pytest.mark.integration
90+
def test_service_properties():
91+
"""Integration test to check properties assignment for Service."""
92+
response = requests.post("{}/reset".format(SDC.base_front_url))
93+
response.raise_for_status()
94+
vendor = Vendor(name="test")
95+
vendor.onboard()
96+
vsp = Vsp(name="test", package=open("{}/ubuntu16.zip".format(
97+
os.path.dirname(os.path.abspath(__file__))), 'rb'))
98+
vsp.vendor = vendor
99+
vsp.onboard()
100+
vf = Vf(name='test', vsp=vsp)
101+
vf.onboard()
102+
properties = [
103+
Property(name="test1", property_type="string", value="123"),
104+
Property(name="test2", property_type="integer")
105+
]
106+
svc = Service(name='test', resources=[vf], properties=properties, inputs=[properties[1]])
107+
svc.onboard()
108+
service_properties = list(svc.properties)
109+
service_inputs = list(svc.inputs)
110+
assert len(service_properties) == 2
111+
assert len(service_inputs) == 1

src/onapsdk/sdc/component.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
# SPDX-License-Identifier: Apache-2.0
4+
"""SDC Component module."""
5+
from dataclasses import dataclass
6+
from typing import Any, Dict, Iterator
7+
8+
from onapsdk.sdc.properties import ComponentProperty
9+
from onapsdk.utils.jinja import jinja_env
10+
11+
12+
@dataclass
13+
class Component: # pylint: disable=too-many-instance-attributes
14+
"""Component dataclass."""
15+
16+
created_from_csar: bool
17+
actual_component_uid: str
18+
unique_id: str
19+
normalized_name: str
20+
name: str
21+
origin_type: str
22+
customization_uuid: str
23+
component_uid: str
24+
component_version: str
25+
tosca_component_name: str
26+
component_name: str
27+
sdc_resource: "SdcResource"
28+
parent_sdc_resource: "SdcResource"
29+
30+
@classmethod
31+
def create_from_api_response(cls,
32+
api_response: Dict[str, Any],
33+
sdc_resource: "SdcResource",
34+
parent_sdc_resource: "SdcResource") -> "Component":
35+
"""Create component from api response.
36+
37+
Args:
38+
api_response (Dict[str, Any]): component API response
39+
sdc_resource (SdcResource): component's SDC resource
40+
parent_sdc_resource (SdcResource): component's parent SDC resource
41+
42+
Returns:
43+
Component: Component created using api_response and SDC resource
44+
45+
"""
46+
return cls(created_from_csar=api_response["createdFromCsar"],
47+
actual_component_uid=api_response["actualComponentUid"],
48+
unique_id=api_response["uniqueId"],
49+
normalized_name=api_response["normalizedName"],
50+
name=api_response["name"],
51+
origin_type=api_response["originType"],
52+
customization_uuid=api_response["customizationUUID"],
53+
component_uid=api_response["componentUid"],
54+
component_version=api_response["componentVersion"],
55+
tosca_component_name=api_response["toscaComponentName"],
56+
component_name=api_response["componentName"],
57+
sdc_resource=sdc_resource,
58+
parent_sdc_resource=parent_sdc_resource)
59+
60+
@property
61+
def properties_url(self) -> str:
62+
"""Url to get component's properties.
63+
64+
Returns:
65+
str: Compoent's properties url
66+
67+
"""
68+
return self.parent_sdc_resource.get_component_properties_url(self)
69+
70+
@property
71+
def properties_value_url(self) -> str:
72+
"""Url to set component property value.
73+
74+
Returns:
75+
str: Url to set component property value
76+
77+
"""
78+
return self.parent_sdc_resource.get_component_properties_value_set_url(self)
79+
80+
@property
81+
def properties(self) -> Iterator["ComponentProperty"]:
82+
"""Component properties.
83+
84+
In SDC it's named as properties, but we uses "inputs" endpoint to fetch them.
85+
Structure is also input's like, but it's a property.
86+
87+
Yields:
88+
ComponentProperty: Component property object
89+
90+
"""
91+
for component_property in self.sdc_resource.send_message_json(\
92+
"GET",
93+
f"Get {self.name} component properties",
94+
self.properties_url,
95+
exception=ValueError):
96+
yield ComponentProperty(unique_id=component_property["uniqueId"],
97+
name=component_property["name"],
98+
property_type=component_property["type"],
99+
_value=component_property.get("value"),
100+
component=self)
101+
102+
def get_property(self, property_name: str) -> "ComponentProperty":
103+
"""Get component property by it's name.
104+
105+
Args:
106+
property_name (str): property name
107+
108+
Raises:
109+
AttributeError: Component has no property with given name
110+
111+
Returns:
112+
ComponentProperty: Component's property object
113+
114+
"""
115+
for property_obj in self.properties:
116+
if property_obj.name == property_name:
117+
return property_obj
118+
raise AttributeError("Component has no property with %s name" % property_name)
119+
120+
def set_property_value(self, property_obj: "ComponentProperty", value: Any) -> None:
121+
"""Set property value.
122+
123+
Set given value to component property
124+
125+
Args:
126+
property_obj (ComponentProperty): Component property object
127+
value (Any): Property value to set
128+
129+
"""
130+
self.sdc_resource.send_message_json(
131+
"POST",
132+
f"Set {self.name} component property {property_obj.name} value",
133+
self.properties_value_url,
134+
data=jinja_env().get_template(\
135+
"sdc_resource_component_set_property_value.json.j2").\
136+
render(
137+
component=self,
138+
value=value,
139+
property=property_obj
140+
),
141+
exception=ValueError
142+
)

0 commit comments

Comments
 (0)