blob: 3de92aca4220a2e0287a37d9593eaa2f1a1e654c [file] [log] [blame]
Matteo Scandoloede125b2017-08-08 13:05:25 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Scott Baker25467ff2016-08-04 09:50:22 -070017import os
18import base64
19import string
20import sys
21import xmlrpclib
22
23if __name__ == '__main__':
24 sys.path.append("/opt/xos")
25 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "xos.settings")
26
27from xos.config import Config
28from core.models import Service
29from services.hpc.models import HpcService
Scott Baker25467ff2016-08-04 09:50:22 -070030from xos.logger import Logger, logging
31
32logger = Logger(level=logging.INFO)
33
34class APIHelper:
35 def __init__(self, proxy, auth, method=None):
36 self.proxy = proxy
37 self.auth = auth
38 self.method = method
39
40 def __getattr__(self, name):
41 if name.startswith("_"):
42 return getattr(self, name)
43 else:
44 return APIHelper(self.proxy, self.auth, name)
45
46 def __call__(self, *args):
47 method = getattr(self.proxy, self.method)
48 return method(self.auth, *args)
49
50class CmiClient:
51 def __init__(self, hostname, port=8003, username="apiuser", password="apiuser"):
52 self.connect_api(hostname, port, username, password)
53
54 def connect_api(self, hostname, port=8003, username="apiuser", password="apiuser"):
55 #print "https://%s:%d/COAPI/" % (hostname, port)
56 cob = xmlrpclib.ServerProxy("https://%s:%d/COAPI/" % (hostname, port), allow_none=True)
57 cob_auth = {}
58 cob_auth["Username"] = username
59 cob_auth["AuthString"] = password
60 cob_auth["AuthMethod"] = "password"
61
62 onev = xmlrpclib.ServerProxy("https://%s:%d/ONEV_API/" % (hostname, port), allow_none=True)
63 onev_auth = {}
64 onev_auth["Username"] = username
65 onev_auth["AuthString"] = password
66 onev_auth["AuthMethod"] = "password"
67
68 self.cob = APIHelper(cob, cob_auth)
69 self.onev = APIHelper(onev, onev_auth)
70
71class HpcLibrary:
72 def __init__(self):
73 self._client = None
74
75 def make_account_name(self, x):
76 x=x.lower()
77 y = ""
78 for c in x:
79 if (c in (string.lowercase + string.digits)):
80 y = y + c
81 return y[:20]
82
83 def get_hpc_service(self):
84 hpc_service_name = getattr(Config(), "observer_hpc_service", None)
85 if hpc_service_name:
86 hpc_service = HpcService.objects.filter(name = hpc_service_name)
87 else:
88 hpc_service = HpcService.objects.all()
89
90 if not hpc_service:
91 if hpc_service_name:
92 raise Exception("No HPC Service with name %s" % hpc_service_name)
93 else:
94 raise Exception("No HPC Services")
95 hpc_service = hpc_service[0]
96
97 return hpc_service
98
99 def get_cmi_hostname(self, hpc_service=None):
100 if getattr(Config(),"observer_cmi_hostname",None):
101 return getattr(Config(),"observer_cmi_hostname")
102
103 if (hpc_service is None):
104 hpc_service = self.get_hpc_service()
105
106 if hpc_service.cmi_hostname:
107 return hpc_service.cmi_hostname
108
109 try:
110 slices = hpc_service.slices.all()
111 except:
112 # deal with buggy data model
113 slices = hpc_service.service.all()
114
115 for slice in slices:
116 if slice.name.endswith("cmi"):
117 for instance in slice.instances.all():
118 if instance.node:
119 return instance.node.name
120
121 raise Exception("Failed to find a CMI instance")
122
123 @property
124 def client(self):
125 if self._client is None:
126 self._client = CmiClient(self.get_cmi_hostname())
127 return self._client
128
129if __name__ == '__main__':
130 import django
131 django.setup()
132
133 lib = HpcLibrary()
134
135 print "testing API connection to", lib.get_cmi_hostname()
136 lib.client.cob.GetNewObjects()
137 lib.client.onev.ListAll("CDN")
138
139
140
141