Search in sources :

Example 46 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project ballerina by ballerina-lang.

the class BLangPackageBuilder method createInvocationNode.

public void createInvocationNode(DiagnosticPos pos, Set<Whitespace> ws, String invocation, boolean argsAvailable) {
    BLangInvocation invocationNode = (BLangInvocation) TreeBuilder.createInvocationNode();
    invocationNode.pos = pos;
    invocationNode.addWS(ws);
    invocationNode.addWS(invocationWsStack.pop());
    if (argsAvailable) {
        List<ExpressionNode> exprNodes = exprNodeListStack.pop();
        exprNodes.forEach(exprNode -> invocationNode.argExprs.add((BLangExpression) exprNode));
        invocationNode.addWS(commaWsStack.pop());
    }
    invocationNode.expr = (BLangVariableReference) exprNodeStack.pop();
    invocationNode.name = (BLangIdentifier) createIdentifier(invocation);
    invocationNode.pkgAlias = (BLangIdentifier) createIdentifier(null);
    addExpressionNode(invocationNode);
}
Also used : ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) SelectExpressionNode(org.ballerinalang.model.tree.clauses.SelectExpressionNode) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 47 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project ballerina by ballerina-lang.

the class BLangPackageBuilder method addAssignmentStatement.

public void addAssignmentStatement(DiagnosticPos pos, Set<Whitespace> ws, boolean isVarDeclaration, boolean safeAssignment) {
    ExpressionNode rExprNode = exprNodeStack.pop();
    ExpressionNode lExprNode = exprNodeStack.pop();
    BLangAssignment assignmentNode = (BLangAssignment) TreeBuilder.createAssignmentNode();
    assignmentNode.setExpression(rExprNode);
    assignmentNode.setDeclaredWithVar(isVarDeclaration);
    assignmentNode.pos = pos;
    assignmentNode.addWS(ws);
    assignmentNode.safeAssignment = safeAssignment;
    assignmentNode.addVariable((BLangVariableReference) lExprNode);
    addStmtToCurrentBlock(assignmentNode);
}
Also used : ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) SelectExpressionNode(org.ballerinalang.model.tree.clauses.SelectExpressionNode) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)

Example 48 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project ballerina by ballerina-lang.

the class BLangPackageBuilder method addTypeInitExpression.

public void addTypeInitExpression(DiagnosticPos pos, Set<Whitespace> ws, String initName, boolean typeAvailable, boolean exprAvailable) {
    BLangTypeInit objectInitNode = (BLangTypeInit) TreeBuilder.createObjectInitNode();
    objectInitNode.pos = pos;
    objectInitNode.addWS(ws);
    if (typeAvailable) {
        objectInitNode.userDefinedType = (BLangUserDefinedType) typeNodeStack.pop();
    }
    BLangInvocation invocationNode = (BLangInvocation) TreeBuilder.createInvocationNode();
    invocationNode.pos = pos;
    invocationNode.addWS(ws);
    if (exprAvailable) {
        List<ExpressionNode> exprNodes = exprNodeListStack.pop();
        Set<Whitespace> cws = commaWsStack.pop();
        exprNodes.forEach(exprNode -> {
            invocationNode.argExprs.add((BLangExpression) exprNode);
            objectInitNode.argsExpr.add((BLangExpression) exprNode);
        });
        invocationNode.addWS(cws);
        objectInitNode.addWS(cws);
    }
    // TODO check whether pkgName can be be empty
    IdentifierNode pkgNameNode = TreeBuilder.createIdentifierNode();
    IdentifierNode nameNode = createIdentifier(initName);
    BLangNameReference nameReference = new BLangNameReference(pos, ws, pkgNameNode, nameNode);
    invocationNode.name = (BLangIdentifier) nameReference.name;
    invocationNode.addWS(nameReference.ws);
    invocationNode.pkgAlias = (BLangIdentifier) nameReference.pkgAlias;
    objectInitNode.objectInitInvocation = invocationNode;
    this.addExpressionNode(objectInitNode);
}
Also used : BLangTypeInit(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit) BLangNameReference(org.wso2.ballerinalang.compiler.tree.BLangNameReference) ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) SelectExpressionNode(org.ballerinalang.model.tree.clauses.SelectExpressionNode) IdentifierNode(org.ballerinalang.model.tree.IdentifierNode) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) Whitespace(org.ballerinalang.model.Whitespace)

Example 49 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project kubernetes by ballerinax.

the class KubernetesPlugin method process.

@Override
public void process(PackageNode packageNode) {
    BLangPackage bPackage = (BLangPackage) packageNode;
    KubernetesContext.getInstance().addDataHolder(bPackage.packageID, sourceDirectory.getPath());
    // Get dependency jar paths
    JarResolver jarResolver = KubernetesContext.getInstance().getCompilerContext().get(JAR_RESOLVER_KEY);
    if (jarResolver != null) {
        Set<Path> dependencyJarPaths = new HashSet<>(jarResolver.allDependencies(bPackage));
        KubernetesContext.getInstance().getDataHolder(bPackage.packageID).getDockerModel().addDependencyJarPaths(dependencyJarPaths);
    }
    // Get the imports with alias _
    List<BLangImportPackage> kubernetesImports = bPackage.getImports().stream().filter(i -> i.symbol.toString().startsWith("ballerina/kubernetes") && i.getAlias().toString().equals("_")).collect(Collectors.toList());
    if (kubernetesImports.size() > 0) {
        for (BLangImportPackage kubernetesImport : kubernetesImports) {
            // Get the units of the file which has kubernetes import as _
            List<TopLevelNode> topLevelNodes = bPackage.getCompilationUnits().stream().filter(cu -> cu.getName().equals(kubernetesImport.compUnit.getValue())).flatMap(cu -> cu.getTopLevelNodes().stream()).collect(Collectors.toList());
            // Filter out the services
            List<ServiceNode> serviceNodes = topLevelNodes.stream().filter(tln -> tln instanceof ServiceNode).map(tln -> (ServiceNode) tln).collect(Collectors.toList());
            // Generate artifacts for services for all services
            serviceNodes.forEach(sn -> process(sn, Collections.singletonList(createAnnotation("Deployment"))));
            // Create Service annotation with NodePort service type
            AnnotationAttachmentNode serviceAnnotation = createAnnotation("Service");
            BLangRecordLiteral svcRecordLiteral = (BLangRecordLiteral) TreeBuilder.createRecordLiteralNode();
            serviceAnnotation.setExpression(svcRecordLiteral);
            BLangLiteral serviceTypeKey = (BLangLiteral) TreeBuilder.createLiteralExpression();
            serviceTypeKey.value = ServiceAnnotationProcessor.ServiceConfiguration.serviceType.name();
            serviceTypeKey.type = new BType(TypeTags.STRING, null);
            BLangLiteral serviceTypeValue = new BLangLiteral();
            serviceTypeValue.value = KubernetesConstants.ServiceType.NodePort.name();
            serviceTypeValue.type = new BType(TypeTags.STRING, null);
            BLangRecordLiteral.BLangRecordKeyValueField serviceTypeRecordField = new BLangRecordLiteral.BLangRecordKeyValueField();
            serviceTypeRecordField.key = new BLangRecordLiteral.BLangRecordKey(serviceTypeKey);
            serviceTypeRecordField.valueExpr = serviceTypeValue;
            svcRecordLiteral.fields.add(serviceTypeRecordField);
            // Filter services with 'new Listener()' and generate services
            for (ServiceNode serviceNode : serviceNodes) {
                Optional<? extends ExpressionNode> initListener = serviceNode.getAttachedExprs().stream().filter(aex -> aex instanceof BLangTypeInit).findAny();
                if (initListener.isPresent()) {
                    serviceNodes.forEach(sn -> process(sn, Collections.singletonList(serviceAnnotation)));
                }
            }
            // Get the variable names of the listeners attached to services
            List<String> listenerNamesToExpose = serviceNodes.stream().map(ServiceNode::getAttachedExprs).flatMap(Collection::stream).filter(aex -> aex instanceof BLangSimpleVarRef).map(aex -> (BLangSimpleVarRef) aex).map(BLangSimpleVarRef::toString).collect(Collectors.toList());
            // Generate artifacts for listeners attached to services
            topLevelNodes.stream().filter(tln -> tln instanceof SimpleVariableNode).map(tln -> (SimpleVariableNode) tln).filter(listener -> listenerNamesToExpose.contains(listener.getName().getValue())).forEach(listener -> process(listener, Collections.singletonList(serviceAnnotation)));
            // Generate artifacts for main functions
            topLevelNodes.stream().filter(tln -> tln instanceof FunctionNode).map(tln -> (FunctionNode) tln).filter(fn -> "main".equals(fn.getName().getValue())).forEach(fn -> process(fn, Collections.singletonList(createAnnotation("Job"))));
        }
    }
}
Also used : JAR_RESOLVER_KEY(org.ballerinalang.compiler.JarResolver.JAR_RESOLVER_KEY) AnnotationProcessorFactory(org.ballerinax.kubernetes.processors.AnnotationProcessorFactory) LoggerFactory(org.slf4j.LoggerFactory) SupportedAnnotationPackages(org.ballerinalang.compiler.plugins.SupportedAnnotationPackages) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) Flag(org.ballerinalang.model.elements.Flag) Map(java.util.Map) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode) Path(java.nio.file.Path) KUBERNETES(org.ballerinax.kubernetes.KubernetesConstants.KUBERNETES) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) JarResolver(org.ballerinalang.compiler.JarResolver) DOCKER(org.ballerinax.kubernetes.KubernetesConstants.DOCKER) KubernetesUtils(org.ballerinax.kubernetes.utils.KubernetesUtils) Collection(java.util.Collection) PackageID(org.ballerinalang.model.elements.PackageID) Set(java.util.Set) Collectors(java.util.stream.Collectors) KubernetesUtils.printError(org.ballerinax.kubernetes.utils.KubernetesUtils.printError) List(java.util.List) KubernetesDataHolder(org.ballerinax.kubernetes.models.KubernetesDataHolder) Optional(java.util.Optional) DependencyValidator(org.ballerinax.kubernetes.utils.DependencyValidator) PackageNode(org.ballerinalang.model.tree.PackageNode) KubernetesContext(org.ballerinax.kubernetes.models.KubernetesContext) SimpleVariableNode(org.ballerinalang.model.tree.SimpleVariableNode) BLangTypeInit(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit) DockerGenUtils.extractJarName(org.ballerinax.docker.generator.utils.DockerGenUtils.extractJarName) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) DiagnosticLog(org.ballerinalang.util.diagnostic.DiagnosticLog) ArrayList(java.util.ArrayList) SourceDirectory(org.wso2.ballerinalang.compiler.SourceDirectory) TypeTags(org.wso2.ballerinalang.compiler.util.TypeTags) HashSet(java.util.HashSet) AbstractCompilerPlugin(org.ballerinalang.compiler.plugins.AbstractCompilerPlugin) ServiceAnnotationProcessor(org.ballerinax.kubernetes.processors.ServiceAnnotationProcessor) TreeBuilder(org.ballerinalang.model.TreeBuilder) ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) DiagnosticSeverity(io.ballerina.tools.diagnostics.DiagnosticSeverity) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) Logger(org.slf4j.Logger) Files(java.nio.file.Files) KubernetesUtils.createAnnotation(org.ballerinax.kubernetes.utils.KubernetesUtils.createAnnotation) FunctionNode(org.ballerinalang.model.tree.FunctionNode) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) ServiceNode(org.ballerinalang.model.tree.ServiceNode) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) Collections(java.util.Collections) CompilerContext(org.wso2.ballerinalang.compiler.util.CompilerContext) ServiceNode(org.ballerinalang.model.tree.ServiceNode) JarResolver(org.ballerinalang.compiler.JarResolver) BLangTypeInit(org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeInit) BLangSimpleVarRef(org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) HashSet(java.util.HashSet) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode) Path(java.nio.file.Path) BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangImportPackage(org.wso2.ballerinalang.compiler.tree.BLangImportPackage) FunctionNode(org.ballerinalang.model.tree.FunctionNode) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) SimpleVariableNode(org.ballerinalang.model.tree.SimpleVariableNode) Collection(java.util.Collection) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 50 with ExpressionNode

use of org.wso2.carbon.identity.core.model.ExpressionNode in project kubernetes by ballerinax.

the class IstioVirtualServiceAnnotationProcessor method processHttpAnnotation.

/**
 * Process http annotation array of the virtual service annotation to a model.
 *
 * @param httpArray The list of http fields.
 * @return Converted list of Istio http routes.
 * @throws KubernetesPluginException When an unknown field is found.
 */
private List<IstioHttpRoute> processHttpAnnotation(BLangListConstructorExpr httpArray) throws KubernetesPluginException {
    List<IstioHttpRoute> httpRoutes = new LinkedList<>();
    for (ExpressionNode expression : httpArray.getExpressions()) {
        BLangRecordLiteral httpFields = (BLangRecordLiteral) expression;
        IstioHttpRoute httpRoute = new IstioHttpRoute();
        for (BLangRecordLiteral.BLangRecordKeyValueField httpField : convertRecordFields(httpFields.getFields())) {
            switch(HttpRouteConfig.valueOf(httpField.getKey().toString())) {
                case route:
                    BLangListConstructorExpr routeFields = (BLangListConstructorExpr) httpField.getValue();
                    httpRoute.setRoute(processRoutesAnnotation(routeFields));
                    break;
                case timeout:
                    httpRoute.setTimeout(getLongValue(httpField.getValue()));
                    break;
                case appendHeaders:
                    httpRoute.setAppendHeaders(getMap(httpField.getValue()));
                    break;
                default:
                    throw new KubernetesPluginException("unknown field found for istio virtual service: " + httpField.getKey().toString());
            }
        }
        httpRoutes.add(httpRoute);
    }
    return httpRoutes;
}
Also used : ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) BLangListConstructorExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangListConstructorExpr) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) IstioHttpRoute(org.ballerinax.kubernetes.models.istio.IstioHttpRoute) LinkedList(java.util.LinkedList)

Aggregations

ExpressionNode (org.ballerinalang.model.tree.expressions.ExpressionNode)21 ArrayList (java.util.ArrayList)16 SelectExpressionNode (org.ballerinalang.model.tree.clauses.SelectExpressionNode)16 ExpressionNode (org.wso2.carbon.identity.core.model.ExpressionNode)15 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)12 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)8 ExpressionNode (org.wso2.charon3.core.utils.codeutils.ExpressionNode)7 Map (java.util.Map)6 IdentityException (org.wso2.carbon.identity.base.IdentityException)6 Attribute (org.wso2.charon3.core.attributes.Attribute)6 ComplexAttribute (org.wso2.charon3.core.attributes.ComplexAttribute)6 MultiValuedAttribute (org.wso2.charon3.core.attributes.MultiValuedAttribute)6 SimpleAttribute (org.wso2.charon3.core.attributes.SimpleAttribute)6 Connection (java.sql.Connection)5 KubernetesPluginException (org.ballerinax.kubernetes.exceptions.KubernetesPluginException)5 JSONObject (org.json.JSONObject)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 Test (org.testng.annotations.Test)5 Node (org.wso2.carbon.identity.core.model.Node)5 IdpSearchResult (org.wso2.carbon.idp.mgt.model.IdpSearchResult)5