1 from datetime import datetime
3 """Contains the bean models for the SlaManager xml/json types
7 class Agreement(object):
11 self.expirationtime = datetime.now()
20 "expirationtime={}, provider={}, consumer={}, service={})>"
22 repr(self.expirationtime),
27 def service_formatted(self):
28 return self.service.replace('_', ' ')
30 def testbed_formatted(self):
31 return self.template_id.replace('Service', ' - ')
33 def time_formatted(self):
34 import dateutil.parser
35 time = dateutil.parser.parse(self.expirationtime)
36 return time.strftime('%d-%m-%Y at %H:%M:%S')
38 class Property(object):
46 str_ = "<Property(name={}, servicename={}, metric={}, location={})>"
49 repr(self.servicename),
53 class GuaranteeTerm(object):
55 class GuaranteeScope(object):
61 return "<GuaranteeScope(servicename={}, scope={}>)".format(
62 repr(self.servicename),
66 class ServiceLevelObjective(object):
69 self.customservicelevel = ""
72 s = "<ServiceLevelObjective(kpiname={}, customservicelevel={})>"
75 repr(self.customservicelevel)
80 self.scopes = [] # item: GuaranteeScope
81 """:type : list[Agreement.GuaranteeTerm.GuaranteeScope]"""
82 self.servicelevelobjective = \
83 Agreement.GuaranteeTerm.ServiceLevelObjective()
86 s = "<GuaranteeTerm(scopes={}, servicelevelobjective={})>"
89 repr(self.servicelevelobjective)
93 """Simple bean model for a ws-agreement agreement/template
95 self.context = Agreement.Context()
96 self.agreement_id = ""
97 self.descriptionterms = {}
98 self.variables = {} # key: Property.name / value: Property
99 """:type : dict[str,Agreement.Property]"""
100 self.guaranteeterms = {} # key: GT.name / value: GT
101 """:type : dict[str,Agreement.GuaranteeTerm]"""
104 s = ("<Agreement(agreement_id={}, context={}, descriptionterms={}, " +
105 "variables={}, guaranteeterms={}>")
107 repr(self.agreement_id),
109 repr(self.descriptionterms),
110 repr(self.variables),
111 repr(self.guaranteeterms)
115 class Template(Agreement):
116 #egarrido this code has been copied from xifi and has not beeing tested
118 super(Template, self).__init__()
119 self.template_id = ""
122 s = ("<Template(template_id={}, context={}, descriptionterms={}, " +
123 "variables={}, guaranteeterms={}>")
125 repr(self.template_id),
127 repr(self.descriptionterms),
128 repr(self.variables),
129 repr(self.guaranteeterms)
133 class Enforcement(object):
135 """Simple bean model for an enforcement"""
136 self.agreement_id = ""
140 return ("<Enforcement(agreement_id={}, enabled={})>".format(
145 class AgreementStatus(object):
148 VIOLATED = "VIOLATED"
149 FULFILLED = "FULFILLED"
150 NON_DETERMINED = "NON_DETERMINED"
152 class GuaranteeTermStatus(object):
158 s = "<GuaranteeTermStatus(name='{}' status='{}')>"
159 return s.format(self.name, self.status)
162 self.agreement_id = ""
163 self.guaranteestatus = ""
164 self.guaranteeterms = []
168 "<AgreementStatus( agreement_id={}, guaranteestatus={}, " +
169 "guaranteeterms={})>").format(
171 self.guaranteestatus,
172 repr(self.guaranteeterms))
175 def json_decode(json_obj):
176 o = AgreementStatus()
177 o.agreement_id = json_obj["AgreementId"]
178 o.guaranteestatus = json_obj["guaranteestatus"]
180 for term in json_obj["guaranteeterms"]:
181 t = AgreementStatus.GuaranteeTermStatus()
182 t.name = term["name"]
183 t.status = term["status"]
184 o.guaranteeterms.append(t)
188 class Violation(object):
190 """Simple bean model for a violation"""
192 self.contract_uuid = ""
193 self.service_scope = ""
194 self.metric_name = ""
195 self.datetime = datetime.utcnow()
196 self.actual_value = 0
199 return ("<Violation(uuid={}, agremeent_id={}, service_scope={}, " +
200 "metric_name={}, datetime={}, actual_value={})>".format(
209 def format_time(self):
210 # return datetime.strptime(self.datetime.datetime.utcnow,'%Y-%m-%d %H:%M:%S')
211 # return str(datetime.fromtimestamp(self.datetime))
212 return str(self.datetime)
215 class Provider(object):
217 """Simple bean model for a provider"""
222 return ("<Provider(uuid={}, name={})>".format(
227 xml = "<provider><uuid>{}</uuid><name>{}</name></provider>""".format(
235 """Creates a Provider object from a dict structure (e.g.
236 a deserialized json string)
239 json_obj = json.loads(json_data)
240 out = wsag_model.Provider.from_dict(json_obj)
242 result = Provider(d["uuid"], d["name"])