| 
									
										
										
										
											2016-09-06 16:37:24 -07:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Copyright 2015 LinkedIn Corp. All rights reserved.
 | 
					
						
							|  |  |  |  *
 | 
					
						
							|  |  |  |  * 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.
 | 
					
						
							|  |  |  |  */
 | 
					
						
							| 
									
										
										
										
											2016-10-10 14:49:14 -07:00
										 |  |  | package models.kafka;
 | 
					
						
							| 
									
										
										
										
											2016-09-06 16:37:24 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | import com.fasterxml.jackson.databind.JsonNode;
 | 
					
						
							|  |  |  | import com.fasterxml.jackson.databind.ObjectMapper;
 | 
					
						
							|  |  |  | import models.daos.DatasetInfoDao;
 | 
					
						
							|  |  |  | import org.apache.avro.generic.GenericData;
 | 
					
						
							|  |  |  | import play.Logger;
 | 
					
						
							|  |  |  | import wherehows.common.schemas.Record;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public class MetadataChangeProcessor {
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-06 13:33:45 -07:00
										 |  |  |   private final String[] CHANGE_ITEMS =
 | 
					
						
							|  |  |  |       {"schema", "owners", "datasetProperties", "references", "partitionSpec", "deploymentInfo", "tags",
 | 
					
						
							| 
									
										
										
										
											2016-11-11 17:25:41 -08:00
										 |  |  |           "constraints", "indices", "capacity", "privacyCompliancePolicy", "securitySpecification"};
 | 
					
						
							| 
									
										
										
										
											2016-10-06 13:33:45 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-06 16:37:24 -07:00
										 |  |  |   /**
 | 
					
						
							|  |  |  |    * Process a MetadataChangeEvent record
 | 
					
						
							|  |  |  |    * @param record GenericData.Record
 | 
					
						
							|  |  |  |    * @param topic String
 | 
					
						
							|  |  |  |    * @throws Exception
 | 
					
						
							|  |  |  |    * @return null
 | 
					
						
							|  |  |  |    */
 | 
					
						
							|  |  |  |   public Record process(GenericData.Record record, String topic)
 | 
					
						
							|  |  |  |       throws Exception {
 | 
					
						
							|  |  |  |     if (record != null) {
 | 
					
						
							| 
									
										
										
										
											2016-10-06 13:33:45 -07:00
										 |  |  |       Logger.debug("Processing Metadata Change Event record. ");
 | 
					
						
							| 
									
										
										
										
											2016-09-06 16:37:24 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       final GenericData.Record auditHeader = (GenericData.Record) record.get("auditHeader");
 | 
					
						
							|  |  |  |       if (auditHeader == null || auditHeader.get("time") == null) {
 | 
					
						
							|  |  |  |         Logger.info("MetadataChangeEvent without auditHeader, abort process. " + record.toString());
 | 
					
						
							|  |  |  |         return null;
 | 
					
						
							|  |  |  |       }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-06 13:33:45 -07:00
										 |  |  |       final GenericData.Record datasetIdentifier = (GenericData.Record) record.get("datasetIdentifier");
 | 
					
						
							|  |  |  |       final GenericData.Record datasetProperties = (GenericData.Record) record.get("datasetProperties");
 | 
					
						
							|  |  |  |       final String urn = String.valueOf(record.get("urn"));
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (urn == null && (datasetProperties == null || datasetProperties.get("uri") == null)
 | 
					
						
							|  |  |  |           && datasetIdentifier == null) {
 | 
					
						
							|  |  |  |         Logger.info("Can't identify dataset from uri/urn/datasetIdentifier, abort process. " + record.toString());
 | 
					
						
							|  |  |  |         return null;
 | 
					
						
							| 
									
										
										
										
											2017-03-14 17:19:30 -07:00
										 |  |  |       } else if (urn != null) {
 | 
					
						
							|  |  |  |         Logger.debug("URN: " + urn);
 | 
					
						
							|  |  |  |       } else if (datasetProperties != null && datasetProperties.get("uri") != null) {
 | 
					
						
							|  |  |  |         Logger.debug("URI: " + datasetProperties.get("uri"));
 | 
					
						
							|  |  |  |       } else {
 | 
					
						
							|  |  |  |         Logger.debug(
 | 
					
						
							|  |  |  |             "Dataset Identifier: " + datasetIdentifier.get("dataPlatformUrn") + datasetIdentifier.get("nativeName"));
 | 
					
						
							| 
									
										
										
										
											2016-09-06 16:37:24 -07:00
										 |  |  |       }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-06 13:33:45 -07:00
										 |  |  |       final JsonNode rootNode = new ObjectMapper().readTree(record.toString());
 | 
					
						
							| 
									
										
										
										
											2016-09-06 16:37:24 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-06 13:33:45 -07:00
										 |  |  |       for (String itemName : CHANGE_ITEMS) {
 | 
					
						
							| 
									
										
										
										
											2016-09-06 16:37:24 -07:00
										 |  |  |         // check if the corresponding change field has content
 | 
					
						
							|  |  |  |         if (record.get(itemName) == null) {
 | 
					
						
							|  |  |  |           continue;
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         switch (itemName) {
 | 
					
						
							| 
									
										
										
										
											2016-10-06 13:33:45 -07:00
										 |  |  |           case "schema":
 | 
					
						
							| 
									
										
										
										
											2016-09-06 16:37:24 -07:00
										 |  |  |             try {
 | 
					
						
							|  |  |  |               DatasetInfoDao.updateDatasetSchema(rootNode);
 | 
					
						
							|  |  |  |             } catch (Exception ex) {
 | 
					
						
							|  |  |  |               Logger.debug("Metadata change exception: schema ", ex);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             break;
 | 
					
						
							|  |  |  |           case "owners":
 | 
					
						
							|  |  |  |             try {
 | 
					
						
							|  |  |  |               DatasetInfoDao.updateDatasetOwner(rootNode);
 | 
					
						
							|  |  |  |             } catch (Exception ex) {
 | 
					
						
							|  |  |  |               Logger.debug("Metadata change exception: owner ", ex);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             break;
 | 
					
						
							| 
									
										
										
										
											2016-10-06 13:33:45 -07:00
										 |  |  |           case "datasetProperties":
 | 
					
						
							|  |  |  |             try {
 | 
					
						
							|  |  |  |               DatasetInfoDao.updateDatasetCaseSensitivity(rootNode);
 | 
					
						
							|  |  |  |             } catch (Exception ex) {
 | 
					
						
							|  |  |  |               Logger.debug("Metadata change exception: case sensitivity ", ex);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             break;
 | 
					
						
							| 
									
										
										
										
											2016-09-06 16:37:24 -07:00
										 |  |  |           case "references":
 | 
					
						
							|  |  |  |             try {
 | 
					
						
							|  |  |  |               DatasetInfoDao.updateDatasetReference(rootNode);
 | 
					
						
							|  |  |  |             } catch (Exception ex) {
 | 
					
						
							|  |  |  |               Logger.debug("Metadata change exception: reference ", ex);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             break;
 | 
					
						
							|  |  |  |           case "partitionSpec":
 | 
					
						
							|  |  |  |             try {
 | 
					
						
							|  |  |  |               DatasetInfoDao.updateDatasetPartition(rootNode);
 | 
					
						
							|  |  |  |             } catch (Exception ex) {
 | 
					
						
							|  |  |  |               Logger.debug("Metadata change exception: partition ", ex);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             break;
 | 
					
						
							|  |  |  |           case "deploymentInfo":
 | 
					
						
							|  |  |  |             try {
 | 
					
						
							|  |  |  |               DatasetInfoDao.updateDatasetDeployment(rootNode);
 | 
					
						
							|  |  |  |             } catch (Exception ex) {
 | 
					
						
							|  |  |  |               Logger.debug("Metadata change exception: deployment ", ex);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             break;
 | 
					
						
							|  |  |  |           case "tags":
 | 
					
						
							|  |  |  |             try {
 | 
					
						
							|  |  |  |               DatasetInfoDao.updateDatasetTags(rootNode);
 | 
					
						
							|  |  |  |             } catch (Exception ex) {
 | 
					
						
							|  |  |  |               Logger.debug("Metadata change exception: tag ", ex);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             break;
 | 
					
						
							|  |  |  |           case "constraints":
 | 
					
						
							|  |  |  |             try {
 | 
					
						
							|  |  |  |               DatasetInfoDao.updateDatasetConstraint(rootNode);
 | 
					
						
							|  |  |  |             } catch (Exception ex) {
 | 
					
						
							|  |  |  |               Logger.debug("Metadata change exception: constraint ", ex);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             break;
 | 
					
						
							|  |  |  |           case "indices":
 | 
					
						
							|  |  |  |             try {
 | 
					
						
							|  |  |  |               DatasetInfoDao.updateDatasetIndex(rootNode);
 | 
					
						
							|  |  |  |             } catch (Exception ex) {
 | 
					
						
							|  |  |  |               Logger.debug("Metadata change exception: index ", ex);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             break;
 | 
					
						
							|  |  |  |           case "capacity":
 | 
					
						
							|  |  |  |             try {
 | 
					
						
							|  |  |  |               DatasetInfoDao.updateDatasetCapacity(rootNode);
 | 
					
						
							|  |  |  |             } catch (Exception ex) {
 | 
					
						
							|  |  |  |               Logger.debug("Metadata change exception: capacity ", ex);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             break;
 | 
					
						
							| 
									
										
										
										
											2016-11-11 17:25:41 -08:00
										 |  |  |           case "privacyCompliancePolicy":
 | 
					
						
							|  |  |  |             try {
 | 
					
						
							|  |  |  |               DatasetInfoDao.updateDatasetCompliance(rootNode);
 | 
					
						
							|  |  |  |             } catch (Exception ex) {
 | 
					
						
							|  |  |  |               Logger.debug("Metadata change exception: compliance ", ex);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             break;
 | 
					
						
							|  |  |  |           case "securitySpecification":
 | 
					
						
							| 
									
										
										
										
											2016-09-06 16:37:24 -07:00
										 |  |  |             try {
 | 
					
						
							|  |  |  |               DatasetInfoDao.updateDatasetSecurity(rootNode);
 | 
					
						
							|  |  |  |             } catch (Exception ex) {
 | 
					
						
							|  |  |  |               Logger.debug("Metadata change exception: security ", ex);
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |             break;
 | 
					
						
							|  |  |  |           default:
 | 
					
						
							|  |  |  |             break;
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |       }
 | 
					
						
							|  |  |  |     }
 | 
					
						
							|  |  |  |     return null;
 | 
					
						
							|  |  |  |   }
 | 
					
						
							|  |  |  | }
 |