Implement vnflcm call_req_aai function 53/8253/3
authorying.yunlong <ying.yunlong@zte.com.cn>
Tue, 22 Aug 2017 09:12:28 +0000 (17:12 +0800)
committerying.yunlong <ying.yunlong@zte.com.cn>
Tue, 22 Aug 2017 09:26:39 +0000 (17:26 +0800)
As aai rest api is https, add new call_req_aai
function to implement the rest call to aai.

Change-Id: I3154d075af0b8290361eafbec1774743b7f75c3d
Issue-ID: VFC-133
Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
lcm/lcm/nf/vnfs/tests/test_vnf_cancel.py
lcm/lcm/nf/vnfs/tests/test_vnf_create.py
lcm/lcm/nf/vnfs/vnf_cancel/delete_vnf_identifier.py
lcm/lcm/pub/aaiapi/aai.py
lcm/lcm/pub/config/config.py
lcm/lcm/pub/utils/restcall.py

index e4cf7d9..52dbbe3 100644 (file)
@@ -97,7 +97,7 @@ class TestNFTerminate(TestCase):
         TermVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
         self.assert_job_result(self.job_id, 255, "VnfInst(%s) does not exist" % self.nf_inst_id)
     """
-"""
+    """
     @mock.patch.object(restcall, 'call_req')
     @mock.patch.object(api, 'call')
     def test_terminate_vnf_success(self, mock_call, mock_call_req):
@@ -117,4 +117,4 @@ class TestNFTerminate(TestCase):
         JobUtil.add_job_status(self.job_id, 0, "INST_VNF_READY")
         TermVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
         self.assert_job_result(self.job_id, 100, "Terminate Vnf success.")
-"""
+    """
index 9edd607..8b24ee4 100644 (file)
@@ -68,7 +68,7 @@ class TestNFInstantiate(TestCase):
         mock_run.re.return_value = None
         response = self.client.post("/api/vnflcm/v1/vnf_instances/12/instantiate", data={}, format='json')
         self.failUnlessEqual(status.HTTP_202_ACCEPTED, response.status_code)
-"""
+    """
     def test_instantiate_vnf_when_inst_id_not_exist(self):
         self.nf_inst_id = str(uuid.uuid4())
         self.job_id = JobUtil.create_job('NF', 'CREATE', self.nf_inst_id)
@@ -179,5 +179,4 @@ class TestNFInstantiate(TestCase):
         data = inst_req_data
         InstVnf(data, nf_inst_id=self.nf_inst_id, job_id=self.job_id).run()
         self.assert_job_result(self.job_id, 100, "Instantiate Vnf success.")
-
-"""
\ No newline at end of file
+    """
\ No newline at end of file
index 33eb7de..1eedb54 100644 (file)
@@ -13,8 +13,6 @@
 # limitations under the License.
 import logging
 
-from lcm.pub.aaiapi.aai import call_aai
-from lcm.pub.config.config import AAI_BASE_URL
 from lcm.pub.database.models import NfInstModel, NfvoRegInfoModel
 
 logger = logging.getLogger(__name__)
@@ -35,8 +33,3 @@ class DeleteVnf:
         #    raise NFLCMException("Don't allow to delete vnf(status:[%s])" % sel_vnf.status)
         NfInstModel.objects.filter(nfinstid=self.nf_inst_id).delete()
         NfvoRegInfoModel.objects.filter(nfvoid=self.nf_inst_id).delete()
-
-    def delete_data_from_aai(self, req_data):
-        full_url = AAI_BASE_URL + "api/aai-service-design-and-creation/v1/%s" % self.nf_inst_id
-        del_result = call_aai(full_url, "DELETE", req_data)
-        pass
\ No newline at end of file
index a512315..d97f159 100644 (file)
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
 # See the License for the specific language governing permissions and\r
 # limitations under the License.\r
+import logging\r
 \r
-def call_aai(full_url, method, req_data):\r
-    pass
\ No newline at end of file
+logger = logging.getLogger(__name__)\r
+\r
+\r
+def create_ns(ns_id, data):\r
+    pass\r
+\r
+\r
+def create_vnf(vnf_id, data):\r
+    pass\r
+\r
+\r
+def create_vserver(cloud_owner, cloud_region_id, tenant_id, vserver_id, data):\r
+    pass\r
index 142a4a6..492595d 100644 (file)
@@ -20,7 +20,12 @@ MSB_SERVICE_PORT = '80'
 # [AAI]
 AAI_SERVICE_IP = '127.0.0.1'
 AAI_SERVICE_PORT = '8443'
-AAI_BASE_URL = "https://%s:%s/" % (AAI_SERVICE_IP, AAI_SERVICE_PORT)
+AAI_BASE_URL = "https://%s:%s/aai/v8" % (AAI_SERVICE_IP, AAI_SERVICE_PORT)
+AAI_USER = "AAI"
+AAI_PASSWORD = "AAI"
+CLOUD_OWNER = "11"
+CLOUD_REGION_ID = ""
+TENANT_ID = ""
 
 # [REDIS]
 REDIS_HOST = '127.0.0.1'
index 663f16c..1fbe59f 100644 (file)
@@ -93,3 +93,52 @@ def combine_url(base_url, resource):
     else:
         full_url = base_url + '/' + resource
     return full_url
+
+
+def call_req_aai(base_url, user, passwd, auth_type, resource, method, content=''):
+    callid = str(uuid.uuid1())
+    logger.debug("[%s]call_req('%s','%s','%s',%s,'%s','%s','%s')" % (
+        callid, base_url, user, passwd, auth_type, resource, method, content))
+    ret = None
+    resp_status = ''
+    try:
+        full_url = combine_url(base_url, resource)
+        headers = {'content-type': 'application/json', 'accept': 'application/json',
+                   'X-FromAppId': 'AAI', 'X-TransactionId': 'get_aai_subscr'}
+        if user:
+            headers['Authorization'] = 'Basic ' + ('%s:%s' % (user, passwd)).encode("base64")
+        ca_certs = None
+        for retry_times in range(3):
+            http = httplib2.Http(ca_certs=ca_certs, disable_ssl_certificate_validation=(auth_type == rest_no_auth))
+            http.follow_all_redirects = True
+            try:
+                resp, resp_content = http.request(full_url, method=method.upper(), body=content, headers=headers)
+                resp_status, resp_body = resp['status'], resp_content.decode('UTF-8')
+                logger.debug("[%s][%d]status=%s,resp_body=%s)" % (callid, retry_times, resp_status, resp_body))
+                if resp_status in status_ok_list:
+                    ret = [0, resp_body, resp_status]
+                else:
+                    ret = [1, resp_body, resp_status]
+                break
+            except Exception as ex:
+                if 'httplib.ResponseNotReady' in str(sys.exc_info()):
+                    logger.debug("retry_times=%d", retry_times)
+                    logger.error(traceback.format_exc())
+                    ret = [1, "Unable to connect to %s" % full_url, resp_status]
+                    continue
+                raise ex
+    except urllib2.URLError as err:
+        ret = [2, str(err), resp_status]
+    except Exception as ex:
+        logger.error(traceback.format_exc())
+        logger.error("[%s]ret=%s" % (callid, str(sys.exc_info())))
+        res_info = str(sys.exc_info())
+        if 'httplib.ResponseNotReady' in res_info:
+            res_info = "The URL[%s] request failed or is not responding." % full_url
+        ret = [3, res_info, resp_status]
+    except:
+        logger.error(traceback.format_exc())
+        ret = [4, str(sys.exc_info()), resp_status]
+
+    logger.debug("[%s]ret=%s" % (callid, str(ret)))
+    return ret
\ No newline at end of file