@Override
public void run() {
try {
- alarmChannel = MessageChannelFactory.getMessageChannel(Constant.RESULT_CHANNEL_KEY);
+ alarmChannel = MessageChannelFactory.getMessageChannel(Constant.RESULT_CHANNEL_KEY);
this.init();
while (!this.isStop) {
String body;
public String receive() throws IOException {
- try{
+ try{
Msg msg = null;
String retString = null;
-
while (retString == null && !this.isStop) {
msg = MessageUtil.readOneMsg(is);
log.debug("msg = " + msg.toString(true));
log.debug("receive login ack");
boolean suc = this.ackLoginAlarm(msg);
if (suc) {
-
if (reqId == Integer.MAX_VALUE)
- reqId=0;
-
+ reqId=0;
reqId++;
Msg msgheart = MessageUtil.putHeartBeatMsg(reqId);
heartBeat = new HeartBeat(socket, msgheart);
log.debug("received alarm message");
retString = msg.getBody();
}
-
if (retString == null) {
Thread.sleep(100);
}
}
public void close() {
-
if (heartBeat != null) {
heartBeat.setStop(true);
}
-
if (is != null) {
try {
is.close();
is = null;
}
}
-
if (dos != null) {
try {
dos.close();
dos = null;
}
}
-
if (socket != null) {
try {
socket.close();
public static Msg putLoginFtp(String user, String passwd) {
String body = String.format(Msg.REQ_LOGIN_ALARM, user, passwd, "ftp");
- return new Msg(body, MsgType.REQ_LOGIN_ALARM);
-
+ return new Msg(body, MsgType.REQ_LOGIN_ALARM);
}
public static Msg putSyncMsg(int reqId, int alarmSeq) {
public static Msg readOneMsg(BufferedInputStream is) throws IOException {
byte[] inputB = new byte[9];
-
Msg msg = new Msg();
try(
DataInputStream dis = new DataInputStream(is);
} catch (Exception e) {
throw new IOException("readOneMsg",e);
}
-
return msg;
}
oos.writeByte(msg.getMsgType().value);
oos.writeInt(Msg.creatMsgTimeStamp());
oos.writeShort(msg.getBodyLenNow());
-
dout.write(byteOutStream.toByteArray());
-
dout.write(msg.getBodyBytes());
dout.flush();
} catch (Exception e) {
}
public static MsgType getMsgTypeValue(int msgTypeValue) {
-
for (MsgType msgType : MsgType.values()) {
if (msgType.value == msgTypeValue) {
return msgType;
*/
package org.onap.vfc.nfvo.emsdriver.collector.alarm;
-import org.junit.Before;
-import org.junit.Test;
-import org.onap.vfc.nfvo.emsdriver.commons.model.CollectVo;
+import static org.junit.Assert.assertNotNull;
import java.io.IOException;
-import static org.junit.Assert.assertNotNull;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.vfc.nfvo.emsdriver.commons.model.CollectVo;
public class AlarmTaskThreadTest {
assertNotNull(alarm);
}
+ @Test
+ public void runAlarmTaskThread(){
+ try {
+ taskThread.run();
+ Thread.sleep(3000);
+ taskThread.setStop(true);
+ server.stop();
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ }
+
}
--- /dev/null
+/*
+ * Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * 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.IOException;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class HeartBeatTest {
+
+ private AlarmSocketServer server;
+
+ @Before
+ public void setUp() throws IOException {
+ new Thread() {
+ public void run() {
+ server = new AlarmSocketServer();
+ server.socketServer();
+ }
+ }.start();
+
+ }
+
+ @Test
+ public void runHeartBeatThread(){
+ try {
+ Socket socket = new Socket("127.0.0.1",12345);
+ Msg msg = MessageUtil.putHeartBeatMsg(1);
+ HeartBeat heartBeat = new HeartBeat(socket,msg);
+ heartBeat.run();
+ Thread.sleep(10);
+ heartBeat.setStop(true);
+ server.stop();
+ } catch (UnknownHostException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ }
+
+
+}
--- /dev/null
+/*
+ * Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onap.vfc.nfvo.emsdriver.collector.alarm;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.UnsupportedEncodingException;
+import java.util.Date;
+
+import org.junit.Test;
+
+
+public class MsgTest {
+
+ @Test
+ public void newBodyfromBytesTest() {
+ try {
+ Msg msg = new Msg("Alarm",MsgType.REALTIME_ALARM);
+ String str = "test";
+ msg.newBodyfromBytes(str.getBytes());
+ } catch (UnsupportedEncodingException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void setTimestampTest() {
+ Msg msg = new Msg("Alarm",MsgType.REALTIME_ALARM);
+ Date date = new Date();
+ int secondes = date.getSeconds();
+ msg.setTimeStamp(secondes);
+ int timestamp = msg.getTimeStamp();
+ assertEquals(secondes,timestamp);
+ }
+
+ @Test
+ public void toStringTest(){
+ Msg msg = new Msg("Alarm",MsgType.REALTIME_ALARM);
+ msg.setBody("NewAlarm");
+ String msgStr = msg.toString();
+ System.out.println("msg to string is "+msgStr);
+ assertNotNull(msgStr);
+ }
+
+}
--- /dev/null
+/*\r
+ * Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.onap.vfc.nfvo.emsdriver.taskscheduler;\r
+\r
+import org.junit.Test;\r
+import org.onap.vfc.nfvo.emsdriver.commons.model.CollectVo;\r
+import org.quartz.Job;\r
+\r
+public class CollectOderJobTest {\r
+ \r
+ @Test\r
+ public void executeCollectOderJob(){\r
+ CollectVo collectVo = new CollectVo();\r
+ collectVo.setEmsName("zteEms");\r
+ collectVo.setType("ems-p");\r
+ collectVo.setIP("127.0.0.1");\r
+ collectVo.setCrontab("*/5 * * * * ?");\r
+ \r
+ String jobName = collectVo.getEmsName() + "_" + collectVo.getType() + collectVo.getIP();\r
+ Job job = new CollectOderJob();\r
+ String jobClass = job.getClass().getName();\r
+ String time = collectVo.getCrontab();\r
+ if (time != null && !"".equals(time)) {\r
+ QuartzManager.addJob(jobName, jobClass, time, collectVo);\r
+ try {\r
+ Thread.sleep(10);\r
+ } catch (InterruptedException e) {\r
+ // TODO Auto-generated catch block\r
+ e.printStackTrace();\r
+ } finally{\r
+ QuartzManager.removeJob(jobName); \r
+ }\r
+ } else {\r
+ System.out.println("type =[" + collectVo.getType() + "]ip=[" + collectVo.getIP() + "] crontab is null");\r
+ }\r
+ \r
+ }\r
+\r
+ \r
+}\r