Remove mso-oof-adapter from SO
[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 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Map;
27 import org.onap.so.monitoring.camunda.model.SoActiveInfraRequests;
28 import org.onap.so.monitoring.configuration.database.DatabaseUrlProvider;
29 import org.onap.so.monitoring.model.SoInfraRequest;
30 import org.onap.so.monitoring.model.SoInfraRequestBuilder;
31 import org.onap.so.rest.service.HttpRestServiceProvider;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.beans.factory.annotation.Qualifier;
34 import org.springframework.stereotype.Service;
35 import com.google.common.base.Optional;
36
37 /**
38  * @author waqas.ikram@ericsson.com
39  */
40 @Service
41 public class DatabaseServiceProviderImpl implements DatabaseServiceProvider {
42
43     private final DatabaseUrlProvider urlProvider;
44
45     private final HttpRestServiceProvider httpRestServiceProvider;
46
47     @Autowired
48     public DatabaseServiceProviderImpl(final DatabaseUrlProvider urlProvider,
49             @Qualifier(DATABASE_HTTP_REST_SERVICE_PROVIDER) @Autowired final HttpRestServiceProvider httpRestServiceProvider) {
50         this.urlProvider = urlProvider;
51         this.httpRestServiceProvider = httpRestServiceProvider;
52     }
53
54     @Override
55     public List<SoInfraRequest> getSoInfraRequest(final Map<String, String[]> filters, final long startTime,
56             final long endTime, final Integer maxResult) {
57         final String url = urlProvider.getSearchUrl(startTime, endTime, maxResult);
58
59         final Optional<SoActiveInfraRequests[]> optionalRequests =
60                 httpRestServiceProvider.post(filters, url, SoActiveInfraRequests[].class);
61         if (optionalRequests.isPresent()) {
62             return getSoInfraRequest(optionalRequests.get());
63         }
64         return Collections.emptyList();
65     }
66
67
68     private List<SoInfraRequest> getSoInfraRequest(final SoActiveInfraRequests[] requests) {
69         final List<SoInfraRequest> result = new ArrayList<>(requests.length);
70         for (final SoActiveInfraRequests activeRequests : requests) {
71             final SoInfraRequest soInfraRequest =
72                     new SoInfraRequestBuilder().setRequestId(activeRequests.getRequestId())
73                             .setServiceInstanceId(activeRequests.getServiceInstanceId())
74                             .setNetworkId(activeRequests.getNetworkId()).setEndTime(activeRequests.getEndTime())
75                             .setRequestStatus(activeRequests.getRequestStatus())
76                             .setServiceInstanceName(activeRequests.getServiceInstanceName())
77                             .setServiceType(activeRequests.getServiceType()).setStartTime(activeRequests.getStartTime())
78                             .build();
79             result.add(soInfraRequest);
80
81         }
82         return result;
83     }
84
85
86 }