support of PDUs
[osm/osmclient.git] / osmclient / sol005 / pdud.py
1 # Copyright 2018 Telefonica
2 #
3 # All Rights Reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
15 # under the License.
16
17 """
18 OSM pdud API handling
19 """
20
21 from osmclient.common.exceptions import NotFound
22 from osmclient.common.exceptions import ClientException
23 from osmclient.common import utils
24 import json
25
26
27 class Pdu(object):
28
29 def __init__(self, http=None, client=None):
30 self._http = http
31 self._client = client
32 self._apiName = '/pdu'
33 self._apiVersion = '/v1'
34 self._apiResource = '/pdu_descriptors'
35 self._apiBase = '{}{}{}'.format(self._apiName,
36 self._apiVersion, self._apiResource)
37
38 def list(self, filter=None):
39 filter_string = ''
40 if filter:
41 filter_string = '?{}'.format(filter)
42 resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string))
43 if resp:
44 return resp
45 return list()
46
47 def get(self, name):
48 if utils.validate_uuid4(name):
49 for pdud in self.list():
50 if name == pdud['_id']:
51 return pdud
52 else:
53 for pdud in self.list():
54 if 'name' in pdud and name == pdud['name']:
55 return pdud
56 raise NotFound("pdud {} not found".format(name))
57
58 def get_individual(self, name):
59 pdud = self.get(name)
60 # It is redundant, since the previous one already gets the whole pdudInfo
61 # The only difference is that a different primitive is exercised
62 resp = self._http.get_cmd('{}/{}'.format(self._apiBase, pdud['_id']))
63 #print yaml.safe_dump(resp)
64 if resp:
65 return resp
66 raise NotFound("pdu {} not found".format(name))
67
68 def delete(self, name, force=False):
69 pdud = self.get(name)
70 querystring = ''
71 if force:
72 querystring = '?FORCE=True'
73 http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase,
74 pdud['_id'], querystring))
75 #print 'HTTP CODE: {}'.format(http_code)
76 #print 'RESP: {}'.format(resp)
77 if http_code == 202:
78 print('Deletion in progress')
79 elif http_code == 204:
80 print('Deleted')
81 else:
82 msg = ""
83 if resp:
84 try:
85 msg = json.loads(resp)
86 except ValueError:
87 msg = resp
88 raise ClientException("failed to delete pdu {} - {}".format(name, msg))
89
90 def create(self, pdu, update_endpoint=None):
91 headers= self._client._headers
92 headers['Content-Type'] = 'text/plain'
93 http_header = ['{}: {}'.format(key,val)
94 for (key,val) in list(headers.items())]
95 self._http.set_http_header(http_header)
96 if update_endpoint:
97 http_code, resp = self._http.put_cmd(endpoint=update_endpoint, postfields_dict=pdu)
98 else:
99 self._apiResource = '/pdu_descriptors_content'
100 self._apiBase = '{}{}{}'.format(self._apiName,
101 self._apiVersion, self._apiResource)
102 endpoint = self._apiBase
103 #endpoint = '{}{}'.format(self._apiBase,ow_string)
104 http_code, resp = self._http.post_cmd(endpoint=endpoint, postfields_dict=pdu)
105 #print 'HTTP CODE: {}'.format(http_code)
106 #print 'RESP: {}'.format(resp)
107 if http_code in (200, 201, 202, 204):
108 if resp:
109 resp = json.loads(resp)
110 if not resp or 'id' not in resp:
111 raise ClientException('unexpected response from server: '.format(
112 resp))
113 print(resp['id'])
114 else:
115 msg = "Error {}".format(http_code)
116 if resp:
117 try:
118 msg = "{} - {}".format(msg, json.loads(resp))
119 except ValueError:
120 msg = "{} - {}".format(msg, resp)
121 raise ClientException("failed to create/update pdu - {}".format(msg))
122
123 def update(self, name, filename):
124 pdud = self.get(name)
125 endpoint = '{}/{}'.format(self._apiBase, pdud['_id'])
126 self.create(filename=filename, update_endpoint=endpoint)
127