8783d30eaee2420741403083805750e52315aa2f
[demo.git] / vnfs / VES5.0 / evel / evel-test-collector / code / collector / test_control.py
1 #!/usr/bin/env python
2 '''
3 Example script to inject a throttling command list to the test_collector.
4
5 Only intended for test purposes.
6
7 License
8 -------
9
10  * ===================================================================
11  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
12  * ===================================================================
13  * Licensed under the Apache License, Version 2.0 (the "License");
14  * you may not use this file except in compliance with the License.
15  * You may obtain a copy of the License at
16  *
17  *        http://www.apache.org/licenses/LICENSE-2.0
18  *
19  * Unless required by applicable law or agreed to in writing, software
20  * distributed under the License is distributed on an "AS IS" BASIS,
21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  * See the License for the specific language governing permissions and
23  * limitations under the License.
24
25 '''
26 import optparse
27 import requests
28 import json
29
30 ###############################################################################
31 # Functions to build up commandList contents
32 ###############################################################################
33 def command_state():
34     "return a provideThrottlingState command"
35     return {'command':
36             {'commandType': 'provideThrottlingState'}}
37
38 def command_interval(interval):
39     "return a measurementIntervalChange command"
40     return {'command':
41             {'commandType': 'measurementIntervalChange',
42              'measurementInterval': interval}}
43
44 def command_throttle(domain, fields, pairs):
45     "return a throttlingSpecification"
46     throttle_spec = {'eventDomain' : domain}
47     if len(fields):
48         throttle_spec['suppressedFieldNames'] = fields
49     if len(pairs):
50         throttle_spec['suppressedNvPairsList'] = pairs
51     return {'command':
52             {'commandType': 'throttlingSpecification',
53              'eventDomainThrottleSpecification': throttle_spec}}
54
55 def command_nvpairs(field_name, pair_names):
56     "return a suppressedNvPairs"
57     return {'nvPairFieldName' : field_name,
58             'suppressedNvPairNames' : pair_names}
59
60 ###############################################################################
61 # Example functions to build up commandLists for various domains.
62 ###############################################################################
63 def command_list_empty():
64     return {'commandList' : []}
65
66 def command_list_provide():
67     return {'commandList' : [command_state()]}
68
69 def command_list_interval(interval):
70     return {'commandList' : [command_interval(interval)]}
71
72 def command_list_fault_suppress_fields():
73     "Throttling Specification - two suppressedFieldNames"
74     fields = ['alarmInterfaceA', 'alarmAdditionalInformation']
75     pairs = []
76     command_list = [command_throttle('fault', fields, pairs)]
77     return {'commandList' : command_list}
78
79 def command_list_fault_suppress_nothing():
80     "Throttling Specification - no suppression"
81     fields = []
82     pairs = []
83     command_list = [command_throttle('fault', fields, pairs)]
84     return {'commandList' : command_list}
85
86 def command_list_fault_suppress_pairs():
87     "Throttling Specification - two suppressedNvPairNames"
88     fields = []
89     pairs = [command_nvpairs('alarmAdditionalInformation',
90                                    ['name1', 'name2'])]
91     command_list = [command_throttle('fault', fields, pairs)]
92     return {'commandList' : command_list}
93
94 def command_list_fault_suppress_fields_and_pairs():
95     "Throttling Specification - a mixture of fields and pairs"
96     fields = ['alarmInterfaceA']
97     pairs = [command_nvpairs('alarmAdditionalInformation',
98                                    ['name1', 'name2'])]
99     command_list = [command_throttle('fault', fields, pairs)]
100     return {'commandList' : command_list}
101
102 def command_list_measurements_suppress_example():
103     "Throttling Specification - measurements"
104     fields = ['numberOfMediaPortsInUse', 'aggregateCpuUsage']
105     pairs = [command_nvpairs('cpuUsageArray',
106                              ['cpu1', 'cpu3'])]
107     command_list = [command_throttle('measurementsForVfScaling',
108                                      fields, pairs)]
109     return {'commandList' : command_list}
110
111 def command_list_mobile_flow_suppress_example():
112     "Throttling Specification - mobile flow"
113     fields = ['radioAccessTechnology', 'samplingAlgorithm']
114     pairs = []
115     command_list = [command_throttle('mobileFlow', fields, pairs)]
116     return {'commandList' : command_list}
117
118 def command_list_state_change_suppress_example():
119     "Throttling Specification - state change"
120     fields = ['reportingEntityId', 'eventType', 'sourceId']
121     pairs = [command_nvpairs('additionalFields', ['Name1'])]
122     command_list = [command_throttle('stateChange', fields, pairs)]
123     return {'commandList' : command_list}
124
125 def command_list_syslog_suppress_example():
126     "Throttling Specification - syslog"
127     fields = ['syslogFacility', 'syslogProc', 'syslogProcId']
128     pairs = [command_nvpairs('additionalFields', ['Name1', 'Name4'])]
129     command_list = [command_throttle('syslog', fields, pairs)]
130     return {'commandList' : command_list}
131
132 def command_list_reset_all_domains():
133     "Throttling Specification - reset all domains"
134     command_list = [command_throttle('fault', [], []),
135                     command_throttle('measurementsForVfScaling', [], []),
136                     command_throttle('mobileFlow', [], []),
137                     command_throttle('stateChange', [], []),
138                     command_throttle('syslog', [], [])]
139     return {'commandList' : command_list}
140
141 def mixed_example():
142     fields = ['alarmInterfaceA']
143     pairs = [command_nvpairs('alarmAdditionalInformation',
144                              ['name1', 'name2'])]
145     command_list = [command_throttle('fault', fields, pairs),
146                     command_interval(10),
147                     command_state()]
148     return {'commandList' : command_list}
149
150 ###############################################################################
151 # Default command line values
152 ###############################################################################
153 DEFAULT_FQDN = "127.0.0.1"
154 DEFAULT_PORT = 30000
155
156 ###############################################################################
157 # Command Line Parsing
158 ###############################################################################
159 parser = optparse.OptionParser()
160 parser.add_option('--fqdn',
161                   action="store",
162                   dest="fqdn",
163                   default=DEFAULT_FQDN)
164 parser.add_option('--port',
165                   action="store",
166                   dest="port",
167                   default=DEFAULT_PORT,
168                   type="int")
169 options, remainder = parser.parse_args()
170
171 ###############################################################################
172 # Derive the Test Control URL
173 ###############################################################################
174 url = 'http://%s:%d/testControl/v1.1/commandList'%(options.fqdn, options.port)
175
176 ###############################################################################
177 # Create JSON and POST it to the Test Control URL.
178 ###############################################################################
179 command_list = command_list_fault_suppress_fields_and_pairs()
180 requests.post(url, json = command_list)