2 * Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.nfvo.emsdriver.collector.alarm;
18 import java.io.BufferedInputStream;
19 import java.io.BufferedOutputStream;
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.DataInputStream;
23 import java.io.DataOutputStream;
27 public class MessageUtil
29 public static String MSG_BODY_ENCODE_CHARSET="UTF-8";
30 public static int MSG_BUF_SIZE=8096 ;
32 public static Msg putLoginMsg(String user,String passwd)
34 String body = String.format(Msg.reqLoginAlarm, user,passwd,"msg");
35 Msg msg = new Msg(body,MsgType.reqLoginAlarm);
41 public static Msg putLoginFtp(String user,String passwd)
43 String body = String.format(Msg.reqLoginAlarm, user,passwd,"ftp");
44 Msg msg = new Msg(body,MsgType.reqLoginAlarm);
51 public static Msg putSyncMsg(int reqId,int alarmSeq)
53 String body = String.format(Msg.syncAlarmMessageMsg, reqId,alarmSeq);
54 Msg msg = new Msg(body,MsgType.reqSyncAlarmMsg);
61 public static Msg putHeartBeatMsg(int reqId)
63 String body = String.format(Msg.reqHeartBeat, reqId);
64 Msg msg = new Msg(body,MsgType.reqHeartBeat);
69 public static Msg reqSyncAlarmFile(int reqId, String startTime,String endTime) {
70 String body = String.format(Msg.syncActiveAlarmFileMsg, reqId,startTime,endTime);
71 Msg msg = new Msg(body,MsgType.reqSyncAlarmFile);
75 public static Msg reqSyncAlarmFileByAlarmSeq(int reqId, int alarmSeq) {
76 String body = String.format(Msg.syncAlarmMessageByalarmSeq, reqId,alarmSeq);
77 Msg msg = new Msg(body,MsgType.reqSyncAlarmFile);
81 public static Msg reqSyncAlarmFileByTime(int reqId, String startTime,String endTime) {
82 String body = String.format(Msg.syncAlarmFileMsg, reqId,startTime,endTime);
83 Msg msg = new Msg(body,MsgType.reqSyncAlarmFile);
87 public static Msg closeConnAlarmMsg()
89 String body = String.format(Msg.disconnectMsg);
90 Msg msg = new Msg(body,MsgType.closeConnAlarm);
94 public static Msg readOneMsg(BufferedInputStream is) throws Exception
96 byte[] inputB = new byte[9];
98 ByteArrayInputStream bais = null;
99 DataInputStream ois = null;
103 DataInputStream dis = new DataInputStream(is);
104 dis.readFully(inputB);
105 bais = new ByteArrayInputStream(inputB);
106 ois = new DataInputStream(bais);
107 short StartSign = ois.readShort();
108 if (StartSign != Msg.StartSign) {
109 throw new Exception("start sign is [" + Msg.StartSign
110 + "],not is [" + StartSign + "]");
112 int msgType = ois.readByte();
113 msg.setMsgType(MsgType.getMsgTypeValue(msgType));
114 int timeStamp = ois.readInt();
115 msg.setTimeStamp(timeStamp);
116 int bodylength = ois.readShort();
117 msg.setLenOfBody(bodylength);
118 byte b[] = new byte[bodylength];
120 msg.newBodyfromBytes(b);
121 } catch (Exception e) {
122 throw new Exception(e);
135 public static void writeMsg(Msg msg,BufferedOutputStream dout) throws Exception{
137 ByteArrayOutputStream byteOutStream = null;
138 DataOutputStream oos = null;
140 byteOutStream = new ByteArrayOutputStream(9);
141 oos = new DataOutputStream(byteOutStream);
142 oos.writeShort(Msg.StartSign);
143 oos.writeByte(msg.getMsgType().value);
144 oos.writeInt(Msg.creatMsgTimeStamp());
145 oos.writeShort(msg.getBodyLenNow());
147 dout.write(byteOutStream.toByteArray());
149 dout.write(msg.getBodyBytes());
151 } catch (Exception e) {
152 throw new Exception(e);
157 if(byteOutStream != null){
158 byteOutStream.close();