workaroud for snor issue
[vfc/nfvo/wfengine.git] / wso2 / logging-sdk / src / main / java / org / openo / log / impl / LogIdTool.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.openo.log.impl;
17
18 import java.util.Date;
19 import java.util.Random;
20
21
22
23 public class LogIdTool {
24   private static Random cmdRand = new Random();
25
26   private static Random sysRand = new Random();
27
28   private static Random secRand = new Random();
29
30   private LogIdTool() {
31
32   }
33
34   public static long getRandomID(int logType, long startDate) {
35     String strCtime = "" + startDate;
36     if (strCtime.length() > 13) {
37       strCtime = strCtime.substring(strCtime.length() - 13);
38     }
39     int seed1;
40     if (logType == LogConst.CMD_LOG_FLAG) {
41       seed1 = cmdRand.nextInt(100000);
42     } else if (logType == LogConst.SYS_LOG_FLAG) {
43       seed1 = sysRand.nextInt(100000);
44     } else if (logType == LogConst.SECRET_LOG_FLAG) {
45       seed1 = secRand.nextInt(100000);
46     } else {
47       return 0;
48     }
49     String seed2 = "" + seed1;
50     int shouldFillLen = 5 - seed2.length();
51     for (int i = 0; i < shouldFillLen; i++) {
52       seed2 = "0" + seed2;
53     }
54     return Long.parseLong(strCtime + seed2);
55   }
56
57   public static long transTimeCond2ID(Date time, boolean isStart) {
58     long timeLong = time.getTime();
59     if (isStart) {
60       timeLong = timeLong * 100000;
61     } else {
62       timeLong = timeLong * 100000 + 99999;
63     }
64     return timeLong;
65
66   }
67 }