diff options
Diffstat (limited to 'src/main/java/com/cmcc/simulator/xml')
-rw-r--r-- | src/main/java/com/cmcc/simulator/xml/CV.java | 39 | ||||
-rw-r--r-- | src/main/java/com/cmcc/simulator/xml/FileHeader.java | 94 | ||||
-rw-r--r-- | src/main/java/com/cmcc/simulator/xml/FileReadyEvent.java | 61 | ||||
-rw-r--r-- | src/main/java/com/cmcc/simulator/xml/GZIPUtil.java | 135 | ||||
-rw-r--r-- | src/main/java/com/cmcc/simulator/xml/Instance.java | 39 | ||||
-rw-r--r-- | src/main/java/com/cmcc/simulator/xml/Measurements.java | 52 | ||||
-rw-r--r-- | src/main/java/com/cmcc/simulator/xml/N.java | 39 | ||||
-rw-r--r-- | src/main/java/com/cmcc/simulator/xml/PmData.java | 30 | ||||
-rw-r--r-- | src/main/java/com/cmcc/simulator/xml/PmFile.java | 40 | ||||
-rw-r--r-- | src/main/java/com/cmcc/simulator/xml/PmName.java | 20 | ||||
-rw-r--r-- | src/main/java/com/cmcc/simulator/xml/SN.java | 39 | ||||
-rw-r--r-- | src/main/java/com/cmcc/simulator/xml/Utils.java | 129 | ||||
-rw-r--r-- | src/main/java/com/cmcc/simulator/xml/V.java | 38 |
13 files changed, 755 insertions, 0 deletions
diff --git a/src/main/java/com/cmcc/simulator/xml/CV.java b/src/main/java/com/cmcc/simulator/xml/CV.java new file mode 100644 index 0000000..492ff75 --- /dev/null +++ b/src/main/java/com/cmcc/simulator/xml/CV.java @@ -0,0 +1,39 @@ +package com.cmcc.simulator.xml; + +import java.util.List; + +import javax.xml.bind.annotation.XmlAttribute; + +public class CV { + + private List<SN> sns; + private String i; + + public CV() { + super(); + } + + public CV(List<SN> sns, String i) { + super(); + this.sns = sns; + this.i = i; + } + + public List<SN> getSns() { + return sns; + } + + public void setSns(List<SN> sns) { + this.sns = sns; + } + + @XmlAttribute(name = "i") + public String getI() { + return i; + } + + public void setI(String i) { + this.i = i; + } + +} diff --git a/src/main/java/com/cmcc/simulator/xml/FileHeader.java b/src/main/java/com/cmcc/simulator/xml/FileHeader.java new file mode 100644 index 0000000..9847458 --- /dev/null +++ b/src/main/java/com/cmcc/simulator/xml/FileHeader.java @@ -0,0 +1,94 @@ +package com.cmcc.simulator.xml; + +import java.util.Date; + +import javax.xml.bind.annotation.XmlElement; + +public class FileHeader { + private String vendorName; + private String elementType; + private String pmVersion; + private String timeZone; + private int period; + private String timeStamp; + private String startTime; + + public FileHeader() { + } + + public FileHeader(String vendorName, String elementType, String pmVersion, String timeZone, int period, + String timeStamp, String startTime) { + super(); + this.vendorName = vendorName; + this.elementType = elementType; + this.pmVersion = pmVersion; + this.timeZone = timeZone; + this.period = period; + this.timeStamp = timeStamp; + this.startTime = startTime; + } + + @XmlElement(name="VendorName") + public String getVendorName() { + return vendorName; + } + + public void setVendorName(String vendorName) { + this.vendorName = vendorName; + } + + @XmlElement(name="ElementType") + public String getElementType() { + return elementType; + } + + public void setElementType(String elementType) { + this.elementType = elementType; + } + + @XmlElement(name="PmVersion") + public String getPmVersion() { + return pmVersion; + } + + public void setPmVersion(String pmVersion) { + this.pmVersion = pmVersion; + } + + @XmlElement(name="TimeZone") + public String getTimeZone() { + return timeZone; + } + + public void setTimeZone(String timeZone) { + this.timeZone = timeZone; + } + + @XmlElement(name="Period") + public int getPeriod() { + return period; + } + + public void setPeriod(int period) { + this.period = period; + } + + @XmlElement(name="TimeStamp") + public String getTimeStamp() { + return timeStamp; + } + + public void setTimeStamp(String timeStamp) { + this.timeStamp = timeStamp; + } + + @XmlElement(name="StartTime") + public String getStartTime() { + return startTime; + } + + public void setStartTime(String startTime) { + this.startTime = startTime; + } + +} diff --git a/src/main/java/com/cmcc/simulator/xml/FileReadyEvent.java b/src/main/java/com/cmcc/simulator/xml/FileReadyEvent.java new file mode 100644 index 0000000..f14f22f --- /dev/null +++ b/src/main/java/com/cmcc/simulator/xml/FileReadyEvent.java @@ -0,0 +1,61 @@ +package com.cmcc.simulator.xml; + +import java.io.Serializable; +import java.util.Date; +import java.util.List; + +public class FileReadyEvent implements Serializable { + + private static final long serialVersionUID = 1L; + + private String nfvoid; + private String datasource; + private String sourceid; + private String createat; + private List<String> fileuri; + + public String getNfvoid() { + return nfvoid; + } + + public void setNfvoid(String nfvoid) { + this.nfvoid = nfvoid; + } + + public String getDatasource() { + return datasource; + } + + public void setDatasource(String datasource) { + this.datasource = datasource; + } + + public String getSourceid() { + return sourceid; + } + + public void setSourceid(String sourceid) { + this.sourceid = sourceid; + } + + public String getCreateat() { + return createat; + } + + public void setCreateat(String createat) { + this.createat = createat; + } + + public List<String> getFileuri() { + return fileuri; + } + + public void setFileuri(List<String> fileuri) { + this.fileuri = fileuri; + } + + public static long getSerialversionuid() { + return serialVersionUID; + } + +} diff --git a/src/main/java/com/cmcc/simulator/xml/GZIPUtil.java b/src/main/java/com/cmcc/simulator/xml/GZIPUtil.java new file mode 100644 index 0000000..3216a8c --- /dev/null +++ b/src/main/java/com/cmcc/simulator/xml/GZIPUtil.java @@ -0,0 +1,135 @@ +package com.cmcc.simulator.xml; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.zip.GZIPOutputStream; + +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; +import org.apache.commons.compress.utils.IOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class GZIPUtil { + + private static final Logger logger = LoggerFactory.getLogger(GZIPUtil.class); + public static File pack(File[] sources, File target) { + FileOutputStream out = null; + try { + out = new FileOutputStream(target); + } catch (FileNotFoundException e1) { + e1.printStackTrace(); + } + TarArchiveOutputStream os = new TarArchiveOutputStream(out); + for (File file : sources) { + try { + TarArchiveEntry tae = new TarArchiveEntry(file); + tae.setName(file.getName()); + os.putArchiveEntry(tae); +// os.putArchiveEntry(tae); + IOUtils.copy(new FileInputStream(file), os); + os.closeArchiveEntry(); + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (os != null) { + try { + os.flush(); + os.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + return target; + } + + + public static File compress(String filePath, File source) { + + File target = new File(filePath + "/" + source.getName() + ".gz"); + FileInputStream in = null; + GZIPOutputStream out = null; + try { + in = new FileInputStream(source); + out = new GZIPOutputStream(new FileOutputStream(target)); + byte[] array = new byte[1024]; + int number = -1; + while ((number = in.read(array, 0, array.length)) != -1) { + out.write(array, 0, number); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + return null; + } catch (IOException e) { + e.printStackTrace(); + return null; + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + if (out != null) { + try { + out.close(); + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + } + return target; + } + + public static void doCompressFileToGz(String inFileName) { + try { + + logger.debug("Creating the GZIP output stream."); + String outFileName = inFileName + ".gz"; + GZIPOutputStream out = null; + try { + out = new GZIPOutputStream(new FileOutputStream(outFileName)); + } catch (FileNotFoundException e) { + logger.info("Could not create file: " + outFileName); + System.exit(1); + } + + logger.debug("Opening the input file."); + FileInputStream in = null; + try { + in = new FileInputStream(inFileName); + } catch (FileNotFoundException e) { + logger.error("File not found. " + inFileName); + System.exit(1); + } + + logger.debug("Transfering bytes from input file to GZIP Format."); + byte[] buf = new byte[1024]; + int len; + while ((len = in.read(buf)) > 0) { + out.write(buf, 0, len); + } + in.close(); + + logger.info("Completing the GZIP file"); + out.finish(); + out.close(); + + } catch (IOException e) { + e.printStackTrace(); + System.exit(1); + } + } +} diff --git a/src/main/java/com/cmcc/simulator/xml/Instance.java b/src/main/java/com/cmcc/simulator/xml/Instance.java new file mode 100644 index 0000000..c6e0f75 --- /dev/null +++ b/src/main/java/com/cmcc/simulator/xml/Instance.java @@ -0,0 +1,39 @@ +package com.cmcc.simulator.xml; + +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; + +public class Instance { + List<V> vs; + List<CV> cvs; + + public Instance() { + super(); + } + + public Instance(List<V> vs, List<CV> cvs) { + super(); + this.vs = vs; + this.cvs = cvs; + } + + @XmlElement(name="V") + public List<V> getVs() { + return vs; + } + + public void setVs(List<V> vs) { + this.vs = vs; + } + + @XmlElement(name="CV") + public List<CV> getCvs() { + return cvs; + } + + public void setCvs(List<CV> cvs) { + this.cvs = cvs; + } + +} diff --git a/src/main/java/com/cmcc/simulator/xml/Measurements.java b/src/main/java/com/cmcc/simulator/xml/Measurements.java new file mode 100644 index 0000000..99eb5df --- /dev/null +++ b/src/main/java/com/cmcc/simulator/xml/Measurements.java @@ -0,0 +1,52 @@ +package com.cmcc.simulator.xml; + +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; + +public class Measurements { + + String objectType; + List<N> n; + PmData pmData; + public Measurements() { + super(); + } + + public Measurements(String objectType, List<N> n, PmData pmData) { + super(); + this.objectType = objectType; + this.n = n; + this.pmData = pmData; + } + + @XmlElementWrapper(name = "PmName") + @XmlElement(name = "N") + public List<N> getN() { + return n; + } + + public void setN(List<N> n) { + this.n = n; + } + + @XmlElement(name = "ObjectType") + public String getObjectType() { + return objectType; + } + + public void setObjectType(String objectType) { + this.objectType = objectType; + } + + @XmlElement(name = "PmData") + public PmData getPmData() { + return pmData; + } + + public void setPmData(PmData pmData) { + this.pmData = pmData; + } + +} diff --git a/src/main/java/com/cmcc/simulator/xml/N.java b/src/main/java/com/cmcc/simulator/xml/N.java new file mode 100644 index 0000000..0fe9978 --- /dev/null +++ b/src/main/java/com/cmcc/simulator/xml/N.java @@ -0,0 +1,39 @@ +package com.cmcc.simulator.xml; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlValue; + +public class N { + String i; + String value; + + public N() { + super(); + // TODO Auto-generated constructor stub + } + + public N(String i, String value) { + super(); + this.i = i; + this.value = value; + } + + @XmlAttribute(name = "i") + public String getI() { + return i; + } + + public void setI(String i) { + this.i = i; + } + + public String getValue() { + return value; + } + + @XmlValue + public void setValue(String value) { + this.value = value; + } + +} diff --git a/src/main/java/com/cmcc/simulator/xml/PmData.java b/src/main/java/com/cmcc/simulator/xml/PmData.java new file mode 100644 index 0000000..b572ace --- /dev/null +++ b/src/main/java/com/cmcc/simulator/xml/PmData.java @@ -0,0 +1,30 @@ +package com.cmcc.simulator.xml; + +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; + +public class PmData { + + List<Instance> objects; + + public PmData() { + super(); + // TODO Auto-generated constructor stub + } + + public PmData(List<Instance> objects) { + super(); + this.objects = objects; + } + + @XmlElement(name="Object") + public List<Instance> getObjects() { + return objects; + } + + public void setObjects(List<Instance> objects) { + this.objects = objects; + } + +} diff --git a/src/main/java/com/cmcc/simulator/xml/PmFile.java b/src/main/java/com/cmcc/simulator/xml/PmFile.java new file mode 100644 index 0000000..1a49e15 --- /dev/null +++ b/src/main/java/com/cmcc/simulator/xml/PmFile.java @@ -0,0 +1,40 @@ +package com.cmcc.simulator.xml; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "PmFile") +public class PmFile { + + private FileHeader fileHeader; + private Measurements measurements; + + public PmFile() { + super(); + } + + public PmFile(FileHeader fileHeader, Measurements measurements) { + super(); + this.fileHeader = fileHeader; + this.measurements = measurements; + } + + @XmlElement(name="FileHeader") + public FileHeader getFileHeader() { + return fileHeader; + } + + public void setFileHeader(FileHeader fileHeader) { + this.fileHeader = fileHeader; + } + + @XmlElement(name="Measurements") + public Measurements getMeasurements() { + return measurements; + } + + public void setMeasurements(Measurements measurements) { + this.measurements = measurements; + } + +} diff --git a/src/main/java/com/cmcc/simulator/xml/PmName.java b/src/main/java/com/cmcc/simulator/xml/PmName.java new file mode 100644 index 0000000..a216c6e --- /dev/null +++ b/src/main/java/com/cmcc/simulator/xml/PmName.java @@ -0,0 +1,20 @@ +package com.cmcc.simulator.xml; + +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; + +public class PmName { + + List<N> ns; + + @XmlElement(name = "N") + public List<N> getNs() { + return ns; + } + + public void setNs(List<N> ns) { + this.ns = ns; + } + +} diff --git a/src/main/java/com/cmcc/simulator/xml/SN.java b/src/main/java/com/cmcc/simulator/xml/SN.java new file mode 100644 index 0000000..fcb9001 --- /dev/null +++ b/src/main/java/com/cmcc/simulator/xml/SN.java @@ -0,0 +1,39 @@ +package com.cmcc.simulator.xml; + +import javax.xml.bind.annotation.XmlElement; + +public class SN { + + private String sn; + private int sv; + + public SN() { + super(); + // TODO Auto-generated constructor stub + } + + public SN(String sn, int sv) { + super(); + this.sn = sn; + this.sv = sv; + } + + @XmlElement(name = "SN") + public String getSn() { + return sn; + } + + public void setSn(String sn) { + this.sn = sn; + } + + @XmlElement(name = "SV") + public int getSv() { + return sv; + } + + public void setSv(int sv) { + this.sv = sv; + } + +} diff --git a/src/main/java/com/cmcc/simulator/xml/Utils.java b/src/main/java/com/cmcc/simulator/xml/Utils.java new file mode 100644 index 0000000..747420a --- /dev/null +++ b/src/main/java/com/cmcc/simulator/xml/Utils.java @@ -0,0 +1,129 @@ +package com.cmcc.simulator.xml; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.StringWriter; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; + +public class Utils { + + public static String beanToXml(Object obj, Class<?> load) throws JAXBException { + JAXBContext context = JAXBContext.newInstance(load); + Marshaller marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8"); + StringWriter writer = new StringWriter(); + marshaller.marshal(obj, writer); + return writer.toString(); + } + + public static void writeToFile(String outputFilePath, String content) { + File file = new File(outputFilePath); + try { + FileWriter writer = new FileWriter(file); + writer.write(content); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static String createAmfFile(List<Instance> objects) throws JAXBException { + + FileHeader fileHeader = new FileHeader(); + fileHeader.setElementType("AMF"); + fileHeader.setPmVersion("V0.4.0"); + fileHeader.setTimeZone("UTC+8"); + fileHeader.setVendorName("CMCC"); + fileHeader.setPeriod(15); + fileHeader.setStartTime(Utils.getTimeShort()); + fileHeader.setTimeStamp(Utils.getTimeShort()); + + Measurements measurements = new Measurements(); + PmData pmData = new PmData(objects); + N n46 = new N("46", "AMF.RegSub._NS"); + List<N> ns = new ArrayList<>(); + ns.add(n46); + measurements.setN(ns); + measurements.setObjectType("AmfFunction"); + measurements.setPmData(pmData); + + PmFile pmFile = new PmFile(fileHeader, measurements); + String str = Utils.beanToXml(pmFile, PmFile.class); + + str = str.replaceAll("<sns>", ""); + str = str.replaceAll("</sns>", ""); + str = str.replaceAll("((\r\n)|\n)[\\s\t ]*(\\1)+", "$1").replaceAll("^((\r\n)|\n)", ""); + return str; + + } + + public static String createUPFFile(List<Instance> objects) throws Exception { + FileHeader fileHeader = new FileHeader(); + fileHeader.setElementType("UPF"); + fileHeader.setPmVersion("V0.4.0"); + fileHeader.setTimeZone("UTC+8"); + fileHeader.setVendorName("CMCC"); + fileHeader.setPeriod(15); + fileHeader.setStartTime(Utils.getTimeShort()); + fileHeader.setTimeStamp(Utils.getTimeShort()); + + Measurements measurements = new Measurements(); + PmData pmData = new PmData(objects); + N n9 = new N("9", "UPF.N3IncPkt._Dnn"); + N n11 = new N("11", "UPF.N3OgPkt._Dnn"); + List<N> ns = new ArrayList<>(); + ns.add(n9); + ns.add(n11); + measurements.setN(ns); + measurements.setObjectType("UpfFunction"); + measurements.setPmData(pmData); + + PmFile pmFile = new PmFile(fileHeader, measurements); + String str = Utils.beanToXml(pmFile, PmFile.class); + + str = str.replaceAll("<sns>", ""); + str = str.replaceAll("</sns>", ""); + str = str.replaceAll("((\r\n)|\n)[\\s\t ]*(\\1)+", "$1").replaceAll("^((\r\n)|\n)", ""); + return str; + + } + + public static boolean delFile(File file) { + if (!file.exists()) { + return false; + } + + if (file.isDirectory()) { + File[] files = file.listFiles(); + for (File f : files) { + delFile(f); + } + } + return file.delete(); + } + + + public static String getTimeShort() { + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"); + Date currentTime = new Date(); + String dateString = formatter.format(currentTime); + return dateString; + } + + public static String getTimeFormat() { + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Date currentTime = new Date(); + String dateString = formatter.format(currentTime); + return dateString; + } + +}
\ No newline at end of file diff --git a/src/main/java/com/cmcc/simulator/xml/V.java b/src/main/java/com/cmcc/simulator/xml/V.java new file mode 100644 index 0000000..a51672e --- /dev/null +++ b/src/main/java/com/cmcc/simulator/xml/V.java @@ -0,0 +1,38 @@ +package com.cmcc.simulator.xml; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlValue; + +public class V { + String v; + int value; + + public V() { + super(); + } + + public V(String i, int value) { + super(); + this.v = i; + this.value = value; + } + + @XmlAttribute(name = "i") + public String getV() { + return v; + } + + public void setV(String v) { + this.v = v; + } + + public int getValue() { + return value; + } + + @XmlValue + public void setValue(int value) { + this.value = value; + } + +} |