aboutsummaryrefslogtreecommitdiffstats
path: root/sdc-tosca/src/main/java/org/onap/sdc/tosca/parser/impl/QueryProcessor.java
blob: 5bbd2aae32bb31b5e6cb7c7576b5efffd9053f0d (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
/*-
 * ============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.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.onap.sdc.tosca.parser.api.IEntityDetails;
import org.onap.sdc.tosca.parser.elements.queries.EntityQuery;
import org.onap.sdc.tosca.parser.elements.queries.TopologyTemplateQuery;
import org.onap.sdc.tosca.parser.enums.EntityTemplateType;
import org.onap.sdc.tosca.parser.enums.SdcTypes;
import org.onap.sdc.toscaparser.api.NodeTemplate;
import org.onap.sdc.toscaparser.api.ToscaTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Performs search for entity templates inside node template according to query criteria
 */
class QueryProcessor {
    private static final Logger logger = LoggerFactory.getLogger(QueryProcessor.class.getName());

    private final EntityQuery entityQuery;
    private final TopologyTemplateQuery topologyTemplateQuery;
    private final ToscaTemplate toscaTemplate;
    private boolean isRecursive = false;

    QueryProcessor(ToscaTemplate toscaTemplate, EntityQuery entityQuery, TopologyTemplateQuery topologyTemplateQuery, boolean isRecursive) {
        this.toscaTemplate = toscaTemplate;
        this.entityQuery = entityQuery == null ? EntityQuery.newBuilder(EntityTemplateType.ALL).build() : entityQuery;
        this.topologyTemplateQuery = topologyTemplateQuery;
        this.isRecursive = isRecursive;
    }

    List<IEntityDetails> doQuery() {
        final List<IEntityDetails> entityDetailsList = Collections.emptyList();
        if (isServiceSearch()) {
            //search for entities inside the service
            if (logger.isDebugEnabled()) {
                logger.debug("Service {} is searched for {}", toscaTemplate.getMetaData().getValue(SdcPropertyNames.PROPERTY_NAME_NAME), entityQuery);
            }
            entityDetailsList.addAll(entityQuery.getEntitiesFromService(toscaTemplate));

            if (!isRecursive) {
                return entityDetailsList;
            }
        }

        List<NodeTemplate> foundTopologyTemplates = getInternalTopologyTemplates(toscaTemplate.getNodeTemplates(), false);
        if (isRecursive) {
            if (logger.isDebugEnabled()) {
                logger.debug("Search for entities recursively");
            }
            //go over internal topology templates of the found templates
            // and search for instances of the same type
            //if the queried topology template is "SERVICE",  search for all instances of templates in the service
            List<NodeTemplate> internalTopologyTemplates = foundTopologyTemplates.stream()
                    .filter(nt->nt.getSubMappingToscaTemplate() != null)
                    .map(nt->getInternalTopologyTemplates(nt.getSubMappingToscaTemplate().getNodeTemplates(), true))
                    .flatMap(List::stream)
                    .collect(Collectors.toList());
            foundTopologyTemplates.addAll(internalTopologyTemplates);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Found topology templates {} matching following query criteria: {}",
                    foundTopologyTemplates, topologyTemplateQuery);
        }
        //go over all node templates found according to query criteria and recursive flag and
        // search for the requested entities.
        entityDetailsList.addAll(searchEntitiesInsideTopologyTemplates(foundTopologyTemplates));

        return entityDetailsList;
    }

    private Map<String, NodeTemplate> convertListToMap(List<NodeTemplate> nodeTemplateList) {
        // we use map to avoid duplicate search through same node templates
        return nodeTemplateList.stream()
                .collect(Collectors.toMap(NodeTemplate::getName, nt->nt, (nt1, nt2)->nt1));
    }

    private List<IEntityDetails> searchEntitiesInsideTopologyTemplates(List<NodeTemplate> foundTopologyTemplates) {
        return convertListToMap(foundTopologyTemplates)
                .values()
                .stream()
                .map(entityQuery::getEntitiesFromTopologyTemplate)
                .flatMap(List::stream)
                .collect(Collectors.toList());
    }

    private boolean isServiceSearch() {
        return topologyTemplateQuery.getNodeTemplateType() == SdcTypes.SERVICE;
    }

    private List<NodeTemplate> getInternalTopologyTemplates(List<NodeTemplate> nodeTemplateList, boolean isRecursive) {
        if (nodeTemplateList != null) {
            return nodeTemplateList
                .stream()
                .map(child -> getTopologyTemplatesByQuery(child, isRecursive))
                .flatMap(List::stream)
                .collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    private List<NodeTemplate> getTopologyTemplatesByQuery(NodeTemplate current, boolean isRecursive) {
        final List<NodeTemplate> topologyTemplateList = Collections.emptyList();

        boolean isTopologyTemplateFound = isRecursive ?
                SdcTypes.isComplex(current.getMetadata().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE))
                : topologyTemplateQuery.isMatchingSearchCriteria(current);
        if (isTopologyTemplateFound) {
            topologyTemplateList.add(current);
            if (!isRecursive) {
                //recursion stop condition
                return topologyTemplateList;
            }
        }
        if (SdcTypes.isComplex(current.getMetadata().getValue(SdcPropertyNames.PROPERTY_NAME_TYPE)) &&
               current.getSubMappingToscaTemplate() != null) {
           //search the node template inside a given topology template
            topologyTemplateList.addAll(current.getSubMappingToscaTemplate().getNodeTemplates()
                    .stream()
                    .map(nt->getTopologyTemplatesByQuery(nt, isRecursive))
                    .flatMap(List::stream)
                    .collect(Collectors.toList()));
        }
        return topologyTemplateList;
    }

}