Release dashboard image at version 2.1.0
[portal/ric-dashboard.git] / dashboard / webapp-backend / src / main / java / org / oransc / ric / portal / dashboard / AppStatsManager.java
index 79c75fe..d9d0b2e 100644 (file)
@@ -25,20 +25,12 @@ import java.lang.invoke.MethodHandles;
 import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.ListIterator;
 
-import javax.servlet.http.HttpServletResponse;
-
-import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
-import org.onap.portalsdk.core.restful.domain.EcompUser;
-import org.oransc.ric.portal.dashboard.model.StatsDetailsTransport;
 import org.oransc.ric.portal.dashboard.exception.StatsManagerException;
-import org.oransc.ric.portal.dashboard.model.IDashboardResponse;
 import org.oransc.ric.portal.dashboard.model.AppStats;
+import org.oransc.ric.portal.dashboard.model.StatsDetailsTransport;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.stereotype.Component;
-import org.springframework.stereotype.Service;
 
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
@@ -65,9 +57,11 @@ public class AppStatsManager {
        /**
         * Development/test-only constructor that uses default file path.
         * 
-        * @param clear If true, start empty and remove any existing file.
+        * @param clear
+        *                  If true, start empty and remove any existing file.
         * 
-        * @throws IOException On file error
+        * @throws IOException
+        *                         On file error
         */
        public AppStatsManager(boolean clear) throws IOException {
                this(STATS_FILE_PATH);
@@ -83,8 +77,10 @@ public class AppStatsManager {
        /**
         * Constructor that accepts a file path
         * 
-        * @param statsFilePath File path
-        * @throws IOException If file cannot be read
+        * @param statsFilePath
+        *                          File path
+        * @throws IOException
+        *                         If file cannot be read
         */
        public AppStatsManager(final String statsFilePath) throws IOException {
                logger.debug("ctor: statsfile {}", statsFilePath);
@@ -96,8 +92,8 @@ public class AppStatsManager {
                        final ObjectMapper mapper = new ObjectMapper();
                        stats = mapper.readValue(statsFile, new TypeReference<List<AppStats>>() {
                        });
-                       for (AppStats st: stats) {
-                               if (st.getStatsDetails().getAppId()>appMaxId) 
+                       for (AppStats st : stats) {
+                               if (st.getStatsDetails().getAppId() > appMaxId)
                                        appMaxId = st.getStatsDetails().getAppId();
                        }
                } else {
@@ -117,11 +113,12 @@ public class AppStatsManager {
        /**
         * Gets the current app metric stats by instance key.
         * 
-        * @param instanceKey Desired instance key
+        * @param instanceKey
+        *                        Desired instance key
         * @return List of App stat objects by instance key, possibly empty
         */
        public List<AppStats> getStatsByInstance(String instanceKey) {
-               List<AppStats> statsByInstance = new ArrayList<AppStats>();
+               List<AppStats> statsByInstance = new ArrayList<>();
                for (AppStats st : this.stats) {
                        if (st.getInstanceKey().equals(instanceKey)) {
                                logger.debug("getStatsByInstance: match on instance key {}", instanceKey);
@@ -134,8 +131,10 @@ public class AppStatsManager {
        /**
         * Gets the stats with the specified app Id and instance key
         * 
-        * @param appId       Desired app Id
-        * @param instanceKey Desired instance key
+        * @param appId
+        *                        Desired app Id
+        * @param instanceKey
+        *                        Desired instance key
         * @return Stats object; null if Id is not known
         */
        public AppStats getStatsById(String instanceKey, int appId) {
@@ -157,8 +156,8 @@ public class AppStatsManager {
        }
 
        /*
-        * Allow at most one thread to create a stats at one time.
-        * Before creating new stat, checks for composite key (appname,url) uniqueness for an instance key
+        * Allow at most one thread to create a stats at one time. Before creating new
+        * stat, checks for composite key (appname,url) uniqueness for an instance key
         */
        public synchronized AppStats createStats(String instanceKey, StatsDetailsTransport statsSetupRequest)
                        throws StatsManagerException, IOException {
@@ -168,15 +167,17 @@ public class AppStatsManager {
                        if (st.getInstanceKey().equals(instanceKey)
                                        && st.getStatsDetails().getAppName().equals(statsSetupRequest.getAppName())
                                        && st.getStatsDetails().getMetricUrl().equals(statsSetupRequest.getMetricUrl())) {
-                               String msg = "App exists with name " + statsSetupRequest.getAppName() + " and url "+statsSetupRequest.getMetricUrl()+ " on instance key " + instanceKey;
+                               // Log the existing object to avoid using tainted (user-supplied) data
+                               String msg = "App exists with name " + st.getStatsDetails().getAppName() + " and url "
+                                               + st.getStatsDetails().getMetricUrl() + " on instance key " + st.getInstanceKey();
                                logger.warn(msg);
                                throw new StatsManagerException(msg);
                        }
                }
 
                AppStats newAppStat = null;
-               //Assigns appId to be 1 more than the largest value stored in memory
-               appMaxId = appMaxId+1;
+               // Assigns appId to be 1 more than the largest value stored in memory
+               appMaxId = appMaxId + 1;
                newAppStat = new AppStats(instanceKey,
                                new StatsDetailsTransport(appMaxId, statsSetupRequest.getAppName(), statsSetupRequest.getMetricUrl()));
                stats.add(newAppStat);
@@ -191,7 +192,7 @@ public class AppStatsManager {
        public synchronized void updateStats(String instanceKey, StatsDetailsTransport statsSetupRequest)
                        throws StatsManagerException, IOException {
                logger.debug("updateStats: appId {}, instanceKey {}", statsSetupRequest.getAppId(), instanceKey);
-               boolean editStatsObjectFound = false;
+               boolean statsObjectFound = false;
 
                for (AppStats st : stats) {
                        if (st.getInstanceKey().equals(instanceKey)
@@ -199,12 +200,12 @@ public class AppStatsManager {
                                AppStats newAppStat = new AppStats(instanceKey, statsSetupRequest);
                                stats.remove(st);
                                stats.add(newAppStat);
-                               editStatsObjectFound = true;
+                               statsObjectFound = true;
                                saveStats();
                                break;
                        }
                }
-               if (!editStatsObjectFound) {
+               if (!statsObjectFound) {
                        String msg = "Stats to be updated does not exist ";
                        logger.warn(msg);
                        throw new StatsManagerException(msg);
@@ -213,13 +214,13 @@ public class AppStatsManager {
 
        public synchronized AppStats deleteStats(String instanceKey, int appId) throws StatsManagerException, IOException {
                logger.debug("deleteStats: appId {}, instanceKey {}", appId, instanceKey);
-               boolean deleteStatsObjectFound = false;
+               boolean statsObjectFound = false;
                AppStats stat = null;
                for (AppStats st : stats) {
                        if (st.getInstanceKey().equals(instanceKey) && st.getStatsDetails().getAppId() == appId) {
                                stat = st;
                                stats.remove(stat);
-                               deleteStatsObjectFound = true;
+                               statsObjectFound = true;
                                try {
                                        saveStats();
                                        break;
@@ -229,8 +230,10 @@ public class AppStatsManager {
 
                        }
                }
-               if (!deleteStatsObjectFound) {
+               if (!statsObjectFound) {
                        String msg = "deleteStats: no match on app id {} of instance key {}";
+                       // Replace log pattern-breaking characters
+                       instanceKey = instanceKey.replaceAll("[\n|\r|\t]", "_");
                        logger.warn(msg, appId, instanceKey);
                        throw new StatsManagerException(msg);
                }