Open Kilda Java Documentation
flow_validation.py
Go to the documentation of this file.
1 # Copyright 2017 Telstra Open Source
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 #
15 
16 import click
17 import requests
18 import sys
19 import uuid
20 
21 
22 @click.command(name='validate-flows-command')
23 @click.pass_obj
24 def validate_flows(ctx):
25  print (ctx)
26  northbound_endpoint = ctx.nb_endpoint
27  user = ctx.nb_user
28  password = ctx.nb_pass
29  correlation_id = str(uuid.uuid4())
30 
31  sys.stdout.write('START VALIDATING FLOW\n')
32  flows = requests.get(northbound_endpoint + '/api/v1/flows',
33  auth=(user, password),
34  headers={'correlation_id': correlation_id}).json()
35  flow_ids = list(map(lambda x: x['flowid'], flows))
36 
37  correct_flows = []
38  incorrect_flows = []
39  for index, flow_id in enumerate(flow_ids):
40  sys.stdout.write('>> Current progress: {}/{}\n'.format(index,
41  len(flow_ids)))
42 
43  path = northbound_endpoint + '/api/v1/flows/{}/validate'.format(flow_id)
44  response = requests.get(path,
45  auth=(user, password),
46  headers={'correlation_id':
47  correlation_id + flow_id})
48  result = response.json()
49 
50  valid = result[0]['as_expected'] and result[1]['as_expected']
51  flow_info = {
52  'FLOW_ID': flow_id,
53  'FORWARD_VALID': result[0]['as_expected'],
54  'REVERSE_VALID': result[1]['as_expected']
55  }
56 
57  if valid:
58  correct_flows.append(flow_info)
59  else:
60  incorrect_flows.append(flow_info)
61 
62  sys.stdout.write('ALL FLOWS ARE PROCESSED\n')
63  print_results(correct_flows, incorrect_flows)
64 
65 
66 def print_results(correct_flows, incorrect_flows):
67  sys.stdout.write('>> VALID FLOWS:\n')
68  for flow in correct_flows:
69  sys.stdout.write('> {}\n'.format(flow))
70 
71  sys.stdout.write('>> INVALID FLOWS:\n')
72  for flow in incorrect_flows:
73  sys.stdout.write('> {}\n'.format(flow))
74 
75  sys.stdout.write('>>> TOTAL VALID FLOWS: {}\n'.format(len(correct_flows)))
76  sys.stdout.write('>>> TOTAL INVALID FLOWS: {}\n'.format(len(incorrect_flows)))
def print_results(correct_flows, incorrect_flows)