+
+ def get_metrics_data(self, metric={}):
+ """Get an individual metric's data of a resource.
+ Params:
+ 'metric_name': Normalized name of metric (string)
+ 'resource_uuid': Resource UUID (string)
+ 'period': Time period in Period Unit for which metrics data to be collected from
+ Monitoring tool from now.
+ 'period_unit': Period measurement unit can be one of 'HR', 'DAY', 'MONTH', 'YEAR'
+
+ Return a dict that contains:
+ 'metric_name': Normalized name of metric (string)
+ 'resource_uuid': Resource UUID (string)
+ 'tenant_id': tenent id name in which the resource is present in string format
+ 'metrics_data': Dictionary containing time_series & metric_series data.
+ 'time_series': List of individual time stamp values in msec
+ 'metric_series': List of individual metrics data values
+ Raises an exception upon error or when network is not found
+ """
+ return_data = {}
+ return_data['schema_version'] = 1.0
+ return_data['schema_type'] = 'read_metric_data_response'
+ return_data['metric_name'] = metric['metric_name']
+ #To do - No metric_uuid in vROPs, thus returning metric_name
+ return_data['metric_uuid'] = metric['metric_name']
+ return_data['correlation_id'] = metric['correlation_id']
+ return_data['resource_uuid'] = metric['resource_uuid']
+ return_data['metrics_data'] = {'time_series':[], 'metric_series':[]}
+ #To do - Need confirmation about uuid & id
+ return_data['tenant_uuid'] = metric['tenant_uuid']
+ return_data['unit'] = None
+ #return_data['tenant_id'] = self.tenant_id
+ #log.warn("return_data: {}".format(return_data))
+
+ #1) Get metric details from plugin specific file & format it into vROPs metrics
+ metric_key_params = self.get_default_Params(metric['metric_name'])
+
+ if not metric_key_params:
+ log.warn("Metric not supported: {}".format(metric['metric_name']))
+ #To Do: Return message
+ return return_data
+
+ return_data['unit'] = metric_key_params['unit']
+
+ #2) Find the resource id in vROPs based on OSM resource_uuid
+ #2.a) Find vm_moref_id from vApp uuid in vCD
+ vm_moref_id = self.get_vm_moref_id(metric['resource_uuid'])
+ if vm_moref_id is None:
+ log.warn("Failed to find vm morefid for vApp in vCD: {}".format(config_dict['resource_uuid']))
+ return return_data
+ #2.b) Based on vm_moref_id, find VM's corresponding resource_id in vROPs to set notification
+ resource_id = self.get_vm_resource_id(vm_moref_id)
+ if resource_id is None:
+ log.warn("Failed to find resource in vROPs: {}".format(config_dict['resource_uuid']))
+ return return_data
+
+ #3) Calculate begin & end time for period & period unit
+ end_time = int(round(time.time() * 1000))
+ if metric['collection_unit'] == 'YR':
+ time_diff = PERIOD_MSEC[metric['collection_unit']]
+ else:
+ time_diff = metric['collection_period']* PERIOD_MSEC[metric['collection_unit']]
+ begin_time = end_time - time_diff
+
+ #4) Get the metrics data
+ log.info("metric_key_params['metric_key'] = {}".format(metric_key_params['metric_key']))
+ log.info("end_time: {}, begin_time: {}".format(end_time, begin_time))
+
+ url_list = ['/suite-api/api/resources/', resource_id, '/stats?statKey=',\
+ metric_key_params['metric_key'], '&begin=', str(begin_time),'&end=',str(end_time)]
+ api_url = ''.join(url_list)
+ headers = {'Accept': 'application/json'}
+
+ resp = requests.get(self.vrops_site + api_url,
+ auth=(self.vrops_user, self.vrops_password),
+ verify = False, headers = headers)
+
+ if resp.status_code is not 200:
+ log.warn("Failed to retrive Metric data from vROPs for {}\nResponse code:{}\nResponse Content: {}"\
+ .format(metric['metric_name'], resp.status_code, resp.content))
+ return return_data
+
+ #5) Convert to required format
+ metrics_data = {}
+ json_data = json.loads(resp.content)
+ for resp_key,resp_val in json_data.iteritems():
+ if resp_key == 'values':
+ data = json_data['values'][0]
+ for data_k,data_v in data.iteritems():
+ if data_k == 'stat-list':
+ stat_list = data_v
+ for stat_list_k,stat_list_v in stat_list.iteritems():
+ for stat_keys,stat_vals in stat_list_v[0].iteritems():
+ if stat_keys == 'timestamps':
+ metrics_data['time_series'] = stat_list_v[0]['timestamps']
+ if stat_keys == 'data':
+ metrics_data['metric_series'] = stat_list_v[0]['data']
+
+ return_data['metrics_data'] = metrics_data
+
+ return return_data
+
+ def reconfigure_alarm(self, config_dict):
+ """
+ """
+ return None
+