def generate_test(topo, name="test"): """Generate a test topology. """ # create compositions of all actions and conditions actions = [new_conf(action) for action in [change_state, worst_state, best_state]] # generate all conditions with all states conditions = [] # for state in range(4): for cond in (at_least, _all, nok): conditions.append(new_conf(cond)) # list of all possible operators operators = actions[:] for cond in conditions: for statement in actions: for _else in actions: if statement != _else: operator = new_conf(condition, condition=cond, statement=statement, _else=_else) operators.append(operator) # for all operatorscreate all node operators and bind them to the topology for operator in operators: # create an operator node op = TopoNode(info={TASK: operator}) topo.add_elts(op) # bind it to the topo edge = TopoEdge(sources=op, targets=topo) topo.add_elts(edge) # and add three mode nodes bound to the operator node nodes = [TopoNode() for i in range(3)] for node in nodes: topo.add_elts(node) edge = TopoEdge(sources=node, targets=op) topo.add_elts(edge)
def cluster(self, condition, statement, _else): ''' condition = at_least statement = then _else = else value = state dans at_least du V2 min_weight = min (v1 vers v2) ''' return new_conf(cond, condition=condition, statement=statement, _else=_else)
def test_change_state(self): """ Test in case of change state. """ # create a change state operation with minor state change_state_conf = new_conf( change_state, state=Check.MINOR ) self.node.operation = change_state_conf self.node.save(self.manager) self.node.process(event=self.check, manager=self.manager) event_processing(event=self.check, engine=self, manager=self.manager) target = self.manager.get_elts(ids=self.node.id) self.assertEqual(target.state, Check.MINOR)
def test_chain_change_state(self): """ Test to change of state in a chain of nodes. This test consists to link three node in such way: self.node(state=0) -> node(state=0) -> root(state=0) And to propagate the change state task with state = 1 in order to check if root state equals 1. """ # create a simple task which consists to change of state change_state_conf = new_conf( change_state, state=Check.MINOR ) # create a root node with the change state task root = TopoNode(operator=change_state_conf) root.save(self.manager) # create a node with the change state task node = TopoNode(operator=change_state_conf) node.save(self.manager) # create a leaf with the change state task self.node.operation = change_state_conf self.node.save(self.manager) # link node to root rootnode = TopoEdge(targets=root.id, sources=node.id) rootnode.save(self.manager) # link self.node to node self_node = TopoEdge(targets=node.id, sources=self.node.id) self_node.save(self.manager) event_processing(event=self.check, engine=self, manager=self.manager) self.assertEqual(self.count, 3) self.node = self.manager.get_elts(ids=self.node.id) self.assertEqual(self.node.state, Check.MINOR)
def build(self, topo_id): ''' Create the topology V2 using the topology V1 data. ''' f = formatter.Formatter(topo_id) # kind=1 means to get formatted components components = f.get_event_type(kind=1) # Components by operator opcomps = f.match_operator(components) # List of Nodes (TopoNodes) node_list = [] # List of connections between components (topoEdge) conn_list = [] # Label dictionnary info = {} # Create components (type=check) comp_check = components.get(f.EVENT_TYPE[1]) if comp_check is not None: for c in comp_check: if c.values()[0].get('resource') : entity = {'component': unicode(c.values()[0].get('component')),'resource': unicode(None),'connector': unicode(c.values()[0].get('connector')),'connector_name':unicode(c.values()[0].get('connector_name')),'type':unicode('resource')} else: entity = {'component': unicode(c.values()[0].get('component')),'resource': unicode(None),'connector': unicode(c.values()[0].get('connector')),'connector_name':unicode(c.values()[0].get('connector_name')),'type':unicode('component')} #entity = {'component': unicode(c.values()[0].get('component')),'resource': unicode(c.values()[0].get('resource')),'connector': unicode(c.values()[0].get('connector')),'connector_name':unicode(c.values()[0].get('connector_name')),'type':unicode(c.values()[0].get('type'))} src_type_value = c.values()[0].get('source_type') entity[Context.NAME] = c.values()[0].get(src_type_value) entity = self.track_nonetype(entity) if c.values()[0].get('label') is None: info['label'] = c.values()[0].get('component') else: info['label'] = c.values()[0].get('label') node_list.append(self.create_component(c.keys()[0], entity, info=info)) info = {} # Create components (type=selector) comp_selct = components.get(f.EVENT_TYPE[2]) if comp_selct is not None: for c in comp_selct: entity = {'component': unicode(None),'resource': unicode(c.values()[0].get('resource')),'connector': unicode('canopsis'),'connector_name':unicode(c.values()[0].get('connector_name')),'type':unicode(c.values()[0].get('type'))} src_type_value = c.values()[0].get('source_type') entity[Context.NAME] = c.values()[0].get('display_name') entity = self.track_nonetype(entity) if c.values()[0].get('label') is None: info['label'] = c.values()[0].get('component') else: info['label'] = c.values()[0].get('label') node_list.append(self.create_component(c.keys()[0], entity, info=info)) info = {} # Create components (type=topology) comp_topo = components.get(f.EVENT_TYPE[3]) if comp_topo is not None: for c in comp_topo: entity = {'component': unicode(None),'resource': unicode(None),'connector': unicode('canopsis'),'connector_name':unicode(c.values()[0].get('connector_name')),'type':unicode('topo')} #entity = {'component': unicode(c.values()[0].get('component')),'resource': unicode(c.values()[0].get('resource')),'connector': unicode(c.values()[0].get('connector')),'connector_name':unicode(c.values()[0].get('connector_name')),'type':unicode(c.values()[0].get('type'))} src_type_value = c.values()[0].get('source_type') entity[Context.NAME] = c.values()[0].get(src_type_value) entity = self.track_nonetype(entity) if c.values()[0].get('label') is None: info['label'] = c.values()[0].get('component') else: info['label'] = c.values()[0].get('label') node_list.append(self.create_component(c.keys()[0], entity, info=info)) info = {} # Create components (type=operator) # OPERATOR_ID[0] --> Cluster comp_opera = opcomps.get(f.OPERATOR_ID[0]) if comp_opera is not None: for cmps in comp_opera: tmpdict = cmps.values()[0] value = tmpdict.get('options') clt_list = tmpdict.get('form').get('items') if value is None: least_value = clt_list[0].get('value') cond_value = clt_list[1].get('value') stat_value = clt_list[2].get('value') else_value = clt_list[3].get('value') else: least_value = value.get('least') cond_value = value.get('state') stat_value = value.get('then') else_value = value.get('else') least_conf = new_conf(at_least, min_weight=int(least_value), state=int(cond_value)) # Create statement/action if stat_value != '-1': statement = new_conf(action.change_state, state=int(stat_value)) else: statement = new_conf(action.worst_state) # Create the else if else_value != '-1': _else = new_conf(action.change_state, state=int(else_value)) else: _else = new_conf(action.worst_state) # Add the component label if tmpdict.get('label') is None: info['label'] = tmpdict.get('component') else: info['label'] = tmpdict.get('label') node_list.append(self.create_component(cmps.keys()[0], dict_op=self.cluster(least_conf, statement, _else), info=info)) info = {} # OPERATOR_ID[1] --> Worst Sate comp_worst = opcomps.get(f.OPERATOR_ID[1]) if comp_worst is not None: for cmps in comp_worst: tmpdict = cmps.values()[0] # Add the component label if tmpdict.get('label') is None: info['label'] = tmpdict.get('component') else: info['label'] = tmpdict.get('label') node_list.append(self.create_component(cmps.keys()[0], dict_op=new_conf(action.worst_state), info=info)) info = {} # OPERATOR_ID[2] --> And comp_and = opcomps.get(f.OPERATOR_ID[2]) if comp_and is not None: for cmps in comp_and: mydict = cmps.values()[0] value = mydict.get('form').get('items') cond_value = value[0].get('value') stat_value = value[1].get('value') else_value = value[2].get('value') entity = {'component': unicode(mydict.get('component')),'resource': unicode(mydict.get('resource')),'connector': unicode(mydict.get('connector')),'connector_name':unicode(mydict.get('connector_name')),'type':unicode(mydict.get('type'))} entity[Context.NAME] = mydict.get(mydict.get('source_type')) entity = self.track_nonetype(entity) dict_and = {} dict_and['state'] = int(cond_value) # Create the condition condition = new_conf(at_least, **dict_and) # Create the statement/action if stat_value != '-1': statement = new_conf(action.change_state, state=int(stat_value)) else: statement = new_conf(action.worst_state) # Create the else if else_value != '-1': _else = new_conf(action.change_state, state=int(else_value)) else: _else = new_conf(action.worst_state) # Add the component label if mydict.get('label') is None: info['label'] = mydict.get('component') else: info['label'] = mydict.get('label') node_list.append(self.create_component(cmps.keys()[0], dict_op=self.cluster(condition, statement, _else), info=info)) info = {} # OPERATOR_ID[3] --> Or comp_or = opcomps.get(f.OPERATOR_ID[3]) if comp_or is not None: for cmps in comp_or: mydict = cmps.values()[0] value = mydict.get('form').get('items') cond_value = value[0].get('value') stat_value = value[1].get('value') else_value = value[2].get('value') condition = "" statement = "" _else = "" dict_or = {} dict_or['state'] = int(cond_value) entity = {'component': unicode(mydict.get('component')),'resource': unicode(mydict.get('resource')),'connector': unicode(mydict.get('connector')),'connector_name':unicode(mydict.get('connector_name')),'type':unicode(mydict.get('type'))} entity[Context.NAME] = mydict.get(mydict.get('source_type')) entity = self.track_nonetype(entity) # Create the condition condition = new_conf(at_least, **dict_or) # Create the statement/action if stat_value != '-1': statement = new_conf(action.change_state, state=int(stat_value)) else: statement = new_conf(action.worst_state) # Create the _else if else_value != '-1': _else = new_conf(action.change_state, state=int(else_value)) else: _else = new_conf(action.worst_state) # Add the component label if mydict.get('label') is None: info['label'] = mydict.get('component') else: info['label'] = mydict.get('label') node_list.append(self.create_component(cmps.keys()[0], dict_op=self.cluster(condition, statement, _else), info=info)) info = {} # OPERATOR_ID[4] --> Best State comp_best = opcomps.get(f.OPERATOR_ID[4]) if comp_best is not None: for cmps in comp_best: mydict = cmps.values()[0] if mydict.get('label') is None: info['label'] = mydict.get('component') else: info['label'] = mydict.get('label') node_list.append(self.create_component(cmps.keys()[0], dict_op=new_conf(action.best_state), info=info)) info = {} # Create connections between components for tween in f.get_comp_graph(): conn_list.append(self.create_connections(tween[0], tween[1])) # Create the Topology root_id = f.get_root_id() self.create_topology(root_id, conn_list, node_list) self.logger.info('Topology {} is created successfully.'.format(root_id)) print 'Topology {} is created successfully.'.format(root_id)
def at_least(self, dict_data): ''' Create the condition statement. ''' return new_conf(condition.at_least, **dict_data)
def get_default_task(self): """Get default task. """ return new_conf(self.DEFAULT_TASK)