Add lcm vnf instance operate
[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
70 def create_vnf_aai(vnf_id, data):
71     resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id
72     ret = call_aai(resource, "PUT", data)
73     if ret[0] != 0:
74         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
75         raise NSLCMException("Vnf instance creation exception in AAI")
76     return json.JSONDecoder().decode(ret[1])
77
78 def delete_vnf_aai(vnf_id, data=''):
79     resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id
80     ret = call_aai(resource, "DELETE", data)
81     if ret[0] != 0:
82         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
83         raise NSLCMException("Vnf instance delete exception in AAI")
84     return json.JSONDecoder().decode(ret[1])
85
86 def query_vnf_aai(vnf_id, data):
87     resource = "/network/generic-vnfs/generic-vnf/%s" % vnf_id
88     ret = call_aai(resource, "GET", data)
89     if ret[0] != 0:
90         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
91         raise NSLCMException("Vnf instance query exception in AAI")
92     return json.JSONDecoder().decode(ret[1])