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;
/**
* 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);
/**
* 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);
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 {
/**
* 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);
/**
* 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) {
}
/*
- * 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 {
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);
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)
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);
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;
}
}
- 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);
}