|
3 | 3 | from time import sleep |
4 | 4 |
|
5 | 5 | import requests |
| 6 | +from mdps_ds_lib.lib.cumulus_stac.item_transformer import ItemTransformer |
6 | 7 | from mdps_ds_lib.lib.utils.file_utils import FileUtils |
7 | 8 |
|
8 | 9 | from mdps_ds_lib.lib.aws.aws_s3 import AwsS3 |
9 | 10 |
|
10 | 11 | from mdps_ds_lib.lib.aws.aws_message_transformers import AwsMessageTransformers |
11 | 12 | from mdps_ds_lib.lib.utils.json_validator import JsonValidator |
| 13 | +from pystac import Item |
12 | 14 |
|
13 | 15 | from cumulus_lambda_functions.lib.uds_db.granules_db_index import GranulesDbIndex |
14 | 16 | from mdps_ds_lib.lib.aws.aws_sns import AwsSns |
@@ -147,6 +149,85 @@ def send_to_daac(self, event: dict): |
147 | 149 | self.send_to_daac_internal(uds_cnm_json) |
148 | 150 | return |
149 | 151 |
|
| 152 | + def __extract_files_maap(self, asset_dict, daac_config): |
| 153 | + result_files = [] |
| 154 | + # https://github.com/podaac/cloud-notification-message-schema |
| 155 | + for k, v in asset_dict.items(): |
| 156 | + # if v.roles[0]['type'] not in archiving_types: |
| 157 | + # continue |
| 158 | + temp = { |
| 159 | + 'type': v.roles[0], |
| 160 | + 'uri': self.revert_to_s3_url(v.href), |
| 161 | + 'name': os.path.basename(v.href), |
| 162 | + 'checksumType': 'md5', |
| 163 | + 'checksum': v.extra_fields['file:checksum'], |
| 164 | + 'size': v.extra_fields['file:size'] |
| 165 | + } |
| 166 | + result_files.append(temp) # TODO remove missing md5? |
| 167 | + |
| 168 | + if 'archiving_types' not in daac_config or len(daac_config['archiving_types']) < 1: |
| 169 | + return result_files # TODO remove missing md5? |
| 170 | + archiving_types = {k['data_type']: [] if 'file_extension' not in k else k['file_extension'] for k in daac_config['archiving_types']} |
| 171 | + result_files1 = [] |
| 172 | + for each_file in result_files: |
| 173 | + if each_file['type'] not in archiving_types: |
| 174 | + continue |
| 175 | + file_extensions = archiving_types[each_file['type']] |
| 176 | + if len(file_extensions) < 1: |
| 177 | + result_files1.append(each_file) # TODO remove missing md5? |
| 178 | + continue |
| 179 | + temp_filename = each_file['name'].upper().strip() |
| 180 | + if any([temp_filename.endswith(k.upper()) for k in file_extensions]): |
| 181 | + result_files1.append(each_file) # TODO remove missing md5? |
| 182 | + return result_files1 |
| 183 | + |
| 184 | + def send_to_daac_maap(self, granules_json): |
| 185 | + daac_configs = self.__archive_index_logic.percolate_maap_document(granules_json) |
| 186 | + if daac_configs is None or len(daac_configs) < 1: |
| 187 | + LOGGER.debug(f'this granule is not configured for archival: {granules_json}') |
| 188 | + return |
| 189 | + granules_item = Item.from_dict(granules_json) |
| 190 | + errors = [] |
| 191 | + for each_daac_config in daac_configs: |
| 192 | + LOGGER.debug(f'working on {each_daac_config}') |
| 193 | + result = JsonValidator(UdsArchiveConfigIndex.db_record_schema).validate(each_daac_config) |
| 194 | + if result is not None: |
| 195 | + errors.append(f'each_daac_config does not have valid schema. Pls re-add the daac config: {result} for {each_daac_config}') |
| 196 | + continue |
| 197 | + try: |
| 198 | + self.__sns.set_topic_arn(each_daac_config['daac_sns_topic_arn']) |
| 199 | + daac_cnm_message = { |
| 200 | + "collection": { |
| 201 | + 'name': each_daac_config['daac_collection_name'], |
| 202 | + 'version': each_daac_config['daac_data_version'], |
| 203 | + }, |
| 204 | + "identifier": granules_item.id, |
| 205 | + "submissionTime": f'{TimeUtils.get_current_time()}Z', |
| 206 | + "provider": each_daac_config['daac_provider'], |
| 207 | + "version": "1.6.0", # TODO this is hardcoded? |
| 208 | + "product": { |
| 209 | + "name": granules_item.id, |
| 210 | + # "dataVersion": daac_config['daac_data_version'], |
| 211 | + 'files': self.__extract_files_maap(granules_item.assets, each_daac_config), |
| 212 | + } |
| 213 | + } |
| 214 | + LOGGER.debug(f'daac_cnm_message: {daac_cnm_message}') |
| 215 | + self.__sns.set_external_role(each_daac_config['daac_role_arn'], |
| 216 | + each_daac_config['daac_role_session_name']).publish_message( |
| 217 | + json.dumps(daac_cnm_message), True) |
| 218 | + return { |
| 219 | + 'archive_status': 'cnm_s_success', |
| 220 | + 'archive_error_message': '', |
| 221 | + 'archive_error_code': '', |
| 222 | + } |
| 223 | + except Exception as e: |
| 224 | + LOGGER.exception(f'failed during archival process') |
| 225 | + return { |
| 226 | + 'archive_status': 'cnm_s_failed', |
| 227 | + 'archive_error_message': str(e), |
| 228 | + } |
| 229 | + return |
| 230 | + |
150 | 231 | def receive_from_daac(self, event: dict): |
151 | 232 | LOGGER.debug(f'receive_from_daac#event: {event}') |
152 | 233 | sns_msg = AwsMessageTransformers().sqs_sns(event) |
|
0 commit comments