Esempio n. 1
0
    def _post(self, request, response):
        try:
            data = request.body.getvalue().decode()
            # constaints are not supported now
            cost_type, _, srcs, dsts = FilteredCostMapLite.parse_input(data)
        except Exception as e:
            logging.warning('Failed to parse input: %s:%s', data, e)
            return errors.bad_request(response, message=e, exception=e)

        try:
            cm = palto.utils.get_json_map(self.costmap, ['meta', 'cost-map'])
            cm_meta = cm['meta']
            cm_data = cm['cost-map']
        except Exception as e:
            logging.error('Failed to get cost map: %s', self.costmap)
            return errors.server_error(response, exception = e)

        if not 'cost-type' in cm_meta:
            TEMPLATE = 'cost map {} returns no \'cost-type\' in meta'
            return errors.server_error(response, TEMPLATE.format(self.costmap))

        if not palto.utils.cost_type_match(cost_type, cm_meta['cost-type']):
            TEMPLATE = 'Cost type {} is not supported by {}'
            return errors.bad_request(TEMPLATE.format(cost_type, self.costmap))

        fcm_data = {}
        srcs = srcs if srcs is not None else cm_data.keys()
        dsts = dsts if dsts is not None else cm_data.keys()
        srcs = srcs & cm_data.keys()
        for src in srcs:
            _dsts = dsts & cm_data[src].keys()
            fcm_data[src] = { dst: cm_data[src][dst] for dst in _dsts }

        output = { 'meta': cm['meta'], 'cost-map': fcm_data }
        return output
Esempio n. 2
0
    def _post(self, request, response):
        try:
            data = request.body.getvalue().decode()
            pids, addrtypes = FilteredNetworkMapLite.parse_input(data)
        except Exception as e:
            logging.warning('Failed to parse input: %s:%s', data, e)
            return errors.bad_request(response, message=e, exception=e)

        try:
            nm = palto.utils.get_json_map(self.networkmap, ['meta', 'network-map'])
            nm_data = nm['network-map']
        except Exception as e:
            logging.error('Failed to get network map: %s', self.networkmap)
            return errors.server_error(response, e)

        fnm_data = {}
        for pid in pids:
            if not pid in nm_data:
                logging.warning('Pid %s is not in %s', pid, self.networkmap)
                continue
            types = addrtypes if addrtypes is not None else nm_data[pid].keys()
            types = types & nm_data[pid].keys()
            fnm_data[pid] = { t: nm_data[pid][t] for t in types }

        output = { 'meta': nm['meta'], 'network-map': fnm_data }
        return output
Esempio n. 3
0
    def _post(self, request, response):
        try:
            data = request.body.getvalue().decode()
            properties, endpoints = EPSLite.parse_input(data)
        except Exception as e:
            logging.warning("Failed to parse input %s", str(request.body))
            return errors.bad_request(response, exception=e)

        try:
            properties = properties & self.prop_urls.keys()
            prop_urls = {prop: self.prop_urls[prop] for prop in properties}
            mapping, vtags = EPSLite.setup_pid_mapping(prop_urls)
        except Exception as e:
            logging.error("Failed to setup mapping")
            return errors.server_error(response, exception=e)

        meta_out = {"dependent-vtags": vtags}
        data_out = {}
        for ep in endpoints:
            family, address = decode_addr(ep)
            if not correct_inet_format(family, address):
                logging.warning("Bad format %s: %s", family, address)
                continue
            props = {}
            for prop in properties:
                prop_map = mapping[prop][family]
                if not address in prop_map:
                    continue
                props.update({prop: prop_map[address]})
            if len(props) > 0:
                data_out[ep] = props

        return {"meta": meta_out, "endpoint-properties": data_out}