4483836d63054e387f356b4e064564a6b77b3c43
[vfc/nfvo/lcm.git] / lcm / pub / msapi / aai.py
1 # Copyright 2017 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import json
16 import logging
17 import uuid
18
19 from lcm.pub.exceptions import NSLCMException
20 from lcm.pub.utils import restcall
21 from lcm.pub.config.config import AAI_BASE_URL, AAI_USER, AAI_PASSWD
22
23 logger = logging.getLogger(__name__)
24
25
26 def call_aai(resource, method, content=''):
27     additional_headers = {
28         'X-FromAppId': 'VFC-NFVO-LCM',
29         'X-TransactionId': str(uuid.uuid1())
30     }
31     return restcall.call_req(base_url=AAI_BASE_URL, 
32         user=AAI_USER, 
33         passwd=AAI_PASSWD, 
34         auth_type=restcall.rest_no_auth, 
35         resource=resource, 
36         method=method, 
37         content=content,
38         additional_headers=additional_headers)
39
40 def create_ns_aai(global_customer_id, service_type, service_instance_id, data):
41     resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
42                "%s/service-instances/service-instance/%s" % \
43                (global_customer_id, service_type, service_instance_id)
44     ret = call_aai(resource, "PUT", data)
45     if ret[0] != 0:
46         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
47         raise NSLCMException("Ns instance creation exception in AAI")
48     return json.JSONDecoder().decode(ret[1])
49
50 def delete_ns_aai(global_customer_id, service_type, service_instance_id, data):
51     resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
52                "%s/service-instances/service-instance/%s" % \
53                (global_customer_id, service_type, service_instance_id)
54     ret = call_aai(resource, "DELETE", data)
55     if ret[0] != 0:
56         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
57         raise NSLCMException("Ns instance delete exception in AAI")
58     return json.JSONDecoder().decode(ret[1])
59
60 def query_ns_aai(global_customer_id, service_type, service_instance_id, data):
61     resource = "/business/customers/customer/%s/service-subscriptions/service-subscription/" \
62                "%s/service-instances/service-instance/%s" % \
63                (global_customer_id, service_type, service_instance_id)
64     ret = call_aai(resource, "GET", data)
65     if ret[0] != 0:
66         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
67         raise NSLCMException("Ns instance query exception in AAI")
68     return json.JSONDecoder().decode(ret[1])
69