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"}
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):
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")
82 return dic.get(key1[0], None).get(key, None)
86 """Return the resource name in the response."""
87 return find_key(response, "rn")
90 def lastModifiedTime(response):
91 """Return the lastModifiedTime in the response."""
92 return find_key(response, "lt")
96 """Return the resource id in the response."""
97 return find_key(response, "ri")
100 def parent(response):
101 """Return the parent resource id in the response."""
102 return find_key(response, "pi")
105 def content(response):
106 """Return the content the response."""
107 return find_key(response, "con")
110 def restype(response):
111 """Return the resource type the response."""
112 return find_key(response, "rty")
115 def status(response):
116 """Return the protocol status code in the response."""
118 return response.status_code
119 except(TypeError, AttributeError):
123 def headers(response):
124 """Return the protocol headers in the response."""
126 return response.headers
127 except(TypeError, AttributeError):
132 """Return the error string in the response."""
134 return response.json()['error']
135 except(TypeError, AttributeError):
139 def normalize(resource_uri):
140 """Remove the first / of /InCSE1/ae1."""
141 if resource_uri is not None:
142 if resource_uri[0] == "/":
143 return resource_uri[1:]
149 """Create the connection."""
151 def __init__(self, server="localhost", base='InCSE1',
152 auth=('admin', 'admin'), protocol="http"):
153 """Connect to a IoTDM server."""
154 self.session = requests.Session()
155 self.session.auth = auth
156 self.session.headers.update({'content-type': 'application/json'})
158 self.payload = cse_payload % (base)
160 # Admittedly these are "magic values" but are required
161 # and until a proper defaulting initializer is in place
163 'content-type': 'application/vnd.onem2m-res+json',
164 'X-M2M-Origin': '//localhost:10000',
168 self.server = "%s://" % (protocol) + server
169 self.url = self.server + op_provision
170 self.response = self.session.post(
171 self.url, data=self.payload, timeout=self.timeout)
173 def modify_headers_origin(self, new_origin):
174 """Modify the headers to test ACP."""
175 self.headers['X-M2M-Origin'] = new_origin
177 def create(self, parent, restype, attr=None):
178 """Create certain resource with attributes under parent URI.
181 :param parent: the target URI
182 :param restype: the resourceType of the resource
183 :param attr: the payload of the resource
187 restype = int(restype)
188 payload = payload_map[restype]
189 payload = payload % (attr)
190 self.headers['content-type'] = 'application/\
191 vnd.onem2m-res+json;ty=%s' % (restype)
192 parent = normalize(parent)
193 self.url = self.server + ":8282/%s?&rcn=1" % (
195 self.response = self.session.post(
196 self.url, payload, timeout=self.timeout, headers=self.headers)
198 def create_with_command(self, parent, restype,
200 """Create certain resource with attributes under parent URI.
203 :param parent: the target URI
204 :param restype: the resourceType of the resource
205 :param command: the command would be in the URI after &
206 :param attr: the payload of the resource
210 restype = int(restype)
211 payload = payload_map[restype]
212 payload = payload % (attr)
213 self.headers['content-type'] = 'application/\
214 vnd.onem2m-res+json;ty=%s' % (restype)
215 parent = normalize(parent)
216 self.url = self.server + ":8282/%s?%s" % (
218 self.response = self.session.post(
219 self.url, payload, timeout=self.timeout, headers=self.headers)
221 def retrieve(self, resource_uri):
222 """Retrieve resource using resource_uri."""
223 if resource_uri is None:
225 resource_uri = normalize(resource_uri)
226 self.url = self.server + ":8282/%s?rcn=5" % (resource_uri)
227 self.headers['X-M2M-NM'] = None
228 self.headers['content-type'] = 'application/vnd.onem2m-res+json'
229 self.response = self.session.get(
230 self.url, timeout=self.timeout, headers=self.headers
233 def retrieve_with_command(self, resource_uri, command):
234 """Retrieve resource using resource_uri with command."""
235 if resource_uri is None:
239 resource_uri = normalize(resource_uri)
240 self.url = self.server + ":8282/%s?%s" % (resource_uri, command)
241 self.headers['X-M2M-NM'] = None
242 self.headers['content-type'] = 'application/vnd.onem2m-res+json'
243 self.response = self.session.get(
244 self.url, timeout=self.timeout, headers=self.headers
247 def update(self, resource_uri, restype, attr=None):
248 """Update resource at resource_uri with new attributes."""
249 if resource_uri is None:
251 resource_uri = normalize(resource_uri)
252 restype = int(restype)
253 payload = payload_map[restype]
254 payload = payload % (attr)
255 self.headers['content-type'] = 'application/vnd.onem2m-res+json'
256 self.url = self.server + ":8282/%s" % (resource_uri)
257 self.response = self.session.put(
258 self.url, payload, timeout=self.timeout, headers=self.headers)
260 def update_with_command(self, resource_uri, restype,
262 """Update resource at resource_uri with new attributes."""
263 if resource_uri is None:
265 resource_uri = normalize(resource_uri)
266 restype = int(restype)
267 payload = payload_map[restype]
268 payload = payload % (attr)
269 self.headers['content-type'] = 'application/vnd.onem2m-res+json'
270 self.url = self.server + ":8282/%s?%s" % (resource_uri, command)
271 self.response = self.session.put(
272 self.url, payload, timeout=self.timeout, headers=self.headers)
274 def delete(self, resource_uri):
275 """Delete the resource at the resource_uri."""
276 if resource_uri is None:
278 resource_uri = normalize(resource_uri)
279 self.url = self.server + ":8282/%s" % (resource_uri)
280 self.headers['X-M2M-NM'] = None
281 self.headers['content-type'] = 'application/vnd.onem2m-res+json'
282 self.response = self.session.delete(self.url, timeout=self.timeout,
283 headers=self.headers)
285 def delete_with_command(self, resource_uri, command):
286 """Delete the resource at the resource_uri."""
287 if resource_uri is None:
289 resource_uri = normalize(resource_uri)
290 self.url = self.server + ":8282/%s?%s" % (resource_uri, command)
291 self.headers['X-M2M-NM'] = None
292 self.headers['content-type'] = 'application/vnd.onem2m-res+json'
293 self.response = self.session.delete(self.url, timeout=self.timeout,
294 headers=self.headers)
297 """Get the resource tree."""
298 self.url = self.server + op_tree
299 self.response = self.session.get(self.url)
303 self.url = self.server + op_cleanup
304 self.response = self.session.post(self.url)