diff options
author | krishna <krishna.moorthy6@wipro.com> | 2020-02-08 14:11:41 +0530 |
---|---|---|
committer | krishna <krishna.moorthy6@wipro.com> | 2020-02-12 11:59:24 +0530 |
commit | cee68e2e64d35019b7b66570062bd6e8e7981dc5 (patch) | |
tree | 3fc3b5301caec66cf2f5787b1a204a552e78e2d1 /src/main/java/org/onap | |
parent | e05d755dfb068c5d88a0b6d5cf44b3db60afe573 (diff) |
Update microservice to run as standalone MS
Issue-ID: DCAEGEN2-2066
Signed-off-by: krishna <krishna.moorthy6@wipro.com>
Change-Id: I3305d6df039ba2c3a9283bf6cd4c50ca073d12c8
Diffstat (limited to 'src/main/java/org/onap')
3 files changed, 151 insertions, 112 deletions
diff --git a/src/main/java/org/onap/dcaegen2/services/sonhms/Application.java b/src/main/java/org/onap/dcaegen2/services/sonhms/Application.java index 9919bed..3e65c8c 100644 --- a/src/main/java/org/onap/dcaegen2/services/sonhms/Application.java +++ b/src/main/java/org/onap/dcaegen2/services/sonhms/Application.java @@ -2,26 +2,34 @@ * ============LICENSE_START======================================================= * son-handler * ================================================================================ - * Copyright (C) 2019 Wipro Limited. + * Copyright (C) 2019-2020 Wipro Limited. * ============================================================================== * 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 org.onap.dcaegen2.services.sonhms; +import com.google.gson.Gson; +import com.google.gson.JsonObject; +import com.google.gson.reflect.TypeToken; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.lang.reflect.Type; import java.time.Duration; +import java.util.Map; import javax.sql.DataSource; @@ -33,6 +41,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; + @SpringBootApplication public class Application { @@ -42,6 +51,40 @@ public class Application { * Main method where the pci context is initially set. */ public static void main(String[] args) { + getConfig(); + log.info("Starting spring boot application"); + SpringApplication.run(Application.class); + + } + + private static void getConfig() { + + Boolean standalone = Boolean.parseBoolean(System.getenv("STANDALONE")); + + if (standalone) { + log.info("Running in standalone mode"); + + String configFile = System.getenv("CONFIG_FILE"); + String configAllJson = readFromFile(configFile); + + JsonObject configAll = new Gson().fromJson(configAllJson, JsonObject.class); + + JsonObject config = configAll.getAsJsonObject("config"); + + Configuration.getInstance().updateConfigurationFromJsonObject(config); + + ConfigPolicy configPolicy = ConfigPolicy.getInstance(); + Type mapType = new TypeToken<Map<String, Object>>() { + }.getType(); + if (configAll.getAsJsonObject("policies") != null) { + JsonObject policyJson = configAll.getAsJsonObject("policies").getAsJsonArray("items").get(0) + .getAsJsonObject().getAsJsonObject("config"); + Map<String, Object> policy = new Gson().fromJson(policyJson, mapType); + configPolicy.setConfig(policy); + log.info("Config policy {}", configPolicy); + } + return; + } ConfigFetchFromCbs configFetchFromCbs = new ConfigFetchFromCbs(Duration.ofSeconds(60)); Thread configFetchThread = new Thread(configFetchFromCbs); @@ -52,9 +95,6 @@ public class Application { log.debug("InterruptedException : {}", e); } log.info("after 10s sleep"); - log.info("Starting spring boot application"); - SpringApplication.run(Application.class); - } /** @@ -70,4 +110,19 @@ public class Application { .password(configuration.getPgPassword()).build(); } -} + private static String readFromFile(String file) { + String content = ""; + try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) { + content = bufferedReader.readLine(); + String temp; + while ((temp = bufferedReader.readLine()) != null) { + content = content.concat(temp); + } + content = content.trim(); + } catch (Exception e) { + content = null; + } + return content; + } + +}
\ No newline at end of file diff --git a/src/main/java/org/onap/dcaegen2/services/sonhms/Configuration.java b/src/main/java/org/onap/dcaegen2/services/sonhms/Configuration.java index faa1b4e..d3d9d39 100644 --- a/src/main/java/org/onap/dcaegen2/services/sonhms/Configuration.java +++ b/src/main/java/org/onap/dcaegen2/services/sonhms/Configuration.java @@ -2,30 +2,42 @@ * ============LICENSE_START======================================================= * son-handler * ================================================================================ - * Copyright (C) 2019 Wipro Limited. + * Copyright (C) 2019-2020 Wipro Limited. * ============================================================================== * 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 org.onap.dcaegen2.services.sonhms; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.reflect.TypeToken; + +import java.lang.reflect.Type; import java.util.List; import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + public class Configuration { + private static Logger log = LoggerFactory.getLogger(Configuration.class); + private static Configuration instance = null; private String pgHost; private int pgPort; @@ -59,8 +71,8 @@ public class Configuration { private int oofTriggerCountTimer; private int oofTriggerCountThreshold; private int policyRespTimer; - - + + public int getPoorCountThreshold() { return poorCountThreshold; } @@ -122,7 +134,7 @@ public class Configuration { */ public boolean isSecured() { return (aafUsername != null); - + } public String getAafUsername() { @@ -355,7 +367,67 @@ public class Configuration { + oofTriggerCountTimer + ", oofTriggerCountThreshold=" + oofTriggerCountThreshold + ", policyRespTimer=" + policyRespTimer + "]"; } - - -} + /** + * updates application configuration. + */ + public void updateConfigurationFromJsonObject(JsonObject jsonObject) { + + log.info("Updating configuration from CBS"); + + Type mapType = new TypeToken<Map<String, Object>>() { + }.getType(); + + JsonObject subscribes = jsonObject.getAsJsonObject("streams_subscribes"); + streamsSubscribes = new Gson().fromJson(subscribes, mapType); + + JsonObject publishes = jsonObject.getAsJsonObject("streams_publishes"); + streamsPublishes = new Gson().fromJson(publishes, mapType); + + pgPort = jsonObject.get("postgres.port").getAsInt(); + pollingInterval = jsonObject.get("sonhandler.pollingInterval").getAsInt(); + pgPassword = jsonObject.get("postgres.password").getAsString(); + numSolutions = jsonObject.get("sonhandler.numSolutions").getAsInt(); + minConfusion = jsonObject.get("sonhandler.minConfusion").getAsInt(); + maximumClusters = jsonObject.get("sonhandler.maximumClusters").getAsInt(); + minCollision = jsonObject.get("sonhandler.minCollision").getAsInt(); + sourceId = jsonObject.get("sonhandler.sourceId").getAsString(); + pgUsername = jsonObject.get("postgres.username").getAsString(); + pgHost = jsonObject.get("postgres.host").getAsString(); + + JsonArray servers = jsonObject.getAsJsonArray("sonhandler.dmaap.server"); + Type listType = new TypeToken<List<String>>() { + }.getType(); + dmaapServers = new Gson().fromJson(servers, listType); + + cg = jsonObject.get("sonhandler.cg").getAsString(); + bufferTime = jsonObject.get("sonhandler.bufferTime").getAsInt(); + cid = jsonObject.get("sonhandler.cid").getAsString(); + configDbService = jsonObject.get("sonhandler.configDb.service").getAsString(); + String namespace = jsonObject.get("sonhandler.namespace").getAsString(); + callbackUrl = "http://" + System.getenv("HOSTNAME") + "." + namespace + ":8080/callbackUrl"; + + pciOptimizer = jsonObject.get("sonhandler.pciOptimizer").getAsString(); + pciAnrOptimizer = jsonObject.get("sonhandler.pciAnrOptimizer").getAsString(); + + oofService = jsonObject.get("sonhandler.oof.service").getAsString(); + oofEndpoint = jsonObject.get("sonhandler.oof.endpoint").getAsString(); + pollingTimeout = jsonObject.get("sonhandler.pollingTimeout").getAsInt(); + + badThreshold = jsonObject.get("sonhandler.badThreshold").getAsInt(); + poorThreshold = jsonObject.get("sonhandler.poorThreshold").getAsInt(); + + poorCountThreshold = jsonObject.get("sonhandler.poorCountThreshold").getAsInt(); + badCountThreshold = jsonObject.get("sonhandler.badCountThreshold").getAsInt(); + oofTriggerCountTimer = jsonObject.get("sonhandler.oofTriggerCountTimer").getAsInt(); + oofTriggerCountThreshold = jsonObject.get("sonhandler.oofTriggerCountThreshold").getAsInt(); + policyRespTimer = jsonObject.get("sonhandler.policyRespTimer").getAsInt(); + + + log.info("configuration from CBS {}", this); + + } + + + +}
\ No newline at end of file diff --git a/src/main/java/org/onap/dcaegen2/services/sonhms/controller/ConfigFetchFromCbs.java b/src/main/java/org/onap/dcaegen2/services/sonhms/controller/ConfigFetchFromCbs.java index afa26d8..448ac27 100644 --- a/src/main/java/org/onap/dcaegen2/services/sonhms/controller/ConfigFetchFromCbs.java +++ b/src/main/java/org/onap/dcaegen2/services/sonhms/controller/ConfigFetchFromCbs.java @@ -2,33 +2,31 @@ * ============LICENSE_START======================================================= * son-handler * ================================================================================ - * Copyright (C) 2019 Wipro Limited. + * Copyright (C) 2019-2020 Wipro Limited. * ============================================================================== * 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 org.onap.dcaegen2.services.sonhms.controller; import com.google.gson.Gson; -import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; import java.lang.reflect.Type; import java.time.Duration; -import java.util.List; import java.util.Map; import org.onap.dcaegen2.services.sdk.rest.services.cbs.client.api.CbsClientFactory; @@ -89,7 +87,7 @@ public class ConfigFetchFromCbs implements Runnable { } } - updateConfigurationFromJsonObject(config); + Configuration.getInstance().updateConfigurationFromJsonObject(config); Type mapType = new TypeToken<Map<String, Object>>() { }.getType(); @@ -103,93 +101,7 @@ public class ConfigFetchFromCbs implements Runnable { }, throwable -> log.warn("Ooops", throwable)); } - private void updateConfigurationFromJsonObject(JsonObject jsonObject) { - - log.info("Updating configuration from CBS"); - Configuration configuration = Configuration.getInstance(); - - Type mapType = new TypeToken<Map<String, Object>>() { - }.getType(); - - JsonObject subscribes = jsonObject.getAsJsonObject("streams_subscribes"); - Map<String, Object> streamsSubscribes = new Gson().fromJson(subscribes, mapType); - - JsonObject publishes = jsonObject.getAsJsonObject("streams_publishes"); - Map<String, Object> streamsPublishes = new Gson().fromJson(publishes, mapType); - - int pgPort = jsonObject.get("postgres.port").getAsInt(); - int pollingInterval = jsonObject.get("sonhandler.pollingInterval").getAsInt(); - String pgPassword = jsonObject.get("postgres.password").getAsString(); - int numSolutions = jsonObject.get("sonhandler.numSolutions").getAsInt(); - int minConfusion = jsonObject.get("sonhandler.minConfusion").getAsInt(); - int maximumClusters = jsonObject.get("sonhandler.maximumClusters").getAsInt(); - int minCollision = jsonObject.get("sonhandler.minCollision").getAsInt(); - String sourceId = jsonObject.get("sonhandler.sourceId").getAsString(); - String pgUsername = jsonObject.get("postgres.username").getAsString(); - String pgHost = jsonObject.get("postgres.host").getAsString(); - - JsonArray servers = jsonObject.getAsJsonArray("sonhandler.dmaap.server"); - Type listType = new TypeToken<List<String>>() { - }.getType(); - List<String> dmaapServers = new Gson().fromJson(servers, listType); - - String cg = jsonObject.get("sonhandler.cg").getAsString(); - int bufferTime = jsonObject.get("sonhandler.bufferTime").getAsInt(); - String cid = jsonObject.get("sonhandler.cid").getAsString(); - String configDbService = jsonObject.get("sonhandler.configDb.service").getAsString(); - String namespace = jsonObject.get("sonhandler.namespace").getAsString(); - String callbackUrl = "http://" + System.getenv("HOSTNAME") + "." + namespace + ":8080/callbackUrl"; - - String pciOptimizer = jsonObject.get("sonhandler.pciOptimizer").getAsString(); - String pciAnrOptimizer = jsonObject.get("sonhandler.pciAnrOptimizer").getAsString(); - - String oofService = jsonObject.get("sonhandler.oof.service").getAsString(); - String oofEndpoint = jsonObject.get("sonhandler.oof.endpoint").getAsString(); - int pollingTimeout = jsonObject.get("sonhandler.pollingTimeout").getAsInt(); - - int badThreshold = jsonObject.get("sonhandler.badThreshold").getAsInt(); - int poorThreshold = jsonObject.get("sonhandler.poorThreshold").getAsInt(); - - int poorCountThreshold = jsonObject.get("sonhandler.poorCountThreshold").getAsInt(); - int badCountThreshold = jsonObject.get("sonhandler.badCountThreshold").getAsInt(); - int oofTriggerCountTimer = jsonObject.get("sonhandler.oofTriggerCountTimer").getAsInt(); - int oofTriggerCountThreshold = jsonObject.get("sonhandler.oofTriggerCountThreshold").getAsInt(); - int policyRespTimer = jsonObject.get("sonhandler.policyRespTimer").getAsInt(); - - configuration.setStreamsSubscribes(streamsSubscribes); - configuration.setStreamsPublishes(streamsPublishes); - configuration.setPgPassword(pgPassword); - configuration.setPgPort(pgPort); - configuration.setPollingInterval(pollingInterval); - configuration.setNumSolutions(numSolutions); - configuration.setMinCollision(minCollision); - configuration.setMinConfusion(minConfusion); - configuration.setMaximumClusters(maximumClusters); - configuration.setPgHost(pgHost); - configuration.setPgUsername(pgUsername); - configuration.setSourceId(sourceId); - configuration.setDmaapServers(dmaapServers); - configuration.setCg(cg); - configuration.setCid(cid); - configuration.setBufferTime(bufferTime); - configuration.setConfigDbService(configDbService); - configuration.setCallbackUrl(callbackUrl); - configuration.setPciOptimizer(pciOptimizer); - configuration.setPciAnrOptimizer(pciAnrOptimizer); - configuration.setOofService(oofService); - configuration.setOofEndpoint(oofEndpoint); - configuration.setPollingTimeout(pollingTimeout); - configuration.setBadThreshold(badThreshold); - configuration.setPoorThreshold(poorThreshold); - configuration.setPoorCountThreshold(poorCountThreshold); - configuration.setBadCountThreshold(badCountThreshold); - configuration.setOofTriggerCountTimer(oofTriggerCountTimer); - configuration.setOofTriggerCountThreshold(oofTriggerCountThreshold); - configuration.setPolicyRespTimer(policyRespTimer); - - log.info("configuration from CBS {}", configuration); - } @Override public void run() { @@ -208,4 +120,4 @@ public class ConfigFetchFromCbs implements Runnable { } } -} +}
\ No newline at end of file |