<commit_before>"""Provides a class for managing BIG-IP Virtual Server resources."""
# coding=utf-8
#
# Copyright (c) 2017-2021 F5 Networks, Inc.
#
# 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.
#



from copy import copy
import logging
import re
from operator import itemgetter
from netaddr import IPAddress

from f5_cccl.resource import Resource
from f5_cccl.resource.ltm.profile import Profile
from f5_cccl.utils.route_domain import normalize_address_with_route_domain


LOGGER = logging.getLogger(__name__)


class VirtualServer(Resource):
    """Virtual Server class for managing configuration on BIG-IP."""

    # FIXME(kenr): This assumes API will include a one-level
    #              path (i.e. the partition)
    ipv4_dest_pattern = re.compile(
        "\\/([a-zA-Z][\\w_\\.-]+)\\/" +
        "((?:[a-zA-Z0-9_\\-\\.]+)(?:%\\d+)?):(\\d+)$"
    )
    ipv6_dest_pattern = re.compile(
        "\\/([a-zA-Z][\\w_\\.-]+)\\/" +
        "((?:[a-fA-F0-9:]+)(?:%\\d+)?)\\.(\\d+)$"
    )
    source_pattern = re.compile(
        r'([\w.:]+)/(\d+)'
    )

    properties = dict(description=None,
                      destination=None,
                      source=None,
                      ipProtocol=None,
                      enabled=None,
                      disabled=None,
                      vlansEnabled=None,
                      vlansDisabled=None,
                      vlans=list(),
                      sourceAddressTranslation=dict(),
                      connectionLimit=0,
                      pool=None,
                      policies=list(),
                      profiles=list(),
                      rules=list(),
                      mask=None)

    def __init__(self, name, partition, default_route_domain, **properties):
        """Create a Virtual server instance."""
        super(VirtualServer, self).__init__(name, partition, **properties)

        for key, default in list(self.properties.items()):
            if key in ["profiles", "policies"]:
                prop = properties.get(key, default)
                self._data[key] = sorted(prop, key=itemgetter('name'))
            elif key == "vlans":
                self._data['vlans'] = sorted(properties.get('vlans', default))
            elif key == "sourceAddressTranslation":
                self._data['sourceAddressTranslation'] = copy(
                    properties.get('sourceAddressTranslation', default))
            else:
                value = properties.get(key, default)
                if value is not None:
                    self._data[key] = value

        # Need to normalize destination and source fields with route domain ID
        try:<commit_msg>Support Translate Address<commit_after>"""Provides a class for managing BIG-IP Virtual Server resources."""
# coding=utf-8
#
# Copyright (c) 2017-2021 F5 Networks, Inc.
#
# 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.
#



from copy import copy
import logging
import re
from operator import itemgetter
from netaddr import IPAddress

from f5_cccl.resource import Resource
from f5_cccl.resource.ltm.profile import Profile
from f5_cccl.utils.route_domain import normalize_address_with_route_domain


LOGGER = logging.getLogger(__name__)


class VirtualServer(Resource):
    """Virtual Server class for managing configuration on BIG-IP."""

    # FIXME(kenr): This assumes API will include a one-level
    #              path (i.e. the partition)
    ipv4_dest_pattern = re.compile(
        "\\/([a-zA-Z][\\w_\\.-]+)\\/" +
        "((?:[a-zA-Z0-9_\\-\\.]+)(?:%\\d+)?):(\\d+)$"
    )
    ipv6_dest_pattern = re.compile(
        "\\/([a-zA-Z][\\w_\\.-]+)\\/" +
        "((?:[a-fA-F0-9:]+)(?:%\\d+)?)\\.(\\d+)$"
    )
    source_pattern = re.compile(
        r'([\w.:]+)/(\d+)'
    )

    properties = dict(description=None,
                      destination=None,
                      source=None,
                      ipProtocol=None,
                      enabled=None,
                      disabled=None,
                      vlansEnabled=None,
                      vlansDisabled=None,
                      vlans=list(),
                      sourceAddressTranslation=dict(),
                      connectionLimit=0,
                      pool=None,
                      policies=list(),
                      profiles=list(),
                      rules=list(),
                      mask=None,
                      translateAddress=None)

    def __init__(self, name, partition, default_route_domain, **properties):
        """Create a Virtual server instance."""
        super(VirtualServer, self).__init__(name, partition, **properties)

        for key, default in list(self.properties.items()):
            if key in ["profiles", "policies"]:
                prop = properties.get(key, default)
                self._data[key] = sorted(prop, key=itemgetter('name'))
            elif key == "vlans":
                self._data['vlans'] = sorted(properties.get('vlans', default))
            elif key == "sourceAddressTranslation":
                self._data['sourceAddressTranslation'] = copy(
                    properties.get('sourceAddressTranslation', default))
            else:
                value = properties.get(key, default)
                if value is not None:
                    self._data[key] = value

        # Need to normalize destination and source fields with route domain ID
        try:
{'fraction_non_alphanumeric': 0.12164916131055024, 'fraction_numerical': 0.007524690390343314, 'mean_word_length': 2.0208333333333335, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, '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': '1983810', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1680, 'n_words': 484}