blob: ed2af32c7bb694c46e630d2f9b6a64223e88290b [file] [log] [blame]
khenaidoofdbad6e2018-11-06 22:26:38 -05001#!/usr/bin/env python
2#
3# Copyright 2017 the original author or authors.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18"""
19Alarm filter CLI commands
20"""
21from optparse import make_option, OptionValueError
22
23from cmd2 import Cmd, options
24from google.protobuf.empty_pb2 import Empty
25
26from table import print_pb_list_as_table
27from python.protos import third_party
28from python.protos import voltha_pb2
29from python.protos.events_pb2 import AlarmEventType, AlarmEventSeverity, AlarmEventCategory
30
31_ = third_party
32
33
34class AlarmFiltersCli(Cmd):
35 def __init__(self, get_stub):
36 Cmd.__init__(self)
37 self.get_stub = get_stub
38 self.prompt = '(' + self.colorize(
39 self.colorize('alarm_filters', 'red'), 'bold') + ') '
40
41 def cmdloop(self):
42 self._cmdloop()
43
44 def help_show(self):
45 self.poutput(
46'''
47Display the list of configured filters.
48
49Valid options:
50
51-i FILTER_ID | --filter-id=FILTER_ID Display the filter rules for a specific filter id (OPTIONAL)
52
53'''
54 )
55
56 @options([
57 make_option('-i', '--filter-id', action="store", dest='filter_id')
58 ])
59 def do_show(self, line, opts):
60 stub = self.get_stub()
61
62 if not opts.filter_id:
63 result = stub.ListAlarmFilters(Empty())
64 print_pb_list_as_table("Alarm Filters:", result.filters, {}, self.poutput)
65 else:
66 result = stub.GetAlarmFilter(voltha_pb2.ID(id=opts.filter_id))
67 print_pb_list_as_table("Rules for Filter ID = {}:".format(opts.filter_id),
68 result.rules, {}, self.poutput)
69
70 @staticmethod
71 def construct_rule(raw_rule):
72 rule = dict()
73
74 rule_kv = raw_rule.strip().split(':')
75
76 if len(rule_kv) == 2:
77 rule['key'] = rule_kv[0].lower()
78 rule['value'] = rule_kv[1].lower()
79 else:
80 raise OptionValueError("Error: A rule must be a colon separated key/value pair")
81
82 return rule
83
84 def parse_filter_rules(option, opt_str, value, parser):
85 rules = getattr(parser.values, option.dest)
86 if rules is None:
87 rules = list()
88 rules.append(AlarmFiltersCli.construct_rule(value))
89
90 for arg in parser.rargs:
91 if (arg[:2] == "--" and len(arg) > 2) or (arg[:1] == "-" and len(arg) > 1 and arg[1] != "-"):
92 break
93 else:
94 rules.append(AlarmFiltersCli.construct_rule(arg))
95
96 setattr(parser.values, option.dest, rules)
97 else:
98 raise OptionValueError('Warning: The filter rule option can only be specified once')
99
100 def help_create(self):
101 types = list(
102 k for k, v in
103 AlarmEventType.DESCRIPTOR.enum_values_by_name.items())
104 categories = list(
105 k for k, v in
106 AlarmEventCategory.DESCRIPTOR.enum_values_by_name.items())
107 severities = list(
108 k for k, v in
109 AlarmEventSeverity.DESCRIPTOR.enum_values_by_name.items())
110
111 alarm_types = types
112 alarm_categories = categories
113 alarm_severities = severities
114
115 usage = '''
116Create a new alarm filter.
117
118Valid options:
119
120-r rule:value ... | --filter-rules rule:value ... Specify one or more filter rules as key/value pairs (REQUIRED)
121
122Valid rule keys and expected values:
123
124id : Identifier of an incoming alarm
125type : Type of an incoming alarm {}
126category : Category of an incoming alarm {}
127severity : Severity of an incoming alarm {}
128resource_id : Resource identifier of an incoming alarm
129device_id : Device identifier of an incoming alarm
130
131Example:
132
133# Filter any alarm that matches the following criteria
134
135create -r type:environment severity:indeterminate
136create -r device_id:754f9dcbe4a6
137
138'''.format(alarm_types, alarm_categories, alarm_severities)
139
140 self.poutput(usage)
141
142 @options([
143 make_option('-r', '--filter-rules', help='<key>:<value>...', action="callback",
144 callback=parse_filter_rules, type='string', dest='filter_rules'),
145 ])
146 def do_create(self, line, opts):
147 if opts.filter_rules:
148 stub = self.get_stub()
149 result = stub.CreateAlarmFilter(voltha_pb2.AlarmFilter(rules=opts.filter_rules))
150 print_pb_list_as_table("Rules for Filter ID = {}:".format(result.id),
151 result.rules, {}, self.poutput)
152
153 def help_delete(self):
154 self.poutput(
155'''
156Delete a specific alarm filter entry.
157
158Valid options:
159
160-i FILTER_ID | --filter-id=FILTER_ID Display the filter rules for a specific filter id (REQUIRED)
161
162'''
163 )
164
165 @options([
166 make_option('-i', '--filter-id', action="store", dest='filter_id')
167 ])
168 def do_delete(self, line, opts):
169 if not opts.filter_id:
170 self.poutput(self.colorize('Error: ', 'red') + 'Specify ' + \
171 self.colorize(self.colorize('"filter id"', 'blue'),
172 'bold') + ' to update')
173 return
174
175 stub = self.get_stub()
176 stub.DeleteAlarmFilter(voltha_pb2.ID(id=opts.filter_id))
177
178 def help_update(self):
179 types = list(
180 k for k, v in
181 AlarmEventType.DESCRIPTOR.enum_values_by_name.items())
182 categories = list(
183 k for k, v in
184 AlarmEventCategory.DESCRIPTOR.enum_values_by_name.items())
185 severities = list(
186 k for k, v in
187 AlarmEventSeverity.DESCRIPTOR.enum_values_by_name.items())
188
189 alarm_types = types
190 alarm_categories = categories
191 alarm_severities = severities
192
193 usage = '''
194Update the filter rules for an existing alarm filter.
195
196Valid options:
197
198-i FILTER_ID | --filter-id=FILTER_ID Indicate the alarm filter identifier to update (REQUIRED)
199-r rule:value ... | --filter-rules rule:value ... Specify one or more filter rules as key/value pairs (REQUIRED)
200
201Valid rule keys and expected values:
202
203id : Identifier of an incoming alarm
204type : Type of an incoming alarm {}
205category : Category of an incoming alarm {}
206severity : Severity of an incoming alarm {}
207resource_id : Resource identifier of an incoming alarm
208device_id : Device identifier of an incoming alarm
209
210Example:
211
212# Filter any alarm that matches the following criteria
213
214update -i 9da115b900bc -r type:environment severity:indeterminate resource_id:1554b0517a07
215
216'''.format(alarm_types, alarm_categories, alarm_severities)
217
218 self.poutput(usage)
219
220 @options([
221 make_option('-r', '--filter-rules', help='<key>:<value>...', action="callback",
222 callback=parse_filter_rules, type='string', dest='filter_rules'),
223 make_option('-i', '--filter-id', action="store", dest='filter_id')
224 ])
225 def do_update(self, line, opts):
226 if not opts.filter_id:
227 self.poutput(self.colorize('Error: ', 'red') + 'Specify ' + \
228 self.colorize(self.colorize('"filter id"', 'blue'),
229 'bold') + ' to update')
230 return
231
232 if opts.filter_rules:
233 stub = self.get_stub()
234 result = stub.UpdateAlarmFilter(
235 voltha_pb2.AlarmFilter(id=opts.filter_id, rules=opts.filter_rules)
236 )
237 print_pb_list_as_table("Rules for Filter ID = {}:".format(result.id),
238 result.rules, {}, self.poutput)