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.vfc.nfvo.emsdriver.collector.alarm;
21 public class MessageUtil {
22 public static final String MSG_BODY_ENCODE_CHARSET = "UTF-8";
23 public static final int MSG_BUF_SIZE = 8096;
25 public static Msg putLoginMsg(String user, String passwd) {
26 String body = String.format(Msg.reqLoginAlarm, user, passwd, "msg");
27 return new Msg(body, MsgType.reqLoginAlarm);
31 public static Msg putLoginFtp(String user, String passwd) {
32 String body = String.format(Msg.reqLoginAlarm, user, passwd, "ftp");
33 return new Msg(body, MsgType.reqLoginAlarm);
37 public static Msg putSyncMsg(int reqId, int alarmSeq) {
38 String body = String.format(Msg.syncAlarmMessageMsg, reqId, alarmSeq);
39 return new Msg(body, MsgType.reqSyncAlarmMsg);
43 public static Msg putHeartBeatMsg(int reqId) {
44 String body = String.format(Msg.reqHeartBeat, reqId);
45 return new Msg(body, MsgType.reqHeartBeat);
49 public static Msg reqSyncAlarmFile(int reqId, String startTime, String endTime) {
50 String body = String.format(Msg.syncActiveAlarmFileMsg, reqId, startTime, endTime);
51 return new Msg(body, MsgType.reqSyncAlarmFile);
54 public static Msg reqSyncAlarmFileByAlarmSeq(int reqId, int alarmSeq) {
55 String body = String.format(Msg.syncAlarmMessageByalarmSeq, reqId, alarmSeq);
56 return new Msg(body, MsgType.reqSyncAlarmFile);
59 public static Msg reqSyncAlarmFileByTime(int reqId, String startTime, String endTime) {
60 String body = String.format(Msg.syncAlarmFileMsg, reqId, startTime, endTime);
61 return new Msg(body, MsgType.reqSyncAlarmFile);
64 public static Msg closeConnAlarmMsg() {
65 String body = String.format(Msg.disconnectMsg);
66 return new Msg(body, MsgType.closeConnAlarm);
69 public static Msg readOneMsg(BufferedInputStream is) throws IOException {
70 byte[] inputB = new byte[9];
74 DataInputStream dis = new DataInputStream(is);
75 ByteArrayInputStream bais = new ByteArrayInputStream(inputB);
76 DataInputStream ois = new DataInputStream(bais)){
77 dis.readFully(inputB);
78 short startSign = ois.readShort();
79 if (startSign != Msg.StartSign) {
80 throw new IOException("start sign is [" + Msg.StartSign
81 + "],not is [" + startSign + "]");
83 int msgType = ois.readByte();
84 msg.setMsgType(MsgType.getMsgTypeValue(msgType));
85 int timeStamp = ois.readInt();
86 msg.setTimeStamp(timeStamp);
87 int bodylength = ois.readShort();
88 msg.setLenOfBody(bodylength);
89 byte[] b = new byte[bodylength];
91 msg.newBodyfromBytes(b);
92 } catch (Exception e) {
93 throw new IOException("readOneMsg",e);
99 public static void writeMsg(Msg msg, BufferedOutputStream dout) throws IOException {
101 ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream(9);
102 DataOutputStream oos = new DataOutputStream(byteOutStream)){
103 oos.writeShort(Msg.StartSign);
104 oos.writeByte(msg.getMsgType().value);
105 oos.writeInt(Msg.creatMsgTimeStamp());
106 oos.writeShort(msg.getBodyLenNow());
108 dout.write(byteOutStream.toByteArray());
110 dout.write(msg.getBodyBytes());
112 } catch (Exception e) {
113 throw new IOException("writeMsg",e);