Search in sources :

Example 1 with BLangAnnotationAttachment

use of org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method processPersistentVolumeClaim.

/**
 * Process PersistentVolumeClaim annotations.
 *
 * @param attachmentNode Attachment Node
 * @return Set of @{@link ConfigMapModel} objects
 */
Set<PersistentVolumeClaimModel> processPersistentVolumeClaim(AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    Set<PersistentVolumeClaimModel> volumeClaimModels = new HashSet<>();
    List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getKeyValuePairs();
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
        List<BLangExpression> secretAnnotation = ((BLangArrayLiteral) keyValue.valueExpr).exprs;
        for (BLangExpression bLangExpression : secretAnnotation) {
            PersistentVolumeClaimModel claimModel = new PersistentVolumeClaimModel();
            List<BLangRecordLiteral.BLangRecordKeyValue> annotationValues = ((BLangRecordLiteral) bLangExpression).getKeyValuePairs();
            for (BLangRecordLiteral.BLangRecordKeyValue annotation : annotationValues) {
                VolumeClaimConfig volumeMountConfig = VolumeClaimConfig.valueOf(annotation.getKey().toString());
                String annotationValue = resolveValue(annotation.getValue().toString());
                switch(volumeMountConfig) {
                    case name:
                        claimModel.setName(getValidName(annotationValue));
                        break;
                    case mountPath:
                        claimModel.setMountPath(annotationValue);
                        break;
                    case accessMode:
                        claimModel.setAccessMode(annotationValue);
                        break;
                    case volumeClaimSize:
                        claimModel.setVolumeClaimSize(annotationValue);
                        break;
                    case readOnly:
                        claimModel.setReadOnly(Boolean.parseBoolean(annotationValue));
                        break;
                    default:
                        break;
                }
            }
            volumeClaimModels.add(claimModel);
        }
    }
    return volumeClaimModels;
}
Also used : PersistentVolumeClaimModel(org.ballerinax.kubernetes.models.PersistentVolumeClaimModel) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) HashSet(java.util.HashSet)

Example 2 with BLangAnnotationAttachment

use of org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method processConfigMap.

/**
 * Process ConfigMap annotations.
 *
 * @param attachmentNode Attachment Node
 * @return Set of @{@link ConfigMapModel} objects
 */
Set<ConfigMapModel> processConfigMap(AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    Set<ConfigMapModel> configMapModels = new HashSet<>();
    List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getKeyValuePairs();
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
        List<BLangExpression> configAnnotation = ((BLangArrayLiteral) keyValue.valueExpr).exprs;
        for (BLangExpression bLangExpression : configAnnotation) {
            ConfigMapModel configMapModel = new ConfigMapModel();
            List<BLangRecordLiteral.BLangRecordKeyValue> annotationValues = ((BLangRecordLiteral) bLangExpression).getKeyValuePairs();
            for (BLangRecordLiteral.BLangRecordKeyValue annotation : annotationValues) {
                VolumeMountConfig volumeMountConfig = VolumeMountConfig.valueOf(annotation.getKey().toString());
                String annotationValue = resolveValue(annotation.getValue().toString());
                switch(volumeMountConfig) {
                    case name:
                        configMapModel.setName(getValidName(annotationValue));
                        break;
                    case mountPath:
                        configMapModel.setMountPath(annotationValue);
                        break;
                    case isBallerinaConf:
                        configMapModel.setBallerinaConf(Boolean.parseBoolean(annotationValue));
                        break;
                    case data:
                        List<BLangExpression> data = ((BLangArrayLiteral) annotation.valueExpr).exprs;
                        configMapModel.setData(getDataForConfigMap(data));
                        break;
                    case readOnly:
                        configMapModel.setReadOnly(Boolean.parseBoolean(annotationValue));
                        break;
                    default:
                        break;
                }
            }
            configMapModels.add(configMapModel);
        }
    }
    return configMapModels;
}
Also used : ConfigMapModel(org.ballerinax.kubernetes.models.ConfigMapModel) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) HashSet(java.util.HashSet)

Example 3 with BLangAnnotationAttachment

use of org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method processPodAutoscalerAnnotation.

/**
 * Process annotations and create service model object.
 *
 * @param attachmentNode annotation attachment node.
 * @return Service model object
 */
PodAutoscalerModel processPodAutoscalerAnnotation(AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    PodAutoscalerModel podAutoscalerModel = new PodAutoscalerModel();
    List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getKeyValuePairs();
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
        PodAutoscalerConfiguration podAutoscalerConfiguration = PodAutoscalerConfiguration.valueOf(keyValue.getKey().toString());
        String annotationValue = resolveValue(keyValue.getValue().toString());
        switch(podAutoscalerConfiguration) {
            case name:
                podAutoscalerModel.setName(getValidName(annotationValue));
                break;
            case labels:
                podAutoscalerModel.setLabels(getMap(((BLangRecordLiteral) keyValue.valueExpr).keyValuePairs));
                break;
            case cpuPercentage:
                podAutoscalerModel.setCpuPercentage(Integer.parseInt(annotationValue));
                break;
            case minReplicas:
                podAutoscalerModel.setMinReplicas(Integer.parseInt(annotationValue));
                break;
            case maxReplicas:
                podAutoscalerModel.setMaxReplicas(Integer.parseInt(annotationValue));
                break;
            default:
                break;
        }
    }
    return podAutoscalerModel;
}
Also used : BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) PodAutoscalerModel(org.ballerinax.kubernetes.models.PodAutoscalerModel)

Example 4 with BLangAnnotationAttachment

use of org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment in project ballerina by ballerina-lang.

the class CommandUtil method getServiceDocumentationByPosition.

/**
 * Get the Documentation attachment for the service.
 * @param bLangPackage      BLangPackage built
 * @param line              Start line of the service in the source
 * @return {@link String}   Documentation attachment for the service
 */
static DocAttachmentInfo getServiceDocumentationByPosition(BLangPackage bLangPackage, int line) {
    // TODO: Currently resource position is invalid and we use the annotation attachment positions.
    for (TopLevelNode topLevelNode : bLangPackage.topLevelNodes) {
        if (topLevelNode instanceof BLangService) {
            BLangService serviceNode = (BLangService) topLevelNode;
            DiagnosticPos servicePos = CommonUtil.toZeroBasedPosition(serviceNode.getPosition());
            List<BLangAnnotationAttachment> annotationAttachments = serviceNode.getAnnotationAttachments();
            if (!annotationAttachments.isEmpty()) {
                DiagnosticPos lastAttachmentPos = CommonUtil.toZeroBasedPosition(annotationAttachments.get(annotationAttachments.size() - 1).getPosition());
                if (lastAttachmentPos.getEndLine() < line && line < servicePos.getEndLine()) {
                    return getServiceNodeDocumentation(serviceNode, lastAttachmentPos.getEndLine() + 1);
                }
            } else if (servicePos.getStartLine() == line) {
                return getServiceNodeDocumentation(serviceNode, line);
            }
        }
    }
    return null;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) TopLevelNode(org.ballerinalang.model.tree.TopLevelNode)

Example 5 with BLangAnnotationAttachment

use of org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment in project ballerina by ballerina-lang.

the class CompilerPluginRunner method notifyProcessors.

private void notifyProcessors(List<BLangAnnotationAttachment> attachments, BiConsumer<CompilerPlugin, List<AnnotationAttachmentNode>> notifier) {
    Map<CompilerPlugin, List<AnnotationAttachmentNode>> attachmentMap = new HashMap<>();
    for (BLangAnnotationAttachment attachment : attachments) {
        DefinitionID aID = new DefinitionID(attachment.annotationSymbol.pkgID.getName().value, attachment.annotationName.value);
        if (!processorMap.containsKey(aID)) {
            continue;
        }
        List<CompilerPlugin> procList = processorMap.get(aID);
        procList.forEach(proc -> {
            List<AnnotationAttachmentNode> attachmentNodes = attachmentMap.computeIfAbsent(proc, k -> new ArrayList<>());
            attachmentNodes.add(attachment);
        });
    }
    for (CompilerPlugin processor : attachmentMap.keySet()) {
        notifier.accept(processor, Collections.unmodifiableList(attachmentMap.get(processor)));
    }
}
Also used : HashMap(java.util.HashMap) BLangAnnotationAttachment(org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment) CompilerPlugin(org.ballerinalang.compiler.plugins.CompilerPlugin) ArrayList(java.util.ArrayList) List(java.util.List) AnnotationAttachmentNode(org.ballerinalang.model.tree.AnnotationAttachmentNode)

Aggregations

BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)10 BLangAnnotationAttachment (org.wso2.ballerinalang.compiler.tree.BLangAnnotationAttachment)9 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)5 HashSet (java.util.HashSet)4 List (java.util.List)4 BLangResource (org.wso2.ballerinalang.compiler.tree.BLangResource)4 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)4 BLangService (org.wso2.ballerinalang.compiler.tree.BLangService)3 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)3 BLangArrayLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral)3 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)3 ArrayList (java.util.ArrayList)2 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)2 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 Optional (java.util.Optional)1 Set (java.util.Set)1