61299612c11d277971b7bef4be6bf4b6da9f2418
[osm/osmclient.git] / osmclient / sol005 / sdncontroller.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 SDN controller API handling
19 """
20
21 from osmclient.common import utils
22 from osmclient.common.exceptions import ClientException
23 from osmclient.common.exceptions import NotFound
24 import json
25
26
27 class SdnController(object):
28 def __init__(self, http=None, client=None):
29 self._http = http
30 self._client = client
31 self._apiName = '/admin'
32 self._apiVersion = '/v1'
33 self._apiResource = '/sdns'
34 self._apiBase = '{}{}{}'.format(self._apiName,
35 self._apiVersion, self._apiResource)
36 def create(self, name, sdn_controller):
37 http_code, resp = self._http.post_cmd(endpoint=self._apiBase,
38 postfields_dict=sdn_controller)
39 #print 'HTTP CODE: {}'.format(http_code)
40 #print 'RESP: {}'.format(resp)
41 if http_code in (200, 201, 202, 204):
42 if resp:
43 resp = json.loads(resp)
44 if not resp or 'id' not in resp:
45 raise ClientException('unexpected response from server - {}'.format(
46 resp))
47 print(resp['id'])
48 else:
49 msg = ""
50 if resp:
51 try:
52 msg = json.loads(resp)
53 except ValueError:
54 msg = resp
55 raise ClientException("failed to create SDN controller {} - {}".format(name, msg))
56
57 def update(self, name, sdn_controller):
58 sdnc = self.get(name)
59 http_code, resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,sdnc['_id']),
60 postfields_dict=sdn_controller)
61 #print 'HTTP CODE: {}'.format(http_code)
62 #print 'RESP: {}'.format(resp)
63 if http_code in (200, 201, 202, 204):
64 if resp:
65 resp = json.loads(resp)
66 if not resp or 'id' not in resp:
67 raise ClientException('unexpected response from server - {}'.format(
68 resp))
69 print(resp['id'])
70 else:
71 msg = ""
72 if resp:
73 try:
74 msg = json.loads(resp)
75 except ValueError:
76 msg = resp
77 raise ClientException("failed to update SDN controller {} - {}".format(name, msg))
78
79 def delete(self, name, force=False):
80 sdn_controller = self.get(name)
81 querystring = ''
82 if force:
83 querystring = '?FORCE=True'
84 http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase,
85 sdn_controller['_id'], querystring))
86 #print 'HTTP CODE: {}'.format(http_code)
87 #print 'RESP: {}'.format(resp)
88 if http_code == 202:
89 print('Deletion in progress')
90 elif http_code == 204:
91 print('Deleted')
92 elif resp and 'result' in resp:
93 print('Deleted')
94 else:
95 msg = ""
96 if resp:
97 try:
98 msg = json.loads(resp)
99 except ValueError:
100 msg = resp
101 raise ClientException("failed to delete SDN controller {} - {}".format(name, msg))
102
103 def list(self, filter=None):
104 """Returns a list of SDN controllers
105 """
106 filter_string = ''
107 if filter:
108 filter_string = '?{}'.format(filter)
109 resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string))
110 #print 'RESP: {}'.format(resp)
111 if resp:
112 return resp
113 return list()
114
115 def get(self, name):
116 """Returns an SDN controller based on name or id
117 """
118 if utils.validate_uuid4(name):
119 for sdnc in self.list():
120 if name == sdnc['_id']:
121 return sdnc
122 else:
123 for sdnc in self.list():
124 if name == sdnc['name']:
125 return sdnc
126 raise NotFound("SDN controller {} not found".format(name))
127
128