e693a05a44b5772d02b48e324dce2d1ecdb31ab3
[it/dep.git] / smo-install / test / pythonsdk / src / oransdk / policy / clamp.py
1 #!/usr/bin/env python3
2 ###
3 # ============LICENSE_START=======================================================
4 # ORAN SMO PACKAGE - PYTHONSDK TESTS
5 # ================================================================================
6 # Copyright (C) 2022 AT&T Intellectual Property. All rights
7 #                             reserved.
8 # ================================================================================
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
12 #
13 # http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 # ============LICENSE_END============================================
21 # ===================================================================
22 #
23 ###
24 """Onap Policy Clamp Tosca Template module."""
25
26 from typing import Dict
27 from onapsdk.clamp.clamp_element import Clamp
28
29 class ClampToscaTemplate(Clamp):
30     """Onap Policy Clamp Tosca Template class."""
31
32     header = {"Accept": "application/json", "Content-Type": "application/json"}
33
34     def __init__(self, basic_auth: Dict[str, str]) -> None:
35         """
36         Initialize loop instance object.
37
38         Args:
39             basic_auth : basic auth
40
41         """
42         super().__init__()
43         self.basic_auth = basic_auth
44
45     def get_template_instance(self) -> dict:
46         """
47         Get tosca template instance.
48
49         Returns:
50             the tosca template instance
51         """
52         url = f"{self.base_url()}/toscaControlLoop/getToscaInstantiation"
53         template_instance = self.send_message_json('GET',
54                                                    'Get tosca template instance',
55                                                    url,
56                                                    basic_auth=self.basic_auth)
57
58         return template_instance
59
60     def upload_commission(self, tosca_template) -> dict:
61         """
62         Upload Tosca to commissioning.
63
64         Args:
65             tosca_template: the tosca template yaml
66         Returns:
67             the response of the uploading action
68
69         """
70         url = f"{self.base_url()}/toscaControlLoop/commissionToscaTemplate"
71         response = self.send_message_json('POST',
72                                           'Upload Tosca to commissioning',
73                                           url,
74                                           data=tosca_template,
75                                           headers=self.header,
76                                           basic_auth=self.basic_auth)
77         return response
78
79     def create_instance(self, tosca_instance_properties) -> dict:
80         """
81         Create Tosca instance.
82
83         Args:
84             tosca_instance_properties (str): the tosca template properties
85         Returns:
86             the response of the creation action
87         """
88         url = f"{self.base_url()}/toscaControlLoop/postToscaInstanceProperties"
89         response = self.send_message_json('POST',
90                                           'Create Tosca instance',
91                                           url,
92                                           data=tosca_instance_properties,
93                                           headers=self.header,
94                                           basic_auth=self.basic_auth)
95         return response
96
97     def get_template_instance_status(self, name, version) -> dict:
98         """
99         Get tosca template instance status.
100
101         Args:
102             name (str): the name of the template instance
103             version (str): the version of the template instance
104         Returns:
105             the template instance
106         """
107         url = f"{self.base_url()}/toscaControlLoop/getInstantiationOrderState?name={name}&version={version}"
108         template_instance = self.send_message_json('GET',
109                                                    'Get tosca template instance',
110                                                    url,
111                                                    basic_auth=self.basic_auth)
112
113         return template_instance
114
115     def change_instance_status(self, new_status, name, version) -> dict:
116         """
117         Update tosca instance status.
118
119         Args:
120             new_status (str): the new instance status
121             name (str): the new instance name
122             version (str): the new instance version
123         Returns:
124             the updated template instance
125         """
126         body = '{"orderedState":"' + new_status + '","controlLoopIdentifierList":[{"name":"' + name + '","version":"' + version + '"}]}'
127         url = f"{self.base_url()}/toscaControlLoop/putToscaInstantiationStateChange"
128         response = self.send_message_json('PUT',
129                                           'Update tosca instance status',
130                                           url,
131                                           data=body,
132                                           headers=self.header,
133                                           basic_auth=self.basic_auth)
134         return response
135
136     def delete_template_instance(self, name: str, version: str) -> dict:
137         """
138         Delete the tosca instance.
139
140         Args:
141             name (str): the instance name.
142             version (str): the instance version.
143         Returns:
144             the response of the deletion action
145         """
146         url = f"{self.base_url()}/toscaControlLoop/deleteToscaInstanceProperties?name={name}&version={version}"
147         response = self.send_message_json('DELETE',
148                                           'Delete the tosca instance',
149                                           url,
150                                           headers=self.header,
151                                           basic_auth=self.basic_auth)
152         return response
153
154     def decommission_template(self, name: str, version: str) -> dict:
155         """
156         Decommission the tosca template.
157
158         Args:
159             name (str): the tosca template name.
160             version (str): the tosca template version.
161         Returns:
162             the response of the decommission action
163         """
164         url = f"{self.base_url()}/toscaControlLoop/decommissionToscaTemplate?name={name}&version={version}"
165         response = self.send_message_json('DELETE',
166                                           'Decommission the tosca template',
167                                           url,
168                                           headers=self.header,
169                                           basic_auth=self.basic_auth)
170         return response