Merge "removed vFW vLB extra executables"
[demo.git] / VES5.0 / 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 Copyright(c) <2016>, AT&T Intellectual Property.  All other rights reserved.
11
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14
15 1. Redistributions of source code must retain the above copyright notice,
16    this list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright notice,
18    this list of conditions and the following disclaimer in the documentation
19    and/or other materials provided with the distribution.
20 3. All advertising materials mentioning features or use of this software
21    must display the following acknowledgement:  This product includes
22    software developed by the AT&T.
23 4. Neither the name of AT&T nor the names of its contributors may be used to
24    endorse or promote products derived from this software without specific
25    prior written permission.
26
27 THIS SOFTWARE IS PROVIDED BY AT&T INTELLECTUAL PROPERTY ''AS IS'' AND ANY
28 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30 DISCLAIMED. IN NO EVENT SHALL AT&T INTELLECTUAL PROPERTY BE LIABLE FOR ANY
31 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
33 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 '''
38 import optparse
39 import requests
40 import json
41
42 ###############################################################################
43 # Functions to build up commandList contents
44 ###############################################################################
45 def command_state():
46     "return a provideThrottlingState command"
47     return {'command':
48             {'commandType': 'provideThrottlingState'}}
49
50 def command_interval(interval):
51     "return a measurementIntervalChange command"
52     return {'command':
53             {'commandType': 'measurementIntervalChange',
54              'measurementInterval': interval}}
55
56 def command_throttle(domain, fields, pairs):
57     "return a throttlingSpecification"
58     throttle_spec = {'eventDomain' : domain}
59     if len(fields):
60         throttle_spec['suppressedFieldNames'] = fields
61     if len(pairs):
62         throttle_spec['suppressedNvPairsList'] = pairs
63     return {'command':
64             {'commandType': 'throttlingSpecification',
65              'eventDomainThrottleSpecification': throttle_spec}}
66
67 def command_nvpairs(field_name, pair_names):
68     "return a suppressedNvPairs"
69     return {'nvPairFieldName' : field_name,
70             'suppressedNvPairNames' : pair_names}
71
72 ###############################################################################
73 # Example functions to build up commandLists for various domains.
74 ###############################################################################
75 def command_list_empty():
76     return {'commandList' : []}
77
78 def command_list_provide():
79     return {'commandList' : [command_state()]}
80
81 def command_list_interval(interval):
82     return {'commandList' : [command_interval(interval)]}
83
84 def command_list_fault_suppress_fields():
85     "Throttling Specification - two suppressedFieldNames"
86     fields = ['alarmInterfaceA', 'alarmAdditionalInformation']
87     pairs = []
88     command_list = [command_throttle('fault', fields, pairs)]
89     return {'commandList' : command_list}
90
91 def command_list_fault_suppress_nothing():
92     "Throttling Specification - no suppression"
93     fields = []
94     pairs = []
95     command_list = [command_throttle('fault', fields, pairs)]
96     return {'commandList' : command_list}
97
98 def command_list_fault_suppress_pairs():
99     "Throttling Specification - two suppressedNvPairNames"
100     fields = []
101     pairs = [command_nvpairs('alarmAdditionalInformation',
102                                    ['name1', 'name2'])]
103     command_list = [command_throttle('fault', fields, pairs)]
104     return {'commandList' : command_list}
105
106 def command_list_fault_suppress_fields_and_pairs():
107     "Throttling Specification - a mixture of fields and pairs"
108     fields = ['alarmInterfaceA']
109     pairs = [command_nvpairs('alarmAdditionalInformation',
110                                    ['name1', 'name2'])]
111     command_list = [command_throttle('fault', fields, pairs)]
112     return {'commandList' : command_list}
113
114 def command_list_measurements_suppress_example():
115     "Throttling Specification - measurements"
116     fields = ['numberOfMediaPortsInUse', 'aggregateCpuUsage']
117     pairs = [command_nvpairs('cpuUsageArray',
118                              ['cpu1', 'cpu3'])]
119     command_list = [command_throttle('measurementsForVfScaling',
120                                      fields, pairs)]
121     return {'commandList' : command_list}
122
123 def command_list_mobile_flow_suppress_example():
124     "Throttling Specification - mobile flow"
125     fields = ['radioAccessTechnology', 'samplingAlgorithm']
126     pairs = []
127     command_list = [command_throttle('mobileFlow', fields, pairs)]
128     return {'commandList' : command_list}
129
130 def command_list_state_change_suppress_example():
131     "Throttling Specification - state change"
132     fields = ['reportingEntityId', 'eventType', 'sourceId']
133     pairs = [command_nvpairs('additionalFields', ['Name1'])]
134     command_list = [command_throttle('stateChange', fields, pairs)]
135     return {'commandList' : command_list}
136
137 def command_list_syslog_suppress_example():
138     "Throttling Specification - syslog"
139     fields = ['syslogFacility', 'syslogProc', 'syslogProcId']
140     pairs = [command_nvpairs('additionalFields', ['Name1', 'Name4'])]
141     command_list = [command_throttle('syslog', fields, pairs)]
142     return {'commandList' : command_list}
143
144 def command_list_reset_all_domains():
145     "Throttling Specification - reset all domains"
146     command_list = [command_throttle('fault', [], []),
147                     command_throttle('measurementsForVfScaling', [], []),
148                     command_throttle('mobileFlow', [], []),
149                     command_throttle('stateChange', [], []),
150                     command_throttle('syslog', [], [])]
151     return {'commandList' : command_list}
152
153 def mixed_example():
154     fields = ['alarmInterfaceA']
155     pairs = [command_nvpairs('alarmAdditionalInformation',
156                              ['name1', 'name2'])]
157     command_list = [command_throttle('fault', fields, pairs),
158                     command_interval(10),
159                     command_state()]
160     return {'commandList' : command_list}
161
162 ###############################################################################
163 # Default command line values
164 ###############################################################################
165 DEFAULT_FQDN = "127.0.0.1"
166 DEFAULT_PORT = 30000
167
168 ###############################################################################
169 # Command Line Parsing
170 ###############################################################################
171 parser = optparse.OptionParser()
172 parser.add_option('--fqdn',
173                   action="store",
174                   dest="fqdn",
175                   default=DEFAULT_FQDN)
176 parser.add_option('--port',
177                   action="store",
178                   dest="port",
179                   default=DEFAULT_PORT,
180                   type="int")
181 options, remainder = parser.parse_args()
182
183 ###############################################################################
184 # Derive the Test Control URL
185 ###############################################################################
186 url = 'http://%s:%d/testControl/v1.1/commandList'%(options.fqdn, options.port)
187
188 ###############################################################################
189 # Create JSON and POST it to the Test Control URL.
190 ###############################################################################
191 command_list = command_list_fault_suppress_fields_and_pairs()
192 requests.post(url, json = command_list)