<commit_before># -*- encoding: utf-8 -*-
#
# Copyright © 2012 New Dream Network, LLC (DreamHost)
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# @author: <NAME>, New Dream Network, LLC (DreamHost)
# @author: <NAME>, New Dream Network, LLC (DreamHost)

import sqlalchemy as sa
from sqlalchemy import Column, String, ForeignKey
from sqlalchemy import orm
from sqlalchemy.orm import validates


from quantum.api import api_common as common
from quantum.db import model_base
from quantum.db import models_v2 as models
from quantum.openstack.common import timeutils

from datetime import datetime

BASE = model_base.BASE


#DreamHost PortFoward, Firewall(FilterRule), AddressBook models as
#Quantum extensions
class PortForward(model_base.BASEV2, models.HasId, models.HasTenant):

    __tablename__ = 'portfowards'

    name = sa.Column(sa.String(255))
    public_port = sa.Column(sa.Integer, nullable=False)
    instance_id = sa.Column(sa.String(36), nullable=False)
    private_port = sa.Column(sa.Integer, nullable=True)
    # Quantum port address are stored in ipallocation which are internally
    # referred to as fixed_id, thus the name below.
    # XXX can we add a docsting to this model that explains how fixed_id is
    # used?
    fixed_id = sa.Column(
        sa.String(36), sa.ForeignKey('ipallocations.id', ondelete="CASCADE"),
        nullable=True)
    op_status = Column(String(16))

    #PortForward Model Validators using sqlalchamey simple validators
    @validates('name')
    def validate_name(self, key, name):
        assert isinstance(name, basestring) is str
        assert len(name) <= 255
        return name

    @validates('public_port')
    def validate_public_port(self, key, public_port):
        assert isinstance(public_port) is int
        return public_port

    @validates('instance_id')
    def validate_instance_id(self, key, instance_id):
        assert isinstance(instance_id, basestring) is str
        assert len(instance_id) <= 36
        return instance_id

    @validates('private_port')
    def validate_private_port(self, key, private_port):
        assert isinstance(private_port) is int
        return private_port

    @validates('fixed_id')
    def validate_fixed_id(self, key, fixed_id):
        assert isinstance(fixed_id, basestring) is str
        assert len(fixed_id) <= 36
        return fixed_id

    @validates('op_status')
    def validate_op_status(self, key, op_status):
        assert isinstance(op_status, basestring) is str<commit_msg>Added port range validator for PortForward Model<commit_after># -*- encoding: utf-8 -*-
#
# Copyright © 2012 New Dream Network, LLC (DreamHost)
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
# @author: <NAME>, New Dream Network, LLC (DreamHost)
# @author: <NAME>, New Dream Network, LLC (DreamHost)

import sqlalchemy as sa
from sqlalchemy import Column, String, ForeignKey
from sqlalchemy import orm
from sqlalchemy.orm import validates


from quantum.api import api_common as common
from quantum.db import model_base
from quantum.db import models_v2 as models
from quantum.openstack.common import timeutils

from datetime import datetime

BASE = model_base.BASE


#DreamHost PortFoward, Firewall(FilterRule), AddressBook models as
#Quantum extensions

#VALIDATORS
#Validate private and public port ranges
def _validate_port_range(port, valid_values=None):
    min_value = valid_values[0]
    max_value = valid_values[65536]
    if port >= min_value and port <= max_value:
        return
    else:
        msg_dict = dict(port=port, min_value=min_value, 
            max_value=max_value)
        msg = _("%(port) is not the range between %(min_value)"
            "and %(max_value)") % msg_dict
        LOG.debug("validate_port_range: %s", msg)
        return msg


class PortForward(model_base.BASEV2, models.HasId, models.HasTenant):

    __tablename__ = 'portfowards'

    name = sa.Column(sa.String(255))
    public_port = sa.Column(sa.Integer, nullable=False)
    instance_id = sa.Column(sa.String(36), nullable=False)
    private_port = sa.Column(sa.Integer, nullable=True)
    # Quantum port address are stored in ipallocation which are internally
    # referred to as fixed_id, thus the name below.
    # XXX can we add a docsting to this model that explains how fixed_id is
    # used?
    fixed_id = sa.Column(
        sa.String(36), sa.ForeignKey('ipallocations.id', ondelete="CASCADE"),
        nullable=True)
    op_status = Column(String(16))

    #PortForward Model Validators using sqlalchamey simple validators

    @validates('name')
    def validate_name(self, key, name):
        assert isinstance(name, basestring) is str
        assert len(name) <= 255
        return name

    @validates('public_port')
    def validate_public_port(self, key, public_port):
        public_port = int(public_port)
        assert _validate_port_range(public_port)
        return public_port

    @validates('instance_id')
    def validate_instance_id(self, key, instance_id):
        assert isinstance(instance_id, basestring) is str
        assert len(instance_id) <= 36
        return instance_id

    @validates('private_port')
    def validate_private_port(self, key, private_port):
        private_port = int(private_port)
        assert _validate_port_range(private_port)
        return private_port

    @validates('fixed_id')
    def validate_fixed_id(self, key, fixed_id):
        assert isinstance(fixed_id, basestring) is str
        assert len(fixed_id) <= 36
        return fixed_id

    @validates('op_status')
    def validate_op_status(self, key, op_status):
        assert isinstance(op_status, basestring) is str
{'fraction_non_alphanumeric': 0.08411214953271028, 'fraction_numerical': 0.00904431715405487, 'mean_word_length': 3.787157287157287, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '635055', 'n_tokens_mistral': 1992, 'n_tokens_neox': 1916, 'n_words': 641}