policy upgrade
[dcaegen2/analytics/tca-gen2.git] / dcae-analytics / dcae-analytics-web / src / main / java / org / onap / dcae / analytics / web / spring / ConfigBindingServiceEnvironmentPostProcessor.java
index 31c01b0..453cd9c 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * ================================================================================
  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright (c) 2019-2020 China Mobile. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -81,6 +82,9 @@ public class ConfigBindingServiceEnvironmentPostProcessor implements Environment
     private String configServicePropertiesKey =
             ConfigBindingServiceConstants.CONFIG_BINDING_SERVICE_PROPERTIES_KEY;
 
+    private String springConfigServicePropertiesKey =
+            ConfigBindingServiceConstants.SPRING_CONFIG_BINDING_SERVICE_PROPERTIES_KEY;
+
     @Override
     public void postProcessEnvironment(final ConfigurableEnvironment environment,
             final SpringApplication application) {
@@ -223,32 +227,39 @@ public class ConfigBindingServiceEnvironmentPostProcessor implements Environment
      */
     public String parseTcaConfig(JsonObject jsonObject) {
 
-        JsonElement jsonConfig = jsonObject.get(ConfigBindingServiceConstants.CONFIG);
+        Optional<String> configServiceJsonOptional = Optional.of(jsonObject.toString());
+
+        JsonElement jsonPolicyConfig = jsonObject.get(ConfigBindingServiceConstants.POLICIES);
 
-        Optional<String> configServiceJsonOptional = Optional.of(jsonConfig.toString());
+        String policies = null;
+        if (jsonPolicyConfig != null) {
+            policies = jsonPolicyConfig.getAsJsonObject().getAsJsonArray(ConfigBindingServiceConstants.ITEMS).get(0)
+                                       .getAsJsonObject().get(ConfigBindingServiceConstants.TCAPOLICY).toString();
+        }
 
         // convert fetch config binding service json string to Map of property key and
         // values
         Map<String, Object> configPropertiesMap = configServiceJsonOptional
                 .map(new JsonStringToMapFunction(configServicePropertiesKey)).orElse(Collections.emptyMap());
 
+        if (policies != null) {
+            configPropertiesMap.put(ConfigBindingServiceConstants.POLICY, policies);
+        }
         if (configPropertiesMap.isEmpty()) {
-
             logger.warn("No properties found in config binding service");
 
         } else {
-
             // remove config service key prefix on spring reserved property key prefixes
             final Set<String> springKeyPrefixes =
                     ConfigBindingServiceConstants.getSpringReservedPropertiesKeyPrefixes();
             final Set<String> springKeys = springKeyPrefixes.stream()
-                    .map(springKeyPrefix -> configServicePropertiesKey + "." + springKeyPrefix)
+                    .map(springKeyPrefix -> springConfigServicePropertiesKey + "." + springKeyPrefix)
                     .collect(Collectors.toSet());
 
             filterKeyMap = configPropertiesMap.entrySet().stream()
                     .collect(Collectors.toMap((Map.Entry<String, Object> e) -> springKeys.stream()
                             .anyMatch(springKey -> e.getKey().startsWith(springKey))
-                                    ? e.getKey().substring(configServicePropertiesKey.toCharArray().length + 1)
+                                    ? e.getKey().substring(springConfigServicePropertiesKey.toCharArray().length + 1)
                                     : e.getKey(),
                             Map.Entry::getValue));
 
@@ -256,7 +267,7 @@ public class ConfigBindingServiceEnvironmentPostProcessor implements Environment
                     .info("Adding property from config service in spring context: {} -> {}", key, value));
             MutablePropertySources sources = env.getPropertySources();
             addJsonPropertySource(sources, new MapPropertySource(configServicePropertiesKey, filterKeyMap));
-
+            
         }
         return configServiceJsonOptional.get();
     }