7cd06ae32a794171520c4ee8ff80084f8136c590
[dcaegen2/platform.git] / mod / distributorapi / distributor / runtime_client.py
1 # ============LICENSE_START=======================================================
2 # Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3 # ================================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #      http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 # ============LICENSE_END=========================================================
16 """Runtime API client"""
17
18 import requests as reqs
19 from distributor import errors
20 from distributor.utils import urljoin, get_json
21
22
23 def get_graph(runtime_url, graph_id):
24     # REVIEW: There's only support for one graph right now..
25     url = urljoin(runtime_url, "api/graph/main")
26     return get_json(url)
27
28
29 def create_graph(runtime_url, graph_id, graph_name):
30     url = urljoin(runtime_url, "api/graph/main")
31
32     resp = reqs.post(url, json={"name": graph_name, "id": graph_id
33         , "description": "", "main": True})
34
35     try:
36         resp.raise_for_status()
37     except Exception as e:
38         raise errors.DistributorAPIError(e)
39
40
41 def delete_graph(runtime_url):
42     url = urljoin(runtime_url, "api/graph/main")
43
44     try:
45         reqs.delete(url).raise_for_status()
46     except Exception as e:
47         raise errors.DistributorAPIError(e)
48
49
50 def post_graph(runtime_url, graph_id, actions):
51     url = urljoin(runtime_url, "api/graph", graph_id, "distribute")
52     graph_request = {"actions": actions}
53
54     resp = reqs.post(url, json=graph_request)
55
56     try:
57         resp.raise_for_status()
58         # REVIEW: Should be blueprint
59         return resp.json()
60     except Exception as e:
61         with open("runtime-request-failed.json", "w+") as f:
62             import json
63             json.dump(graph_request, f)
64         raise errors.DistributorAPIError(e)
65
66
67 def ensure_graph(runtime_url, pg_id, pg_name, max_attempts=6):
68     """Ensures the graph with the specified id will exist"""
69     # TODO: Remove this when runtime API more mature
70     # Added this attempted delete call here to make sure repeated calls to post
71     # flows works by making sure the runtime API main graph is always empty
72     try:
73         delete_graph(runtime_url)
74     except:
75         # Probably a 404, doesn't matter
76         pass
77
78     # The attempts are not *really* attempts because attempts equates to looping
79     # twice
80     for i in range(0, max_attempts):
81         resp = None
82
83         try:
84             resp = get_graph(runtime_url, pg_id)
85         except Exception as e:
86             # Assuming you are here because graph needs to be created
87             create_graph(runtime_url, pg_id, pg_name)
88
89         # TODO: Runtime api only supports 1 graph which is why this check is
90         # here. Make sure it will support many graphs and remove this
91
92         if resp == None:
93             # You are here because the graph was created for first time or
94             # the graph was deleted then created. Anyways next loop should
95             # check if it got created ok
96             continue
97         elif resp != None and resp["id"] != pg_id:
98             delete_graph(runtime_url)
99         elif resp != None and resp["id"] == pg_id:
100             return True
101
102     return False