Add prefix according to the specification.

            The following keys can be used:

            vrf             ID of VRF to place the prefix in
            prefix          the prefix to add if already known
            family          address family (4 or 6)
            description     A short description
            expires         Expiry time of assignment
            comment         Longer comment
            node            Hostname of node
            type            Type of prefix; reservation, assignment, host
            status          Status of prefix; assigned, reserved, quarantine
            pool            ID of pool
            country         Country where the prefix is used
            order_id        Order identifier
            customer_id     Customer identifier
            vlan            VLAN ID
            alarm_priority  Alarm priority of prefix
            monitor         If the prefix should be monitored or not

            from-prefix     A prefix the prefix is to be allocated from
            from-pool       A pool (ID) the prefix is to be allocated from
            prefix_length   Prefix length of allocated prefix

def add_prefix(self):
        """ Add prefix according to the specification.

            The following keys can be used:

            vrf             ID of VRF to place the prefix in
            prefix          the prefix to add if already known
            family          address family (4 or 6)
            description     A short description
            expires         Expiry time of assignment
            comment         Longer comment
            node            Hostname of node
            type            Type of prefix; reservation, assignment, host
            status          Status of prefix; assigned, reserved, quarantine
            pool            ID of pool
            country         Country where the prefix is used
            order_id        Order identifier
            customer_id     Customer identifier
            vlan            VLAN ID
            alarm_priority  Alarm priority of prefix
            monitor         If the prefix should be monitored or not

            from-prefix     A prefix the prefix is to be allocated from
            from-pool       A pool (ID) the prefix is to be allocated from
            prefix_length   Prefix length of allocated prefix
        """

        p = Prefix()

        # Sanitize input parameters
        if 'vrf' in request.json:
            try:
                if request.json['vrf'] is None or len(unicode(request.json['vrf'])) == 0:
                    p.vrf = None
                else:
                    p.vrf = VRF.get(int(request.json['vrf']))
            except ValueError:
                return json.dumps({'error': 1, 'message': "Invalid VRF ID '%s'" % request.json['vrf']})
            except NipapError, e:
                return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})

        if 'description' in request.json:
            p.description = validate_string(request.json, 'description')
        if 'expires' in request.json:
            p.expires = validate_string(request.json, 'expires')
        if 'comment' in request.json:
            p.comment = validate_string(request.json, 'comment')
        if 'node' in request.json:
            p.node = validate_string(request.json, 'node')
        if 'status' in request.json:
            p.status = validate_string(request.json, 'status')
        if 'type' in request.json:
            p.type = validate_string(request.json, 'type')

        if 'pool' in request.json:
            if request.json['pool'] is not None:
                try:
                    p.pool = Pool.get(int(request.json['pool']))
                except NipapError, e:
                    return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})

        if 'country' in request.json:
            p.country = validate_string(request.json, 'country')
        if 'order_id' in request.json:
            p.order_id = validate_string(request.json, 'order_id')
        if 'customer_id' in request.json:
            p.customer_id = validate_string(request.json, 'customer_id')
        if 'alarm_priority' in request.json:
            p.alarm_priority = validate_string(request.json, 'alarm_priority')
        if 'monitor' in request.json:
            p.monitor = request.json['monitor']

        if 'vlan' in request.json:
            p.vlan = request.json['vlan']
        if 'tags' in request.json:
            p.tags = request.json['tags']
        if 'avps' in request.json:
            p.avps = request.json['avps']

        # arguments
        args = {}
        if 'from_prefix' in request.json:
            args['from-prefix'] = request.json['from_prefix']
        if 'from_pool' in request.json:
            try:
                args['from-pool'] = Pool.get(int(request.json['from_pool']))
            except NipapError, e:
                return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})
        if 'family' in request.json:
            args['family'] = request.json['family']
        if 'prefix_length' in request.json:
            args['prefix_length'] = request.json['prefix_length']

        # manual allocation?
        if args == {}:
            if 'prefix' in request.json:
                p.prefix = request.json['prefix']

        try:
            p.save(args)
        except NipapError, e:
            return json.dumps({'error': 1, 'message': e.args, 'type': type(e).__name__})

        return json.dumps(p, cls=NipapJSONEncoder)