Add Vnf config code of gvnfm-mgr
authorfujinhua <fu.jinhua@zte.com.cn>
Thu, 23 Feb 2017 06:27:56 +0000 (14:27 +0800)
committerfujinhua <fu.jinhua@zte.com.cn>
Thu, 23 Feb 2017 06:27:56 +0000 (14:27 +0800)
Change-Id: I8979da7746175eba74b2a4890504f1b8d477713d
Issue-Id: GVNFM-41
Signed-off-by: fujinhua <fu.jinhua@zte.com.cn>
mgr/mgr/vnfreg/tests.py
mgr/mgr/vnfreg/views.py

index 3610ede..11bfd6c 100644 (file)
 
 import unittest
 import json
+import mock
 from django.test import Client
 from rest_framework import status
 
 from mgr.pub.database.models import VnfRegModel
+from mgr.pub.utils import restcall
 
 class VnfRegTest(unittest.TestCase):
     def setUp(self):
@@ -37,6 +39,67 @@ class VnfRegTest(unittest.TestCase):
             "username": "admin1",
             "password": "admin1234"
         }
+        self.vnfconfig = {
+            "vnfInstanceId": "1",
+            "vnfConfigurationData": {
+                "cp": [
+                    {
+                        "cpId": "cp-1",
+                        "cpdId": "cpd-a",
+                        "cpAddress": [
+                            {
+                                "addresses": [
+                                    {
+                                        "addressType": "MAC",
+                                        "l2AddressData": "00:f3:43:20:a2:a3"
+                                    },
+                                    {
+                                        "addressType": "IP",
+                                        "l3AddressData": {
+                                            "iPAddressType": "IPv4",
+                                            "iPAddress": "192.168.104.2"
+                                        }
+                                    }
+                                ],
+                                "useDynamicAddress": "FALSE"
+                            }
+                        ]
+                    }
+                ],
+                "vnfSpecificData": {
+                    "autoScalable": "FALSE",
+                    "autoHealable": "FALSE"
+                }
+            },
+            "vnfcConfigurationData": {
+                "vnfcId": "vnfc-1",
+                "cp": [
+                    {
+                        "cpId": "cp-11",
+                        "cpdId": "cpd-1a",
+                        "cpAddress": [
+                            {
+                                "addresses": [
+                                    {
+                                        "addressType": "MAC",
+                                        "l2AddressData": "00:f3:43:21:a2:a3"
+                                    },
+                                    {
+                                        "addressType": "IP",
+                                        "l3AddressData": {
+                                            "iPAddressType": "IPv4",
+                                            "iPAddress": "192.168.105.2"
+                                        }
+                                    }
+                                ],
+                                "useDynamicAddress": "FALSE"
+                            }
+                        ]
+                    }
+                ],
+                "vnfcSpecificData": {}
+            }
+        }
 
     def tearDown(self):
         pass
@@ -104,7 +167,14 @@ class VnfRegTest(unittest.TestCase):
         self.assertEqual(status.HTTP_404_NOT_FOUND, response.status_code, response.content)
         self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))
         
-    def test_url(self):
-        pass
-        #resp_data = json.loads(response.content)
-        #self.assertEqual({"status": "active"}, resp_data)
+    @mock.patch.object(restcall, 'call_req')
+    def test_vnf_config_normal(self, mock_call_req):
+        mock_call_req.return_value = [0, "", '204']
+        self.client.post("/openoapi/vnfmgr/v1/vnfs", self.vnfInst1, format='json')
+        response = self.client.post("/openoapi/vnfmgr/v1/configuration", self.vnfconfig, format='json')
+        self.assertEqual(status.HTTP_202_ACCEPTED, response.status_code, response.content)
+        
+    def test_vnf_config_when_not_exist(self):
+        response = self.client.post("/openoapi/vnfmgr/v1/configuration", self.vnfconfig, format='json')
+        self.assertEqual(status.HTTP_500_INTERNAL_SERVER_ERROR, response.status_code, response.content)
+        self.assertEqual({'error': "Vnf(1) does not exist."}, json.loads(response.content))
index 0510e87..6df7ce9 100644 (file)
@@ -13,6 +13,7 @@
 # limitations under the License.
 
 import logging
+import json
 
 from rest_framework import status
 from rest_framework.decorators import api_view
@@ -21,6 +22,7 @@ from rest_framework.response import Response
 from mgr.pub.utils.values import ignore_case_get
 from mgr.pub.utils.syscomm import fun_name
 from mgr.pub.database.models import VnfRegModel
+from mgr.pub.utils import restcall
 
 logger = logging.getLogger(__name__)
 
@@ -89,4 +91,22 @@ def access_vnf(request, *args, **kwargs):
 @api_view(http_method_names=['POST'])
 def vnf_config(request, *args, **kwargs):
     logger.info("Enter %s, data is %s", fun_name(), request.data)
+    vnf_inst_id = ignore_case_get(request.data, "vnfInstanceId")
+    try:
+        vnf = VnfRegModel.objects.filter(id=vnf_inst_id)
+        if not vnf:
+            raise Exception("Vnf(%s) does not exist." % vnf_inst_id)
+        ret = restcall.call_req(
+            base_url="http://%s:%s/" % (vnf[0].ip, vnf[0].port), 
+            user=vnf[0].username, 
+            passwd=vnf[0].password, 
+            auth_type=restcall.rest_no_auth,
+            resource="v1/vnfconfig", 
+            method="POST", 
+            content=json.dumps(request.data))
+        if ret[0] != 0:
+            raise Exception("Failed to config Vnf(%s): %s" % (vnf_inst_id, ret[1]))
+    except Exception as e:
+        return Response(data={'error': e.message}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
+    return Response(data={}, status=status.HTTP_202_ACCEPTED)
     
\ No newline at end of file