Added description of resources on each SLA agreement when they are created
[unfold.git] / sla / slaclient / templates / fed4fire / fed4fire.py
index efb939c..572d9b4 100755 (executable)
@@ -1,5 +1,5 @@
 # -*- coding: utf-8 -*-\r
 # -*- coding: utf-8 -*-\r
-"""Template system for xifi project.\r
+"""Template system for fed4fire project.\r
 \r
 The specific template system is configured with the factory module variable.\r
 \r
 \r
 The specific template system is configured with the factory module variable.\r
 \r
@@ -32,7 +32,7 @@ Usage:
     data = sla.slaclient.templates.fed4fire.TemplateInput(template_id="template-test")\r
     slatemplate_xml = sla.slaclient.templates.fed4fire.render_slatemplate(data)\r
 \r
     data = sla.slaclient.templates.fed4fire.TemplateInput(template_id="template-test")\r
     slatemplate_xml = sla.slaclient.templates.fed4fire.render_slatemplate(data)\r
 \r
-Notes about agreements in XiFi:\r
+Notes about agreements in fed4fire:\r
     The ws-agreement specification does not address where to place the name/id\r
     of the service (as known outside SLA) being defined in the\r
     agreement/template xml. So, it has been defined an element\r
     The ws-agreement specification does not address where to place the name/id\r
     of the service (as known outside SLA) being defined in the\r
     agreement/template xml. So, it has been defined an element\r
@@ -43,7 +43,7 @@ Notes about agreements in XiFi:
     The guarantee terms, service description terms, etc, use the attribute\r
     serviceName to reference (internally in the xml) the service. So, there\r
     could be more than one serviceName in a xml (as opposed to the former\r
     The guarantee terms, service description terms, etc, use the attribute\r
     serviceName to reference (internally in the xml) the service. So, there\r
     could be more than one serviceName in a xml (as opposed to the former\r
-    serviceId). In Xifi, there is only one service per agreement, so we\r
+    serviceId). In fed4fire, there is only one service per agreement, so we\r
     can give serviceId and serviceName the same value.\r
 \r
     A ServiceReference defines how a serviceName is known externally: a\r
     can give serviceId and serviceName the same value.\r
 \r
     A ServiceReference defines how a serviceName is known externally: a\r
@@ -68,7 +68,7 @@ Notes about agreements in XiFi:
     (they are used to describe the service to be instantiated), so we can\r
     extrapolate the location as the "abstract location of the metric".\r
 \r
     (they are used to describe the service to be instantiated), so we can\r
     extrapolate the location as the "abstract location of the metric".\r
 \r
-    In summary, in XiFi, the service properties will hold the metrics being\r
+    In summary, in fed4fire, the service properties will hold the metrics being\r
     monitored for a service.\r
 \r
     And the guarantee terms hold the constraints that are being enforced for\r
     monitored for a service.\r
 \r
     And the guarantee terms hold the constraints that are being enforced for\r
@@ -88,11 +88,11 @@ Notes about agreements in XiFi:
             </wsag:ServiceLevelObjective>\r
         </wsag:GuaranteeTerm>\r
 \r
             </wsag:ServiceLevelObjective>\r
         </wsag:GuaranteeTerm>\r
 \r
-    * Name is a name for the guarantee term. In Xifi, the name will have the\r
+    * Name is a name for the guarantee term. In fed4fire, the name will have the\r
       value "GT_<metric_name>"\r
     * ServiceName is an internal reference in the agreement to the service\r
       being enforced, as an agreement can created for more than one service.\r
       value "GT_<metric_name>"\r
     * ServiceName is an internal reference in the agreement to the service\r
       being enforced, as an agreement can created for more than one service.\r
-      In Xifi, to my knowledge, one service: one agreement, so this service\r
+      In fed4fire, to my knowledge, one service: one agreement, so this service\r
       name is not really important.\r
     * KpiName is a name given to the constraint, and I am using the same name\r
       as the service property used in the constraint. This makes more sense\r
       name is not really important.\r
     * KpiName is a name given to the constraint, and I am using the same name\r
       as the service property used in the constraint. This makes more sense\r
@@ -106,7 +106,7 @@ Notes about agreements in XiFi:
 """\r
 \r
 from sla.slaclient import wsag_model\r
 """\r
 \r
 from sla.slaclient import wsag_model\r
-import pdb\r
+import json\r
 \r
 from sla.slaclient.templates.fed4fire.django.factory import Factory\r
 factory = Factory()\r
 \r
 from sla.slaclient.templates.fed4fire.django.factory import Factory\r
 factory = Factory()\r
@@ -133,7 +133,7 @@ def render_slaagreement(data):
     print "render_slaagreement"\r
     template = _getfactory().slaagreement()\r
     #pdb.set_trace()\r
     print "render_slaagreement"\r
     template = _getfactory().slaagreement()\r
     #pdb.set_trace()\r
-    rendered = template.render(data) \r
+    rendered = template.render(data)\r
     return rendered\r
 \r
 \r
     return rendered\r
 \r
 \r
@@ -196,20 +196,49 @@ class AgreementInput(object):
 \r
     class GuaranteeTerm(object):\r
 \r
 \r
     class GuaranteeTerm(object):\r
 \r
+        class GuaranteeScope(object):\r
+\r
+            def __init__(self,\r
+                         servicename="",\r
+                         scope=""):\r
+\r
+                self.servicename = servicename\r
+                self.scope = scope\r
+\r
+            def __repr__(self):\r
+                s = "<GuaranteeScope(servicename={}, scope={})>"\r
+                return s.format(\r
+                    self.servicename,\r
+                    self.scope\r
+                )\r
+\r
         def __init__(self,\r
                      metric_name="",\r
         def __init__(self,\r
                      metric_name="",\r
-                     bounds=(0, 0)):\r
+                     bounds=(0, 0),\r
+                     guarantee_scopes=()):\r
             """Creates a GuaranteeTerm.\r
 \r
             Take into account that the GT's name is based on the metric_name.\r
             """Creates a GuaranteeTerm.\r
 \r
             Take into account that the GT's name is based on the metric_name.\r
-            :param str metric_name: name of the service property being enforced.\r
-            :param bounds: (lower, upper) bounds of the metric values.\r
+            :param str metric_name: name of the service property being enforced\r
+            :param bounds: (lower, upper) bounds of the metric values\r
             :type bounds: (float, float)\r
             """\r
             self.name = "GT_{}".format(metric_name)\r
             self.metric_name = metric_name\r
             self.kpiname = metric_name\r
             self.bounds = bounds\r
             :type bounds: (float, float)\r
             """\r
             self.name = "GT_{}".format(metric_name)\r
             self.metric_name = metric_name\r
             self.kpiname = metric_name\r
             self.bounds = bounds\r
+            self.guarantee_scopes = guarantee_scopes\r
+\r
+        def __repr__(self):\r
+            s = "<GuaranteeTerm(name={}, metric_name={}, " \\r
+                "kpiname={}, bounds={}, guarantee_scopes={})>"\r
+            return s.format(\r
+                self.name,\r
+                self.metric_name,\r
+                self.kpiname,\r
+                self.bounds,\r
+                repr(self.guarantee_scopes)\r
+            )\r
 \r
     def __init__(self,\r
                  agreement_id="",\r
 \r
     def __init__(self,\r
                  agreement_id="",\r
@@ -228,7 +257,7 @@ class AgreementInput(object):
         :param str agreement_name: optional agreement name\r
         :param str service_id: Domain id/name of the service.\r
         :param str consumer: Id of the consumer party in the agreement.\r
         :param str agreement_name: optional agreement name\r
         :param str service_id: Domain id/name of the service.\r
         :param str consumer: Id of the consumer party in the agreement.\r
-        :param str provider: Resource Id of the provider party in the agreement.\r
+        :param str provider: Resource Id of the provider party in the agreement\r
           The provider must exist previously in the SlaManager.\r
         :param str template_id: TemplateId of the template this agreement is\r
           based on.\r
           The provider must exist previously in the SlaManager.\r
         :param str template_id: TemplateId of the template this agreement is\r
           based on.\r
@@ -248,7 +277,7 @@ class AgreementInput(object):
         self.template_id = template_id\r
         self.expiration_time = expiration_time\r
         self.expiration_time_iso = \\r
         self.template_id = template_id\r
         self.expiration_time = expiration_time\r
         self.expiration_time_iso = \\r
-            expiration_time.isoformat() if expiration_time else None\r
+            expiration_time if expiration_time else None\r
         self.service_properties = service_properties\r
         self.guarantee_terms = guarantee_terms\r
 \r
         self.service_properties = service_properties\r
         self.guarantee_terms = guarantee_terms\r
 \r
@@ -278,6 +307,11 @@ class AgreementInput(object):
         #\r
         # NOTE: templateinput does not address guaranteeterms (yet)\r
         #\r
         #\r
         # NOTE: templateinput does not address guaranteeterms (yet)\r
         #\r
+\r
+        for _, gt in slatemplate.guaranteeterms.items():\r
+            gt.scopes[0].scope = self.guarantee_terms[0].guarantee_scopes.scope\r
+            gt.scopes[0].scope = [x.encode('utf-8') for x in gt.scopes[0].scope]\r
+\r
         result = AgreementInput(\r
             agreement_id=self.agreement_id,\r
             agreement_name=self.agreement_name,\r
         result = AgreementInput(\r
             agreement_id=self.agreement_id,\r
             agreement_name=self.agreement_name,\r
@@ -287,7 +321,15 @@ class AgreementInput(object):
             template_id=slatemplate.template_id,\r
             expiration_time=self.expiration_time,\r
             service_properties=slatemplate.variables.values(),\r
             template_id=slatemplate.template_id,\r
             expiration_time=self.expiration_time,\r
             service_properties=slatemplate.variables.values(),\r
+            #guarantee_terms=self.guarantee_terms\r
             guarantee_terms=slatemplate.guaranteeterms.values()\r
         )\r
             guarantee_terms=slatemplate.guaranteeterms.values()\r
         )\r
-        print result.guarantee_terms[0]\r
+\r
+        #print self.guarantee_terms[0], "\nVVVVVVVVVVVVVVVVV"\r
+        #print slatemplate.guaranteeterms['GT_CPULoad'], "\nVVVVVVVVVVVVVVVVV"\r
+        #print self.guarantee_terms[0].guarantee_scopes.scope, "\nVVVVVVVVVVVVVVVVV"\r
+        #print "TIPO: ", type(self.guarantee_terms[0])\r
+        #print "TIPO 2", type(slatemplate.guaranteeterms['GT_CPULoad']), "\nVVVVVVVVVVVVVVVVV"\r
+        #print slatemplate.guaranteeterms['GT_CPULoad'].scopes, "\nVVVVVVVVVVVVVVVVV"\r
+        #print result.guarantee_terms[0], "\nVVVVVVVVVVVVVVVVV"\r
         return result\r
         return result\r