b7ac856e6620fc9581f8036ce9062332840f2d5b
[osm/osmclient.git] / osmclient / sol005 / vnf.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 vnf API handling
19 """
20
21 from osmclient.common import utils
22 from osmclient.common.exceptions import NotFound
23
24
25 class Vnf(object):
26
27 def __init__(self, http=None, client=None):
28 self._http = http
29 self._client = client
30 self._apiName = '/nslcm'
31 self._apiVersion = '/v1'
32 self._apiResource = '/vnfrs'
33 self._apiBase = '{}{}{}'.format(self._apiName,
34 self._apiVersion, self._apiResource)
35
36 def list(self, ns=None, filter=None):
37 """Returns a list of VNF instances
38 """
39 self._client.get_token()
40 filter_string = ''
41 if filter:
42 filter_string = '?{}'.format(filter)
43 if ns:
44 ns_instance = self._client.ns.get(ns)
45 if filter_string:
46 filter_string += ',nsr-id-ref={}'.format(ns_instance['_id'])
47 else:
48 filter_string = '?nsr-id-ref={}'.format(ns_instance['_id'])
49 resp = self._http.get_cmd('{}{}'.format(self._apiBase,filter_string))
50 #print('RESP: {}'.format(resp))
51 if resp:
52 return resp
53 return list()
54
55 def get(self, name):
56 """Returns a VNF instance based on name or id
57 """
58 self._client.get_token()
59 if utils.validate_uuid4(name):
60 for vnf in self.list():
61 if name == vnf['_id']:
62 return vnf
63 else:
64 for vnf in self.list():
65 if name == vnf['name']:
66 return vnf
67 raise NotFound("vnf {} not found".format(name))
68
69 def get_individual(self, name):
70 self._client.get_token()
71 vnf_id = name
72 if not utils.validate_uuid4(name):
73 for vnf in self.list():
74 if name == vnf['name']:
75 vnf_id = vnf['_id']
76 break
77 resp = self._http.get_cmd('{}/{}'.format(self._apiBase, vnf_id))
78 #print('RESP: {}'.format(resp))
79 if resp:
80 return resp
81 raise NotFound("vnf {} not found".format(name))
82