Cleaned code and improved communication with SLA Collector
[unfold.git] / sla / slaclient / service / fed4fire / fed4fireservice.py
1 # -*- coding: utf-8 -*-\r
2 """Builds templates/agreements based on input data (in json format), submitting\r
3 to sla manager.\r
4 \r
5 It is intended as backend service for a rest interface.\r
6 \r
7 The json input must work together with the templates to form a valid template\r
8  or agreement for fed4fire (be careful!)\r
9 \r
10 This (very simple) service is coupled to the way fed4fire is interpreting\r
11 ws-agreement.\r
12 \r
13 \r
14 """\r
15 import json\r
16 import jsonparser\r
17 from sla.slaclient import wsag_model\r
18 from sla.slaclient import restclient\r
19 from sla.slaclient.templates.fed4fire.django.factory import Factory as TemplateFactory\r
20 import sla.slaclient.templates.fed4fire as fed4fire\r
21 #from time import localtime, strftime\r
22 import uuid\r
23 import dateutil.parser\r
24 \r
25 \r
26 class ServiceContext(object):\r
27     def __init__(self, restfactory=None, templatefactory=None):\r
28         """\r
29         :type restfactory: restclient.Factory\r
30         """\r
31         self.restfactory = restfactory\r
32         self.templatefactory = templatefactory\r
33 \r
34 \r
35 def createprovider(json_data, context):\r
36     """Creates a provider in the SlaManager.\r
37     :type json_data:str\r
38     :type context: ServiceContext\r
39 \r
40     An example input is:\r
41     {\r
42         "uuid": "f4c993580-03fe-41eb-8a21-a56709f9370f",\r
43         "name": "provider"\r
44     }\r
45     """\r
46     json_obj = json.loads(json_data)\r
47     p = wsag_model.Provider.from_dict(json_obj)\r
48     provider_client = context.restfactory.providers()\r
49     provider_client.create(p)\r
50 \r
51 \r
52 def createtemplate(json_data, context):\r
53     """Creates a template in the SlaManager\r
54 \r
55     An example input is:\r
56     {\r
57         "template_id" : "template-id",\r
58         "template_name" : "template-name",\r
59         "provider" : "provider-1",\r
60         "service_id" : "service-id",\r
61         "expiration_time" : "2014-03-28T13:55:00Z",\r
62         "service_properties" : [\r
63             {\r
64                 "name" : "uptime",\r
65                 "servicename" : "service-a",\r
66                 "metric" : "xs:double",\r
67                 "location" : "//service-a/uptime"\r
68             }\r
69         ]\r
70     }\r
71 \r
72     :type json_data:str\r
73     :type context: ServiceContext\r
74     """\r
75     data = jsonparser.templateinput_from_json(json_data)\r
76     slatemplate = sla.slaclient.templates.fed4fire.render_slatemplate(data)\r
77     client = context.restfactory.templates()\r
78     client.create(slatemplate)\r
79 \r
80 \r
81 def createagreement(json_data, context):\r
82     """Creates an agreement in the SlaManager.\r
83 \r
84     The template with template_id is retrieved and the properties and some\r
85     context info is copied to the agreement.\r
86 \r
87     An example input is:\r
88     {\r
89         "template_id" : "template-id",\r
90         "agreement_id" : "agreement-id",\r
91         "expiration_time" : "2014-03-28T13:55:00Z",\r
92         "consumer" : "consumer-a",\r
93         "guarantees" : [\r
94             {\r
95                 "name" : "uptime",\r
96                 "bounds" : [ "0", "1" ]\r
97             }\r
98         ]\r
99     }\r
100     :type json_data:str\r
101     :type context: ServiceContext\r
102     """\r
103     client_templates = context.restfactory.templates()\r
104 \r
105     # Builds AgreementInput from json\r
106     data = jsonparser.agreementinput_from_json(json_data)\r
107     # Read template from manager\r
108     slatemplate, request = client_templates.getbyid(data.template_id, data.template_id)\r
109     # Copy (overriding if necessary) from template to AgreementInput\r
110     final_data = data.from_template(slatemplate)\r
111     slaagreement = fed4fire.render_slaagreement(final_data)\r
112 \r
113     client_agreements = context.restfactory.agreements()\r
114     return client_agreements.create(slaagreement, data.template_id)\r
115 \r
116 \r
117 def createagreementsimplified(template_id, user, expiration_time, resources):\r
118     context = ServiceContext(\r
119         restclient.Factory(),\r
120         TemplateFactory()\r
121     )\r
122 \r
123     print "Expiration time: ", expiration_time\r
124 \r
125     # time = dateutil.parser.parse(expiration_time)\r
126     # print "ISO FORMAT: ", time.strftime('%Y-%m-%dT%H:%M:%S%Z')\r
127     print "ISO FORMAT: ", expiration_time.strftime('%Y-%m-%dT%H:%M:%S%Z')\r
128 \r
129     agreement = {\r
130         "agreement_id": str(uuid.uuid4()),\r
131         "template_id": template_id,\r
132         "expiration_time": expiration_time.strftime('%Y-%m-%dT%H:%M:%S%Z'),\r
133         "consumer": user,\r
134     }\r
135 \r
136     json_data = json.dumps(agreement)\r
137 \r
138     return createagreement(json_data, context)\r
139