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