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 |
1 |
""" |
18 |
|
OSM v1 client API |
19 |
|
""" |
20 |
|
|
21 |
1 |
from osmclient.v1 import vnf |
22 |
1 |
from osmclient.v1 import vnfd |
23 |
1 |
from osmclient.v1 import ns |
24 |
1 |
from osmclient.v1 import nsd |
25 |
1 |
from osmclient.v1 import vim |
26 |
1 |
from osmclient.v1 import package |
27 |
1 |
from osmclient.v1 import vca |
28 |
1 |
from osmclient.v1 import utils |
29 |
1 |
from osmclient.common import http |
30 |
1 |
from osmclient.common import package_tool |
31 |
|
|
32 |
|
|
33 |
1 |
class Client(object): |
34 |
|
|
35 |
1 |
def __init__( |
36 |
|
self, |
37 |
|
host=None, |
38 |
|
so_port=8008, |
39 |
|
so_project='default', |
40 |
|
ro_host=None, |
41 |
|
ro_port=9090, |
42 |
|
upload_port=8443, |
43 |
|
**kwargs): |
44 |
1 |
self._user = 'admin' |
45 |
1 |
self._password = 'admin' |
46 |
|
|
47 |
1 |
if len(host.split(':')) > 1: |
48 |
|
# backwards compatible, port provided as part of host |
49 |
0 |
self._host = host.split(':')[0] |
50 |
0 |
self._so_port = host.split(':')[1] |
51 |
|
else: |
52 |
1 |
self._host = host |
53 |
1 |
self._so_port = so_port |
54 |
|
|
55 |
1 |
self._so_project = so_project |
56 |
|
|
57 |
1 |
http_client = http.Http( |
58 |
|
'https://{}:{}/'.format( |
59 |
|
self._host, |
60 |
|
self._so_port)) |
61 |
1 |
http_client.set_http_header( |
62 |
|
['Accept: application/vnd.yand.data+json', |
63 |
|
'Content-Type: application/json']) |
64 |
|
|
65 |
1 |
self._so_version = self.get_so_version(http_client) |
66 |
|
|
67 |
1 |
if ro_host is None: |
68 |
1 |
ro_host = host |
69 |
1 |
ro_http_client = http.Http('http://{}:{}/'.format(ro_host, ro_port)) |
70 |
1 |
ro_http_client.set_http_header( |
71 |
|
['Accept: application/vnd.yand.data+json', |
72 |
|
'Content-Type: application/json']) |
73 |
|
|
74 |
1 |
upload_client_url = 'https://{}:{}/composer/upload?api_server={}{}'.format( |
75 |
|
self._host, |
76 |
|
upload_port, |
77 |
|
'https://localhost&upload_server=https://', |
78 |
|
self._host) |
79 |
|
|
80 |
1 |
if self._so_version == 'v3': |
81 |
0 |
upload_client_url = 'https://{}:{}/composer/upload?api_server={}{}&project_name={}'.format( |
82 |
|
self._host, |
83 |
|
upload_port, |
84 |
|
'https://localhost&upload_server=https://', |
85 |
|
self._host, |
86 |
|
self._so_project) |
87 |
|
|
88 |
1 |
upload_client = http.Http(upload_client_url) |
89 |
|
|
90 |
1 |
self.vnf = vnf.Vnf(http_client, client=self, **kwargs) |
91 |
1 |
self.vnfd = vnfd.Vnfd(http_client, client=self, **kwargs) |
92 |
1 |
self.ns = ns.Ns(http=http_client, client=self, **kwargs) |
93 |
1 |
self.nsd = nsd.Nsd(http_client, client=self, **kwargs) |
94 |
1 |
self.vim = vim.Vim( |
95 |
|
http=http_client, |
96 |
|
ro_http=ro_http_client, |
97 |
|
client=self, |
98 |
|
**kwargs) |
99 |
1 |
self.package = package.Package( |
100 |
|
http=http_client, |
101 |
|
upload_http=upload_client, |
102 |
|
client=self, |
103 |
|
**kwargs) |
104 |
1 |
self.vca = vca.Vca(http_client, client=self, **kwargs) |
105 |
1 |
self.utils = utils.Utils(http_client, **kwargs) |
106 |
1 |
self.package_tool = package_tool.PackageTool(client=self) |
107 |
|
|
108 |
1 |
@property |
109 |
|
def so_rbac_project_path(self): |
110 |
1 |
if self._so_version == 'v3': |
111 |
0 |
return 'project/{}/'.format(self._so_project) |
112 |
|
else: |
113 |
1 |
return '' |
114 |
|
|
115 |
1 |
def get_so_version(self, http_client): |
116 |
1 |
try: |
117 |
1 |
resp = http_client.get_cmd('api/operational/version') |
118 |
0 |
if not resp or 'rw-base:version' not in resp: |
119 |
0 |
return 'v2' |
120 |
|
|
121 |
0 |
if resp['rw-base:version']['version'].split('.')[0] == '5': |
122 |
|
# SO Version 5.x.x.x.x translates to OSM V3 |
123 |
0 |
return 'v3' |
124 |
0 |
return 'v2' |
125 |
1 |
except Exception: |
126 |
1 |
return 'v2' |
127 |
|
|
128 |
|
|