<commit_before># AM API V2 version of Gram aggregate manager
# For testing against tools (Flack, portal) that speak AM API V2
# Since Gram is written to support V3
from gram import config
from geni.am.am2 import ReferenceAggregateManager
from geni.am.am2 import AggregateManager, AggregateManagerServer
from am3 import GramReferenceAggregateManager as GramReferenceAggregateManager_V3
from GramSecureXMLRPCServer import GramSecureXMLRPCServer
from GramSecureXMLRPCServer import GSecureXMLRPCRequestHandler
import base64
import os
import socket
import uuid
import zlib


class GramReferenceAggregateManager(ReferenceAggregateManager):

    def __init__(self, root_cert, urn_authority, url, certfile, server):

        ReferenceAggregateManager.__init__(self, root_cert, urn_authority, 
                                           url)
        self._v3_am = GramReferenceAggregateManager_V3(root_cert, 
                                                       urn_authority, 
                                                       certfile, url)
        self._certfile = certfile
        self._am_type = "gram"
        self._server = server
        self._v3_am._server = server

    def GetVersion(self, options):
        result = ReferenceAggregateManager.GetVersion(self, options)
        hostname = socket.getfqdn()
        v3_url = "https://%s:%d" % (hostname, config.gram_am_port)
        v2_url = "https://%s:%d" % (hostname, config.gram_am_v2_port)
        geni_api_versions = {'2' : v2_url, '3' : v3_url}
        result['value']['geni_api_versions'] = geni_api_versions
        result['code']['am_type'] = 'GRAM'
        return result

    def ListResources(self, credentials, options):
#        print  "OPTIONS = " + str(options)
        credentials = [self.transform_credential(c) for c in credentials]
        if 'geni_slice_urn' in options:
            slice_urn = options['geni_slice_urn']
            slice_urns = [slice_urn]


            ret_v3 = self._v3_am.Describe(slice_urns, credentials, options)
#            print "LR.Describe = " + str(ret_v3)
            if ret_v3['code']['geni_code'] != 0: return ret_v3
            result = ret_v3['value']['geni_rspec']

        else:
            ret_v3 = self._v3_am.ListResources(credentials, options)
            if ret_v3['code']['geni_code'] != 0: return ret_v3
            print "LR.ListResources = " + str(ret_v3)
            result = ret_v3['value']

#             result = self.advert_header()
#             component_manager_id = self._v3_am._my_urn
#             component_name = str(uuid.uuid4())
#             component_id = 'urn:publicid:geni:gpo:vm+' + component_name
#             exclusive = False
#             sliver_type = 'virtual-machine'
#             available = True
#             tmpl = '''  <node component_manager_id="%s"
#         component_name="%s"
#         component_id="%s"
#         exclusive="%s">
#     <sliver_type name="%s"/>
#     <available now="%s"/>
#   </node></rspec>
#   '''
#             result = self.advert_header() + \
#             (tmpl % (component_manager_id, component_name, \
#                          component_id, exclusive, sliver_type, available)) 

# #            result = self._v3_am.ListResources(credentials, options)
# #            if result['code'] != 0: return result
# #            result = result['value']

#         # Optionally compress the result
#         if 'geni_compressed' in options and options['geni_compressed']:
#             try:
#                 result = base64.b64encode(zlib.compress(result))
#             except Exception, exc:
#                 import traceback
#                 self.logger.error("Error compressing and encoding resource list: %s", traceback.format_exc())
#                 raise Exception("Server error compressing resource list", exc)


#        print "RET_V3 = " + str(type(ret_v3)) + " " + str(ret_v3)
        return self.successResult(result)

    def CreateSliver(self, slice_urn, credentials, rspec, users, options):
#        print "CREDS = " + str(credentials)
#        print "RSPEC = " + str(rspec)
#        print "USERS = " + str(users)
#        print "OPTS = " + str(options)
        credentials = [self.transform_credential(c) for c in credentials]
        urns = [slice_urn]
        # Allocate
        ret_allocate_v3 = self._v3_am.Allocate(slice_urn, credentials, \
                                          rspec, options)
#        print "ALLOC_RET " + str(ret_allocate_v3)

        if ret_allocate_v3['code']['geni_code'] != 0:
            return ret_allocate_v3

        # Provision
        options['geni_users'] = users # In v3, users is an option
        ret_provision_v3 = self._v3_am.Provision(urns, credentials, options)
#        print "PROV_RET " + str(ret_provision_v3)

        if ret_provision_v3['code']['geni_code'] != 0:
            return ret_provision_v3

        manifest = ret_provision_v3['value']['geni_rspec']

         # PerformOperationalAction(geni_start)
        action = 'geni_start'
        self._v3_am.PerformOperationalAction(urns, credentials, \
                                                 action, options)
        return self.successResult(manifest)

    def DeleteSliver(self, slice_urn, credentials, options):
        credentials = [self.transform_credential(c) for c in credentials]
        urns = [slice_urn]
        ret_v3 = self._v3_am.Delete(urns, credentials, options)
        return self.successResult(True)

    def SliverStatus(self, slice_urn, credentials, options):
        credentials = [self.transform_credential(c) for c in credentials]
        urns = [slice_urn]
        ret_v3 = self._v3_am.Status(urns, credentials, options)<commit_msg>Fixed return struct from deletesliver<commit_after># AM API V2 version of Gram aggregate manager
# For testing against tools (Flack, portal) that speak AM API V2
# Since Gram is written to support V3
from gram import config
from geni.am.am2 import ReferenceAggregateManager
from geni.am.am2 import AggregateManager, AggregateManagerServer
from am3 import GramReferenceAggregateManager as GramReferenceAggregateManager_V3
from GramSecureXMLRPCServer import GramSecureXMLRPCServer
from GramSecureXMLRPCServer import GSecureXMLRPCRequestHandler
import base64
import os
import socket
import uuid
import zlib


class GramReferenceAggregateManager(ReferenceAggregateManager):

    def __init__(self, root_cert, urn_authority, url, certfile, server):

        ReferenceAggregateManager.__init__(self, root_cert, urn_authority, 
                                           url)
        self._v3_am = GramReferenceAggregateManager_V3(root_cert, 
                                                       urn_authority, 
                                                       certfile, url)
        self._certfile = certfile
        self._am_type = "gram"
        self._server = server
        self._v3_am._server = server

    def GetVersion(self, options):
        result = ReferenceAggregateManager.GetVersion(self, options)
        hostname = socket.getfqdn()
        v3_url = "https://%s:%d" % (hostname, config.gram_am_port)
        v2_url = "https://%s:%d" % (hostname, config.gram_am_v2_port)
        geni_api_versions = {'2' : v2_url, '3' : v3_url}
        result['value']['geni_api_versions'] = geni_api_versions
        result['code']['am_type'] = 'GRAM'
        return result

    def ListResources(self, credentials, options):
#        print  "OPTIONS = " + str(options)
        credentials = [self.transform_credential(c) for c in credentials]
        if 'geni_slice_urn' in options:
            slice_urn = options['geni_slice_urn']
            slice_urns = [slice_urn]


            ret_v3 = self._v3_am.Describe(slice_urns, credentials, options)
#            print "LR.Describe = " + str(ret_v3)
            if ret_v3['code']['geni_code'] != 0: return ret_v3
            result = ret_v3['value']['geni_rspec']

        else:
            ret_v3 = self._v3_am.ListResources(credentials, options)
            if ret_v3['code']['geni_code'] != 0: return ret_v3
            print "LR.ListResources = " + str(ret_v3)
            result = ret_v3['value']

#             result = self.advert_header()
#             component_manager_id = self._v3_am._my_urn
#             component_name = str(uuid.uuid4())
#             component_id = 'urn:publicid:geni:gpo:vm+' + component_name
#             exclusive = False
#             sliver_type = 'virtual-machine'
#             available = True
#             tmpl = '''  <node component_manager_id="%s"
#         component_name="%s"
#         component_id="%s"
#         exclusive="%s">
#     <sliver_type name="%s"/>
#     <available now="%s"/>
#   </node></rspec>
#   '''
#             result = self.advert_header() + \
#             (tmpl % (component_manager_id, component_name, \
#                          component_id, exclusive, sliver_type, available)) 

# #            result = self._v3_am.ListResources(credentials, options)
# #            if result['code'] != 0: return result
# #            result = result['value']

#         # Optionally compress the result
#         if 'geni_compressed' in options and options['geni_compressed']:
#             try:
#                 result = base64.b64encode(zlib.compress(result))
#             except Exception, exc:
#                 import traceback
#                 self.logger.error("Error compressing and encoding resource list: %s", traceback.format_exc())
#                 raise Exception("Server error compressing resource list", exc)


#        print "RET_V3 = " + str(type(ret_v3)) + " " + str(ret_v3)
        return self.successResult(result)

    def CreateSliver(self, slice_urn, credentials, rspec, users, options):
#        print "CREDS = " + str(credentials)
#        print "RSPEC = " + str(rspec)
#        print "USERS = " + str(users)
#        print "OPTS = " + str(options)
        credentials = [self.transform_credential(c) for c in credentials]
        urns = [slice_urn]
        # Allocate
        ret_allocate_v3 = self._v3_am.Allocate(slice_urn, credentials, \
                                          rspec, options)
#        print "ALLOC_RET " + str(ret_allocate_v3)

        if ret_allocate_v3['code']['geni_code'] != 0:
            return ret_allocate_v3

        # Provision
        options['geni_users'] = users # In v3, users is an option
        ret_provision_v3 = self._v3_am.Provision(urns, credentials, options)
#        print "PROV_RET " + str(ret_provision_v3)

        if ret_provision_v3['code']['geni_code'] != 0:
            return ret_provision_v3

        manifest = ret_provision_v3['value']['geni_rspec']

         # PerformOperationalAction(geni_start)
        action = 'geni_start'
        self._v3_am.PerformOperationalAction(urns, credentials, \
                                                 action, options)
        return self.successResult(manifest)

    def DeleteSliver(self, slice_urn, credentials, options):
        credentials = [self.transform_credential(c) for c in credentials]
        urns = [slice_urn]
        ret_v3 = self._v3_am.Delete(urns, credentials, options)
        return ret_v3

    def SliverStatus(self, slice_urn, credentials, options):
        credentials = [self.transform_credential(c) for c in credentials]
        urns = [slice_urn]
        ret_v3 = self._v3_am.Status(urns, credentials, options)
{'fraction_non_alphanumeric': 0.1026612618963694, 'fraction_numerical': 0.011191399365526966, 'mean_word_length': 2.3557066824364283, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1744668', 'n_tokens_mistral': 3388, 'n_tokens_neox': 3338, 'n_words': 721}