Code Coverage

Cobertura Coverage Report > osmclient.sol005 >

vnf.py

Trend

Classes100%
 
Lines18%
   
Conditionals100%
 

File Coverage summary

NameClassesLinesConditionals
vnf.py
100%
1/1
18%
10/56
100%
0/0

Coverage Breakdown by Class

NameLinesConditionals
vnf.py
18%
10/56
N/A

Source

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