Fix VES problem with subsequent fetching from CBS
[dcaegen2/collectors/ves.git] / src / main / java / org / onap / dcae / configuration / cbs / CbsConfigResolver.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dcaegen2.collectors.ves
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.dcae.configuration.cbs;
21
22 import com.google.gson.JsonObject;
23 import io.vavr.control.Option;
24 import org.json.JSONObject;
25 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory;
26 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests;
27 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsClientConfiguration;
28 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest;
29 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35 public class CbsConfigResolver {
36
37     private static final Logger log = LoggerFactory.getLogger(CbsConfigResolver.class);
38
39     private final CbsClientConfigurationResolver cbsClientConfigurationResolver;
40     private final RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
41     private final CbsRequest cbsConfigurationRequest = CbsRequests.getConfiguration(diagnosticContext);
42
43     CbsConfigResolver(CbsClientConfigurationResolver cbsClientConfigurationResolver) {
44         this.cbsClientConfigurationResolver = cbsClientConfigurationResolver;
45     }
46
47     public Option<JSONObject> getAppConfig() {
48         JsonObject emptyJson = new JsonObject();
49         CbsClientConfiguration cbsClientConfiguration = cbsClientConfigurationResolver.resolveCbsClientConfiguration();
50         JsonObject jsonObject = CbsClientFactory.createCbsClient(cbsClientConfiguration)
51             .flatMap(cbsClient -> cbsClient.get(cbsConfigurationRequest))
52             .doOnError(error -> log.warn("Failed to fetch configuration from CBS " + error.getMessage()))
53             .onErrorReturn(emptyJson)
54             .block();
55
56         return emptyJson.equals(jsonObject) ? Option.none() : Option.of(new JSONObject(jsonObject.toString()));
57     }
58 }