Added CorsConfigurer class and cleaned up typos
[so.git] / so-monitoring / so-monitoring-handler / src / main / java / org / onap / so / monitoring / db / service / DatabaseServiceProviderImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.monitoring.db.service;
21
22 import static org.onap.so.monitoring.configuration.rest.HttpServiceProviderConfiguration.DATABASE_HTTP_REST_SERVICE_PROVIDER;
23
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Map;
28
29 import org.onap.so.monitoring.camunda.model.SoActiveInfraRequests;
30 import org.onap.so.monitoring.configuration.database.DatabaseUrlProvider;
31 import org.onap.so.monitoring.model.SoInfraRequest;
32 import org.onap.so.monitoring.model.SoInfraRequestBuilder;
33 import org.onap.so.monitoring.rest.service.HttpRestServiceProvider;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.beans.factory.annotation.Qualifier;
36 import org.springframework.stereotype.Service;
37
38 import com.google.common.base.Optional;
39
40 /**
41  * @author waqas.ikram@ericsson.com
42  */
43 @Service
44 public class DatabaseServiceProviderImpl implements DatabaseServiceProvider {
45
46     private final DatabaseUrlProvider urlProvider;
47
48     private final HttpRestServiceProvider httpRestServiceProvider;
49
50     @Autowired
51     public DatabaseServiceProviderImpl(final DatabaseUrlProvider urlProvider,
52             @Qualifier(DATABASE_HTTP_REST_SERVICE_PROVIDER) @Autowired final HttpRestServiceProvider httpRestServiceProvider) {
53         this.urlProvider = urlProvider;
54         this.httpRestServiceProvider = httpRestServiceProvider;
55     }
56
57     @Override
58     public List<SoInfraRequest> getSoInfraRequest(final Map<String, String[]> filters, final long startTime,
59             final long endTime, final Integer maxResult) {
60         final String url = urlProvider.getSearchUrl(startTime, endTime, maxResult);
61
62         final Optional<SoActiveInfraRequests[]> optionalRequests =
63                 httpRestServiceProvider.postHttpRequest(filters, url, SoActiveInfraRequests[].class);
64         if (optionalRequests.isPresent()) {
65             return getSoInfraRequest(optionalRequests.get());
66         }
67         return Collections.emptyList();
68     }
69
70
71     private List<SoInfraRequest> getSoInfraRequest(final SoActiveInfraRequests[] requests) {
72         final List<SoInfraRequest> result = new ArrayList<>(requests.length);
73         for (final SoActiveInfraRequests activeRequests : requests) {
74             final SoInfraRequest soInfraRequest =
75                     new SoInfraRequestBuilder().setRequestId(activeRequests.getRequestId())
76                             .setServiceInstanceId(activeRequests.getServiceInstanceId())
77                             .setNetworkId(activeRequests.getNetworkId()).setEndTime(activeRequests.getEndTime())
78                             .setRequestStatus(activeRequests.getRequestStatus())
79                             .setServiceIstanceName(activeRequests.getServiceInstanceName())
80                             .setServiceType(activeRequests.getServiceType()).setStartTime(activeRequests.getStartTime())
81                             .build();
82             result.add(soInfraRequest);
83
84         }
85         return result;
86     }
87
88
89 }