Merge "Added STD sim 2.0.0 tests"
[nonrtric.git] / policy-agent / src / main / java / org / oransc / policyagent / configuration / ApplicationConfigParser.java
index 530ac98..14e836b 100644 (file)
 
 package org.oransc.policyagent.configuration;
 
-import com.google.gson.Gson;
-import com.google.gson.GsonBuilder;
 import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 
-import java.net.MalformedURLException;
-import java.net.URL;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
 import java.util.Map.Entry;
-import java.util.Properties;
 import java.util.Set;
-import java.util.Vector;
 
 import javax.validation.constraints.NotNull;
 
+import org.immutables.gson.Gson;
+import org.immutables.value.Value;
 import org.oransc.policyagent.exceptions.ServiceException;
 
+/**
+ * Parser for the Json representing of the component configuration.
+ */
 public class ApplicationConfigParser {
 
     private static final String CONFIG = "config";
+    private static final String CONTROLLER = "controller";
 
-    private static Gson gson = new GsonBuilder() //
-        .serializeNulls() //
-        .create(); //
+    @Value.Immutable
+    @Gson.TypeAdapters
+    public interface ConfigParserResult {
+        List<RicConfig> ricConfigs();
 
-    private Vector<RicConfig> ricConfig;
-    private Properties dmaapConsumerConfig;
+        Map<String, ControllerConfig> controllerConfigs();
 
-    public ApplicationConfigParser() {
-    }
+        String dmaapConsumerTopicUrl();
+
+        String dmaapProducerTopicUrl();
 
-    public void parse(JsonObject root) throws ServiceException {
-        JsonObject ricConfigJson = root.getAsJsonObject(CONFIG);
-        ricConfig = parseRics(ricConfigJson);
-        JsonObject dmaapConfigJson = root.getAsJsonObject("streams_subscribes");
-        dmaapConsumerConfig = parseDmaapConsumerConfig(dmaapConfigJson);
     }
 
-    public Vector<RicConfig> getRicConfigs() {
-        return this.ricConfig;
+    public ConfigParserResult parse(JsonObject root) throws ServiceException {
+
+        String dmaapProducerTopicUrl = "";
+        String dmaapConsumerTopicUrl = "";
+
+        JsonObject agentConfigJson = root.getAsJsonObject(CONFIG);
+
+        if (agentConfigJson == null) {
+            throw new ServiceException("Missing root configuration \"" + CONFIG + "\" in JSON: " + root);
+        }
+
+        JsonObject json = agentConfigJson.getAsJsonObject("streams_publishes");
+        if (json != null) {
+            dmaapProducerTopicUrl = parseDmaapConfig(json);
+        }
+
+        json = agentConfigJson.getAsJsonObject("streams_subscribes");
+        if (json != null) {
+            dmaapConsumerTopicUrl = parseDmaapConfig(json);
+        }
+
+        List<RicConfig> ricConfigs = parseRics(agentConfigJson);
+        Map<String, ControllerConfig> controllerConfigs = parseControllerConfigs(agentConfigJson);
+        checkConfigurationConsistency(ricConfigs, controllerConfigs);
+
+        return ImmutableConfigParserResult.builder() //
+            .dmaapConsumerTopicUrl(dmaapConsumerTopicUrl) //
+            .dmaapProducerTopicUrl(dmaapProducerTopicUrl) //
+            .ricConfigs(ricConfigs) //
+            .controllerConfigs(controllerConfigs) //
+            .build();
     }
 
-    public Properties getDmaapConsumerConfig() {
-        return dmaapConsumerConfig;
+    private void checkConfigurationConsistency(List<RicConfig> ricConfigs,
+        Map<String, ControllerConfig> controllerConfigs) throws ServiceException {
+        Set<String> ricUrls = new HashSet<>();
+        Set<String> ricNames = new HashSet<>();
+        for (RicConfig ric : ricConfigs) {
+            if (!ricUrls.add(ric.baseUrl())) {
+                throw new ServiceException("Configuration error, more than one RIC URL: " + ric.baseUrl());
+            }
+            if (!ricNames.add(ric.name())) {
+                throw new ServiceException("Configuration error, more than one RIC with name: " + ric.name());
+            }
+            if (!ric.controllerName().isEmpty() && controllerConfigs.get(ric.controllerName()) == null) {
+                throw new ServiceException(
+                    "Configuration error, controller configuration not found: " + ric.controllerName());
+            }
+
+        }
     }
 
-    private Vector<RicConfig> parseRics(JsonObject config) throws ServiceException {
-        Vector<RicConfig> result = new Vector<RicConfig>();
+    private List<RicConfig> parseRics(JsonObject config) throws ServiceException {
+        List<RicConfig> result = new ArrayList<>();
         for (JsonElement ricElem : getAsJsonArray(config, "ric")) {
-            result.add(gson.fromJson(ricElem.getAsJsonObject(), ImmutableRicConfig.class));
+            JsonObject ricAsJson = ricElem.getAsJsonObject();
+            JsonElement controllerNameElement = ricAsJson.get(CONTROLLER);
+            ImmutableRicConfig ricConfig = ImmutableRicConfig.builder() //
+                .name(get(ricAsJson, "name").getAsString()) //
+                .baseUrl(get(ricAsJson, "baseUrl").getAsString()) //
+                .managedElementIds(parseManagedElementIds(get(ricAsJson, "managedElementIds").getAsJsonArray())) //
+                .controllerName(controllerNameElement != null ? controllerNameElement.getAsString() : "") //
+                .build();
+            result.add(ricConfig);
         }
         return result;
     }
 
+    Map<String, ControllerConfig> parseControllerConfigs(JsonObject config) throws ServiceException {
+        if (config.get(CONTROLLER) == null) {
+            return new HashMap<>();
+        }
+        Map<String, ControllerConfig> result = new HashMap<>();
+        for (JsonElement element : getAsJsonArray(config, CONTROLLER)) {
+            JsonObject controllerAsJson = element.getAsJsonObject();
+            ImmutableControllerConfig controllerConfig = ImmutableControllerConfig.builder() //
+                .name(get(controllerAsJson, "name").getAsString()) //
+                .baseUrl(get(controllerAsJson, "baseUrl").getAsString()) //
+                .password(get(controllerAsJson, "password").getAsString()) //
+                .userName(get(controllerAsJson, "userName").getAsString()) // )
+                .build();
+
+            if (result.put(controllerConfig.name(), controllerConfig) != null) {
+                throw new ServiceException(
+                    "Configuration error, more than one controller with name: " + controllerConfig.name());
+            }
+        }
+        return result;
+    }
+
+    private List<String> parseManagedElementIds(JsonArray asJsonObject) {
+        Iterator<JsonElement> iterator = asJsonObject.iterator();
+        List<String> managedElementIds = new ArrayList<>();
+        while (iterator.hasNext()) {
+            managedElementIds.add(iterator.next().getAsString());
+
+        }
+        return managedElementIds;
+    }
+
     private static JsonElement get(JsonObject obj, String memberName) throws ServiceException {
         JsonElement elem = obj.get(memberName);
         if (elem == null) {
-            throw new ServiceException("Could not find member: " + memberName + " in: " + obj);
+            throw new ServiceException("Could not find member: '" + memberName + "' in: " + obj);
         }
         return elem;
     }
@@ -86,69 +172,18 @@ public class ApplicationConfigParser {
         return get(obj, memberName).getAsJsonArray();
     }
 
-    private Properties parseDmaapConsumerConfig(JsonObject consumerCfg) throws ServiceException {
-        Set<Entry<String, JsonElement>> topics = consumerCfg.entrySet();
-        if (topics.size() != 1) {
-            throw new ServiceException("Invalid configuration, number of topic must be one, config: " + topics);
-        }
-        JsonObject topic = topics.iterator().next().getValue().getAsJsonObject();
-        JsonObject dmaapInfo = get(topic, "dmaap_info").getAsJsonObject();
-        String topicUrl = getAsString(dmaapInfo, "topic_url");
-
-        Properties dmaapProps = new Properties();
-        try {
-            URL url = new URL(topicUrl);
-            String passwd = "";
-            String userName = "";
-            if (url.getUserInfo() != null) {
-                String[] userInfo = url.getUserInfo().split(":");
-                userName = userInfo[0];
-                passwd = userInfo[1];
-            }
-            String urlPath = url.getPath();
-            DmaapConsumerUrlPath path = parseDmaapUrlPath(urlPath);
-
-            dmaapProps.put("port", url.getPort());
-            dmaapProps.put("server", url.getHost());
-            dmaapProps.put("topic", path.dmaapTopicName);
-            dmaapProps.put("consumerGroup", path.consumerGroup);
-            dmaapProps.put("consumerInstance", path.consumerId);
-            dmaapProps.put("fetchTimeout", 15000);
-            dmaapProps.put("fetchLimit", 1000);
-            dmaapProps.put("userName", userName);
-            dmaapProps.put("password", passwd);
-        } catch (MalformedURLException e) {
-            throw new ServiceException("Could not parse the URL", e);
+    private String parseDmaapConfig(JsonObject streamCfg) throws ServiceException {
+        Set<Entry<String, JsonElement>> streamConfigEntries = streamCfg.entrySet();
+        if (streamConfigEntries.size() != 1) {
+            throw new ServiceException(
+                "Invalid configuration. Number of streams must be one, config: " + streamConfigEntries);
         }
-
-        return dmaapProps;
+        JsonObject streamConfigEntry = streamConfigEntries.iterator().next().getValue().getAsJsonObject();
+        JsonObject dmaapInfo = get(streamConfigEntry, "dmaap_info").getAsJsonObject();
+        return getAsString(dmaapInfo, "topic_url");
     }
 
     private static @NotNull String getAsString(JsonObject obj, String memberName) throws ServiceException {
         return get(obj, memberName).getAsString();
     }
-
-    private class DmaapConsumerUrlPath {
-        final String dmaapTopicName;
-        final String consumerGroup;
-        final String consumerId;
-
-        DmaapConsumerUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
-            this.dmaapTopicName = dmaapTopicName;
-            this.consumerGroup = consumerGroup;
-            this.consumerId = consumerId;
-        }
-    }
-
-    private DmaapConsumerUrlPath parseDmaapUrlPath(String urlPath) throws ServiceException {
-        String[] tokens = urlPath.split("/"); // /events/A1-P/users/sdnc1
-        if (tokens.length != 5) {
-            throw new ServiceException("The path has incorrect syntax: " + urlPath);
-        }
-
-        final String dmaapTopicName = tokens[1] + "/" + tokens[2]; // /events/A1-P
-        final String consumerGroup = tokens[3]; // users
-        final String consumerId = tokens[4]; // sdnc1
-        return new DmaapConsumerUrlPath(dmaapTopicName, consumerGroup, consumerId);
-    }
 }