Exemple #1
0
    def __init__(self, component, logger, netcfg,
    read_only=False, client=None):
        # Define ruleset attributes
        if not client:
            self.client = createLocalClient()
        else:
            self.client = client
        self.name = None
        self.filename = None
        self.filetype = "ruleset"
        self.is_template = False
        self.read_only = read_only
        self.input_output_rules = component.input_output_rules
        self.format_version = RULESET_VERSION
        self.config = component.config

        # Create object libraries
        self.resources = Resources(self)
        self.protocols = Protocols(self)
        self.applications = Applications(self)
        self.periodicities = Periodicities(self)
        self.operating_systems = OperatingSystems(self)
        self.user_groups = UserGroups(self)
        self.durations = Durations(self)
        self.acls_ipv4 = AclIPv4Rules(self)
        self.acls_ipv6 = AclIPv6Rules(self)
        self.nats = NatRules(self)
        self.rules = {
            'acls-ipv4': self.acls_ipv4,
            'acls-ipv6': self.acls_ipv6,
            'nats': self.nats,
        }
        self.platforms = Platforms(self)

        self.custom_rules = CustomRules(self)
        self.include_templates = {}   # name => IncludeTemplate object
        # order is cosmetic
        self._libraries = odict((
            ('resources', self.resources),
            ('protocols', self.protocols),
            ('platforms', self.platforms),
            ('applications', self.applications),
            ('periodicities', self.periodicities),
            ('operating_systems', self.operating_systems),
            ('user_groups', self.user_groups),
            ('durations', self.durations),
            ('acls_ipv4', self.acls_ipv4),
            ('acls_ipv6', self.acls_ipv6),
            ('nats', self.nats),
        ))

        self.createBaseObjects(logger, netcfg)

        # Generic links, fusion and actions attributes
        self.generic_links = GenericLinks(self)
        self.generic_links.load()
        self.fusion = Fusion(self)
        self.actions = ActionStack(self, UNDO_MAX_SIZE)
Exemple #2
0
 def _createAttributes(cls):
     attributes = []
     for name in dir(cls):
         attr = getattr(cls, name)
         if not isinstance(attr, Attribute):
             continue
         attributes.append((name, attr))
     attributes.sort(key=lambda(name,attr): attr.index)
     return odict(attributes)
Exemple #3
0
    def __init__(self, window, rule_type, rule_class):
        Model.__init__(self, window, rule_type)
        self.compatibility = window.compatibility
        self.rule_type = rule_type
        self.name = rule_type
        self.rule_class = rule_class
        self.enabled = True

        # identifier (int) => AclIPv4, AclIPv6 or Nat object
        self.rules = {}

        # chain key => InputChain, OutputChain or ForwardChain
        # eg. chain=("eth0", "eth2") or chain="INPUT"
        self.chains = odict()
Exemple #4
0
def createUpdatesOdict(all_updates):
    updates_odict = odict()
    for domain, updates_list in all_updates:
        updates = Updates()
        for update in updates_list:
            type, identifiers = update
            if domain.endswith("-chains"):
                _identifiers = []
                for chain, rule_id in identifiers:
                    if isinstance(chain, list):
                        # ['eth0', 'eth2'] => ('eth0', 'eth2')
                        chain = tuple(chain)
                    _identifiers.append((chain, rule_id))
                identifiers = _identifiers
            update = Update(domain, type, *identifiers)
            updates.addUpdate(update)
        updates_odict[domain] = updates
    return updates_odict
Exemple #5
0
    def paint(self, painter, rect, widget_size, draw_highlight):
        GraphXYView.paint(self, painter, rect, widget_size, draw_highlight)
        smooth = 2.5 # this is invert smoothing, increase it to sharpen graph

        if not self.model():
            return

        option = self.viewOptions()

        background = option.palette.base()
        foreground = QPen(option.palette.color(QPalette.Foreground))

        # Handle step by step size increment
        step_width = floor(widget_size.width() / RESIZE_INCREMENT) * RESIZE_INCREMENT
        step_height = floor(widget_size.height() / RESIZE_INCREMENT) * RESIZE_INCREMENT

        if self.model().rowCount(self.rootIndex()) != 0:
            value_max = self.getValueMaxAll()
        else:
            value_max = 0

        # If there is no data yet, or the max is at 0, use a max of 10 to dispaly an axe
        if self.fetcher.fragment.type == 'LoadStream':
            if value_max < 100:
                value_max = 100
        else:
            if value_max == 0:
                value_max = 10

        if len(self.data) != 0:
            xmin = self.getValueMin(0)
            xmax = self.getValueMax(0)
        else:
            return

        if xmin == xmax:
            return # avoid division by 0

        painter.save()

        # Offset to cerrectly center the graph after the resizing due to the step by step increment
        painter.translate((widget_size.width() - step_width) / 2.0, (widget_size.height() - step_height) / 2.0)

        painter.setPen(foreground)

        # Draw lines
        fm = QFontMetrics(painter.font())
        margin_size = fm.width("0")

        if self.fetcher.fragment.type == 'TrafficStream':
            x_axe_off = fm.width(unicode(value_max * 2000))
        else:
            x_axe_off = fm.width(unicode(value_max * 200))

        # Order them by max mean value
        means = {}
        for col in xrange(1, len(self.data[0])):
            means[col] = self.getMeanValue(col)

        def mean_sorter(x, y):
            return cmp(y[1], x[1])

        means_sorted = means.items()
        means_sorted.sort(mean_sorter)

        def slope_by_index(points, index, sens):
            if index >= len(points):
                return 0
            if index == 0:
                return points[index].y() + (points[index+1].y()-points[index].y())/smooth
            if index == (len(points) - 1):
                return points[index].y() - (points[index].y()-points[index-1].y())/smooth
            vpoints = [ points[index-1].y(), points[index].y(), points[index+1].y()]
            if points[index].y() == max(vpoints):
                return points[index].y()
            if points[index].y() == min(vpoints):
                return points[index].y()
            if sens == 0:
                return points[index].y() + (points[index+1].y()-points[index].y())/smooth
                #return points[index].y() + (points[index+1].y() - points[index-1].y()) / (points[index+1].x()-points[index-1].x()) * (points[index+1].x() - points[index].x()) / smooth
            else:
                return points[index].y() - (points[index].y()-points[index-1].y())/smooth
                #return points[index].y() + (points[index+1].y() - points[index-1].y()) / (points[index+1].x()-points[index-1].x()) * (points[index-1].x() - points[index].x()) / smooth

        colors = odict()

        for _col in means_sorted:
            col = _col[0]
            path = QPainterPath()
            path.setFillRule(Qt.WindingFill)
            last_point = None
            height_max = 0.0
            width_max =  step_width - 2 * margin_size - x_axe_off
            height_max = (step_height * (1.0 - TITLE_AREA)) - 2 * margin_size

            points = []

            for row in xrange(len(self.data)):
                index = self.model().index(row, col, self.rootIndex())
                value = index.data().toInt()[0]
                x_value = int(self.data[row][0])
                #print "x=", x_value, "y=", value

                if value >= 0.0:
                    height = height_max * value/value_max

                    x = (float(x_value - xmin) / float(xmax - xmin)) * width_max
                    point = QPointF(x + margin_size + x_axe_off, height_max - height + margin_size)

                    points.append(point)

            for index, point in enumerate(points):
                # draw simple point
                painter.setBrush(QBrush(QColor(self.colours[col]).dark(200)))
                painter.drawEllipse(QRectF(point.x() - 2, point.y() - 2, 4, 4))
            # init drawing
                if index == 0:
                    path.moveTo(point)
                    continue

                px = points[index-1].x() + (points[index].x() - points[index-1].x()) / smooth
                py = slope_by_index(points, index-1, 0)
                c1 = QPointF(px, py)
                px = points[index].x() - ( points[index].x() - points[index-1].x() ) / smooth
                py = slope_by_index(points, index, 1)
                c2 = QPointF(px, py)

                path.cubicTo(c1, c2, point)

            last_point = points[len(points)-1]
            if last_point:
                txt = self.model().headerData(col, Qt.Horizontal).toString()
                txt_width = fm.width(txt)
                txt_height = fm.height()

                colors[txt] = QColor(self.colours[col])
                path.lineTo(QPointF(x + margin_size + x_axe_off, height_max + margin_size))
                path.lineTo(QPointF(margin_size + x_axe_off, height_max + margin_size))

            color = QColor(self.colours[col])
            color.setAlpha(200)

            ## Create the gradient effect
            grad = QLinearGradient(QPointF(0.0, 0.0), QPointF(0.0, height_max))
            grad.setColorAt(1.0, color.dark(150))
            grad.setColorAt(0.95, color)
            grad.setColorAt(0.05, color)
            grad.setColorAt(0.0, Qt.white)

            painter.setBrush(QBrush(grad))

            painter.drawPath(path)

        # Graduations
        nbr_grad = xmax - xmin
        dgrad = 1
        while nbr_grad > 10:
            nbr_grad = floor(nbr_grad / 10)
            dgrad = dgrad * 10

        if nbr_grad <= 2:
            dgrad = dgrad / 10
            nbr_grad = 10

        # Prevent for infinite loops.
        if dgrad < 1:
            dgrad = 1

        dx = (float(dgrad) / float(xmax-xmin)) * width_max
        text_dy = fm.height()

        i = 0
        while (i * dgrad) <= xmax - xmin:

            if self.fetcher.fragment.type != 'TrafficStream' or (self.fetcher.fragment.type == 'TrafficStream' and i % 4 == 0):
                grad_width = fm.width("0")
                painter.drawLine(margin_size + x_axe_off + (i*dx), height_max + margin_size, margin_size + x_axe_off + (i*dx), height_max + margin_size + grad_width)
                text = unicode(int(dgrad * i))

                # Legend drawing:
                painter.translate(margin_size + x_axe_off + (i*dx), height_max + margin_size + 2*grad_width)
                painter.rotate(-45.0)
                int_time = (i * dgrad) + xmin
                text = QString('%ds' % (xmax - int_time))

                txt_width = fm.width(text)
                painter.drawText(QRect(-txt_width, -dx, txt_width, 2*dx), Qt.AlignRight|Qt.AlignVCenter, text)
                painter.rotate(45.0)
                painter.translate(-(margin_size + x_axe_off + (i*dx)), -(height_max + margin_size + 2*grad_width))

            i = i + 1

        interval = 0
        height = height_max + margin_size + txt_width * sin(45) + 30
        for k, v in colors.iteritems():
            painter.setPen(v)
            legendRect = QRect(interval, height, 10, 10)
            painter.drawRect(legendRect)
            painter.fillRect(legendRect, QBrush(v))
            painter.setPen(foreground)
            txt_width = fm.width(k)
            txt_height = fm.height()
            painter.drawText(QRect(interval + 10, height, txt_width, txt_height), Qt.AlignRight|Qt.AlignVCenter, k)
            interval += 20 + txt_width

        painter.translate((step_width - widget_size.width()) / 2.0, (step_height - widget_size.height()) / 2.0)
        painter.restore()