Open Kilda Java Documentation
main.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 time
18 import socket
19 
20 import click
21 
22 from kilda.probe.command.flow_validation import validate_flows
23 from kilda.probe.command.dump_resource_state import dump_resource_state
24 from kilda.probe.command.list import list_command
25 from kilda.probe.command.monitor import monitor_command, bolt_command
26 from kilda.probe.command.dump_state import dump_state_command
27 from kilda.probe.command.switch_port_status import switch_port_status_command
28 
29 LOG = logging.getLogger(__name__)
30 
31 
32 def init_logger(level):
33  if level <= logging.DEBUG:
34  logging.basicConfig(level=level,
35  format='%(asctime)s - %(name)s - %(levelname)s | '
36  '%(message)s')
37  else:
38  logging.basicConfig(level=level,
39  format='%(asctime)s | %(message)s')
40 
41  logging.getLogger('kafka').setLevel(logging.ERROR)
42  logging.getLogger('neo4j').setLevel(logging.ERROR)
43  logging.getLogger('httpstream').setLevel(logging.ERROR)
44 
45 
47  return 'probe-{}-{}'.format(socket.gethostname(),
48  int(round(time.time() * 1000)))
49 
50 
51 class Context(object):
52  def __init__(self):
53  self._debug = False
54  self._correlation_id = None
55  self._kafka_bootstrap_servers = None
56  self._kafka_topic = None
57  self._timeout = None
58  self._fl_host = None
59  self._neo4j_host = None
60  self._neo4j_user = None
61  self._neo4j_pass = None
62  self._nb_endpoint = None
63  self._nb_user = None
64  self._nb_pass = None
65 
66  @property
67  def debug(self):
68  return self._debug
69 
70  @debug.setter
71  def debug(self, value):
72  self._debug = value
73 
74  @property
75  def correlation_id(self):
76  return self._correlation_id
77 
78  @correlation_id.setter
79  def correlation_id(self, value):
80  self._correlation_id = value
81 
82  @property
84  return self._kafka_bootstrap_servers
85 
86  @kafka_bootstrap_servers.setter
87  def kafka_bootstrap_servers(self, value):
88  self._kafka_bootstrap_servers = value
89 
90  @property
91  def kafka_topic(self):
92  return self._kafka_topic
93 
94  @kafka_topic.setter
95  def kafka_topic(self, value):
96  self._kafka_topic = value
97 
98  @property
99  def fl_host(self):
100  return self._fl_host
101 
102  @fl_host.setter
103  def fl_host(self, value):
104  self._fl_host = value
105 
106  @property
107  def neo4j_host(self):
108  return self._neo4j_host
109 
110  @neo4j_host.setter
111  def neo4j_host(self, value):
112  self._neo4j_host = value
113 
114  @property
115  def neo4j_user(self):
116  return self._neo4j_user
117 
118  @neo4j_user.setter
119  def neo4j_user(self, value):
120  self._neo4j_user = value
121 
122  @property
123  def neo4j_pass(self):
124  return self._neo4j_pass
125 
126  @neo4j_pass.setter
127  def neo4j_pass(self, value):
128  self._neo4j_pass = value
129 
130  @property
131  def timeout(self):
132  return self._timeout
133 
134  @timeout.setter
135  def timeout(self, value):
136  self._timeout = value
137 
138  @property
139  def nb_endpoint(self):
140  return self._nb_endpoint
141 
142  @nb_endpoint.setter
143  def nb_endpoint(self, value):
144  self._nb_endpoint = value
145 
146  @property
147  def nb_user(self):
148  return self._nb_user
149 
150  @nb_user.setter
151  def nb_user(self, value):
152  self._nb_user = value
153 
154  @property
155  def nb_pass(self):
156  return self._nb_pass
157 
158  @nb_pass.setter
159  def nb_pass(self, value):
160  self._nb_pass = value
161 
162 
163 @click.group()
164 @click.option('--debug/--no-debug', default=False, envvar='DEBUG')
165 @click.option('--correlation-id', default=generate_correlation_id())
166 @click.option('--kafka-bootstrap-servers', default='localhost',
167  envvar='KAFKA_BOOTSTRAP_SERVERS')
168 @click.option('--kafka-topic', default='kilda.ctrl', envvar='KAFKA_TOPIC')
169 @click.option('--fl-host', default='http://localhost:8180', envvar='FL')
170 @click.option('--neo4j-host', default='localhost',
171  envvar='NEO4G_HOST')
172 @click.option('--neo4j-user', default='neo4j', envvar='NEO4G_USER')
173 @click.option('--neo4j-pass', default='temppass', envvar='NEO4G_PASS')
174 @click.option('--timeout', default=2)
175 @click.option('--nb-endpoint', default='http://localhost:8080',
176  envvar='NB_ENDPOINT')
177 @click.option('--nb-user', default='kilda', envvar='NB_USER')
178 @click.option('--nb-pass', default='kilda', envvar='NB_PASS')
179 @click.pass_obj
180 def cli(ctx, debug, correlation_id, kafka_bootstrap_servers, kafka_topic,
181  fl_host, neo4j_host, neo4j_user, neo4j_pass, timeout, nb_endpoint,
182  nb_user, nb_pass):
183  init_logger(logging.DEBUG if debug else logging.INFO)
184  ctx.debug = debug
185  ctx.correlation_id = correlation_id
186  LOG.debug('correlation_id = %s', correlation_id)
187  ctx.kafka_bootstrap_servers = kafka_bootstrap_servers
188  ctx.kafka_topic = kafka_topic
189  ctx.timeout = timeout
190  ctx.fl_host = fl_host
191  ctx.neo4j_host = neo4j_host
192  ctx.neo4j_user = neo4j_user
193  ctx.neo4j_pass = neo4j_pass
194  ctx.nb_endpoint = nb_endpoint
195  ctx.nb_user = nb_user
196  ctx.nb_pass = nb_pass
197 
198 
199 cli.add_command(list_command)
200 cli.add_command(monitor_command)
201 cli.add_command(bolt_command)
202 cli.add_command(dump_state_command)
203 cli.add_command(switch_port_status_command)
204 cli.add_command(dump_resource_state)
205 cli.add_command(validate_flows)
206 
207 
208 def main():
209  cli(obj=Context())
def nb_pass(self)
Definition: main.py:155
def correlation_id(self)
Definition: main.py:75
def kafka_topic(self)
Definition: main.py:91
def timeout(self)
Definition: main.py:131
def main()
Definition: main.py:208
def neo4j_user(self)
Definition: main.py:115
def cli(ctx, debug, correlation_id, kafka_bootstrap_servers, kafka_topic, fl_host, neo4j_host, neo4j_user, neo4j_pass, timeout, nb_endpoint, nb_user, nb_pass)
Definition: main.py:182
def nb_user(self)
Definition: main.py:147
def kafka_bootstrap_servers(self)
Definition: main.py:83
def neo4j_pass(self)
Definition: main.py:123
def __init__(self)
Definition: main.py:52
def fl_host(self)
Definition: main.py:99
def init_logger(level)
Definition: main.py:32
def debug(self)
Definition: main.py:67
def neo4j_host(self)
Definition: main.py:107
def nb_endpoint(self)
Definition: main.py:139
def generate_correlation_id()
Definition: main.py:46