Esempio n. 1
0
    def save(self):
        interface_code = self.cleaned_data['file'].read()
        parser = InterfaceParser()

        interface = parser.parse(interface_code)

        with transaction.commit_on_success():
            i = Interface(name=interface['interface_name'], interface_file=self.cleaned_data['file'])
            i.save()

            for method in interface['precondition_methods']:
                m = Method(name=method['method_name'], interface=i)
                m.save()

                for parameter in method['parameters']:
                    mp = MethodParameter(method=m, name=parameter)
                    mp.save()

            for trigger in interface['trigger_methods']:
                m = Method.objects.get(interface__name=interface['interface_name'], name=trigger)

                t = Trigger(method=m)
                t.save()

            # Send file to Mirri
            event_handler = EventHandler()
            try:
                client = MirriClient()
                client.upload_interface_file(interface['interface_name'], self.cleaned_data['file'])
            except (MirriConnectionError, MirriTimeoutError, MirriNotFoundError), e:
                logger.error('Interface file %s could not be posted to Mirri: %s' % (self.cleaned_data['file'].name, e))
                event_handler.add_event('Interface file %s could not be posted to Mirri: %s' % (self.cleaned_data['file'].name, e))
            else:
Esempio n. 2
0
def schedule_file(request):
    if request.method == 'POST':
        form = UploadScheduleFileForm(request.POST, request.FILES)
        
        if form.is_valid():
            try:
                form.save()           
            except Exception, e:
                return HttpResponseServerError(e)
            
            event_handler = EventHandler()
            event_handler.add_event(u'Schedule file %s was uploaded and parsed successfully' % form.cleaned_data['file'].name)
            
            return HttpResponseRedirect(reverse('admin:core_schedule_changelist'))
Esempio n. 3
0
def parse_schedules(request):
    form = UploadScheduleFileForm(request.POST, request.FILES)

    if form.is_valid():
        try:
            form.save()
        except Exception, e:
            return HttpResponseBadRequest(e)

        event_handler = EventHandler()
        event_handler.add_event(u'Schedule file %s was uploaded and parsed successfully' % request.FILES['file'].name)
        logger.debug(u'Schedule file %s was uploaded and parsed successfully' % request.FILES['file'].name)

        return HttpResponse(status=201)
Esempio n. 4
0
    def save(self):
        action_code = self.cleaned_data['file'].read()
        parser = ActionParser()

        action = parser.parse(action_code)

        with transaction.commit_on_success():
            a = Action(name=action['action_name'], precondition_expression=action['precondition_expression'], action_file=self.cleaned_data['file'])
            a.save()

            for parameter in action['parameters']:
                ap = ActionParameter(action=a, name=parameter['name'], parameter_position=parameter['position'])
                ap.save()

            action_devices = {}

            for key, device in action['devices'].items():
                ad = ActionDevice(action=a, name=key, parameter_position=device['parameter_position'])
                ad.save()

                action_devices[key] = ad

                for interface_name in device['interfaces']:

                    i = Interface.objects.get(name=interface_name)

                    adi = ActionDeviceInterface(action_device=ad, interface=i)
                    adi.save()

            for position, replacement in enumerate(action['precondition_replacements']):

                apm = ActionPreconditionMethod(expression_position=position, action=a, action_device=action_devices[replacement['device']])

                m = Method.objects.get(name=replacement['method'], interface__name=replacement['interface'])

                apm.method = m
                apm.save()

            # Send file to Mirri
            event_handler = EventHandler()
            try:
                client = MirriClient()
                client.upload_action_file(self.cleaned_data['file'])
            except (MirriConnectionError, MirriTimeoutError, MirriNotFoundError), e:
                logger.error('Action file %s could not be posted to Mirri: %s' % (self.cleaned_data['file'].name, e))
                event_handler.add_event('Action file %s could not be posted to Mirri: %s' % (self.cleaned_data['file'].name, e))
            else:
Esempio n. 5
0
     
     for interface in methods.keys():
         for m in methods[interface]:
             attribute_name = '%s_%s' % (models.Method.objects.get(id=m).name, mac_address)
             value = "true" if models.StateValue.objects.filter(device=device.id, method=m, value="True").exists() else "false"
             selections.append({'name': attribute_name, 'value': value})
 
 # Send configuration selections to Caas
 client = CaasClient()
 try:
     response = client.get_configuration(configuration_model_name, selections)
 except (CaasConnectionError, CaasTimeoutError, CaasNotFoundError, CaasInternalServerError), e:
     logger.error('The request to Caas failed: %s' % e)
     event_handler.add_event('The request to Caas failed: %s' % e)
 else:
     event_handler.add_event('Configuration request for model %s with selections %r sent to Caas' % (configuration_model_name, selections))
     configuration = response
 
     logger.debug(configuration)
 
 
     action_name = configuration['action_name']
     mirri_payload = []
     argument_values = []
     
     device_list = []
     for device in configuration['devices']:
         try:
             device_obj = models.Device.objects.get(mac_address=device['mac_address'])
         except models.Device.DoesNotExist:
             logger.error('Device %s does not exist' % mac_address)
Esempio n. 6
0
                
                configurations += configuration
            
        else:            
            wcrl_model = model_generator.generate_configuration_model(actions)
            wcrl_selections = model_generator.generate_configuration_selections(actions)
            model_name = 'test'
            
            try:
                configurations = client.upload_and_get_wcrl_configuration(model_name, wcrl_model, wcrl_selections)
            except (CaasConnectionError, CaasTimeoutError, CaasNotFoundError, CaasInternalServerError), e:
                logger.error('The request to Caas failed: {0}'.format(e))
                event_handler.add_event('The request to Caas failed: {0}'.format(e))
                return HttpResponseBadRequest()
            else:
                event_handler.add_event('Configuration request for model {0} with selections {1} sent to Caas'.format(model_name, wcrl_selections))
        
        if configurations == None:
            return HttpResponse(status=204)

        # let's create a simple random-largest -recommender
        try:
            recommender = Recommender()
            selected = recommender.recommend( configurations )
        except RecommenderException, e:
            logger.debug( 'Caught recommender exception: {0}'.format(e) )
            selected = random.choice( configurations )[0]
        
        # clear out the added string elements for configuration
        ret = {}
        ret_data = {'roles':[]}
Esempio n. 7
0
        client = ProximityClient()
        try:
            client.set_group(self.mac_address, neighbours)
        except ProximityClientConnectionError, e:
            logger.error(e)
            event_handler.add_event(e)
        else:
            true_neighbour_devices = (Device.objects
                                            .filter(mac_address__in=neighbours))
            true_neighbours = []
            for device in true_neighbour_devices:
                neighbours.remove(device.mac_address)
                true_neighbours.append(device.mac_address)

            event_handler.add_event(u'%s updated its proximity devices: %r - %r' %
                                    (self.mac_address,
                                     true_neighbours,
                                     neighbours))

    def get_proximity_mac_addresses(self):
        """Get the mac addresses of the proximity devices.

        Returns:
            * A list of mac_addresses as strings, e.g.,
              ['aa:aa:aa:aa:aa:aa', 'bb:bb:bb:bb:bb:bb']
            * None, if the connection to the proximity server failed.

        """
        client = ProximityClient()
        try:
            mac_addresses = client.get_group(self.mac_address)
        except ProximityClientConnectionError, e: