blob: 0a32ac2e9a28ff35385d0bf2f66d0f20f5730efa [file] [log] [blame]
Ilya Tyaptinaeff7552016-11-17 15:57:36 +00001-- Copyright 2016 Mirantis, Inc.
2--
3-- Licensed under the Apache License, Version 2.0 (the "License");
4-- you may not use this file except in compliance with the License.
5-- You may obtain a copy of the License at
6--
7-- http://www.apache.org/licenses/LICENSE-2.0
8--
9-- Unless required by applicable law or agreed to in writing, software
10-- distributed under the License is distributed on an "AS IS" BASIS,
11-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-- See the License for the specific language governing permissions and
13-- limitations under the License.
14
15local cjson = cjson
16local string = string
Ilya Tyaptinaeff7552016-11-17 15:57:36 +000017local setmetatable = setmetatable
18local ipairs = ipairs
19local pairs = pairs
20local pcall = pcall
21local type = type
22
23local patt = require 'patterns'
24local utils = require 'lma_utils'
25local l = require 'lpeg'
26l.locale(l)
27
28function normalize_uuid(uuid)
29 return patt.Uuid:match(uuid)
30end
31
Ilya Tyaptinaeff7552016-11-17 15:57:36 +000032local ResourcesDecoder = {}
33ResourcesDecoder.__index = ResourcesDecoder
34
35setfenv(1, ResourcesDecoder) -- Remove external access to contain everything in the module
36
37function normalize_uuid(uuid)
38 return patt.Uuid:match(uuid)
39end
40
41-- Mapping table defining transformation functions to be applied, keys are the
42-- attributes in the notification's payload and values are Lua functions
43local transform_functions = {
44 created_at = utils.format_datetime,
45 launched_at = utils.format_datetime,
46 deleted_at = utils.format_datetime,
47 terminated_at = utils.format_datetime,
48 user_id = normalize_uuid,
49 project_id = normalize_uuid,
50}
51
52function map(func, tbl)
53 local mapped_table = {}
54 for i,v in pairs(tbl) do
55 mapped_table[i] = func(v)
56 end
57 return mapped_table
58end
59
60local resource_msg = {
61 Timestamp = nil,
62 Type = "ceilometer_resources",
63 Payload = nil
64}
65
66function add_resource_to_payload(sample, payload)
67 local counter_name, _ = string.gsub(sample.counter_name, "%.", "\\")
68
Ilya Tyaptin4364b7e2017-02-14 17:19:09 +040069 local metadata = sample.resource_metadata
70 local local_metadata = {}
71
72 if type(metadata) == 'table' then
73 for name, value in ipairs(metadata) do
74 local transform = transform_functions[name]
75 if value ~= '' and value ~= nil then
76 if transform ~= nil then
77 value = transform(value)
78 end
79 local_metadata[name] = value
80 end
81 end
82 end
83
Ilya Tyaptinaeff7552016-11-17 15:57:36 +000084 local resource_data = {
Ilya Tyaptin4364b7e2017-02-14 17:19:09 +040085 timestamp = utils.format_datetime(sample.timestamp),
Ilya Tyaptinaeff7552016-11-17 15:57:36 +000086 resource_id = sample.resource_id,
87 source = sample.source or "",
Ilya Tyaptin4364b7e2017-02-14 17:19:09 +040088 metadata = local_metadata,
Ilya Tyaptinaeff7552016-11-17 15:57:36 +000089 user_id = sample.user_id,
90 project_id = sample.project_id,
91 meter = {
92 [counter_name] = {
93 type = sample.counter_type,
94 unit = sample.counter_unit
95 }
96 }
97 }
98 payload[sample.resource_id] = resource_data
99end
100
101function ResourcesDecoder.new()
102 local e = {}
103 setmetatable(e, ResourcesDecoder)
104 return e
105end
106
107-- Decode Ceilometer samples to resource messages
108
109-- data: oslo.messaging message with Ceilometer samples
110-- returns ok and resource or error message
Ildar Svetlov8f33dbb2017-07-21 16:01:18 +0400111function ResourcesDecoder:decode (message_body)
Ilya Tyaptinaeff7552016-11-17 15:57:36 +0000112 local resource_payload = {}
113 if message_body['payload'] then
114 for _, sample in ipairs(message_body["payload"]) do
115 add_resource_to_payload(sample, resource_payload)
116 end
Ildar Svetlovbbe37be2017-07-14 13:57:49 +0400117 local ok, payload = pcall(cjson.encode, resource_payload)
118 if not ok then
119 return -1, "Cannot encode resource_payload"
120 end
121 resource_msg.Payload = payload
Ilya Tyaptinaeff7552016-11-17 15:57:36 +0000122 resource_msg.Timestamp = patt.Timestamp:match(message_body.timestamp)
123 return 0, resource_msg
124 end
Ilya Tyaptin7ee03412016-12-20 18:25:32 +0400125 return -1, "Empty message"
Ilya Tyaptinaeff7552016-11-17 15:57:36 +0000126end
127
128return ResourcesDecoder