Removing unused imports in python scripts
[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
29 ###############################################################################
30 # Functions to build up commandList contents
31 ###############################################################################
32 def command_state():
33     "return a provideThrottlingState command"
34     return {'command':
35             {'commandType': 'provideThrottlingState'}}
36
37 def command_interval(interval):
38     "return a measurementIntervalChange command"
39     return {'command':
40             {'commandType': 'measurementIntervalChange',
41              'measurementInterval': interval}}
42
43 def command_throttle(domain, fields, pairs):
44     "return a throttlingSpecification"
45     throttle_spec = {'eventDomain' : domain}
46     if len(fields):
47         throttle_spec['suppressedFieldNames'] = fields
48     if len(pairs):
49         throttle_spec['suppressedNvPairsList'] = pairs
50     return {'command':
51             {'commandType': 'throttlingSpecification',
52              'eventDomainThrottleSpecification': throttle_spec}}
53
54 def command_nvpairs(field_name, pair_names):
55     "return a suppressedNvPairs"
56     return {'nvPairFieldName' : field_name,
57             'suppressedNvPairNames' : pair_names}
58
59 ###############################################################################
60 # Example functions to build up commandLists for various domains.
61 ###############################################################################
62 def command_list_empty():
63     return {'commandList' : []}
64
65 def command_list_provide():
66     return {'commandList' : [command_state()]}
67
68 def command_list_interval(interval):
69     return {'commandList' : [command_interval(interval)]}
70
71 def command_list_fault_suppress_fields():
72     "Throttling Specification - two suppressedFieldNames"
73     fields = ['alarmInterfaceA', 'alarmAdditionalInformation']
74     pairs = []
75     command_list = [command_throttle('fault', fields, pairs)]
76     return {'commandList' : command_list}
77
78 def command_list_fault_suppress_nothing():
79     "Throttling Specification - no suppression"
80     fields = []
81     pairs = []
82     command_list = [command_throttle('fault', fields, pairs)]
83     return {'commandList' : command_list}
84
85 def command_list_fault_suppress_pairs():
86     "Throttling Specification - two suppressedNvPairNames"
87     fields = []
88     pairs = [command_nvpairs('alarmAdditionalInformation',
89                                    ['name1', 'name2'])]
90     command_list = [command_throttle('fault', fields, pairs)]
91     return {'commandList' : command_list}
92
93 def command_list_fault_suppress_fields_and_pairs():
94     "Throttling Specification - a mixture of fields and pairs"
95     fields = ['alarmInterfaceA']
96     pairs = [command_nvpairs('alarmAdditionalInformation',
97                                    ['name1', 'name2'])]
98     command_list = [command_throttle('fault', fields, pairs)]
99     return {'commandList' : command_list}
100
101 def command_list_measurements_suppress_example():
102     "Throttling Specification - measurements"
103     fields = ['numberOfMediaPortsInUse', 'aggregateCpuUsage']
104     pairs = [command_nvpairs('cpuUsageArray',
105                              ['cpu1', 'cpu3'])]
106     command_list = [command_throttle('measurementsForVfScaling',
107                                      fields, pairs)]
108     return {'commandList' : command_list}
109
110 def command_list_mobile_flow_suppress_example():
111     "Throttling Specification - mobile flow"
112     fields = ['radioAccessTechnology', 'samplingAlgorithm']
113     pairs = []
114     command_list = [command_throttle('mobileFlow', fields, pairs)]
115     return {'commandList' : command_list}
116
117 def command_list_state_change_suppress_example():
118     "Throttling Specification - state change"
119     fields = ['reportingEntityId', 'eventType', 'sourceId']
120     pairs = [command_nvpairs('additionalFields', ['Name1'])]
121     command_list = [command_throttle('stateChange', fields, pairs)]
122     return {'commandList' : command_list}
123
124 def command_list_syslog_suppress_example():
125     "Throttling Specification - syslog"
126     fields = ['syslogFacility', 'syslogProc', 'syslogProcId']
127     pairs = [command_nvpairs('additionalFields', ['Name1', 'Name4'])]
128     command_list = [command_throttle('syslog', fields, pairs)]
129     return {'commandList' : command_list}
130
131 def command_list_reset_all_domains():
132     "Throttling Specification - reset all domains"
133     command_list = [command_throttle('fault', [], []),
134                     command_throttle('measurementsForVfScaling', [], []),
135                     command_throttle('mobileFlow', [], []),
136                     command_throttle('stateChange', [], []),
137                     command_throttle('syslog', [], [])]
138     return {'commandList' : command_list}
139
140 def mixed_example():
141     fields = ['alarmInterfaceA']
142     pairs = [command_nvpairs('alarmAdditionalInformation',
143                              ['name1', 'name2'])]
144     command_list = [command_throttle('fault', fields, pairs),
145                     command_interval(10),
146                     command_state()]
147     return {'commandList' : command_list}
148
149 ###############################################################################
150 # Default command line values
151 ###############################################################################
152 DEFAULT_FQDN = "127.0.0.1"
153 DEFAULT_PORT = 30000
154
155 ###############################################################################
156 # Command Line Parsing
157 ###############################################################################
158 parser = optparse.OptionParser()
159 parser.add_option('--fqdn',
160                   action="store",
161                   dest="fqdn",
162                   default=DEFAULT_FQDN)
163 parser.add_option('--port',
164                   action="store",
165                   dest="port",
166                   default=DEFAULT_PORT,
167                   type="int")
168 options, remainder = parser.parse_args()
169
170 ###############################################################################
171 # Derive the Test Control URL
172 ###############################################################################
173 url = 'http://%s:%d/testControl/v1.1/commandList'%(options.fqdn, options.port)
174
175 ###############################################################################
176 # Create JSON and POST it to the Test Control URL.
177 ###############################################################################
178 command_list = command_list_fault_suppress_fields_and_pairs()
179 requests.post(url, json = command_list)