sonar fix:Collector Message & Alarm 11/36211/1
authorajay priyadarshi <ajay.priyadarshi@ril.com>
Fri, 16 Mar 2018 12:06:04 +0000 (17:36 +0530)
committerajay priyadarshi <ajay.priyadarshi@ril.com>
Fri, 16 Mar 2018 12:06:04 +0000 (17:36 +0530)
collector/alarm/MsgType.java
file name: HeartBeat.java,MsgType.java

Change-Id: I6761e520337d58dcb98d378fbbedc8a96493bf0e
Issue-ID: VFC-823
Signed-off-by: ajay priyadarshi <ajay.priyadarshi@ril.com>
ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/AlarmTaskThread.java
ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/HeartBeat.java
ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/MessageUtil.java
ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/MsgType.java

index 9224128..7470ff3 100644 (file)
@@ -29,7 +29,6 @@ import java.io.IOException;
 import java.net.Socket;
 import java.net.SocketException;
 import java.net.UnknownHostException;
-import java.lang.NumberFormatException;
 
 
 public class AlarmTaskThread extends Thread {
@@ -39,7 +38,7 @@ public class AlarmTaskThread extends Thread {
 
     private boolean isStop = false;
     private CollectVo collectVo = null;
-    private int read_timeout = Constant.READ_TIMEOUT_MILLISECOND;
+    private int readTimeout = Constant.READ_TIMEOUT_MILLISECOND;
     private int reqId;
 
     private Socket socket = null;
@@ -93,7 +92,9 @@ public class AlarmTaskThread extends Thread {
                        boolean suc = this.ackLoginAlarm(msg);
                        if (suc) {
 
-                           if (reqId == Integer.MAX_VALUE) reqId=0;
+                           if (reqId == Integer.MAX_VALUE) 
+                               reqId=0;
+
                            reqId++;
                            Msg msgheart = MessageUtil.putHeartBeatMsg(reqId);
                            heartBeat = new HeartBeat(socket, msgheart);
@@ -127,7 +128,7 @@ public class AlarmTaskThread extends Thread {
        }
 }
 
-    public void init() throws NumberFormatException, IOException{
+    public void init() throws IOException{
         isStop = false;
         //host
         String host = collectVo.getIP();
@@ -139,7 +140,8 @@ public class AlarmTaskThread extends Thread {
         String password = collectVo.getPassword();
 
        try{
-               if((collectVo.getRead_timeout()).trim().length()>0) this.read_timeout = Integer.parseInt(collectVo.getRead_timeout());
+               if((collectVo.getRead_timeout()).trim().length()>0) 
+                       this.readTimeout = Integer.parseInt(collectVo.getRead_timeout());
 
        } catch (NumberFormatException e) {
                log.error("Unable to parse read_timout: ",e);
@@ -159,7 +161,7 @@ public class AlarmTaskThread extends Thread {
             throw new SocketException("create socket IOException " + e1);
         }
         try {
-            socket.setSoTimeout(this.read_timeout);
+            socket.setSoTimeout(this.readTimeout);
             socket.setTcpNoDelay(true);
             socket.setKeepAlive(true);
         } catch (SocketException e) {
@@ -188,7 +190,7 @@ public class AlarmTaskThread extends Thread {
     }
 
     private boolean ackLoginAlarm(Msg msg) throws IOException {
-        boolean is_success = false;
+        boolean ret = false;
        try {
                String loginres = msg.getBody();
                String[] loginbody = loginres.split(";");
@@ -197,8 +199,9 @@ public class AlarmTaskThread extends Thread {
                                if (str.contains("=")) {
                                        String[] paras1 = str.split("=", -1);
                                        if ("result".equalsIgnoreCase(paras1[0].trim())) {
-                                               if("succ".equalsIgnoreCase(paras1[1].trim())) is_success = true; 
-                                               else is_success = false;
+                                               if("succ".equalsIgnoreCase(paras1[1].trim())) 
+                                                       ret = true; 
+                                               else ret = false;
                                        }
                                }
                        }
@@ -209,7 +212,7 @@ public class AlarmTaskThread extends Thread {
        } catch (Exception e) {
             log.error("pocess login ack fail" + StringUtil.getStackTrace(e));
         }
-        if (is_success) {
+        if (ret) {
             log.info("login sucess receive login ack " + msg.getBody());
         } else {
             log.error("login fail receive login ack  " + msg.getBody());
@@ -217,7 +220,7 @@ public class AlarmTaskThread extends Thread {
             this.isStop = true;
            throw new IOException("pocess login ack fail");
         }
-        return is_success;
+        return ret;
     }
 
     public void close() {
index ec9005d..b9b6f1a 100644 (file)
@@ -23,7 +23,7 @@ import java.io.BufferedOutputStream;
 import java.net.Socket;
 
 public class HeartBeat extends Thread {
-    public Log log = LogFactory.getLog(HeartBeat.class);
+    private static final Log log = LogFactory.getLog(HeartBeat.class);
     private BufferedOutputStream out = null;
     private Socket socket = null;
     private Msg heartStr;
@@ -42,6 +42,7 @@ public class HeartBeat extends Thread {
         this.stop = stop;
     }
 
+    @Override
     public void run() {
         log.info("HeartBeat start heartStr:" + heartStr.toString(false));
         this.stop = false;
index ff34554..8672bca 100644 (file)
@@ -13,6 +13,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+
 package org.onap.vfc.nfvo.emsdriver.collector.alarm;
 
 import java.io.*;
@@ -24,78 +25,61 @@ public class MessageUtil {
 
     public static Msg putLoginMsg(String user, String passwd) {
         String body = String.format(Msg.reqLoginAlarm, user, passwd, "msg");
-        Msg msg = new Msg(body, MsgType.reqLoginAlarm);
-
-
-        return msg;
+        return new Msg(body, MsgType.reqLoginAlarm);
 
     }
 
     public static Msg putLoginFtp(String user, String passwd) {
         String body = String.format(Msg.reqLoginAlarm, user, passwd, "ftp");
-        Msg msg = new Msg(body, MsgType.reqLoginAlarm);
-
-
-        return msg;
+       return new Msg(body, MsgType.reqLoginAlarm);
 
     }
 
     public static Msg putSyncMsg(int reqId, int alarmSeq) {
         String body = String.format(Msg.syncAlarmMessageMsg, reqId, alarmSeq);
-        Msg msg = new Msg(body, MsgType.reqSyncAlarmMsg);
-
-
-        return msg;
+        return new Msg(body, MsgType.reqSyncAlarmMsg);
 
     }
 
     public static Msg putHeartBeatMsg(int reqId) {
         String body = String.format(Msg.reqHeartBeat, reqId);
-        Msg msg = new Msg(body, MsgType.reqHeartBeat);
-        return msg;
+        return new Msg(body, MsgType.reqHeartBeat);
 
     }
 
     public static Msg reqSyncAlarmFile(int reqId, String startTime, String endTime) {
         String body = String.format(Msg.syncActiveAlarmFileMsg, reqId, startTime, endTime);
-        Msg msg = new Msg(body, MsgType.reqSyncAlarmFile);
-        return msg;
+        return new Msg(body, MsgType.reqSyncAlarmFile);
     }
 
     public static Msg reqSyncAlarmFileByAlarmSeq(int reqId, int alarmSeq) {
         String body = String.format(Msg.syncAlarmMessageByalarmSeq, reqId, alarmSeq);
-        Msg msg = new Msg(body, MsgType.reqSyncAlarmFile);
-        return msg;
+        return new Msg(body, MsgType.reqSyncAlarmFile);
     }
 
     public static Msg reqSyncAlarmFileByTime(int reqId, String startTime, String endTime) {
         String body = String.format(Msg.syncAlarmFileMsg, reqId, startTime, endTime);
-        Msg msg = new Msg(body, MsgType.reqSyncAlarmFile);
-        return msg;
+        return new Msg(body, MsgType.reqSyncAlarmFile);
     }
 
     public static Msg closeConnAlarmMsg() {
         String body = String.format(Msg.disconnectMsg);
-        Msg msg = new Msg(body, MsgType.closeConnAlarm);
-        return msg;
+        return new Msg(body, MsgType.closeConnAlarm);
     }
 
-    public static Msg readOneMsg(BufferedInputStream is) throws Exception {
+    public static Msg readOneMsg(BufferedInputStream is) throws IOException {
         byte[] inputB = new byte[9];
 
-        ByteArrayInputStream bais = null;
-        DataInputStream ois = null;
-
         Msg msg = new Msg();
-        try {
+        try
             DataInputStream dis = new DataInputStream(is);
+            ByteArrayInputStream bais = new ByteArrayInputStream(inputB);
+            DataInputStream ois = new DataInputStream(bais)){
             dis.readFully(inputB);
-            bais = new ByteArrayInputStream(inputB);
-            ois = new DataInputStream(bais);
-            short StartSign = ois.readShort();
-            if (StartSign != Msg.StartSign) {
+            short startSign = ois.readShort();
+            if (startSign != Msg.StartSign) {
                 throw new Exception("start sign is [" + Msg.StartSign
-                        + "],not is [" + StartSign + "]");
+                        + "],not is [" + startSign + "]");
             }
             int msgType = ois.readByte();
             msg.setMsgType(MsgType.getMsgTypeValue(msgType));
@@ -107,26 +91,16 @@ public class MessageUtil {
             dis.readFully(b);
             msg.newBodyfromBytes(b);
         } catch (Exception e) {
-            throw new Exception(e);
-        } finally {
-            if (bais != null) {
-                bais.close();
-            }
-            if (ois != null) {
-                ois.close();
-            }
-        }
+            throw new IOException("readOneMsg",e);
+        } 
 
         return msg;
     }
 
-    public static void writeMsg(Msg msg, BufferedOutputStream dout) throws Exception {
-
-        ByteArrayOutputStream byteOutStream = null;
-        DataOutputStream oos = null;
-        try {
-            byteOutStream = new ByteArrayOutputStream(9);
-            oos = new DataOutputStream(byteOutStream);
+    public static void writeMsg(Msg msg, BufferedOutputStream dout) throws IOException {
+        try( 
+            ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream(9);
+            DataOutputStream oos = new DataOutputStream(byteOutStream)){
             oos.writeShort(Msg.StartSign);
             oos.writeByte(msg.getMsgType().value);
             oos.writeInt(Msg.creatMsgTimeStamp());
@@ -137,15 +111,8 @@ public class MessageUtil {
             dout.write(msg.getBodyBytes());
             dout.flush();
         } catch (Exception e) {
-            throw new Exception(e);
-        } finally {
-            if (oos != null) {
-                oos.close();
-            }
-            if (byteOutStream != null) {
-                byteOutStream.close();
-            }
-        }
+            throw new IOException("writeMsg",e);
+        } 
 
     }
 
index 0e69518..fdf4b5e 100644 (file)
@@ -53,13 +53,14 @@ public enum MsgType {
     public static MsgType getMsgTypeName(String msgTypeName) {
 
         for (MsgType msgType : MsgType.values()) {
-            if (msgType.name.toLowerCase().equals(msgTypeName.toLowerCase())) {
+            if (msgType.name.equalsIgnoreCase(msgTypeName)) {
                 return msgType;
             }
         }
         return undefined;
     }
 
+    @Override
     public String toString() {
         return this.name;
     }