@Override
public void updateMessageStarted(long messageId, Date timestamp) {
- updateMessageStatus("started_timestamp", messageId, null, timestamp);
+ // duplicate code with updateMessageCompleted to avoid SQL injection issue for sonar
+ try (Connection con = dataSource.getConnection()) {
+ try {
+ con.setAutoCommit(false);
+ String sql = "UPDATE message SET started_timestamp = ? WHERE message_id = ?";
+ try (PreparedStatement ps = con.prepareStatement(sql)) {
+ ps.setTimestamp(1, new Timestamp(timestamp.getTime()));
+ ps.setLong(2, messageId);
+ ps.executeUpdate();
+ }
+ con.commit();
+ } catch (SQLException ex) {
+ con.rollback();
+ throw ex;
+ }
+ } catch (SQLException e) {
+ throw new RuntimeException("Error updating message status in DB: " + e.getMessage(), e);
+ }
}
@Override
public void updateMessageCompleted(long messageId, String resolution, Date timestamp) {
- updateMessageStatus("completed_timestamp", messageId, resolution, timestamp);
- }
-
- private void updateMessageStatus(String timestampColumn, long messageId, String resolution, Date timestamp) {
+ // duplicate code with updateMessageStarted to avoid SQL injection issue for sonar
try (Connection con = dataSource.getConnection()) {
try {
con.setAutoCommit(false);
- String sql = "UPDATE message SET " + timestampColumn + " = ? WHERE message_id = ?";
+ String sql = "UPDATE message SET completed_timestamp = ? WHERE message_id = ?";
try (PreparedStatement ps = con.prepareStatement(sql)) {
ps.setTimestamp(1, new Timestamp(timestamp.getTime()));
ps.setLong(2, messageId);
} catch (SQLException e) {
throw new RuntimeException("Error updating message status in DB: " + e.getMessage(), e);
}
+
}
@Override
private Event waitForNewAction(int holdTime) {
long startTime = System.currentTimeMillis();
long currentTime = startTime;
- while (currentTime - startTime <= (holdTime + 1) * 1000) {
+ while (currentTime - startTime <= (holdTime + 1) * 1000L) {
try {
Thread.sleep(5000);
- } catch (Exception e) {
+ } catch (InterruptedException e) {
+ log.info("Break sleep : " + e.getMessage());
+ Thread.currentThread().interrupt();
}
MessageAction nextAction = messageDao.getNextAction(message.getMessageId());
try {
Thread.sleep(startTime);
} catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
}
MessageData r = interceptor.processRequest(request);
try {
Thread.sleep(processTime);
} catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
}
interceptor.processResponse(response);
try {
Thread.sleep(processTime);
} catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
}
}
}
try {\r
logger.trace("Initializing NPM Configurations from:({})", configFilePath);\r
if (new File(configFilePath).exists()) {\r
- npmConfigurations.load(new FileInputStream(configFilePath));\r
+ try (FileInputStream configInputStream = new FileInputStream(configFilePath)) {\r
+ npmConfigurations.load(configInputStream);\r
+ }\r
} else {\r
logger.warn("Config File:({}) not found, Initializing NPM with default configurations.", configFilePath);\r
configFilePath = "properties" + File.separator + NpmConstants.NPM_CONFIG_PROPERTIES_FILE_NAME;\r
mapper.enable(SerializationFeature.INDENT_OUTPUT);\r
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(instance);\r
} catch (JsonProcessingException e) {\r
- e.printStackTrace();\r
+ logger.warn(e.getMessage(), e);\r
}\r
return null;\r
}\r
try {
Thread.sleep(lockWait * 1000L);
} catch (InterruptedException ex) {
+ Thread.currentThread().interrupt();
}
}
}
package org.onap.ccsdk.features.lib.rlock;
+import java.security.SecureRandom;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
}
private static String generateLockRequester() {
- return "SynchronizedFunction-" + (int) (Math.random() * 1000000);
+ SecureRandom random = new SecureRandom();
+ return "SynchronizedFunction-" + (random.nextInt() % 1000000);
}
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
log.warn("Thread interrupted: " + e.getMessage(), e);
}
// GET configuration from properties file
config = new HashMap<String, String>();
- try {
- FileInputStream fileInput = new FileInputStream(propDir + PROPERTIES_FILE_NAME);
+ try (FileInputStream fileInput = new FileInputStream(propDir + PROPERTIES_FILE_NAME)) {
Properties properties = new Properties();
properties.load(fileInput);
- fileInput.close();
for (String param : new String[] {"url", "user", "password",
"authentication, dmi-service-name"}) {
config.put(param, properties.getProperty(param));
}
- } catch (FileNotFoundException e) {
- e.printStackTrace();
} catch (IOException e) {
- e.printStackTrace();
+ LOG.error("Error while reading properties file: ", e);
}
LOG.info("addCMHandle Session Initiated");
@Override
public void onCreated(NodeId nNodeId, NetconfNode netconfNode) {
- LOG.info("NetConf device connected ", nNodeId.getValue());
+ LOG.info("NetConf device connected {}", nNodeId.getValue());
JSONObject obj = new JSONObject();
obj.put("cm-handle-id", nNodeId.getValue());
obj.put("dmi-service-name", config.get("dmi-service-name"));
String authenticationMethod = config.get("authentication");
ClientResponse response = null;
try {
- if (authenticationMethod.equals("basic")) {
+ if ("basic".equals(authenticationMethod)) {
LOG.debug("Sending message to dmaap-message-router: {}", obj.toString());
dmaapClient.addFilter(new HTTPBasicAuthFilter(config.get("user"), config.get("password")));
response = dmaapClient.resource(config.get("url")).type(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, obj);
}
+ LOG.info("Received response from dmaap-message-router: \n {}", response.toString());
} catch (Exception e) {
- LOG.error("Error while posting message to CM_HANDLE topic: {}", e);
+ LOG.error("Error while posting message to CM_HANDLE topic: ", e);
}
- LOG.info("Received response from dmaap-message-router: \n {}", response.toString());
}
@Override
HashMap<String, String> dmaapPolicyHttpParams = new HashMap<String, String>();
HashMap<String, String> energySavingsServerHttpParams = new HashMap<String, String>();
- try {
- FileInputStream fileInput = new FileInputStream(propDir + PROPERTIES_FILE_NAME);
+ try (FileInputStream fileInput = new FileInputStream(propDir + PROPERTIES_FILE_NAME)) {
Properties properties = new Properties();
properties.load(fileInput);
fileInput.close();
energySavingsServerHttpParams.put(param, properties.getProperty("energySavingsServer." + param));
}
} catch (FileNotFoundException e) {
- e.printStackTrace();
+ LOG.error("Unexpected value for energy savings server authentication: ");
} catch (IOException e) {
- e.printStackTrace();
+ LOG.error("Unexpected value for energy savings server authentication: ");
}
// Create a web resource for the Energy Savings server