Fix bugs and formatting issues
[dcaegen2/services/son-handler.git] / src / main / java / org / onap / dcaegen2 / services / sonhms / controller / ConfigFetchFromCbs.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  son-handler
4  *  ================================================================================
5  *   Copyright (C) 2019 Wipro Limited.
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  *******************************************************************************/
21
22 package org.onap.dcaegen2.services.sonhms.controller;
23
24 import com.google.gson.Gson;
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonObject;
27 import com.google.gson.reflect.TypeToken;
28
29 import java.lang.reflect.Type;
30 import java.util.List;
31 import java.util.Map;
32
33 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory;
34 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsRequests;
35 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.CbsRequest;
36 import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.model.EnvProperties;
37 import org.onap.dcaegen2.services.sdk.rest.services.model.logging.RequestDiagnosticContext;
38 import org.onap.dcaegen2.services.sonhms.ConfigPolicy;
39 import org.onap.dcaegen2.services.sonhms.Configuration;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43
44 public class ConfigFetchFromCbs {
45
46     private static Logger log = LoggerFactory.getLogger(ConfigFetchFromCbs.class);
47
48     /**
49      * Gets app config from CBS.
50      */
51     public void getAppConfig() {
52
53         // Generate RequestID and InvocationID which will be used when logging and in
54         // HTTP requests
55         log.info("getAppconfig start ..");
56         RequestDiagnosticContext diagnosticContext = RequestDiagnosticContext.create();
57         // Read necessary properties from the environment
58         final EnvProperties env = EnvProperties.fromEnvironment();
59         log.debug("environments {}",env);
60         ConfigPolicy configPolicy = ConfigPolicy.getInstance();
61         
62         // Create the client and use it to get the configuration
63         final CbsRequest request = CbsRequests.getAll(diagnosticContext);
64         CbsClientFactory.createCbsClient(env).flatMap(cbsClient -> cbsClient.get(request))
65                 .subscribe(jsonObject -> {
66                     log.debug("configuration from CBS {}", jsonObject);
67                     JsonObject config = jsonObject.getAsJsonObject("config");
68                     
69                     updateConfigurationFromJsonObject(config);
70                     
71                     Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
72                     JsonObject policyJson = jsonObject.getAsJsonObject("policy");
73                     Map<String,Object> policy = new Gson().fromJson(policyJson, mapType);
74                     configPolicy.setConfig(policy);
75                 }, throwable -> log.warn("Ooops", throwable)) ;
76
77     }
78
79     private void updateConfigurationFromJsonObject(JsonObject jsonObject) {
80         
81         log.info("Updating configuration from CBS");
82         Configuration configuration = Configuration.getInstance();
83         log.info("configuration from CBS {}", jsonObject);
84         
85         Type mapType = new TypeToken<Map<String, Object>>() {}.getType();
86         
87         JsonObject subscribes = jsonObject.getAsJsonObject("streams_subscribes");
88         Map<String, Object> streamsSubscribes = new Gson().fromJson(subscribes, mapType);
89         
90         JsonObject publishes = jsonObject.getAsJsonObject("streams_publishes");
91         Map<String, Object> streamsPublishes = new Gson().fromJson(publishes, mapType);
92         
93         int pgPort = jsonObject.get("postgres.port").getAsInt();
94         int pollingInterval = jsonObject.get("sonhandler.pollingInterval").getAsInt();
95         String pgPassword = jsonObject.get("postgres.password").getAsString();
96         int numSolutions = jsonObject.get("sonhandler.numSolutions").getAsInt();
97         int minConfusion = jsonObject.get("sonhandler.minConfusion").getAsInt();
98         int maximumClusters = jsonObject.get("sonhandler.maximumClusters").getAsInt();
99         int minCollision = jsonObject.get("sonhandler.minCollision").getAsInt();
100         String sourceId = jsonObject.get("sonhandler.sourceId").getAsString();
101         String pgUsername = jsonObject.get("postgres.username").getAsString();
102         String pgHost = jsonObject.get("postgres.host").getAsString();
103
104         JsonArray servers = jsonObject.getAsJsonArray("sonhandler.dmaap.server");
105         Type listType = new TypeToken<List<String>>() {}.getType();
106         List<String> dmaapServers = new Gson().fromJson(servers, listType);
107         
108         String cg = jsonObject.get("sonhandler.cg").getAsString();
109         int bufferTime = jsonObject.get("sonhandler.bufferTime").getAsInt();
110         String cid = jsonObject.get("sonhandler.cid").getAsString();
111         String configDbService = jsonObject.get("sonhandler.configDb.service").getAsString();
112         String namespace = jsonObject.get("sonhandler.namespace").getAsString();
113         String callbackUrl = "http://" + System.getenv("HOSTNAME") + "." + namespace + ":8080/callbackUrl";
114         
115         JsonArray optimizersJson = jsonObject.getAsJsonArray("sonhandler.optimizers");
116         List<String> optimizers = new Gson().fromJson(optimizersJson, listType);                   
117         
118         String oofService = jsonObject.get("sonhandler.oof.service").getAsString();
119         int pollingTimeout = jsonObject.get("sonhandler.pollingTimeout").getAsInt();
120
121         int badThreshold = jsonObject.get("sonhandler.badThreshold").getAsInt();
122         int poorThreshold = jsonObject.get("sonhandler.poorThreshold").getAsInt();
123
124         configuration.setStreamsSubscribes(streamsSubscribes);
125         configuration.setStreamsPublishes(streamsPublishes);
126         configuration.setPgPassword(pgPassword);
127         configuration.setPgPort(pgPort);
128         configuration.setPollingInterval(pollingInterval);
129         configuration.setNumSolutions(numSolutions);
130         configuration.setMinCollision(minCollision);
131         configuration.setMinConfusion(minConfusion);
132         configuration.setMaximumClusters(maximumClusters);
133         configuration.setPgHost(pgHost);
134         configuration.setPgUsername(pgUsername);
135         configuration.setSourceId(sourceId);
136         configuration.setDmaapServers(dmaapServers);
137         configuration.setCg(cg);
138         configuration.setCid(cid);
139         configuration.setBufferTime(bufferTime);
140         configuration.setConfigDbService(configDbService);
141         configuration.setCallbackUrl(callbackUrl);
142         configuration.setOptimizers(optimizers);
143         configuration.setOofService(oofService);
144         configuration.setPollingTimeout(pollingTimeout);
145         configuration.setBadThreshold(badThreshold);
146         configuration.setPoorThreshold(poorThreshold);
147         log.info("configuration from CBS {}", configuration.toString());
148
149     }
150
151 }