use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.connection.ConnectionPointer in project legend-engine by finos.
the class ServiceTestGenerationHelper method hasModelChainConnection.
private static boolean hasModelChainConnection(Runtime runtime, PureModelContextData pureModelContextData) {
EngineRuntime engineRuntime = resolveRuntime(runtime, pureModelContextData);
List<StoreConnections> storeConnections = engineRuntime.connections;
for (StoreConnections s : storeConnections) {
List<IdentifiedConnection> identifiedConnection = s.storeConnections;
for (IdentifiedConnection ic : identifiedConnection) {
if (ic.connection instanceof ModelChainConnection || (("ModelStore").equals(s.store.path) && ic.connection instanceof ConnectionPointer)) {
return true;
}
}
}
return false;
}
use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.connection.ConnectionPointer in project legend-engine by finos.
the class HelperRuntimeBuilder method buildEngineRuntime.
public static org.finos.legend.pure.m3.coreinstance.meta.pure.runtime.Runtime buildEngineRuntime(EngineRuntime engineRuntime, CompileContext context) {
if (engineRuntime.mappings.isEmpty()) {
throw new EngineException("Runtime must cover at least one mapping", engineRuntime.sourceInformation, EngineErrorType.COMPILATION);
}
// verify if each mapping associated with the PackageableRuntime exists
List<Mapping> mappings = engineRuntime.mappings.stream().map(mappingPointer -> context.resolveMapping(mappingPointer.path, mappingPointer.sourceInformation)).collect(Collectors.toList());
// build connections
List<Connection> connections = new ArrayList<>();
Set<String> ids = new HashSet<>();
ListIterate.forEach(engineRuntime.connections, storeConnections -> {
if (!storeConnections.store.path.equals("ModelStore")) {
// verify stores used for indexing exist
context.resolveStore(storeConnections.store.path, storeConnections.store.sourceInformation);
}
ListIterate.forEach(storeConnections.storeConnections, identifiedConnection -> {
// ID must be unique across all connections of the runtime
if (ids.contains(identifiedConnection.id)) {
throw new EngineException("Runtime connection with ID '" + identifiedConnection.id + "' has already been specified", identifiedConnection.sourceInformation, EngineErrorType.COMPILATION);
} else {
ids.add(identifiedConnection.id);
}
if (identifiedConnection.connection instanceof ConnectionPointer) {
ConnectionPointer pointer = (ConnectionPointer) identifiedConnection.connection;
Store connectionStore = HelperRuntimeBuilder.getConnectionStore(context.resolveConnection(pointer.connection, pointer.sourceInformation));
String connectionStorePath = HelperModelBuilder.getElementFullPath(connectionStore, context.pureModel.getExecutionSupport());
if (!storeConnections.store.path.equals(connectionStorePath)) {
throw new EngineException("Connection pointer for store '" + connectionStorePath + "' should not be indexed to store '" + storeConnections.store.path + "'", pointer.sourceInformation, EngineErrorType.COMPILATION);
}
connections.add(identifiedConnection.connection);
} else if (storeConnections.store.path.equals(identifiedConnection.connection.element)) {
connections.add(identifiedConnection.connection);
} else if (identifiedConnection.connection.element == null) {
identifiedConnection.connection.element = storeConnections.store.path;
identifiedConnection.connection.elementSourceInformation = identifiedConnection.connection.sourceInformation;
connections.add(identifiedConnection.connection);
} else {
throw new EngineException("Connection for store '" + identifiedConnection.connection.element + "' should not be indexed to store '" + storeConnections.store.path + "'", identifiedConnection.connection.sourceInformation, EngineErrorType.COMPILATION);
}
});
});
// convert EngineRuntime with connection as a map indexes by store to Pure runtime which only contains an array of connections
org.finos.legend.pure.m3.coreinstance.meta.pure.runtime.Runtime pureRuntime = new Root_meta_pure_runtime_Runtime_Impl("Root::meta::pure::runtime::Runtime");
ListIterate.forEach(connections, connection -> {
final org.finos.legend.pure.m3.coreinstance.meta.pure.runtime.Connection pureConnection = connection.accept(new ConnectionFirstPassBuilder(context));
connection.accept(new ConnectionSecondPassBuilder(context, pureConnection));
pureRuntime._connectionsAdd(pureConnection);
});
// verify runtime mapping coverage
checkRuntimeMappingCoverage(pureRuntime, mappings, context, engineRuntime.sourceInformation);
return pureRuntime;
}
use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.connection.ConnectionPointer in project legend-engine by finos.
the class ServiceTestGenerationHelper method getNullableTestConnection.
private static Optional<Connection> getNullableTestConnection(Connection connection, Runtime parentRuntime, String mappingPath, Pair<PureModelContextData, PureModel> pureModelPairs, StoreConnections parentStoreConnection, Function<String, String> testDataAccessor, String testData, String idTestDataAccessorResult) {
if (connection instanceof ConnectionPointer) {
String connectionFullPath = ((ConnectionPointer) connection).connection;
PackageableElement found = Iterate.detect(pureModelPairs.getOne().getElements(), e -> connectionFullPath.equals(e.getPath()));
if (!(found instanceof PackageableConnection)) {
throw new RuntimeException("Can't find connection '" + connectionFullPath + "'");
}
connection = ((PackageableConnection) found).connectionValue;
}
if (connection instanceof JsonModelConnection) {
JsonModelConnection conn = (JsonModelConnection) connection;
JsonModelConnection testJsonModelConnection = new JsonModelConnection();
testJsonModelConnection._class = conn._class;
testJsonModelConnection.element = conn.element != null ? conn.element : (parentStoreConnection.store != null ? parentStoreConnection.store.path : null);
String executorId = conn.url.split(":")[1];
String connectionTestData = resolveTestData(executorId, idTestDataAccessorResult, testDataAccessor, testData);
if (idTestDataAccessorResult != null) {
((JsonModelConnection) connection).url = DataProtocolHandler.DATA_PROTOCOL_NAME + ":" + MediaType.APPLICATION_JSON + ";base64," + Base64.getEncoder().encodeToString(idTestDataAccessorResult.getBytes(StandardCharsets.UTF_8));
}
testJsonModelConnection.url = DataProtocolHandler.DATA_PROTOCOL_NAME + ":" + MediaType.APPLICATION_JSON + ";base64," + Base64.getEncoder().encodeToString(connectionTestData.getBytes(StandardCharsets.UTF_8));
return Optional.of(conn);
}
if (connection instanceof XmlModelConnection) {
XmlModelConnection conn = (XmlModelConnection) connection;
XmlModelConnection testXmlModelConnection = new XmlModelConnection();
testXmlModelConnection._class = conn._class;
testXmlModelConnection.element = conn.element != null ? conn.element : (parentStoreConnection.store != null ? parentStoreConnection.store.path : null);
String executorId = conn.url.split(":")[1];
String connectionTestData = resolveTestData(executorId, idTestDataAccessorResult, testDataAccessor, testData);
if (idTestDataAccessorResult != null) {
((XmlModelConnection) connection).url = DataProtocolHandler.DATA_PROTOCOL_NAME + ":" + MediaType.APPLICATION_XML + ";base64," + Base64.getEncoder().encodeToString(idTestDataAccessorResult.getBytes(StandardCharsets.UTF_8));
}
testXmlModelConnection.url = DataProtocolHandler.DATA_PROTOCOL_NAME + ":" + MediaType.APPLICATION_XML + ";base64," + Base64.getEncoder().encodeToString(connectionTestData.getBytes(StandardCharsets.UTF_8));
return Optional.of(testXmlModelConnection);
}
if (connection instanceof ExternalFormatConnection) {
ExternalFormatConnection conn = (ExternalFormatConnection) connection;
ExternalFormatConnection testConn = new ExternalFormatConnection();
testConn.element = conn.element;
UrlStreamExternalSource source = new UrlStreamExternalSource();
source.url = DataProtocolHandler.DATA_PROTOCOL_NAME + ":" + MediaType.APPLICATION_XML + ";base64," + Base64.getEncoder().encodeToString(idTestDataAccessorResult.getBytes(StandardCharsets.UTF_8));
testConn.externalSource = source;
return Optional.of(testConn);
}
if (connection instanceof ModelChainConnection) {
return Optional.of(connection);
}
if (connection instanceof DatabaseConnection) {
List<String> sql = getSql(parentRuntime, mappingPath, idTestDataAccessorResult, pureModelPairs.getTwo());
return Optional.of(newRelationalConnection(connection, idTestDataAccessorResult, sql));
}
String element = connection.element != null ? connection.element : (parentStoreConnection.store != null ? parentStoreConnection.store.path : null);
Connection testConnectionFromFactories = getTestConnectionFromFactories(connection, idTestDataAccessorResult == null ? testData : idTestDataAccessorResult, element);
return testConnectionFromFactories != null ? Optional.of(testConnectionFromFactories) : Optional.empty();
}
use of org.finos.legend.engine.protocol.pure.v1.model.packageableElement.connection.ConnectionPointer in project legend-engine by finos.
the class RuntimeParseTreeWalker method addConnectionsByStore.
private void addConnectionsByStore(RuntimeParserGrammar.ConnectionsContext connectionsContext, EngineRuntime engineRuntime) {
if (connectionsContext != null) {
ListIterate.forEach(connectionsContext.storeConnections(), storeConnectionsContext -> {
String store = storeConnectionsContext.qualifiedName().getText().equals("ModelStore") ? "ModelStore" : PureGrammarParserUtility.fromQualifiedName(storeConnectionsContext.qualifiedName().packagePath() == null ? Collections.emptyList() : storeConnectionsContext.qualifiedName().packagePath().identifier(), storeConnectionsContext.qualifiedName().identifier());
StoreConnections storeConnections = new StoreConnections();
storeConnections.sourceInformation = walkerSourceInformation.getSourceInformation(storeConnectionsContext);
PackageableElementPointer storePointer = new PackageableElementPointer();
storePointer.type = PackageableElementType.STORE;
storePointer.path = store;
storePointer.sourceInformation = walkerSourceInformation.getSourceInformation(storeConnectionsContext.qualifiedName());
storeConnections.store = storePointer;
if (engineRuntime.getStoreConnections(store) != null) {
throw new EngineException("Connections for store '" + store + "' is already specified", storeConnections.sourceInformation, EngineErrorType.PARSER);
}
// walk each connection
ListIterate.forEach(storeConnectionsContext.identifiedConnection(), identifiedConnectionContext -> {
IdentifiedConnection identifiedConnection = new IdentifiedConnection();
identifiedConnection.sourceInformation = walkerSourceInformation.getSourceInformation(identifiedConnectionContext);
identifiedConnection.id = PureGrammarParserUtility.fromIdentifier(identifiedConnectionContext.identifier());
if (identifiedConnectionContext.connectionPointer() != null) {
RuntimeParserGrammar.ConnectionPointerContext connectionPointerContext = identifiedConnectionContext.connectionPointer();
ConnectionPointer connectionPointer = new ConnectionPointer();
connectionPointer.connection = PureGrammarParserUtility.fromQualifiedName(connectionPointerContext.qualifiedName().packagePath() == null ? Collections.emptyList() : connectionPointerContext.qualifiedName().packagePath().identifier(), connectionPointerContext.qualifiedName().identifier());
connectionPointer.sourceInformation = walkerSourceInformation.getSourceInformation(connectionPointerContext.qualifiedName());
identifiedConnection.connection = connectionPointer;
} else if (identifiedConnectionContext.embeddedConnection() != null) {
RuntimeParserGrammar.EmbeddedConnectionContext embeddedConnectionContext = identifiedConnectionContext.embeddedConnection();
StringBuilder embeddedConnectionText = new StringBuilder();
for (RuntimeParserGrammar.EmbeddedConnectionContentContext fragment : embeddedConnectionContext.embeddedConnectionContent()) {
embeddedConnectionText.append(fragment.getText());
}
String embeddedConnectionParsingText = embeddedConnectionText.length() > 0 ? embeddedConnectionText.substring(0, embeddedConnectionText.length() - 2) : embeddedConnectionText.toString();
// prepare island grammar walker source information
int startLine = embeddedConnectionContext.ISLAND_OPEN().getSymbol().getLine();
int lineOffset = walkerSourceInformation.getLineOffset() + startLine - 1;
// only add current walker source information column offset if this is the first line
int columnOffset = (startLine == 1 ? walkerSourceInformation.getColumnOffset() : 0) + embeddedConnectionContext.ISLAND_OPEN().getSymbol().getCharPositionInLine() + embeddedConnectionContext.ISLAND_OPEN().getSymbol().getText().length();
ParseTreeWalkerSourceInformation embeddedConnectionWalkerSourceInformation = new ParseTreeWalkerSourceInformation.Builder(walkerSourceInformation.getSourceId(), lineOffset, columnOffset).withReturnSourceInfo(this.walkerSourceInformation.getReturnSourceInfo()).build();
SourceInformation embeddedConnectionSourceInformation = walkerSourceInformation.getSourceInformation(embeddedConnectionContext);
identifiedConnection.connection = this.connectionParser.parseEmbeddedRuntimeConnections(embeddedConnectionParsingText, embeddedConnectionWalkerSourceInformation, embeddedConnectionSourceInformation);
} else {
throw new UnsupportedOperationException();
}
if (engineRuntime.getStoreConnections(store) == null) {
// NOTE: only add this store runtime connections if it has child connections
engineRuntime.connections.add(storeConnections);
}
engineRuntime.getStoreConnections(store).storeConnections.add(identifiedConnection);
});
});
}
}
Aggregations