DR695H | 45b0fbf | 2019-05-24 14:28:57 -0400 | [diff] [blame] | 1 | # Copyright 2019 AT&T Intellectual Property. All rights reserved. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
DR695H | 335a1c8 | 2019-07-11 11:57:29 -0400 | [diff] [blame] | 14 | |
| 15 | from ONAPLibrary.RequestsHelper import RequestsHelper |
DR695H | 45b0fbf | 2019-05-24 14:28:57 -0400 | [diff] [blame] | 16 | from robot.api import logger |
| 17 | from robot.api.deco import keyword |
| 18 | from robot.libraries.BuiltIn import BuiltIn |
| 19 | |
DR695H | 45b0fbf | 2019-05-24 14:28:57 -0400 | [diff] [blame] | 20 | |
| 21 | class RequestSOKeywords(object): |
| 22 | """SO is an ONAP testing library for Robot Framework that provides functionality for interacting with the serivce |
| 23 | orchestrator. """ |
| 24 | |
| 25 | def __init__(self): |
| 26 | super(RequestSOKeywords, self).__init__() |
DR695H | 45b0fbf | 2019-05-24 14:28:57 -0400 | [diff] [blame] | 27 | self.builtin = BuiltIn() |
DR695H | 335a1c8 | 2019-07-11 11:57:29 -0400 | [diff] [blame] | 28 | self.reqs = RequestsHelper() |
DR695H | 45b0fbf | 2019-05-24 14:28:57 -0400 | [diff] [blame] | 29 | |
| 30 | @keyword |
| 31 | def run_polling_get_request(self, endpoint, data_path, complete_states=None, fail_states=None, tries=20, |
| 32 | interval=15, auth=None): |
| 33 | """Runs an SO get request until a certain state is received.""" |
| 34 | if fail_states is None: |
| 35 | fail_states = ["FAILED"] |
| 36 | if complete_states is None: |
| 37 | complete_states = ["COMPLETE"] |
| 38 | # do this until it is done |
| 39 | for i in range(tries): |
DR695H | afd5237 | 2019-08-12 18:17:19 -0400 | [diff] [blame] | 40 | resp = self.reqs.get_request(alias="so", endpoint=endpoint, data_path=data_path, auth=auth) |
DR695H | 45b0fbf | 2019-05-24 14:28:57 -0400 | [diff] [blame] | 41 | logger.info(resp.json()['request']['requestStatus']['requestState']) |
DR695H | e74c39b | 2019-07-29 14:50:13 -0400 | [diff] [blame] | 42 | if resp.json()['request']['requestStatus']['requestState'] in fail_states: |
| 43 | self.builtin.fail("Received failure response from so " + resp.text) |
DR695H | 45b0fbf | 2019-05-24 14:28:57 -0400 | [diff] [blame] | 44 | if resp.json()['request']['requestStatus']['requestState'] in complete_states: |
DR695H | e74c39b | 2019-07-29 14:50:13 -0400 | [diff] [blame] | 45 | logger.info("Received complete response from so " + resp.text) |
DR695H | 843119f | 2019-07-09 18:10:15 -0400 | [diff] [blame] | 46 | return True, resp |
DR695H | 45b0fbf | 2019-05-24 14:28:57 -0400 | [diff] [blame] | 47 | else: |
| 48 | self.builtin.sleep(interval, "Response from SO is not in requested status") |
DR695H | 843119f | 2019-07-09 18:10:15 -0400 | [diff] [blame] | 49 | return False, None |
DR695H | 45b0fbf | 2019-05-24 14:28:57 -0400 | [diff] [blame] | 50 | |
| 51 | @keyword |
DR695H | 843119f | 2019-07-09 18:10:15 -0400 | [diff] [blame] | 52 | def run_create_request(self, endpoint, data_path, data, auth=None): |
DR695H | 45b0fbf | 2019-05-24 14:28:57 -0400 | [diff] [blame] | 53 | """Runs an SO create request and returns the request id and instance id.""" |
DR695H | afd5237 | 2019-08-12 18:17:19 -0400 | [diff] [blame] | 54 | response = self.reqs.post_request(alias="so", endpoint=endpoint, data_path=data_path, data=data, auth=auth) |
DR695H | 45b0fbf | 2019-05-24 14:28:57 -0400 | [diff] [blame] | 55 | logger.info("Creation request submitted to SO, got response") |
| 56 | |
| 57 | req_id = response.get('requestReferences', {}).get('requestId', '') |
| 58 | instance_id = response.get('requestReferences', {}).get('instanceId', '') |
| 59 | |
| 60 | return req_id, instance_id |