aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/io/swagger/api/impl/DcaeServicesGroupByApiServiceImpl.java
blob: fff2bd4732fd2109de6e33cc43af040852fd8737 (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
/*-
 * ============LICENSE_START=======================================================
 * dcae-inventory
 * ================================================================================
 * Copyright (C) 2019 AT&T and Nokia 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 io.swagger.api.impl;

import io.swagger.api.DcaeServicesApi;
import io.swagger.api.DcaeServicesGroupbyApiService;
import org.onap.dcae.inventory.daos.InventoryDataAccessManager;
import org.onap.dcae.inventory.dbthings.models.DCAEServiceObject;
import io.swagger.model.DCAEServiceGroupByResults;
import io.swagger.model.DCAEServiceGroupByResultsPropertyValues;
import org.skife.jdbi.v2.Handle;
import org.skife.jdbi.v2.Query;

import javax.ws.rs.core.Link;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.ws.rs.core.UriInfo;
import java.util.List;
import java.util.Map;

@javax.annotation.Generated(value = "class io.swagger.codegen.languages.JavaJerseyServerCodegen", date = "2016-04-18T20:16:22.119Z")
public class DcaeServicesGroupByApiServiceImpl extends DcaeServicesGroupbyApiService {

    public static final String PROPERTY_NAME_TYPE = "type";
    public static final String PROPERTY_NAME_VNF_LOCATION = "vnfLocation";
    public static final String PROPERTY_NAME_VNF_TYPE = "vnfType";
    private static final String UNKNOWN_PROPERTY_NAME = "n/a";

    private final RunningDcaeServicesProvider runningDcaeServicesProvider;
    private final DcaeServicesLinkResolver dcaeServicesLinkResolver;

    public DcaeServicesGroupByApiServiceImpl(
            RunningDcaeServicesProvider runningDcaeServicesProvider,
            DcaeServicesLinkResolver dcaeServicesLinkResolver) {
        this.runningDcaeServicesProvider = runningDcaeServicesProvider;
        this.dcaeServicesLinkResolver = dcaeServicesLinkResolver;
    }

    @Override
    public Response dcaeServicesGroupbyPropertyNameGet(
            String propertyName, UriInfo uriInfo, SecurityContext securityContext) {

        String columnName = resolveColumnName(propertyName);
        if(columnName.equals(UNKNOWN_PROPERTY_NAME)) {
            return Response.status(Response.Status.BAD_REQUEST).build();
        }

        List<Map<String, Object>> runningDcaeServices = runningDcaeServicesProvider.fetch(columnName);
        DCAEServiceGroupByResults response = generateResponse(propertyName, uriInfo, columnName, runningDcaeServices);

        return Response.ok().entity(response).build();
    }

    DCAEServiceGroupByResults generateResponse(
            String propertyName, UriInfo uriInfo, String columnName, List<Map<String, Object>> runningDcaeServices) {

        DCAEServiceGroupByResults response = new DCAEServiceGroupByResults();
        response.setPropertyName(propertyName);

        for (Map<String, Object> result : runningDcaeServices) {
            DCAEServiceGroupByResultsPropertyValues value = new DCAEServiceGroupByResultsPropertyValues();
            value.setCount(((Long) result.get("num")).intValue());

            String propertyValue = (String) result.get(columnName);
            value.setPropertyValue(propertyValue);

            Link dcaeServicesLink = dcaeServicesLinkResolver.resolveLink(propertyName, uriInfo, propertyValue);
            value.setDcaeServiceQueryLink(dcaeServicesLink);

            response.getPropertyValues().add(value);
        }
        return response;
    }

    String resolveColumnName(String propertyName) {
        String columnName;

        switch (propertyName) {
            case PROPERTY_NAME_TYPE:
                columnName = "type_id";
                break;
            case PROPERTY_NAME_VNF_LOCATION:
                columnName = "vnf_location";
                break;
            case PROPERTY_NAME_VNF_TYPE:
                columnName = "vnf_type";
                break;
            default:
                columnName = UNKNOWN_PROPERTY_NAME;
        }
        return columnName;
    }

    public static class DcaeServicesLinkResolver {

        private static final String RELATION = "dcae_services";

        Link resolveLink(String propertyName, UriInfo uriInfo, String propertyValue) {
            Link dcaeServices = null;
            switch (propertyName) {
                case PROPERTY_NAME_TYPE:
                    dcaeServices = DcaeServicesApi.buildLinkForGetByTypeId(uriInfo, RELATION,
                            propertyValue);
                    break;
                case PROPERTY_NAME_VNF_LOCATION:
                    dcaeServices = DcaeServicesApi.buildLinkForGetByVnfLocation(uriInfo, RELATION,
                            propertyValue);
                    break;
                case PROPERTY_NAME_VNF_TYPE:
                    dcaeServices = DcaeServicesApi.buildLinkForGetByVnfType(uriInfo, RELATION,
                            propertyValue);
                    break;
                default:
                    throw new UnsupportedOperationException(String.format("Unsupported '%s' property name!", propertyName));
            }
            return dcaeServices;
        }
    }


    public static class RunningDcaeServicesProvider {

        private static final String SERVICE_STATUS_PLACE_HOLDER = "serviceStatus";

        public RunningDcaeServicesProvider(InventoryDataAccessManager inventoryDataAccessManager) {
            this.inventoryDataAccessManager = inventoryDataAccessManager;
        }

        private final InventoryDataAccessManager inventoryDataAccessManager;

        public List<Map<String, Object>> fetch(String columnName) {

            String queryString = createQuery(columnName);
            try (Handle jdbiHandle = inventoryDataAccessManager.getHandle()) {
                return executeQuery(queryString, jdbiHandle);
            }
        }

        List<Map<String, Object>> executeQuery(String queryString, Handle jdbiHandle) {
            // NOTE: This is hardcoded because service status is only used internally.
            final Query<Map<String, Object>> query = jdbiHandle.createQuery(queryString);
            final Query<Map<String, Object>> bind = query
                    .bind(SERVICE_STATUS_PLACE_HOLDER, DCAEServiceObject.DCAEServiceStatus.RUNNING);
            return bind.list();
        }

        static String createQuery(String columnName) {
            StringBuilder sb = new StringBuilder();
            sb.append(String.format("select %s, count(1) as num ", columnName));
            sb.append(String.format(" from dcae_services where status = :%s",SERVICE_STATUS_PLACE_HOLDER));
            sb.append(String.format(" group by %s order by count(1) desc", columnName));
            return sb.toString();
        }
    }
}