aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/sdc/tosca/parser/elements/queries/EntityQuery.java
blob: ba174230cab62595583fda50ce72599e575fd424 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package org.onap.sdc.tosca.parser.elements.queries;

import org.apache.commons.lang3.StringUtils;
import org.onap.sdc.tosca.parser.enums.EntityTemplateType;
import org.onap.sdc.tosca.parser.enums.SdcTypes;
import org.onap.sdc.toscaparser.api.EntityTemplate;

import java.util.List;

/**
 * This class describes an entity searched and retrieved by SDC Tosca Parser API
 * It is used as the API input parameter. See the {@link org.onap.sdc.tosca.parser.api.ISdcCsarHelper}
 */
public abstract class EntityQuery {

    private final EntityTemplateType entityType;

    private final SdcTypes nodeTemplateType;

    private final String toscaType;

    private String uUID;

    private String customizationUUID;

    EntityQuery(EntityTemplateType entityType, SdcTypes nodeTemplateType, String toscaType) {
        this.entityType = entityType;
        this.nodeTemplateType = nodeTemplateType;
        this.toscaType = toscaType;
    }

    void setUUID(String uUID) {
        this.uUID = uUID;
    }

    void setCustomizationUUID(String customizationUUID) {
        this.customizationUUID = customizationUUID;
    }

    public abstract List<EntityTemplate> searchByTopologyTemplate(TopologyTemplateQuery topologyTemplateQuery);

    public abstract EntityTemplateType getType();

    public EntityTemplateType getEntityType() {
        return entityType;
    }

    public SdcTypes getNodeTemplateType() {
        return nodeTemplateType;
    }

    public String getToscaType() {
        return toscaType;
    }

    public String getUUID() {
        return uUID;
    }

    public String getCustomizationUUID() {
        return customizationUUID;
    }

    public boolean searchAllEntities() {
        return StringUtils.isEmpty(toscaType) && nodeTemplateType == null;
    }

    public static EntityQueryBuilder newBuilder(EntityTemplateType entityTemplateType) {
        return new EntityQueryBuilder(entityTemplateType);
    }

    public static EntityQueryBuilder newBuilder(SdcTypes sdcType) {
        return new EntityQueryBuilder(sdcType);
    }

    public static EntityQueryBuilder newBuilder(String toscaType) {
        return new EntityQueryBuilder(toscaType);
    }

    /**
     * Builds instance of EntityQuery object according to provided parameters
     */
    public static class EntityQueryBuilder {
        private static final String GROUPS_NAME_SPACE = ".groups.";
        private static final String POLICIES_NAME_SPACE = ".policies.";

        private EntityQuery entityQuery;

        private EntityQueryBuilder(EntityTemplateType entityTemplateType) {
            switch(entityTemplateType) {
                case NODE_TEMPLATE:
                    entityQuery = new NodeTemplateEntityQuery();
                    break;
                case GROUP:
                    entityQuery = new GroupEntityQuery();
                    break;
                case POLICY:
                    entityQuery =  new PolicyEntityQuery();
                    break;
                default:
                    throw new IllegalArgumentException("Wrong entity query type: " + entityTemplateType);
            }
        }

        private EntityQueryBuilder(SdcTypes sdcType) {
            entityQuery = new NodeTemplateEntityQuery(sdcType);
        }

        private EntityQueryBuilder(String toscaType) {
            if (toscaType.contains(GROUPS_NAME_SPACE)) {
                entityQuery = new GroupEntityQuery(toscaType);
            }
            else if (toscaType.contains(POLICIES_NAME_SPACE)) {
                entityQuery = new PolicyEntityQuery(toscaType);
            }
            else {
                entityQuery = new NodeTemplateEntityQuery(toscaType);
            }
        }

        public EntityQueryBuilder uUID(String uUID) {
            entityQuery.setUUID(uUID);
            return this;
        }

        public EntityQueryBuilder customizationUUID(String customizationUUID) {
            entityQuery.setCustomizationUUID(customizationUUID);
            return this;
        }

        public EntityQuery build() {
            return entityQuery;
        }
    }



}