Improvement: Return more states in the 'detailed-status' field - client side
[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 import wait as WaitForStatus
23 from osmclient.common.exceptions import ClientException
24 from osmclient.common.exceptions import NotFound
25 import json
26
27
28 class SdnController(object):
29 def __init__(self, http=None, client=None):
30 self._http = http
31 self._client = client
32 self._apiName = '/admin'
33 self._apiVersion = '/v1'
34 self._apiResource = '/sdns'
35 self._apiBase = '{}{}{}'.format(self._apiName,
36 self._apiVersion, self._apiResource)
37
38 # SDNC '--wait' option
39 def _wait(self, id, deleteFlag=False):
40 # Endpoint to get operation status
41 apiUrlStatus = '{}{}{}'.format(self._apiName, self._apiVersion, '/sdns')
42 # Wait for status for SDN instance creation/update/deletion
43 WaitForStatus.wait_for_status(
44 'SDNC',
45 str(id),
46 WaitForStatus.TIMEOUT_SDNC_OPERATION,
47 apiUrlStatus,
48 self._http.get2_cmd,
49 deleteFlag=deleteFlag)
50
51 def _get_id_for_wait(self, name):
52 # Returns id of name, or the id itself if given as argument
53 for sdnc in self.list():
54 if name == sdnc['_id']:
55 return sdnc['_id']
56 for sdnc in self.list():
57 if name == sdnc['name']:
58 return sdnc['_id']
59 return ''
60
61 def create(self, name, sdn_controller, wait=False):
62 http_code, resp = self._http.post_cmd(endpoint=self._apiBase,
63 postfields_dict=sdn_controller)
64 #print 'HTTP CODE: {}'.format(http_code)
65 #print 'RESP: {}'.format(resp)
66 if http_code in (200, 201, 202, 204):
67 if resp:
68 resp = json.loads(resp)
69 if not resp or 'id' not in resp:
70 raise ClientException('unexpected response from server - {}'.format(
71 resp))
72 if wait:
73 # Wait for status for SDNC instance creation
74 self._wait(resp.get('id'))
75 print(resp['id'])
76 else:
77 msg = ""
78 if resp:
79 try:
80 msg = json.loads(resp)
81 except ValueError:
82 msg = resp
83 raise ClientException("failed to create SDN controller {} - {}".format(name, msg))
84
85 def update(self, name, sdn_controller, wait=False):
86 sdnc = self.get(name)
87 sdnc_id_for_wait = self._get_id_for_wait(name)
88 http_code, resp = self._http.put_cmd(endpoint='{}/{}'.format(self._apiBase,sdnc['_id']),
89 postfields_dict=sdn_controller)
90 # print 'HTTP CODE: {}'.format(http_code)
91 # print 'RESP: {}'.format(resp)
92 if http_code in (200, 201, 202, 204):
93 if wait:
94 # In this case, 'resp' always returns None, so 'resp['id']' cannot be used.
95 # Use the previously obtained id instead.
96 wait_id = sdnc_id_for_wait
97 # Wait for status for VI instance update
98 self._wait(wait_id)
99 else:
100 pass
101 else:
102 msg = ""
103 if resp:
104 try:
105 msg = json.loads(resp)
106 except ValueError:
107 msg = resp
108 raise ClientException("failed to update SDN controller {} - {}".format(name, msg))
109
110 def delete(self, name, force=False, wait=False):
111 sdn_controller = self.get(name)
112 sdnc_id_for_wait = self._get_id_for_wait(name)
113 querystring = ''
114 if force:
115 querystring = '?FORCE=True'
116 http_code, resp = self._http.delete_cmd('{}/{}{}'.format(self._apiBase,
117 sdn_controller['_id'], querystring))
118 #print 'HTTP CODE: {}'.format(http_code)
119 #print 'RESP: {}'.format(resp)
120 if http_code == 202:
121 if wait:
122 # Wait for status for SDNC instance deletion
123 self._wait(sdnc_id_for_wait, deleteFlag=True)
124 else:
125 print('Deletion in progress')
126 elif http_code == 204:
127 print('Deleted')
128 elif resp and 'result' in resp:
129 print('Deleted')
130 else:
131 msg = ""
132 if resp:
133 try:
134 msg = json.loads(resp)
135 except ValueError:
136 msg = resp
137 raise ClientException("failed to delete SDN controller {} - {}".format(name, msg))
138
139 def list(self, filter=None):
140 """Returns a list of SDN controllers
141 """
142 filter_string = ''
143 if filter:
144 filter_string = '?{}'.format(filter)
145 resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string))
146 #print 'RESP: {}'.format(resp)
147 if resp:
148 return resp
149 return list()
150
151 def get(self, name):
152 """Returns an SDN controller based on name or id
153 """
154 if utils.validate_uuid4(name):
155 for sdnc in self.list():
156 if name == sdnc['_id']:
157 return sdnc
158 else:
159 for sdnc in self.list():
160 if name == sdnc['name']:
161 return sdnc
162 raise NotFound("SDN controller {} not found".format(name))
163
164