aboutsummaryrefslogtreecommitdiffstats
path: root/sdc-tosca/src/main/java/org/onap/sdc/tosca/parser/elements/queries/EntityQuery.java
blob: e3af94f56d7ea1309dbe25fdc4dfe5faca5c8096 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*-
 * ============LICENSE_START=======================================================
 * sdc-tosca
 * ================================================================================
 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.sdc.tosca.parser.elements.queries;

import org.onap.sdc.tosca.parser.api.IEntityDetails;
import org.onap.sdc.tosca.parser.enums.EntityTemplateType;
import org.onap.sdc.tosca.parser.enums.SdcTypes;
import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
import org.onap.sdc.toscaparser.api.NodeTemplate;
import org.onap.sdc.toscaparser.api.ToscaTemplate;
import org.onap.sdc.toscaparser.api.elements.Metadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Objects;

/**
 * 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 static final Logger logger = LoggerFactory.getLogger(EntityQuery.class.getName());

    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<IEntityDetails> getEntitiesFromTopologyTemplate(NodeTemplate nodeTemplate);

    public abstract List<IEntityDetails> getEntitiesFromService(ToscaTemplate toscaTemplate);

    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;
    }

    boolean isSearchCriteriaMatched(Metadata metadata, String toscaType, String uuidKeyName, String cuuidKeyName) {
        return Objects.nonNull(metadata)
                && isStringMatchingOrNull(metadata.getValue(uuidKeyName), getUUID())
                && isStringMatchingOrNull(metadata.getValue(cuuidKeyName), getCustomizationUUID())
                && isStringMatchingOrNull(toscaType, getToscaType());
    }

    boolean isSearchCriteriaMatched(Metadata metadata, String toscaType) {
        return isSearchCriteriaMatched(metadata, toscaType, SdcPropertyNames.PROPERTY_NAME_UUID, SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID);
    }

    static boolean isStringMatchingOrNull(String currentUid, String uidInQuery) {
        return uidInQuery == null || uidInQuery.equals(currentUid);
    }

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

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

    @Override
    public String toString() {
        return String.format("EntityType=%s, nodeTemplateType=%s, toscaType=%s, uUID=%s, customizationUUID=%s",
                entityType, nodeTemplateType, toscaType, uUID, customizationUUID);
    }

    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;
                case ALL:
                    entityQuery = new AllEntitiesQuery();
                    break;
                default:
                    String wrongTypeMsg = (String.format("Wrong entity query type: %s", entityTemplateType));
                    logger.error(wrongTypeMsg);
                    throw new IllegalArgumentException(wrongTypeMsg);
            }
        }

        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;
        }
    }



}