Create ns instance to aai and fix tests 07/12307/2
authorying.yunlong <ying.yunlong@zte.com.cn>
Thu, 14 Sep 2017 02:18:33 +0000 (10:18 +0800)
committerying.yunlong <ying.yunlong@zte.com.cn>
Thu, 14 Sep 2017 02:21:00 +0000 (10:21 +0800)
Create ns instance to aai and fix the related unit test

Change-Id: Ic6cbf95b8ffa9d382a491bb306ee515341e21473
Issue-ID: VFC-354
Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
lcm/ns/ns_create.py
lcm/ns/tests/test_ns_create.py
lcm/pub/msapi/aai.py

index 69d7ae5..9017a09 100644 (file)
@@ -16,6 +16,7 @@ import uuid
 
 from lcm.pub.database.models import NSDModel, NSInstModel
 from lcm.pub.exceptions import NSLCMException
+from lcm.pub.msapi.aai import create_customer_aai
 from lcm.pub.utils.timeutil import now_time
 
 logger = logging.getLogger(__name__)
@@ -33,6 +34,7 @@ class CreateNSService(object):
         self.check_nsd_valid()
         self.check_ns_inst_name_exist()
         self.create_ns_inst()
+        self.create_ns_in_aai()
         logger.debug("CreateNSService::do_biz::ns_inst_id=%s" % self.ns_inst_id)
         return self.ns_inst_id
 
@@ -53,7 +55,37 @@ class CreateNSService(object):
     def create_ns_inst(self):
         self.ns_inst_id = str(uuid.uuid4())
         logger.debug("CreateNSService::create_ns_inst::ns_inst_id=%s" % self.ns_inst_id)
-        NSInstModel(id=self.ns_inst_id, name=self.ns_name, nspackage_id=self.ns_package_id, 
-                    nsd_id=self.nsd_id, description=self.description, status='empty', 
+        NSInstModel(id=self.ns_inst_id, name=self.ns_name, nspackage_id=self.ns_package_id,
+                    nsd_id=self.nsd_id, description=self.description, status='empty',
                     lastuptime=now_time()).save()
 
+    def create_ns_in_aai(self):
+        logger.debug("CreateNSService::create_ns_in_aai::report ns instance[%s] to aai." % self.ns_inst_id)
+        global_customer_id = "global-customer-id-" + self.ns_inst_id
+        data = {
+                "global-customer-id": "global-customer-id-" + self.ns_inst_id,
+                "subscriber-name": "subscriber-name-" + self.ns_inst_id,
+                "subscriber-type": "subscriber-type-" + self.ns_inst_id,
+                "service-subscriptions": {
+                    "service-subscription": [
+                        {
+                            "service-type": "service-type-" + self.ns_inst_id,
+                            "service-instances": {
+                                "service-instance": [
+                                    {
+                                        "service-instance-id": self.ns_inst_id,
+                                        "service-instance-name": self.ns_name,
+                                        "service-type": "service-type-" + self.ns_inst_id,
+                                        "service-role": "service-role-" + self.ns_inst_id
+                                    }
+                                ]
+                            }
+                        }
+                    ]
+                }
+            }
+        resp_data, resp_status = create_customer_aai(global_customer_id, data)
+        if resp_data:
+            logger.debug("Fail to create ns instance[%s] to aai, resp_status: [%s]." % (self.ns_inst_id, resp_status) )
+        else:
+            logger.debug("Success to create ns instance[%s] to aai, resp_status: [%s]." % (self.ns_inst_id, resp_status) )
index a1b836e..c9e1e08 100644 (file)
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
+import json
 import uuid
+import mock
 
 from django.test import TestCase, Client
 from rest_framework import status
 
 from lcm.pub.database.models import NSInstModel, NSDModel
+from lcm.pub.utils import restcall
 
 
 class TestNsInstantiate(TestCase):
@@ -30,7 +33,10 @@ class TestNsInstantiate(TestCase):
         NSDModel.objects.all().delete()
         NSInstModel.objects.all().delete()
 
-    def test_create_ns(self):
+    @mock.patch.object(restcall, 'call_req')
+    def test_create_ns(self, mock_call_req):
+        r1_create_ns_to_aai = [0, json.JSONEncoder().encode({}), '201']
+        mock_call_req.side_effect = [r1_create_ns_to_aai]
         data = {
             'nsdid': self.nsd_id,
             'nsname': 'ns',
index c22b038..402a9da 100644 (file)
@@ -45,7 +45,7 @@ def create_customer_aai(global_customer_id, data):
     if ret[0] != 0:
         logger.error("Status code is %s, detail is %s.", ret[2], ret[1])
         raise NSLCMException("Customer creation exception in AAI")
-    return json.JSONDecoder().decode(ret[1])
+    return json.JSONDecoder().decode(ret[1]), ret[2]
 
 
 def get_customer_aai(global_customer_id):