Merge "Reorder modifiers"
[so.git] / adapters / mso-adapter-utils / src / main / java / org / openecomp / mso / cloud / CloudConfig.java
index 2323007..2c15391 100644 (file)
@@ -22,20 +22,22 @@ package org.openecomp.mso.cloud;
 
 import java.io.FileReader;
 import java.io.IOException;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Optional;
 
-import org.codehaus.jackson.JsonParseException;
-import org.codehaus.jackson.annotate.JsonProperty;
-import org.codehaus.jackson.map.DeserializationConfig;
-import org.codehaus.jackson.map.JsonMappingException;
-import org.codehaus.jackson.map.ObjectMapper;
-import org.codehaus.jackson.map.annotate.JsonRootName;
-import org.openecomp.mso.logger.MessageEnum;
 import org.openecomp.mso.logger.MsoLogger;
 import org.openecomp.mso.openstack.exceptions.MsoCloudIdentityNotFound;
 
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonRootName;
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
 /**
  * JavaBean JSON class for a CloudConfig. This bean maps a JSON-format cloud
  * configuration file to Java. The CloudConfig contains information about
@@ -54,41 +56,45 @@ import org.openecomp.mso.openstack.exceptions.MsoCloudIdentityNotFound;
 @JsonRootName("cloud_config")
 public class CloudConfig {
 
-    private boolean                    validCloudConfig = false;
+    private static final String CLOUD_SITE_VERSION = "2.5";
+    private static final String DEFAULT_CLOUD_SITE_ID = "default";
+    private boolean validCloudConfig = false;
+    private static ObjectMapper mapper = new ObjectMapper();
+    private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
+    protected String configFilePath;
+    protected int refreshTimerInMinutes;
     @JsonProperty("identity_services")
     private Map<String, CloudIdentity> identityServices = new HashMap<>();
     @JsonProperty("cloud_sites")
-    private Map<String, CloudSite>     cloudSites       = new HashMap<>();
-
-    private static ObjectMapper        mapper           = new ObjectMapper();
-
-    private static final MsoLogger     LOGGER           = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
-
-    protected String                   configFilePath;
-
-    protected int                      refreshTimerInMinutes;
+    private Map <String, CloudSite> cloudSites = new HashMap <String, CloudSite> ();
+    @JsonProperty("cloudify_managers")
+    private Map <String, CloudifyManager> cloudifyManagers = new HashMap <String, CloudifyManager> ();
 
     public CloudConfig() {
-        mapper.enable(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE);
-        mapper.enable(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
+        mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
+        mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
     }
 
     /**
-     * Get a Map of all IdentityServices that have been loaded.
-     * 
-     * @return the Map
+     * Get a map of all identity services that have been loaded.
      */
     public synchronized Map<String, CloudIdentity> getIdentityServices() {
         return identityServices;
     }
 
     /**
-     * Get a Map of all CloudSites that have been loaded.
-     * 
-     * @return the Map
+     * Get a map of all cloud sites that have been loaded.
      */
-    public synchronized Map<String, CloudSite> getCloudSites() {
-        return cloudSites;
+    public Map<String, CloudSite> getCloudSites() {
+        return Collections.unmodifiableMap(cloudSites);
+    }
+
+       /**
+        * Get a Map of all CloudifyManagers that have been loaded.
+        * @return the Map
+        */
+    public synchronized Map <String, CloudifyManager> getCloudifyManagers () {
+        return cloudifyManagers;
     }
 
     /**
@@ -96,59 +102,38 @@ public class CloudConfig {
      * against the regions, and if no match is found there, then against
      * individual entries to try and find one with a CLLI that matches the ID
      * and an AIC version of 2.5.
-     * 
-     * @param id
-     *            the ID to match
-     * @return a CloudSite, or null of no match found
+     *
+     * @param id the ID to match
+     * @return an Optional of CloudSite object.
      */
-    public synchronized CloudSite getCloudSite(String id) {
-        if (id != null) {
-            if (cloudSites.containsKey(id)) {
-                return cloudSites.get(id);
-            }
-            // check for id == CLLI now as well
-            return getCloudSiteWithClli(id, "2.5");
+    public synchronized Optional<CloudSite> getCloudSite(String id) {
+        if (id == null) {
+            return Optional.empty();
+        }
+        if (cloudSites.containsKey(id)) {
+            return Optional.ofNullable(cloudSites.get(id));
         }
         return null;
     }
 
-    /**
-     * Get a specific CloudSites, based on a CLLI and (optional) version, which
-     * will be matched against the aic_version field of the CloudSite.
-     * 
-     * @param clli
-     *            the CLLI to match
-     * @param version
-     *            the version to match; may be null in which case any version
-     *            matches
-     * @return a CloudSite, or null of no match found
-     */
-    public synchronized CloudSite getCloudSiteWithClli(String clli, String version) {
-        if (clli != null) {
-            // New with 1610 - find cloud site called "DEFAULT" - return that
-            // object,with the name modified to match what they asked for. We're
-            // looping thru the cloud sites anyway - so save off the default one in case we
-            // need it.
-            CloudSite defaultCloudSite = null;
-            for (CloudSite cs : cloudSites.values()) {
-                if (cs.getClli() != null && clli.equals(cs.getClli())) {
-                    if (version == null || version.equals(cs.getAic_version())) {
-                        return cs;
-                    }
-                } else if ("default".equalsIgnoreCase(cs.getId())) {
-                    // save it off in case we need it
-                    defaultCloudSite = cs.clone();
-                }
-            }
-            // If we get here - we didn't find a match - so return the default
-            // cloud site
-            if (defaultCloudSite != null) {
-                defaultCloudSite.setRegionId(clli);
-                defaultCloudSite.setId(clli);
-            }
+     private CloudSite getCloudSiteWithClli(String clli) {
+        Optional <CloudSite> cloudSiteOptional = cloudSites.values().stream().filter(cs ->
+                cs.getClli() != null && clli.equals(cs.getClli()) && (CLOUD_SITE_VERSION.equals(cs.getAic_version())))
+                .findAny();
+        return cloudSiteOptional.orElse(getDefaultCloudSite(clli));
+    }
+
+    private CloudSite getDefaultCloudSite(String clli) {
+        Optional<CloudSite> cloudSiteOpt = cloudSites.values().stream()
+                .filter(cs -> cs.getId().equalsIgnoreCase(DEFAULT_CLOUD_SITE_ID)).findAny();
+        if (cloudSiteOpt.isPresent()) {
+            CloudSite defaultCloudSite = cloudSiteOpt.get();
+            defaultCloudSite.setRegionId(clli);
+            defaultCloudSite.setId(clli);
             return defaultCloudSite;
+        } else {
+            return null;
         }
-        return null;
     }
 
     /**
@@ -165,34 +150,50 @@ public class CloudConfig {
         return null;
     }
 
-    protected synchronized void reloadPropertiesFile() throws JsonParseException, JsonMappingException, IOException, MsoCloudIdentityNotFound {
+       /**
+        * Get a specific CloudifyManager, based on an ID.
+        * @param id the ID to match
+        * @return a CloudifyManager, or null of no match found
+        */
+       public synchronized CloudifyManager getCloudifyManager (String id) {
+               if (cloudifyManagers.containsKey (id)) {
+                       return cloudifyManagers.get (id);
+               }
+               return null;
+       }
+
+    protected synchronized void reloadPropertiesFile() throws IOException, MsoCloudIdentityNotFound {
         this.loadCloudConfig(this.configFilePath, this.refreshTimerInMinutes);
     }
 
     protected synchronized void loadCloudConfig(String configFile, int refreshTimer)
-            throws JsonParseException, JsonMappingException, IOException, MsoCloudIdentityNotFound {
+            throws IOException, MsoCloudIdentityNotFound {
 
         FileReader reader = null;
         configFilePath = configFile;
         this.refreshTimerInMinutes = refreshTimer;
-
-        CloudConfig cloudConfig = null;
         this.validCloudConfig=false;
-        
+
         try {
             reader = new FileReader(configFile);
             // Parse the JSON input into a CloudConfig
 
-            cloudConfig = mapper.readValue(reader, CloudConfig.class);
+            CloudConfig cloudConfig = mapper.readValue(reader, CloudConfig.class);
 
             this.cloudSites = cloudConfig.cloudSites;
             this.identityServices = cloudConfig.identityServices;
+               this.cloudifyManagers = cloudConfig.cloudifyManagers;
 
             // Copy Cloud Identity IDs to CloudIdentity objects
             for (Entry<String, CloudIdentity> entry : cloudConfig.getIdentityServices().entrySet()) {
                 entry.getValue().setId(entry.getKey());
             }
 
+               // Copy Cloduify IDs to CloudifyManager objects
+               for (Entry <String, CloudifyManager> entry : cloudConfig.getCloudifyManagers ().entrySet ()) {
+                   entry.getValue ().setId (entry.getKey ());
+               }
+
             // Copy Cloud Site IDs to CloudSite objects, and set up internal
             // pointers to their corresponding identity service.
             for (Entry<String, CloudSite> entry : cloudConfig.getCloudSites().entrySet()) {
@@ -203,6 +204,8 @@ public class CloudConfig {
                 if (cloudIdentity == null) {
                     throw new MsoCloudIdentityNotFound(s.getId()+" Cloud site refers to a non-existing identity service: "+s.getIdentityServiceId());
                 }
+                CloudifyManager cloudifyManager = cloudConfig.getCloudifyManager(s.getCloudifyId());
+                s.setCloudifyManager(cloudifyManager);
             }
             this.validCloudConfig=true;
             
@@ -240,6 +243,12 @@ public class CloudConfig {
 
             ccCopy.cloudSites.put(e.getKey(), e.getValue().clone());
         }
+
+               for (Entry<String,CloudifyManager> e:cloudifyManagers.entrySet()) {
+
+                       ccCopy.cloudifyManagers.put(e.getKey(), e.getValue().clone());
+               }
+
         ccCopy.configFilePath = this.configFilePath;
         ccCopy.refreshTimerInMinutes = this.refreshTimerInMinutes;
         ccCopy.validCloudConfig = this.validCloudConfig;