aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/io/swagger/api/impl/DcaeServiceTypeObjectRepository.java
blob: 3212fe04d7ccf873b4e9ebf93ce7f4ec746bf85b (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
/*-
 * ============LICENSE_START=======================================================
 * dcae-inventory
 * ================================================================================
 * Copyright (C) 2020 Nokia. 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 io.swagger.api.impl;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.onap.dcae.inventory.daos.InventoryDataAccessManager;
import org.onap.dcae.inventory.dbthings.mappers.DCAEServiceTypeObjectMapper;
import org.onap.dcae.inventory.dbthings.models.DCAEServiceTypeObject;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.function.Consumer;

class DcaeServiceTypeObjectRepository {
    private static final Logger metricsLogger = LoggerFactory.getLogger("metricsLogger");
    private final InventoryDataAccessManager instance;

    public DcaeServiceTypeObjectRepository(InventoryDataAccessManager instance) {
        this.instance = instance;
    }

    List<DCAEServiceTypeObject> fetch(String typeName, Boolean onlyLatest, Boolean onlyActive, String vnfType,
                                      String serviceId, String serviceLocation, String asdcServiceId,
                                      String asdcResourceId, String application, String component, String owner) {

        List<DCAEServiceTypeObject> serviceTypeObjects;

        // TODO: Make this variable also a URL parameter.
        DateTime createdCutoff = DateTime.now(DateTimeZone.UTC);

        try (Handle jdbiHandle = instance.getHandle()) {
            final String queryStatement = DcaeServiceTypesQueryStatement.create(typeName, onlyLatest, onlyActive,
                    vnfType, serviceId, serviceLocation, asdcServiceId, asdcResourceId, owner, application, component);

            metricsLogger.info("Query created as: {}." + queryStatement);

            Query<DCAEServiceTypeObject> query = getQuery(jdbiHandle, queryStatement);

            if (typeName != null){
                typeName = resolveTypeName(typeName);
            }

            ifNotNullBind(typeName, it -> query.bind("typeName", it));
            ifNotNullBind(vnfType, it -> query.bind("vnfType", it));
            ifNotNullBind(serviceId, it -> query.bind("serviceId", it));
            ifNotNullBind(serviceLocation, it -> query.bind("serviceLocation", it));
            ifNotNoneBind(asdcServiceId, it -> query.bind("asdcServiceId", it));
            ifNotNoneBind(asdcResourceId, it -> query.bind("asdcResourceId", it));
            ifNotNullBind(application, it -> query.bind("application", it));
            ifNotNullBind(component, it -> query.bind("component", it));
            ifNotNullBind(owner, it -> query.bind("owner", it));
            bindCreatedCutoff(createdCutoff, query);

            serviceTypeObjects = query.list();
        }

        return serviceTypeObjects;
    }

    private void ifNotNullBind(String value, Consumer<String> bind) {
        if (value != null) {
            bind.accept(value);
        }
    }

    private void ifNotNoneBind(String value, Consumer<String> bind) {
        if (value != null && !"NONE".equalsIgnoreCase(value)) {
            bind.accept(value);
        }
    }

    void bindCreatedCutoff(DateTime createdCutoff, Query<DCAEServiceTypeObject> query) {
        query.bind("createdCutoff", createdCutoff);
    }

    Query<DCAEServiceTypeObject> getQuery(Handle jdbiHandle, String queryStatement) {
        return jdbiHandle.createQuery(queryStatement).map(new DCAEServiceTypeObjectMapper());
    }

    static String resolveTypeName(String typeName){
        if (typeName.contains("*")) {
            return typeName.replaceAll("\\*", "%");
        }
        return typeName;
    }
}