Fix bgpcep-1node-throughpcep
[integration/test.git] / csit / libraries / IoTDM / criotdm.py
1 """This library should work with ciotdm, both work for iotdm project."""
2 import ciotdm
3
4
5 def connect_to_iotdm(host, user, password, prot="http"):
6     """According to protocol, connect to iotdm."""
7     return ciotdm.connect(host, base="InCSE1", auth=(user, password), protocol=prot)
8
9
10 def modify_headers_origin(connection, new_origin):
11     """Replace the headers origin with the neworigin to test ACP."""
12     connection.modify_headers_origin(new_origin)
13
14
15 def create_resource(connection, parent, restype, attribute=None):
16     """Create resource without command."""
17     connection.create(parent, restype, attribute)
18     check_response(connection.response, "create")
19     return connection.response
20
21
22 def create_resource_with_command(connection, parent, restype, command, attribute=None):
23     """According to command in the header, create the resource."""
24     connection.create_with_command(parent, restype, command, attribute)
25     check_response(connection.response, "create")
26     return connection.response
27
28
29 def create_subscription(connection, parent, ip, port):
30     """Create subscription."""
31     uri = "http://%s:%d" % (ip, int(port))
32     connection.create(
33         parent,
34         "subscription",
35         {"notificationURI": uri, "notificationContentType": "wholeResource"},
36     )
37     check_response(connection.response, "create")
38     return connection.response
39
40
41 def retrieve_resource(connection, resid):
42     """Retrieve resource according to resourceID."""
43     connection.retrieve(resid)
44     check_response(connection.response, "retrieve")
45     return connection.response
46
47
48 def retrieve_resource_with_command(connection, resid, command):
49     """According to command, retrieve source with the resourceID."""
50     connection.retrieve_with_command(resid, command)
51     check_response(connection.response, "retrieve")
52     return connection.response
53
54
55 def update_resource(connection, resid, restype, attr):
56     """According to resourceID, update resource."""
57     connection.update(resid, restype, attr)
58     check_response(connection.response, "update")
59     return connection.response
60
61
62 def update_resource_with_command(connection, resid, restype, command, attr):
63     """According to command, update resource with resourceID."""
64     connection.update_with_command(resid, restype, command, attr)
65     check_response(connection.response, "update")
66     return connection.response
67
68
69 def delete_resource(connection, resid):
70     """According to resourceID, delete the resource."""
71     connection.delete(resid)
72     check_response(connection.response, "delete")
73     return connection.response
74
75
76 def delete_resource_with_command(connection, resid, command):
77     """According to command, delete the resource with resourceID."""
78     connection.delete_with_command(resid, command)
79     check_response(connection.response, "delete")
80     return connection.response
81
82
83 def child_resource(response):
84     """Return child resource."""
85     return ciotdm.childResource(response)
86
87
88 def child_resource_first(response):
89     """Return child resource on top of dictionary."""
90     return ciotdm.childResourceFirst(response)
91
92
93 def resid(response):
94     """Return resource ID."""
95     return ciotdm.resid(response)
96
97
98 def parent_id(response):
99     """Return parent ID."""
100     return ciotdm.parent(response)
101
102
103 def name(response):
104     """Return resourceName."""
105     resource_name = ciotdm.name(response)
106     if resource_name is None:
107         raise AssertionError("Cannot find this resource")
108     return resource_name
109
110
111 def text(response):
112     """Return whole resource in text."""
113     return response.text
114
115
116 def last_modified_time(response):
117     """Return resource lastModifiedTime."""
118     return ciotdm.lastModifiedTime(response)
119
120
121 def state_tag(response):
122     """Return resource state tag."""
123     return ciotdm.stateTag(response)
124
125
126 def current_number_of_instances(response):
127     """Return current number of instances."""
128     return ciotdm.currentNumberOfInstances(response)
129
130
131 def current_byte_size(response):
132     """Return current byte size."""
133     return ciotdm.currentByteSize(response)
134
135
136 def max_number_of_instances(response):
137     """Return max number of instances."""
138     return ciotdm.maxNumberOfInstances(response)
139
140
141 def content(response):
142     """Return content child from response."""
143     return ciotdm.content(response)
144
145
146 def max_byte_size(response):
147     """Return max byte size."""
148     return ciotdm.maxByteSize(response)
149
150
151 def status_code(response):
152     """Return resource status_code."""
153     return response.status_code
154
155
156 def json(response):
157     """Return resource in json format."""
158     return response.json()
159
160
161 def elapsed(response):
162     """Return resource elapsed."""
163     return response.elapsed.total_seconds()
164
165
166 def location(response):
167     """Return response content-location."""
168     return response.headers["Content-Location"]
169
170
171 def kill_the_tree(host, cseid, username, password):
172     """Delete the whole tree."""
173     connection = ciotdm.connect(
174         host, base=cseid, auth=(username, password), protocol="http"
175     )
176     connection.kill()
177
178
179 def check_response(response, operation):
180     """Check whether the connection is none."""
181     if response is None:
182         raise AssertionError("Cannot %s this resource") % (operation)
183     elif hasattr(response, "status_code"):
184         if response.status_code < 200 or response.status_code > 299:
185             raise AssertionError(
186                 "Cannot %s this resource [%d] : %s"
187                 % (operation, response.status_code, response.text)
188             )