#!/usr/bin/env python

# Copyright (c) 2015-2018 Dell Inc. or its subsidiaries.
#
# 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 settings.config import Settings
import re
import os
import json
import inspect
import logging
from ConfigParser import ConfigParser
from osp_deployer.settings.config import Settings

logger = logging.getLogger("osp_deployer")


class Profile():
    def __init__(self):

        self.settings = Settings.settings
        self.profile_name = self.settings.profile
        self.definition = self.settings.foreman_configuration_scripts + \
            '/deploy/osp_deployer/profiles/' + self.profile_name + ".json"
        if self.profile_name == "custom":
            logger.info("Using Custom profile")
            return
        if not os.path.isfile(self.definition):
            raise AssertionError(self.profile_name +
                                 " is not a valid " +
                                 " profile in your .ini settings")

    def validate_configuration(self):
        # Make sure the user ini matches the associated profile settings
        if self.profile_name == "custom":
            return
        with open(self.definition) as profile_config:
            profile_definition = json.load(profile_config)
        user_config = ConfigParser()
        user_config.read(self.settings.settings_file)
        profile_config = ConfigParser()
        profile_config.read(os.path.dirname(inspect.getfile(Settings)) +
                            "/" + profile_definition['sample_ini'])
        Err = ''
        for items in profile_definition['associated_settings']:
            for stanza, value in items.iteritems():
                for set, vals in value.iteritems():
                    allowed_settings = []
                    validated = False
                    try:
                        if vals['valid_values']:
                            allowed_settings = vals['valid_values']
                    except:
                        pass
                    try:
                        if vals['validate']:
                            for test in vals['validate']:
                                if 'should_be_valid_ip' in test:
                                    if self.is_valid_ip(user_config.get(str(
                                            stanza), set)) is False:
                                        Err = Err + "\nSetting for " + set + \
                                            " Should be a valid ip adress\n"
                                if 'should_be_valid_int' in test:
                                    if self.is_int(user_config.get(str(
                                            stanza), set)) is False:
                                        Err = Err + "\nSetting for " + set + \
                                            " Should be a valid integer\n"
                                if 'valid_buffer_pool_size' in test:
                                    if self.valid_buffer_pool_size(
                                            user_config.get(str(
                                            stanza), set)) is False:
                                        Err = Err + "\nSetting for " + set + \
                                            " Should be a valid value\n"

                            validated = True
                        if vals['in_range']:
                            for test in vals['in_range']:
                                if self.is_in_range(user_config.get(str(
                                        stanza), set), test) is False:
                                    Err = Err + "\nSetting for " + set + \
                                        " Should be in valid" \
                                        " range "+test+"\n"
                    except:
                        pass
                    if len(allowed_settings) > 0:
                        if user_config.get(str(stanza), set) in \
                                allowed_settings:
                            pass
                        else:
                            Err = Err + "\nYour setting for " + stanza + \
                                "::" + set + \
                                " is not valid for the profile " + \
                                self.profile_name + \
                                ".It should be set to one of the " + \
                                "following options : " + \
                                json.dumps(allowed_settings)
                    elif validated is True:
                        pass
                    else:
                        usr_sett = user_config.get(str(stanza), set)
                        if profile_config.get(str(stanza), set) != usr_sett:
                            Err = Err + "\nYour setting for " + stanza + \
                                "::" + set + \
                                " is not valid for the profile " + \
                                self.profile_name + \
                                ".It should be set to " + \
                                profile_config.get(str(stanza), set)

        if len(Err) > 0:
            logger.info(Err)
            msg = "Your settings diverge from the " + \
                  self.profile_name + " profile" + \
                  "\nReview the settings and errors above" + \
                  " in your .ini file " + \
                  "or change to a custom profile."
            raise AssertionError(msg)

    def is_valid_ip(self, address):
        valid = True
        octets = address.split('.')
        if len(octets) != 4:
            valid = False
        try:
            for octet in octets:
                octet_num = int(octet)
                if octet_num < 0 or octet_num > 255:
                    valid = False
                    break
        except ValueError:
            valid = False
        return valid

    def is_int(self, value):
        valid = True
        try:
            int(value)
        except ValueError:
            valid = False
        return valid

    def is_in_range(self, value, input):
        valid = True
        val_list = input.split('-')
        min_val = int(val_list[0])
        max_val = int(val_list[1]) + 1
        if int(value) not in range(min_val, max_val):
            valid = False
        return valid

    def valid_buffer_pool_size(self, value):
        if value != "dynamic":
            pattern = '^\d+G$'
            valid = re.match(pattern, value)
            if not valid:
                return False
            val = int(value.replace('G', ''))
            if val < 0:
                return False
        return True

@@ -6614,20 +6614,16 @@
         
-val_
 list = i
@@ -6649,31 +6649,23 @@
      min
-_val
  = int(
-val_
 list%5B0%5D)
@@ -6680,23 +6680,15 @@
  max
-_val
  = int(
-val_
 list
@@ -6738,21 +6738,13 @@
 (min
-_val
 , max
-_val
 ):%0A 

modify changes