<commit_before>
#----------------------------------------------------------------------
# Copyright (c) 2011-2013 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
#
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
#----------------------------------------------------------------------

from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
import dateutil.parser
import dateutil.tz
import logging
import os
import re
import uuid
from sqlalchemy import *
from sqlalchemy.orm import mapper

import amsoil.core.pluginmanager as pm

import sfa.trust.gid as gid
from sfa.trust.certificate import Certificate
import geni.util.cred_util as cred_util
import geni.util.cert_util as cert_util
from chapi.Exceptions import *
import chapi.Parameters
from chapi.SliceAuthority import SAv1DelegateBase
import tools.SA_constants as SA
from tools.dbutils import *
from tools.cert_utils import *
from tools.geni_constants import *
from tools.geni_utils import *
from tools.guard_utils import *
from tools.ABACManager import *
from tools.cs_utils import *
from tools.chapi_log import *
from tools.chapi_utils import *

# classes for mapping to sql tables

class Slice(object):
    pass

class Project(object):
    pass

class SliceMember(object):
    pass

class ProjectMember(object):
    pass

class SliverInfo(object):
    pass

class ProjectInvitation(object):
    pass

# Implementation of SA that speaks to GPO Slice and projects table schema
class SAv1PersistentImplementation(SAv1DelegateBase):

    def __init__(self):
        super(SAv1PersistentImplementation, self).__init__()
        self.db = pm.getService('chdbengine')
        self.config = pm.getService('config')
        self.cert = self.config.get('chapi.sa_cert')
        self.key = self.config.get('chapi.sa_key')

        self.logging_service = pm.getService('loggingv1handler')
        self._ma_handler = pm.getService('mav1handler')

        self.portal_admin_email = self.config.get('chapi.portal_admin_email')
        self.portal_help_email = self.config.get('chapi.portal_help_email')
        self.ch_from_email = self.config.get('chapi.ch_from_email')

        self.trusted_root = self.config.get('chapiv1rpc.ch_cert_root')

        self.trusted_root_files = \
            [os.path.join(self.trusted_root, f) \
                 for f in os.listdir(self.trusted_root) if not f.startswith('CAT')]
#        print "TR = " + str(self.trusted_root_files)


        mapper(Slice, self.db.SLICE_TABLE)
        mapper(SliceMember, self.db.SLICE_MEMBER_TABLE)
        mapper(Project, self.db.PROJECT_TABLE)
        mapper(ProjectMember, self.db.PROJECT_MEMBER_TABLE)
        mapper(SliverInfo, self.db.SLIVER_INFO_TABLE)
        mapper(ProjectInvitation, self.db.PROJECT_INVITATION_TABLE, \
                   primary_key = self.db.PROJECT_INVITATION_TABLE.c.id)

    def get_version(self, session):
        version_info = {"VERSION" : chapi.Parameters.VERSION_NUMBER, 
                        "SERVICES" : SA.services,
                        "CREDENTIAL_TYPES" : SA.credential_types, 
                        "FIELDS": SA.supplemental_fields}
        result = self._successReturn(version_info)

        return result

    def get_expiration_query(self, session, type, old_flag, resurrect):
        if type == 'slice':
            table = Slice
            q = session.query(Slice.slice_id, \
                                  Slice.project_id, \
                                  Slice.slice_name)
        else:
            table = Project
            q = session.query(Project.project_id, \
                                  Project.project_name)
        q = q.filter(table.expired == old_flag)
        if resurrect:
            q = q.filter(or_(table.expiration > datetime.utcnow(),table.expiration == None))
        else:
            q = q.filter(table.expiration < datetime.utcnow())

        return q

    def update_expirations(self, client_uuid, type, resurrect, session):
        if resurrect:
            old_flag = True
            new_flag = False
            label = "Restored previously expired "
        else:
            old_flag = False
            new_flag = True
            label = "Expired "

        q = self.get_expiration_query(session, type, old_flag, resurrect)
        rows = q.all()

        if len(rows) > 0:
            q = self.get_expiration_query(session, type, old_flag, resurrect)
            update_fields = {'expired' : new_flag}
            q = q.update(update_fields)

        for row in rows:
            if type == 'slice':
                name = row.slice_name
                attrs = {"SLICE" : row.slice_id, "PROJECT" : row.project_id}
            else:
                name = row.project_name
                attrs = {"PROJECT" : row.project_id}
            self.logging_service.log_event("%s %s %s" % (label, type, name), 
                                           attrs, client_uuid)


    # Check for
    #   Recently expired slices and set their expired flags to 't'
    def update_slice_expirations(self, client_uuid, session):
        self.update_expirations(client_uuid, 'slice', False, session)

    # Check for <commit_msg>fix spacing in log message on expiring/unexpiring slices/projects<commit_after>
#----------------------------------------------------------------------
# Copyright (c) 2011-2013 <NAME>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
#
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
#----------------------------------------------------------------------

from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
import dateutil.parser
import dateutil.tz
import logging
import os
import re
import uuid
from sqlalchemy import *
from sqlalchemy.orm import mapper

import amsoil.core.pluginmanager as pm

import sfa.trust.gid as gid
from sfa.trust.certificate import Certificate
import geni.util.cred_util as cred_util
import geni.util.cert_util as cert_util
from chapi.Exceptions import *
import chapi.Parameters
from chapi.SliceAuthority import SAv1DelegateBase
import tools.SA_constants as SA
from tools.dbutils import *
from tools.cert_utils import *
from tools.geni_constants import *
from tools.geni_utils import *
from tools.guard_utils import *
from tools.ABACManager import *
from tools.cs_utils import *
from tools.chapi_log import *
from tools.chapi_utils import *

# classes for mapping to sql tables

class Slice(object):
    pass

class Project(object):
    pass

class SliceMember(object):
    pass

class ProjectMember(object):
    pass

class SliverInfo(object):
    pass

class ProjectInvitation(object):
    pass

# Implementation of SA that speaks to GPO Slice and projects table schema
class SAv1PersistentImplementation(SAv1DelegateBase):

    def __init__(self):
        super(SAv1PersistentImplementation, self).__init__()
        self.db = pm.getService('chdbengine')
        self.config = pm.getService('config')
        self.cert = self.config.get('chapi.sa_cert')
        self.key = self.config.get('chapi.sa_key')

        self.logging_service = pm.getService('loggingv1handler')
        self._ma_handler = pm.getService('mav1handler')

        self.portal_admin_email = self.config.get('chapi.portal_admin_email')
        self.portal_help_email = self.config.get('chapi.portal_help_email')
        self.ch_from_email = self.config.get('chapi.ch_from_email')

        self.trusted_root = self.config.get('chapiv1rpc.ch_cert_root')

        self.trusted_root_files = \
            [os.path.join(self.trusted_root, f) \
                 for f in os.listdir(self.trusted_root) if not f.startswith('CAT')]
#        print "TR = " + str(self.trusted_root_files)


        mapper(Slice, self.db.SLICE_TABLE)
        mapper(SliceMember, self.db.SLICE_MEMBER_TABLE)
        mapper(Project, self.db.PROJECT_TABLE)
        mapper(ProjectMember, self.db.PROJECT_MEMBER_TABLE)
        mapper(SliverInfo, self.db.SLIVER_INFO_TABLE)
        mapper(ProjectInvitation, self.db.PROJECT_INVITATION_TABLE, \
                   primary_key = self.db.PROJECT_INVITATION_TABLE.c.id)

    def get_version(self, session):
        version_info = {"VERSION" : chapi.Parameters.VERSION_NUMBER, 
                        "SERVICES" : SA.services,
                        "CREDENTIAL_TYPES" : SA.credential_types, 
                        "FIELDS": SA.supplemental_fields}
        result = self._successReturn(version_info)

        return result

    def get_expiration_query(self, session, type, old_flag, resurrect):
        if type == 'slice':
            table = Slice
            q = session.query(Slice.slice_id, \
                                  Slice.project_id, \
                                  Slice.slice_name)
        else:
            table = Project
            q = session.query(Project.project_id, \
                                  Project.project_name)
        q = q.filter(table.expired == old_flag)
        if resurrect:
            q = q.filter(or_(table.expiration > datetime.utcnow(),table.expiration == None))
        else:
            q = q.filter(table.expiration < datetime.utcnow())

        return q

    def update_expirations(self, client_uuid, type, resurrect, session):
        if resurrect:
            old_flag = True
            new_flag = False
            label = "Restored previously expired"
        else:
            old_flag = False
            new_flag = True
            label = "Expired"

        q = self.get_expiration_query(session, type, old_flag, resurrect)
        rows = q.all()

        if len(rows) > 0:
            q = self.get_expiration_query(session, type, old_flag, resurrect)
            update_fields = {'expired' : new_flag}
            q = q.update(update_fields)

        for row in rows:
            if type == 'slice':
                name = row.slice_name
                attrs = {"SLICE" : row.slice_id, "PROJECT" : row.project_id}
            else:
                name = row.project_name
                attrs = {"PROJECT" : row.project_id}
            self.logging_service.log_event("%s %s %s" % (label, type, name), 
                                           attrs, client_uuid)


    # Check for
    #   Recently expired slices and set their expired flags to 't'
    def update_slice_expirations(self, client_uuid, session):
        self.update_expirations(client_uuid, 'slice', False, session)

    # Check for 
{'fraction_non_alphanumeric': 0.10393855205098872, 'fraction_numerical': 0.0026148063409053767, 'mean_word_length': 2.443725379853686, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2488918', 'n_tokens_mistral': 3560, 'n_tokens_neox': 3423, 'n_words': 983}