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