1 # Copyright 2017 Sandvine
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
21 from osmclient
.common
import utils
22 from osmclient
.common
.exceptions
import ClientException
23 from osmclient
.common
.exceptions
import NotFound
29 def __init__(self
,http
=None,client
=None):
34 """Returns a list of ns's
36 resp
= self
._http
.get_cmd('api/running/ns-instance-config')
38 if not resp
or 'nsr:ns-instance-config' not in resp
:
41 if 'nsr' not in resp
['nsr:ns-instance-config']:
44 return resp
['nsr:ns-instance-config']['nsr']
47 """Returns an ns based on name
49 for ns
in self
.list():
50 if name
== ns
['name']:
52 raise NotFound("ns {} not found".format(name
))
54 def create(self
,nsd_name
,nsr_name
,account
,vim_network_prefix
=None,ssh_keys
=None,description
='default description',admin_status
='ENABLED'):
56 postdata
['nsr'] = list()
58 nsr
['id']=str(uuid
.uuid1())
60 nsd
=self
._client
.nsd
.get(nsd_name
)
62 datacenter
=self
._client
.vim
.get_datacenter(account
)
63 if datacenter
is None:
64 raise NotFound("cannot find datacenter account {}".format(account
))
68 nsr
['short-name']=nsr_name
69 nsr
['description']=description
70 nsr
['admin-status']=admin_status
71 nsr
['om-datacenter']=datacenter
['uuid']
73 if ssh_keys
is not None:
74 # ssh_keys is comma separate list
76 for key
in ssh_keys
.split(','):
77 ssh_keys_format
.append({'key-pair-ref':key
})
79 nsr
['ssh-authorized-key']=ssh_keys_format
81 if vim_network_prefix
is not None:
82 for index
,vld
in enumerate(nsr
['nsd']['vld']):
83 network_name
= vld
['name']
84 nsr
['nsd']['vld'][index
]['vim-network-name'] = '{}-{}'.format(vim_network_prefix
,network_name
)
86 postdata
['nsr'].append(nsr
)
88 resp
= self
._http
.post_cmd('api/config/ns-instance-config/nsr',postdata
)
90 if 'success' not in resp
:
91 raise ClientException("failed to create ns: {} nsd: {} result: {}".format(nsr_name
,nsd_name
,resp
))
93 def get_opdata(self
,id):
94 return self
._http
.get_cmd('api/operational/ns-instance-opdata/nsr/{}?deep'.format(id))
96 def get_field(self
,ns_name
,field
):
99 raise NotFound("failed to retrieve ns {}".format(ns_name
))
104 nsopdata
=self
.get_opdata(nsr
['id'])
106 if field
in nsopdata
['nsr:nsr']:
107 return nsopdata
['nsr:nsr'][field
]
109 raise NotFound("failed to find {} in ns {}".format(field
,ns_name
))
111 def _terminate(self
,ns_name
):
114 raise NotFound("cannot find ns {}".format(ns_name
))
116 return self
._http
.delete_cmd('api/config/ns-instance-config/nsr/'+ns
['id'])
118 def delete(self
,ns_name
,wait
=True):
119 vnfs
= self
.get_field(ns_name
,'constituent-vnfr-ref')
121 resp
=self
._terminate
(ns_name
)
122 if 'success' not in resp
:
123 raise ClientException("failed to delete ns {}".format(name
))
125 # helper method to check if pkg exists
126 def check_not_exists( func
):
134 if not utils
.wait_for_value(lambda: check_not_exists(lambda: self
._client
.vnf
.get(vnf
['vnfr-id']))):
135 raise ClientException("vnf {} failed to delete".format(vnf
['vnfr-id']))
136 if not utils
.wait_for_value(lambda: check_not_exists(lambda: self
.get(ns_name
))):
137 raise ClientException("ns {} failed to delete".format(ns_name
))
139 def get_monitoring(self
,ns_name
):
145 vnfs
=self
._client
.vnf
.list()
147 if ns
['id'] == vnf
['nsr-id-ref']:
148 if 'monitoring-param' in vnf
:
149 mon_list
[vnf
['name']] = vnf
['monitoring-param']