1 # Copyright 2018 Telefonica
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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
18 OSM SDN controller API handling
21 from osmclient
.common
import utils
22 from osmclient
.common
.exceptions
import ClientException
23 from osmclient
.common
.exceptions
import NotFound
27 class SdnController(object):
28 def __init__(self
, http
=None, client
=None):
31 self
._apiName
= '/admin'
32 self
._apiVersion
= '/v1'
33 self
._apiResource
= '/sdn_controllers'
34 self
._apiBase
= '{}{}{}'.format(self
._apiName
,
35 self
._apiVersion
, self
._apiResource
)
36 def create(self
, name
, sdn_controller
):
37 if 'type' not in vim_access
:
38 raise Exception("type not provided")
40 resp
= self
._http
.post_cmd(endpoint
=self
._apiBase
,
41 postfields_dict
=sdn_controller
)
42 if not resp
or '_id' not in resp
:
43 raise ClientException('failed to create SDN controller: '.format(
48 def delete(self
, name
):
49 sdn_controller
= self
.get(name
)
50 resp
= self
._http
.delete_cmd('{}/{}'.format(self
._apiBase
,sdn_controller
['_id']))
51 #print 'RESP: '.format(resp)
52 if 'result' not in resp
:
53 raise ClientException("failed to delete vim {} - {}".format(name
, resp
))
57 def list(self
, filter=None):
58 """Returns a list of SDN controllers
62 filter_string
= '?{}'.format(filter)
63 resp
= self
._http
.get_cmd('{}{}'.format(self
._apiBase
,filter_string
))
69 """Returns an SDN controller based on name or id
71 if utils
.validate_uuid4(name
):
72 for sdnc
in self
.list():
73 if name
== sdnc
['_id']:
76 for sdnc
in self
.list():
77 if name
== sdnc
['name']:
79 raise NotFound("SDN controller {} not found".format(name
))