1 """This is the base library for criotdm. Both work for IoTDM project."""
5 op_provision = ":8181/restconf/operations/onem2m:onem2m-cse-provisioning"
6 op_tree = ":8181/restconf/operational/onem2m:onem2m-resource-tree"
7 op_cleanup = ":8181/restconf/operations/onem2m:onem2m-cleanup-store"
67 resources = {"m2m:ae", "m2m:cnt", "m2m:cin", "m2m:sub",
68 "m2m:acp", "m2m:nod", "m2m:grp", "m2m:cb", "ch"}
70 payload_map = {1: acp_payload, 2: ae_payload, 3: con_payload,
71 4: cin_payload, 14: nod_payload, 23: sub_payload}
74 def find_key(response, key, first=None):
75 """Deserialize response, return value for key or None."""
77 key1 = list(dic.keys())
79 raise ValueError("The response should be json object")
80 if key1[0] not in resources:
81 raise ValueError("The resource is not recognized")
83 return dic.get(key1[0], None)
85 return dic.get(key1[0], None).get(key, None)
88 def childResource(response):
89 """Return child resources from the response."""
90 return find_key(response, "ch")
93 def childResourceFirst(response):
94 """Return child resources from the response."""
95 return find_key(response, "ch", True)
99 """Return the resource name in the response."""
100 return find_key(response, "rn")
103 def lastModifiedTime(response):
104 """Return the lastModifiedTime in the response."""
105 return find_key(response, "lt")
108 def stateTag(response):
109 """Return the state tag from the response."""
110 return find_key(response, "st")
114 """Return the resource id in the response."""
115 return find_key(response, "ri")
118 def parent(response):
119 """Return the parent resource id in the response."""
120 return find_key(response, "pi")
123 def content(response):
124 """Return the content the response."""
125 return find_key(response, "con")
128 def restype(response):
129 """Return the resource type the response."""
130 return find_key(response, "rty")
133 def currentNumberOfInstances(response):
134 """Return current number of instances from the response."""
135 return find_key(response, "cni")
138 def currentByteSize(response):
139 """Return current byte size from the response."""
140 return find_key(response, "cbs")
143 def maxNumberOfInstances(response):
144 """Return max number of instances from the response."""
145 return find_key(response, "mni")
148 def maxByteSize(response):
149 """Return max byte size from the response."""
150 return find_key(response, "mbs")
153 def status(response):
154 """Return the protocol status code in the response."""
156 return response.status_code
157 except(TypeError, AttributeError):
161 def headers(response):
162 """Return the protocol headers in the response."""
164 return response.headers
165 except(TypeError, AttributeError):
170 """Return the error string in the response."""
172 return response.json()['error']
173 except(TypeError, AttributeError):
177 def normalize(resource_uri):
178 """Remove the first / of /InCSE1/ae1."""
179 if resource_uri is not None:
180 if resource_uri[0] == "/":
181 return resource_uri[1:]
187 """Create the connection."""
189 def __init__(self, server="localhost", base='InCSE1',
190 auth=('admin', 'admin'), protocol="http"):
191 """Connect to a IoTDM server."""
192 self.session = requests.Session()
193 self.session.auth = auth
194 self.session.headers.update({'content-type': 'application/json'})
196 self.payload = cse_payload % (base)
198 # Admittedly these are "magic values" but are required
199 # and until a proper defaulting initializer is in place
201 'content-type': 'application/vnd.onem2m-res+json',
202 'X-M2M-Origin': 'iotdm-robot-tests',
206 self.server = "%s://" % (protocol) + server
207 self.url = self.server + op_provision
208 self.response = self.session.post(
209 self.url, data=self.payload, timeout=self.timeout)
211 def modify_headers_origin(self, new_origin):
212 """Modify the headers to test ACP."""
213 self.headers['X-M2M-Origin'] = new_origin
215 def create(self, parent, restype, attr=None):
216 """Create certain resource with attributes under parent URI.
219 :param parent: the target URI
220 :param restype: the resourceType of the resource
221 :param attr: the payload of the resource
225 restype = int(restype)
226 payload = payload_map[restype]
227 payload = payload % (attr)
228 self.headers['content-type'] = 'application/\
229 vnd.onem2m-res+json;ty=%s' % (restype)
230 parent = normalize(parent)
231 self.url = self.server + ":8282/%s?&rcn=1" % (
233 self.response = self.session.post(
234 self.url, payload, timeout=self.timeout, headers=self.headers)
236 def create_with_command(self, parent, restype,
238 """Create certain resource with attributes under parent URI.
241 :param parent: the target URI
242 :param restype: the resourceType of the resource
243 :param command: the command would be in the URI after &
244 :param attr: the payload of the resource
248 restype = int(restype)
249 payload = payload_map[restype]
250 payload = payload % (attr)
251 self.headers['content-type'] = 'application/\
252 vnd.onem2m-res+json;ty=%s' % (restype)
253 parent = normalize(parent)
254 self.url = self.server + ":8282/%s?%s" % (
256 self.response = self.session.post(
257 self.url, payload, timeout=self.timeout, headers=self.headers)
259 def retrieve(self, resource_uri):
260 """Retrieve resource using resource_uri."""
261 if resource_uri is None:
263 resource_uri = normalize(resource_uri)
264 self.url = self.server + ":8282/%s?rcn=5" % (resource_uri)
265 self.headers['X-M2M-NM'] = None
266 self.headers['content-type'] = 'application/vnd.onem2m-res+json'
267 self.response = self.session.get(
268 self.url, timeout=self.timeout, headers=self.headers
271 def retrieve_with_command(self, resource_uri, command):
272 """Retrieve resource using resource_uri with command."""
273 if resource_uri is None:
277 resource_uri = normalize(resource_uri)
278 self.url = self.server + ":8282/%s?%s" % (resource_uri, command)
279 self.headers['X-M2M-NM'] = None
280 self.headers['content-type'] = 'application/vnd.onem2m-res+json'
281 self.response = self.session.get(
282 self.url, timeout=self.timeout, headers=self.headers
285 def update(self, resource_uri, restype, attr=None):
286 """Update resource at resource_uri with new attributes."""
287 if resource_uri is None:
289 resource_uri = normalize(resource_uri)
290 restype = int(restype)
291 payload = payload_map[restype]
292 payload = payload % (attr)
293 self.headers['content-type'] = 'application/vnd.onem2m-res+json'
294 self.url = self.server + ":8282/%s" % (resource_uri)
295 self.response = self.session.put(
296 self.url, payload, timeout=self.timeout, headers=self.headers)
298 def update_with_command(self, resource_uri, restype,
300 """Update resource at resource_uri with new attributes."""
301 if resource_uri is None:
303 resource_uri = normalize(resource_uri)
304 restype = int(restype)
305 payload = payload_map[restype]
306 payload = payload % (attr)
307 self.headers['content-type'] = 'application/vnd.onem2m-res+json'
308 self.url = self.server + ":8282/%s?%s" % (resource_uri, command)
309 self.response = self.session.put(
310 self.url, payload, timeout=self.timeout, headers=self.headers)
312 def delete(self, resource_uri):
313 """Delete the resource at the resource_uri."""
314 if resource_uri is None:
316 resource_uri = normalize(resource_uri)
317 self.url = self.server + ":8282/%s" % (resource_uri)
318 self.headers['X-M2M-NM'] = None
319 self.headers['content-type'] = 'application/vnd.onem2m-res+json'
320 self.response = self.session.delete(self.url, timeout=self.timeout,
321 headers=self.headers)
323 def delete_with_command(self, resource_uri, command):
324 """Delete the resource at the resource_uri."""
325 if resource_uri is None:
327 resource_uri = normalize(resource_uri)
328 self.url = self.server + ":8282/%s?%s" % (resource_uri, command)
329 self.headers['X-M2M-NM'] = None
330 self.headers['content-type'] = 'application/vnd.onem2m-res+json'
331 self.response = self.session.delete(self.url, timeout=self.timeout,
332 headers=self.headers)
335 """Get the resource tree."""
336 self.url = self.server + op_tree
337 self.response = self.session.get(self.url)
341 self.url = self.server + op_cleanup
342 self.response = self.session.post(self.url)