restructure osmclient
[osm/osmclient.git] / osmclient / v1 / ns.py
1 # Copyright 2017 Sandvine
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 ns 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 uuid
25 import pprint
26
27
28 class Ns(object):
29 def __init__(self,http=None,client=None):
30 self._http=http
31 self._client=client
32
33 def list(self):
34 """Returns a list of ns's
35 """
36 resp = self._http.get_cmd('api/running/ns-instance-config')
37
38 if not resp or 'nsr:ns-instance-config' not in resp:
39 return list()
40
41 if 'nsr' not in resp['nsr:ns-instance-config']:
42 return list()
43
44 return resp['nsr:ns-instance-config']['nsr']
45
46 def get(self,name):
47 """Returns an ns based on name
48 """
49 for ns in self.list():
50 if name == ns['name']:
51 return ns
52 raise NotFound("ns {} not found".format(name))
53
54 def create(self,nsd_name,nsr_name,account,vim_network_prefix=None,ssh_keys=None,description='default description',admin_status='ENABLED'):
55 postdata={}
56 postdata['nsr'] = list()
57 nsr={}
58 nsr['id']=str(uuid.uuid1())
59
60 nsd=self._client.nsd.get(nsd_name)
61
62 datacenter=self._client.vim.get_datacenter(account)
63 if datacenter is None:
64 raise NotFound("cannot find datacenter account {}".format(account))
65
66 nsr['nsd']=nsd
67 nsr['name']=nsr_name
68 nsr['short-name']=nsr_name
69 nsr['description']=description
70 nsr['admin-status']=admin_status
71 nsr['om-datacenter']=datacenter['uuid']
72
73 if ssh_keys is not None:
74 # ssh_keys is comma separate list
75 ssh_keys_format=[]
76 for key in ssh_keys.split(','):
77 ssh_keys_format.append({'key-pair-ref':key})
78
79 nsr['ssh-authorized-key']=ssh_keys_format
80
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)
85
86 postdata['nsr'].append(nsr)
87
88 resp = self._http.post_cmd('api/config/ns-instance-config/nsr',postdata)
89
90 if 'success' not in resp:
91 raise ClientException("failed to create ns: {} nsd: {} result: {}".format(nsr_name,nsd_name,resp))
92
93 def get_opdata(self,id):
94 return self._http.get_cmd('api/operational/ns-instance-opdata/nsr/{}?deep'.format(id))
95
96 def get_field(self,ns_name,field):
97 nsr=self.get(ns_name)
98 if nsr is None:
99 raise NotFound("failed to retrieve ns {}".format(ns_name))
100
101 if field in nsr:
102 return nsr[field]
103
104 nsopdata=self.get_opdata(nsr['id'])
105
106 if field in nsopdata['nsr:nsr']:
107 return nsopdata['nsr:nsr'][field]
108
109 raise NotFound("failed to find {} in ns {}".format(field,ns_name))
110
111 def _terminate(self,ns_name):
112 ns=self.get(ns_name)
113 if ns is None:
114 raise NotFound("cannot find ns {}".format(ns_name))
115
116 return self._http.delete_cmd('api/config/ns-instance-config/nsr/'+ns['id'])
117
118 def delete(self,ns_name,wait=True):
119 vnfs = self.get_field(ns_name,'constituent-vnfr-ref')
120
121 resp=self._terminate(ns_name)
122 if 'success' not in resp:
123 raise ClientException("failed to delete ns {}".format(name))
124
125 # helper method to check if pkg exists
126 def check_not_exists( func ):
127 try:
128 func()
129 return False
130 except NotFound:
131 return True
132
133 for vnf in vnfs:
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))
138
139 def get_monitoring(self,ns_name):
140 ns=self.get(ns_name)
141 mon_list={}
142 if ns is None:
143 return mon_list
144
145 vnfs=self._client.vnf.list()
146 for vnf in vnfs:
147 if ns['id'] == vnf['nsr-id-ref']:
148 if 'monitoring-param' in vnf:
149 mon_list[vnf['name']] = vnf['monitoring-param']
150
151 return mon_list