Automatically add default policies

Change-Id: I38fcd336e7eca90264590d4174c8cf65e89c60a1
Issue-ID: INT-795
Signed-off-by: Haibin Huang <haibin.huang@intel.com>
diff --git a/test/hpa_automation/tosca/hpa_automation.py b/test/hpa_automation/tosca/hpa_automation.py
index d8e1d37..b7b8eb7 100755
--- a/test/hpa_automation/tosca/hpa_automation.py
+++ b/test/hpa_automation/tosca/hpa_automation.py
@@ -235,12 +235,10 @@
     )
 
     mycursor = mydb.cursor()
-
-    sql = "INSERT INTO optimizationmodels (modelname, description, dependency, imported_by, \
-          attributes, ref_attributes, sub_attributes, version, annotation, enumValues, \
-          dataOrderInfo) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
-    val = [
-        ('hpaPolicy', 'HPA Tests Model', '[]', 'demo', 'identity=string:defaultValue-null:required-true:MANY-false:description-null', \
+    hpa_sql = "INSERT INTO optimizationmodels (modelname, description, dependency, imported_by, \
+              attributes, ref_attributes, sub_attributes, version, annotation, enumValues, \
+              dataOrderInfo) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
+    hpa_val = ('hpaPolicy', 'HPA Tests Model', '[]', 'demo', 'identity=string:defaultValue-null:required-true:MANY-false:description-null', \
          'policyScope=MANY-true,policyType=POLICYTYPE:MANY-false,resources=MANY-true,flavorFeatures=flavorFeatures_properties:MANY-true:description-null', \
          '{"directives_properties":{"attributes":"directives_attributes_properties:required-false:MANY-true:description-null",\
          "type":"string:defaultValue-null:required-false:MANY-false:description-null"},\
@@ -262,7 +260,14 @@
          "hpa-attribute-key":"string:defaultValue-null:required-true:MANY-false:description-null",\
          "operator":"OPERATOR:defaultValue-null:required-false:MANY-false:description-null"}}', \
          'test1', 'policyScope=matching-true, policyType=matching-true', \
-         'OPERATOR=[<,<equal-sign,>,>equal-sign,equal-sign,!equal-sign,any,all,subset,], POLICYTYPE=[hpa,]', '""'),
+         'OPERATOR=[<,<equal-sign,>,>equal-sign,equal-sign,!equal-sign,any,all,subset,], POLICYTYPE=[hpa,]', '""')
+
+    mycursor.execute(hpa_sql, hpa_val)
+    
+    sql = "INSERT INTO microservicemodels (modelname, description, dependency, imported_by, \
+          attributes, ref_attributes, sub_attributes, version, annotation, enumValues, \
+          dataOrderInfo) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
+    val = [
         ('distancePolicy', 'distancePolicy', '[]', 'demo', 'identity=string:defaultValue-null:required-true:MANY-false:description-null', \
          'policyScope=MANY-true,distanceProperties=distanceProperties_properties:MANY-false:description-null,policyType=POLICYTYPE:MANY-false,\
           resources=MANY-true,applicableResources=APPLICABLERESOURCES:MANY-false', \
@@ -329,7 +334,22 @@
     mycursor.executemany(sql, val)
     mydb.commit()
     print(mycursor.rowcount, "was inserted.")
+    
+def add_policies(parameters):
+    #Loop through policy, put in resource_model_name and create policies
+    for policy in os.listdir(parameters["policy_directory"]):
+      policy_name = "{}.{}".format(parameters["policy_scope"], os.path.splitext(policy)[0])
+      policy_file = (os.path.join(parameters["policy_directory"], policy))
+      #Create policy
+      os.system("oclip policy-create-outdated -m {} -u {} -p {} -x {} -S {} -T {} -o {} -b $(cat {})".format(parameters["policy_url"],\
+      parameters["policy_username"], parameters["policy_password"], policy_name, parameters["policy_scope"], \
+      parameters["policy_config_type"], parameters["policy_onapName"], policy_file))
 
+      #Push policy
+      os.system("oclip policy-push-outdated -m {} -u {} -p {} -x {} -b {} -c {}".format(parameters["policy_url"], \
+        parameters["policy_username"], parameters["policy_password"], policy_name, parameters["policy_config_type"],\
+        parameters["policy_pdp_group"]))
+    
 def onboard_vnf(parameters):
     vnfs = parameters["vnfs"]
     vnf_onboard_outputs = {}
@@ -459,6 +479,7 @@
 
 # 6.add_policies function not currently working, using curl commands
 add_policy_models(parameters)
+add_policies(parameters)
 
 # 7. VFC part
 ns_instance_id = create_ns(parameters, ns_package_output)
diff --git a/test/hpa_automation/tosca/vcpe_config.json b/test/hpa_automation/tosca/vcpe_config.json
index 811124d..0300f7c 100755
--- a/test/hpa_automation/tosca/vcpe_config.json
+++ b/test/hpa_automation/tosca/vcpe_config.json
@@ -4,7 +4,7 @@
     "aai_url" : "https://10.12.5.224:30233",
     "aai_username" : "AAI",
     "aai_password" : "AAI",
-    "sdc_onboarding_url" : "http://10.12.5.202:8081",
+    "sdc_onboarding_url" : "http://10.43.89.129:8081",
     "sdc_catalog_url" : "http://10.12.5.180:30205",
     "sdc_password" : "demo123456!",
     "sdc_creator" : "cs0008",
@@ -13,11 +13,10 @@
     "sdc_operator" : "op0001",
 
     "multicloud_url" : "http://10.12.5.224:30280",
-    "policy_url" : "https://10.12.6.235:30240",
+    "policy_url" : "https://10.42.9.13:8081",
     "policy_username" : "testpdp",
     "policy_password" : "alpha123",
-    "policy_models_directory" : "/root/tosca/optf-osdf/osdf/models/policy/placement/tosca_upload/",
-    "policy_directory" : "/root/tosca/vcpe_policies",
+    "policy_directory" : "/opt/oclip/dublin/vcpe_policies",
     "policy_scope" : "OSDF_DUBLIN",
     "policy_onapName" : "SampleDemo",
     "policy_config_type": "MicroService",
@@ -133,7 +132,7 @@
     "vfc-url": "http://10.12.6.88:30280",
     "vnfs":{
         "infra":{
-            "path": "/root/oclip/dublin/infra.csar",
+            "path": "/opt/oclip/dublin/infra.csar",
             "csar-id": "You need change it",
             "vsp-name" : "infra-hpa-vsp",
             "vsp-desc" : "infra-hpa-vsp-desc",
@@ -146,7 +145,7 @@
             "value": "value2"
         },
         "vgmux":{
-            "path": "/root/oclip/dublin/vgmux.csar",
+            "path": "/opt/oclip/dublin/vgmux.csar",
             "csar-id": "You need change it",
             "vsp-name" : "vgmux-hpa-vsp",
             "vsp-desc" : "vgmux-hpa-vsp-desc",
@@ -159,7 +158,7 @@
             "value": "value2"
         },
         "vbng":{
-            "path": "/root/oclip/dublin/vbng.csar",
+            "path": "/opt/oclip/dublin/vbng.csar",
             "csar-id": "You need change it",
             "vsp-name" : "vbng-hpa-vsp",
             "vsp-desc" : "vbng-hpa-vsp-desc",
@@ -172,7 +171,7 @@
             "value": "value2"
         },
         "vbrgemu":{
-            "path": "/root/oclip/dublin/vbrgemu.csar",
+            "path": "/opt/oclip/dublin/vbrgemu.csar",
             "csar-id": "You need change it",
             "vsp-name" : "vbrgemu-hpa-vsp",
             "vsp-desc" : "vbgremu-hpa-vsp-desc",
diff --git a/test/hpa_automation/tosca/vcpe_policies/distance_policy.txt b/test/hpa_automation/tosca/vcpe_policies/distance_policy.txt
new file mode 100644
index 0000000..6fbba2b
--- /dev/null
+++ b/test/hpa_automation/tosca/vcpe_policies/distance_policy.txt
@@ -0,0 +1 @@
+{\"service\":\"distancePolicy\",\"guard\":\"False\",\"content\":{\"policyType\":\"distance_to_location\",\"applicableResources\":\"any\",\"identity\":\"distance-vFW\",\"policyScope\":[\"vcpe\",\"vcpe_vgw\",\"us\"],\"distanceProperties\":{\"distance\":{\"value\":\"100\",\"unit\":\"km\",\"operator\":\"<\"},\"locationInfo\":\"customer_loc\"},\"resources\":[\"vCPE_Infrastructure_GW_demo_app\",\"vCPE_Infrastructure_vGMUX_demo_app\",\"vCPE_Infrastructure_BGREMU_demo_app\",\"vCPE_Infrastructure_Metro_vBNG_demo_app\",\"vCPE_Infrastructure_demo_app\"]},\"priority\":\"3\",\"templateVersion\":\"OpenSource.version.1\",\"riskLevel\":\"2\",\"description\":\"DistancePolicyforvCPE\",\"policyName\":\"OSDF_DUBLIN.Distance_vCPEHPA_2\",\"version\":\"test1\",\"riskType\":\"test\"}
diff --git a/test/hpa_automation/tosca/vcpe_policies/optimization_policy.txt b/test/hpa_automation/tosca/vcpe_policies/optimization_policy.txt
new file mode 100644
index 0000000..35183e6
--- /dev/null
+++ b/test/hpa_automation/tosca/vcpe_policies/optimization_policy.txt
@@ -0,0 +1 @@
+{\"service\":\"optimizationPolicy\",\"guard\":\"False\",\"content\":{\"policyType\":\"placement_optimization\",\"objective\":\"minimize\",\"objectiveParameter\":{\"operator\":\"sum\",\"parameterAttributes\":[{\"parameter\":\"distance\",\"operator\":\"product\",\"customerLocationInfo\":\"customer_loc\",\"weight\":\"100\",\"resources\":[\"vCPE_Infrastructure_GW_demo_app\",\"vCPE_Infrastructure_vGMUX_demo_app\",\"vCPE_Infrastructure_BGREMU_demo_app\",\"vCPE_Infrastructure_Metro_vBNG_demo_app\",\"vCPE_Infrastructure_demo_app\"]},{\"parameter\":\"hpa_score\",\"operator\":\"product\",\"weight\":\"200\",\"resources\":[\"vCPE_Infrastructure_GW_demo_app\",\"vCPE_Infrastructure_vGMUX_demo_app\",\"vCPE_Infrastructure_BGREMU_demo_app\",\"vCPE_Infrastructure_Metro_vBNG_demo_app\",\"vCPE_Infrastructure_demo_app\"]}]},\"identity\":\"optimization\",\"policyScope\":[\"vcpe\",\"us\",\"vcpe_vgw\"]},\"priority\":\"5\",\"templateVersion\":\"OpenSource.version.1\",\"riskLevel\":\"3\",\"description\":\"PlacementOptimizationPolicyforvGMuxInfra\",\"policyName\":\"OSDF_DUBLIN.Placement_Optimization_3\",\"version\":\"test1\",\"riskType\":\"test\"}
diff --git a/test/hpa_automation/tosca/vcpe_policies/query_policy.txt b/test/hpa_automation/tosca/vcpe_policies/query_policy.txt
new file mode 100644
index 0000000..ef01cf4
--- /dev/null
+++ b/test/hpa_automation/tosca/vcpe_policies/query_policy.txt
@@ -0,0 +1 @@
+{\"service\":\"queryPolicy\",\"guard\":\"False\",\"content\":{\"policyType\":\"request_param_query\",\"queryProperties\":[{\"attribute\":\"customerLatitude\",\"attribute_location\":\"customerLatitude\"},{\"attribute\":\"customerLongitude\",\"attribute_location\":\"customerLongitude\"}],\"identity\":\"vCPE_Query_Policy\",\"serviceName\":\"vCPE\",\"policyScope\":[\"vcpe\",\"vcpe_vgw\",\"us\"]},\"priority\":\"3\",\"templateVersion\":\"OpenSource.version.1\",\"riskLevel\":\"2\",\"description\":\"QuerypolicyforvCPE\",\"policyName\":\"OSDF_DUBLIN.QueryPolicy_vCPEHPA_3\",\"version\":\"test1\",\"riskType\":\"test\"}
diff --git a/test/hpa_automation/tosca/vcpe_policies/vnf_policy.txt b/test/hpa_automation/tosca/vcpe_policies/vnf_policy.txt
new file mode 100644
index 0000000..b839398
--- /dev/null
+++ b/test/hpa_automation/tosca/vcpe_policies/vnf_policy.txt
@@ -0,0 +1 @@
+{\"service\":\"vnfPolicy\",\"guard\":\"False\",\"content\":{\"policyType\":\"vnfPolicy\",\"policyScope\":[\"vcpe\",\"vcpe_vgw\",\"us\"],\"identity\":\"vnf_vFW\",\"resources\":[\"vCPE_Infrastructure_GW_demo_app\",\"vCPE_Infrastructure_vGMUX_demo_app\",\"vCPE_Infrastructure_BGREMU_demo_app\",\"vCPE_Infrastructure_Metro_vBNG_demo_app\",\"vCPE_Infrastructure_demo_app\"],\"applicableResources\":\"any\",\"vnfProperties\":[{\"equipmentRole\":\"\",\"inventoryProvider\":\"aai\",\"inventoryType\":\"cloud\",\"serviceType\":\"\",\"customerId\":\"\",\"orchestrationStatus\":\"\"}]},\"priority\":\"6\",\"templateVersion\":\"OpenSource.version.1\",\"riskLevel\":\"3\",\"description\":\"vnfPolicy\",\"policyName\":\"OSDF_CASABLANCA.vnfPolicy_vFWHPA_3\",\"version\":\"test1\",\"riskType\":\"test\"}