Open Kilda Java Documentation
dump_resource_state.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 logging
17 import pprint
18 import json
19 from collections import OrderedDict
20 
21 import click
22 from datetime import datetime
23 import prettytable.prettytable as prettytable
24 from prettytable import PrettyTable
25 
26 import pprint
27 
28 from kilda.probe.entity.message import create_dump_state, \
29  create_resource_dump_state
30 from kilda.probe.messaging import send_with_context
31 from kilda.probe.messaging import receive_with_context_async
32 
33 LOG = logging.getLogger(__name__)
34 
35 
36 def chunks(l, n):
37  """Yield successive n-sized chunks from l."""
38  for i in range(0, len(l), n):
39  yield l[i:i + n]
40 
41 
42 def print_table(records, border):
43  for record in records:
44  data = json.loads(record.value)
45  payload = data['payload']
46  LOG.debug(pprint.pformat(data))
47  table = PrettyTable(['Topology', 'Component', 'Task ID'],
48  border=border)
49  table.add_row(
50  [payload['topology'], payload['component'], payload['task_id']])
51 
52  del payload['state']['clazz']
53 
54  print(table)
55 
56  table = PrettyTable(['Resource', 'List', 'Count'], border=border)
57 
58  for k in ('cookies', 'vlans'):
59 
60  #payload['state'][k] = list(range(1, 4096))
61 
62  data = payload['state'][k]
63 
64  rep = format_int_list(data)
65 
66  table.add_row([k, rep, len(payload['state'][k])])
67 
68  print(table)
69 
70  table = PrettyTable(['SW', 'List', 'Count'])
71 
72  for sw in payload['state']['meters']:
73  data = payload['state']['meters'][sw]
74  rep = format_int_list(data)
75  table.add_row([sw, rep, len(data)])
76 
77  print(table)
78 
79  print('\n')
80 
81 
82 def format_int_list(data):
83  data = list(map(str, data))
84  rep = ""
85  for x in chunks(data, 10):
86  rep += ", ".join(x)
87  rep += "\n"
88  return rep
89 
90 
91 @click.command(name='dump-resource-state')
92 @click.argument('destination')
93 @click.option('--border/--no-border', default=True)
94 @click.option('--table', 'output_type', flag_value='table', default=True)
95 @click.option('--json', 'output_type', flag_value='json')
96 @click.pass_obj
97 def dump_resource_state(ctx, destination, border, output_type):
98  message = create_resource_dump_state(ctx.correlation_id,
99  destination=destination)
100  LOG.debug('command = {}'.format(message.serialize()))
101 
102  with receive_with_context_async(ctx) as records:
103  send_with_context(ctx, message.serialize())
104 
105  if output_type == 'table':
106  print_table(records, border)
107  elif output_type == 'json':
108  for record in records:
109  data = json.loads(record.value)
110  pprint.pprint(data)
def send_with_context(context, message)
Definition: messaging.py:28
def dump_resource_state(ctx, destination, border, output_type)
def create_resource_dump_state(correlation_id, destination)
Definition: message.py:87
def receive_with_context_async(context, expected_count=None)
Definition: messaging.py:43