com.cognite.client.servicesV1.parser.ExtractionPipelineParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cdf-sdk-java Show documentation
Show all versions of cdf-sdk-java Show documentation
Java SDK for reading and writing from/to CDF resources.
/*
* Copyright (c) 2020 Cognite AS
*
* 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.
*/
package com.cognite.client.servicesV1.parser;
import com.cognite.client.dto.ExtractionPipeline;
import com.cognite.client.dto.ExtractionPipelineRun;
import com.cognite.client.dto.Relationship;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableMap;
import java.util.*;
import static com.cognite.client.servicesV1.ConnectorConstants.MAX_LOG_ELEMENT_LENGTH;
/**
* This class contains a set of methods to help parse file objects between Cognite api representations
* (json and proto) and typed objects.
*/
public class ExtractionPipelineParser {
private static final String logPrefix = "ExtractionPipelineParser - ";
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final ImmutableBiMap statusMap = ImmutableBiMap
.builder()
.put("success", ExtractionPipelineRun.Status.SUCCESS)
.put("failure", ExtractionPipelineRun.Status.FAILURE)
.put("seen", ExtractionPipelineRun.Status.SEEN)
.build();
/**
* Parses an extraction pipeline json string to {@link ExtractionPipeline} proto object.
*
* @param json
* @return
* @throws Exception
*/
public static ExtractionPipeline parseExtractionPipeline(String json) throws Exception {
String jsonExcerpt = json.substring(0, Math.min(json.length() - 1, MAX_LOG_ELEMENT_LENGTH));
JsonNode root = objectMapper.readTree(json);
ExtractionPipeline.Builder pipelineBuilder = ExtractionPipeline.newBuilder();
// Required attributes.
if (root.path("externalId").isTextual()) {
pipelineBuilder.setExternalId(root.get("externalId").textValue());
} else {
throw new Exception(ExtractionPipelineParser.buildParsingExceptionString("externalId", jsonExcerpt));
}
if (root.path("name").isTextual()) {
pipelineBuilder.setName(root.get("name").textValue());
} else {
throw new Exception(ExtractionPipelineParser.buildParsingExceptionString("name", jsonExcerpt));
}
if (root.path("dataSetId").isIntegralNumber()) {
pipelineBuilder.setDataSetId(root.get("dataSetId").longValue());
} else {
throw new Exception(ExtractionPipelineParser.buildParsingExceptionString("dataSetId", jsonExcerpt));
}
// The rest of the attributes are optional.
if (root.path("id").isIntegralNumber()) {
pipelineBuilder.setId(root.get("id").longValue());
}
if (root.path("description").isTextual()) {
pipelineBuilder.setDescription(root.get("description").textValue());
}
if (root.path("rawTables").isArray()) {
for (JsonNode node : root.path("rawTables")) {
if (node.isObject()
&& node.path("dbName").isTextual()
&& node.path("tableName").isTextual()) {
pipelineBuilder.addRawTables(ExtractionPipeline.RawTable.newBuilder()
.setDbName(node.path("dbName").textValue())
.setTableName(node.path("tableName").textValue())
.build());
} else {
throw new Exception(ExtractionPipelineParser.buildParsingExceptionString("rawTables", jsonExcerpt));
}
}
}
if (root.path("schedule").isTextual()) {
pipelineBuilder.setSchedule(root.get("schedule").textValue());
}
if (root.path("contacts").isArray()) {
for (JsonNode node : root.path("contacts")) {
if (node.isObject()) {
ExtractionPipeline.Contact.Builder contactBuilder = ExtractionPipeline.Contact.newBuilder();
if (node.path("name").isTextual()) {
contactBuilder.setName(node.path("name").textValue());
}
if (node.path("email").isTextual()) {
contactBuilder.setEmail(node.path("email").textValue());
}
if (node.path("role").isTextual()) {
contactBuilder.setRole(node.path("role").textValue());
}
if (node.path("sendNotification").isBoolean()) {
contactBuilder.setSendNotification(node.path("sendNotification").booleanValue());
}
pipelineBuilder.addContacts(contactBuilder.build());
} else {
throw new Exception(ExtractionPipelineParser.buildParsingExceptionString("contacts", jsonExcerpt));
}
}
}
if (root.path("metadata").isObject()) {
Iterator> fieldIterator = root.path("metadata").fields();
while (fieldIterator.hasNext()) {
Map.Entry entry = fieldIterator.next();
if (entry.getValue().isTextual()) {
pipelineBuilder.putMetadata(entry.getKey(), entry.getValue().textValue());
}
}
}
if (root.path("source").isTextual()) {
pipelineBuilder.setSource(root.get("source").textValue());
}
if (root.path("documentation").isTextual()) {
pipelineBuilder.setDocumentation(root.get("documentation").textValue());
}
if (root.path("lastSuccess").isIntegralNumber()) {
pipelineBuilder.setLastSuccess(root.get("lastSuccess").longValue());
}
if (root.path("lastFailure").isIntegralNumber()) {
pipelineBuilder.setLastFailure(root.get("lastFailure").longValue());
}
if (root.path("lastMessage").isTextual()) {
pipelineBuilder.setLastMessage(root.get("lastMessage").textValue());
}
if (root.path("lastSeen").isIntegralNumber()) {
pipelineBuilder.setLastSeen(root.get("lastSeen").longValue());
}
if (root.path("createdTime").isIntegralNumber()) {
pipelineBuilder.setCreatedTime(root.get("createdTime").longValue());
}
if (root.path("lastUpdatedTime").isIntegralNumber()) {
pipelineBuilder.setLastUpdatedTime(root.get("lastUpdatedTime").longValue());
}
if (root.path("createdBy").isTextual()) {
pipelineBuilder.setCreatedBy(root.get("createdBy").textValue());
}
return pipelineBuilder.build();
}
/**
* Parses an extraction pipeline json string to {@link ExtractionPipelineRun} proto object.
*
* @param json
* @return
* @throws Exception
*/
public static ExtractionPipelineRun parseExtractionPipelineRun(String json) throws Exception {
String jsonExcerpt = json.substring(0, Math.min(json.length() - 1, MAX_LOG_ELEMENT_LENGTH));
JsonNode root = objectMapper.readTree(json);
ExtractionPipelineRun.Builder pipelineRunBuilder = ExtractionPipelineRun.newBuilder();
// Required attributes.
if (root.path("status").isTextual() && statusMap.containsKey(root.get("status").textValue())) {
pipelineRunBuilder.setStatus(statusMap.get(root.get("status").textValue()));
} else {
throw new Exception(ExtractionPipelineParser.buildParsingExceptionString("status", jsonExcerpt));
}
// The rest of the attributes are optional.
if (root.path("id").isIntegralNumber()) {
pipelineRunBuilder.setId(root.get("id").longValue());
}
if (root.path("message").isTextual()) {
pipelineRunBuilder.setMessage(root.get("message").textValue());
}
if (root.path("createdTime").isIntegralNumber()) {
pipelineRunBuilder.setCreatedTime(root.get("createdTime").longValue());
}
return pipelineRunBuilder.build();
}
/**
* Builds a request insert item object from {@link ExtractionPipeline}.
*
* An insert item object creates a new asset data object in the Cognite system.
*
* @param element
* @return
*/
public static Map toRequestInsertItem(ExtractionPipeline element) {
Preconditions.checkNotNull(element, "Input cannot be null.");
Preconditions.checkState(element.hasExternalId(), "Input must contain externalId.");
Preconditions.checkState(element.hasName(), "Input must contain name.");
Preconditions.checkState(element.hasDataSetId(), "Input must contain dataSetId.");
// Add all the mandatory attributes
ImmutableMap.Builder mapBuilder = ImmutableMap.builder()
.put("externalId", element.getExternalId())
.put("name", element.getName())
.put("dataSetId", element.getDataSetId());
// Add optional attributes
if (element.hasDescription()) {
mapBuilder.put("description", element.getDescription());
}
if (element.getRawTablesCount() > 0) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy