diff options
Diffstat (limited to 'cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common')
14 files changed, 0 insertions, 1084 deletions
diff --git a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/.gitignore b/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/.gitignore deleted file mode 100644 index 6b468b6..0000000 --- a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.class diff --git a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/AsHex.java b/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/AsHex.java deleted file mode 100644 index 05d8f37..0000000 --- a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/AsHex.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this code 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.dcae.cdf.util.common; - -public class AsHex -{ - public static String asHex(byte[] data, int offset, int length, String sep) { - return asHex(data, offset, length, true); - } - public static String asHex(byte[] data, String sep) { - return asHex(data, 0, data.length, sep); - } - public static String asHex(byte[] data, int offset, int length) { - return asHex(data, offset, length, " "); - } - public static String asHex(byte[] data) { - return asHex(data, 0, data.length); - } - - public static String asHex(String data) { - return asHex(data.getBytes()); - } - - static private int asHexBlockLength = 16; - public static void setAsHexBlockLength(int n) { asHexBlockLength = n; } - public static int getAsHexBlockLength() { return asHexBlockLength; } - - private final static char[] hexdigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; - - /** - * return a byte buf as a hex string - */ - public static String asHex(byte[] buf, int offset, int length, boolean addFinalNL) { - StringBuilder ret = new StringBuilder(); - return asHex(ret, buf, offset, length, addFinalNL).toString(); - } - - /** - * Return a byte buf as hex into the provided StringBuilder. - */ - public static StringBuilder asHex(StringBuilder ret, byte[] buf, int offset, int length, boolean addFinalNL) { - final int blockLength = asHexBlockLength; - for (int o = 0; o < length; o += blockLength) { - int iend = (o + blockLength < length) ? (o + blockLength) : length; - int pend = o + blockLength; - for (int i = o; i < iend; i++) { - int b = (int)(buf[i+offset] & 0xFF); - ret.append(hexdigits[b/16]); - ret.append(hexdigits[b%16]); - } - for (int i = iend; i < pend; i++) { - ret.append(" "); - } - ret.append(" "); - for (int i = o; i < iend; i++) { - byte b = buf[i+offset]; - int ib = (int)(b & 0xFF); - if ((ib >= 0x20) && (ib < 0x7f)) ret.append((char)b); - else ret.append('.'); - } - if (iend < length) ret.append('\n'); - } - if (addFinalNL && (length%blockLength != 0)) ret.append('\n'); - return ret; - } - - /** - * Return a byte buf as hex with a maximum number of lines. - */ - public static String asHexWithMaxLines(byte[] buf, int offset, int length, int maxLines, boolean addFinalNL) { - StringBuilder ret = new StringBuilder(); - return asHexWithMaxLines(ret, buf, offset, length, maxLines, addFinalNL).toString(); - } - - /** - * Return a byte buf as hex into the provided StringBuilder with a maximum number of lines. - */ - public static StringBuilder asHexWithMaxLines(StringBuilder ret, byte[] buf, int offset, int length, int maxLines, boolean addFinalNL) { - int bytesToPrint = length - offset; - if (maxLines < 1) maxLines = 1; - int maxBytesToPrint = maxLines * asHexBlockLength; - if (bytesToPrint <= maxBytesToPrint) { - return asHex(ret, buf, offset, length, addFinalNL); - } else { - if (bytesToPrint > maxBytesToPrint) bytesToPrint = maxBytesToPrint; - asHex(ret, buf, offset, offset + bytesToPrint, false); - ret.append(" ...."); - if (addFinalNL) ret.append("\n"); - return ret; - // return asHex(ret, buf, length - halfBytesToPrint, length, addFinalNL); - } - } - - // Convert a hex string back to a byte array. - // This assumes that there is no whitespace within the string. - // public static byte[] fromHex(String hexStr) { - // byte[] bts = new byte[hexStr.length() / 2]; - // for (int i = 0; i < bts.length; i++) { - // bts[i] = (byte) Integer.parseInt(hexStr.substring(2*i, 2*i+2), 16); - // } - // return bts; - // } -} diff --git a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Classify.java b/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Classify.java deleted file mode 100644 index e0b9203..0000000 --- a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Classify.java +++ /dev/null @@ -1,109 +0,0 @@ -/* - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this code 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.dcae.cdf.util.common; - -import java.lang.Character; - -/** - * Classify holds various checking functions. - */ -public final class Classify { - - /** - * isHex(ch) - is a character a hex value? - * - * @param ch (char) - * @return boolean - */ - public static boolean isHex(char ch) { - return (ch >= '0' && ch <= '9') || - (ch >= 'A' && ch <= 'F') || - (ch >= 'a' && ch <= 'f'); - } - - /** - * isValidGuid - * - * @param input (String) - * @return boolean - */ - - public static boolean isValidGuid(String input) { - // Checks if the GUID has the following format: "0f114b6f-3f1d-4c8f-a065-2a2ec3d0f522" - - if ( (input == null) || (input.length() != 36)) return false; - - for (int i=0; i < 36; i++) { - char c = input.charAt(i); - if ( (i==8 || i==13 || i==18 || i==23)) { - if (c != '-') return false; - } - else if (!isHex(c)) return false; - } - return true; - } - - - /** - * isValidClli - * - * @param input (String) - * @return boolean - */ - - public static boolean isValidClli(String input) { - // Checks if the CLLI only contains letters or digits. - - if (input == null) return false; - int len = input.length(); - if (len == 0) return false; - - for (int i=0; i < len; i++) { - char c = input.charAt(i); - if (!Character.isLetterOrDigit(c)) return false; - } - return true; - } - - - /** - * isValidCanonicalIpv4Address - * - * @param ipAddress (String) - * @return boolean - */ - - public static boolean isValidCanonicalIpv4Address(String ipAddress) { - - String[] parts = ipAddress.split( "\\." ); - - if ( parts.length != 4 ) { - return false; - } - for ( String s : parts ) { - try { - int i = Integer.parseInt( s ); - if ( (i < 0) || (i > 255) ) { - return false; - } - } catch (Exception ex) { - return false; - } - } - - return true; - } -} diff --git a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Compress.java b/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Compress.java deleted file mode 100644 index 86ff60b..0000000 --- a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Compress.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this code 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.dcae.cdf.util.common; - -import java.util.zip.GZIPOutputStream; -import java.util.zip.ZipOutputStream; -import java.util.zip.ZipEntry; -// import java.io.InputStream; -import java.io.FileOutputStream; -import java.io.FileInputStream; -import java.io.File; -import java.io.IOException; - -public class Compress { - - /** - * Compress a file with the gzip algorithm, sending output to outFilename. - * Based on code at http://www.java-tips.org/java-se-tips/java.util.zip/how-to-compress-a-file-in-the-gip-format.html. - */ - public static void gzip(String inFilename, String outFilename) throws IOException { - String tmpFilename = outFilename + ".tmp"; - try { - // Create the GZIP output stream - GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(tmpFilename)); - - // Open the input file - FileInputStream in = new FileInputStream(inFilename); - - // Transfer bytes from the input file to the GZIP output stream - byte[] buf = new byte[4096]; - int len; - while ((len = in.read(buf)) > 0) { - out.write(buf, 0, len); - } - in.close(); - - // Complete the GZIP file - out.finish(); - out.close(); - - // rename .gz.tmp to .gz - File target = new File(outFilename); - if (target.exists()) target.delete(); - File file = new File(tmpFilename); - boolean result = file.renameTo(target); - if (!result) throw new IOException("Cannot rename " + tmpFilename + " to " + outFilename); - } catch (IOException e) { - // If we can't write the gzip file, remove it and pass on the exception. - File f = new File(outFilename); - f.delete(); - throw e; - } - } - - /** - * Compress a file with the gzip algorithm, sending output to filename+".gz". - */ - public static void gzip(String filename) throws IOException { - gzip(filename, filename + ".gz"); - } - - /** - * Compress a file with the zip algorithm, sending output to outFilename - * Based on code at http://www.java-tips.org/java-se-tips/java.util.zip/how-to-compress-a-file-in-the-gip-format.html. - */ - public static void zip(String inFilename, String outFilename) throws IOException { - String tmpFilename = outFilename + ".tmp"; - try { - // Create the ZIP output stream - ZipOutputStream out = new ZipOutputStream(new FileOutputStream(tmpFilename)); - ZipEntry zipEntry = new ZipEntry(inFilename); - out.putNextEntry(zipEntry); - - // Open the input file - FileInputStream in = new FileInputStream(inFilename); - - // Transfer bytes from the input file to the ZIP output stream - byte[] buf = new byte[4096]; - int len; - while ((len = in.read(buf)) > 0) { - out.write(buf, 0, len); - } - in.close(); - - // Complete the ZIP file - out.finish(); - out.close(); - - // rename .zip.tmp to .zip - File target = new File(outFilename); - if (target.exists()) target.delete(); - File file = new File(tmpFilename); - boolean result = file.renameTo(target); - if (!result) throw new IOException("Cannot rename " + tmpFilename + " to " + outFilename); - } catch (IOException e) { - // If we can't write the zip file, remove it and pass on the exception. - File f = new File(outFilename); - f.delete(); - throw e; - } - } - - /** - * Compress a file with the gzip algorithm, sending output to filename+".zip". - */ - public static void zip(String filename) throws IOException { - zip(filename, filename + ".zip"); - } - - public static void main(String args[]) throws Exception { - if (args.length == 1) { - gzip(args[0]); - zip(args[0]); - } else { - System.err.println("Usage: java Compress filename"); - } - } -} diff --git a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Convert.java b/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Convert.java deleted file mode 100644 index 6182fde..0000000 --- a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Convert.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this code 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.dcae.cdf.util.common; - -import java.lang.Character; - -/** - * Covert holds various conversion functions. - */ -public final class Convert { - - private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray(); - - /** - * toHexString(String) - convert a string into its hex equivalent - */ - public static String toHexString(String buf) { - if (buf == null) return ""; - return toHexString(buf.getBytes()); - } - - /** - * toHexString(byte[]) - convert a byte-string into its hex equivalent - */ - public static String toHexString(byte[] buf) { - if (buf == null) return ""; - char[] chars = new char[2 * buf.length]; - for (int i = 0; i < buf.length; ++i) { - chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4]; - chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F]; - } - return new String(chars); - } - - // alternate implementation that's slightly slower - // protected static final byte[] Hexhars = { - // '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' - // }; - // public static String encode(byte[] b) { - // StringBuilder s = new StringBuilder(2 * b.length); - // for (int i = 0; i < b.length; i++) { - // int v = b[i] & 0xff; - // s.append((char)Hexhars[v >> 4]); - // s.append((char)Hexhars[v & 0xf]); - // } - // return s.toString(); - // } - - /** - * Convert a hex string to its equivalent value. - */ - public static String stringFromHex(String hexString) throws Exception { - if (hexString == null) return ""; - return stringFromHex(hexString.toCharArray()); - } - - public static String stringFromHex(char[] hexCharArray) throws Exception { - if (hexCharArray == null) return ""; - return new String(bytesFromHex(hexCharArray)); - } - - public static byte[] bytesFromHex(String hexString) throws Exception { - if (hexString == null) return new byte[0]; - return bytesFromHex(hexString.toCharArray()); - } - - public static byte[] bytesFromHex(char[] hexCharArray) throws Exception { - if (hexCharArray == null) return new byte[0]; - int len = hexCharArray.length; - if ((len % 2) != 0) throw new Exception("Odd number of characters: '" + hexCharArray + "'"); - byte [] txtInByte = new byte [len / 2]; - int j = 0; - for (int i = 0; i < len; i += 2) { - txtInByte[j++] = (byte)(((fromHexDigit(hexCharArray[i], i) << 4) | fromHexDigit(hexCharArray[i+1], i)) & 0xFF); - } - return txtInByte; - } - - protected final static int fromHexDigit(char ch, int index) throws Exception { - int digit = Character.digit(ch, 16); - if (digit == -1) throw new Exception("Illegal hex character '" + ch + "' at index " + index); - return digit; - } -} diff --git a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/G2CookieGen.java b/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/G2CookieGen.java deleted file mode 100644 index 738fb3c..0000000 --- a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/G2CookieGen.java +++ /dev/null @@ -1,209 +0,0 @@ -/* - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this code 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.dcae.cdf.util.common; - -import javax.crypto.Cipher; -// import javax.crypto.SecretKey; -// import javax.crypto.KeyGenerator; -// import javax.crypto.spec.IvParameterSpec; -import java.security.Key; -// import java.security.NoSuchAlgorithmException; -// import java.security.SecureRandom; -// import javax.crypto.SecretKey; -// import sun.misc.*; -import java.util.*; - -public class G2CookieGen -{ - private Cipher cipher; - private Key key = null; - - private static String alg = "DES"; - private static String desecb = "DES/ECB/PKCS5Padding"; - - public static String G2_CLIENT_MEC_ID_1 = "MEC0001"; - private static String G2_CLIENT_MEC_ID_2 = "MEC0002"; - public static String G2_ENCRYPT_KEY = "secretK9"; - public static String G2_EPOCH_TM_STR = null; - - - private static long G2_TM_DELTA_IN_MILLISECONDS = 10*60*1000; - - class G2WSSKey implements Key - { - private final byte[] keyBytes; - private final String alg; - - G2WSSKey(String algorithm, byte[] keyBytes) - { - this.alg = algorithm; - this.keyBytes = keyBytes; - } - - public String getAlgorithm() - { - return alg; - } - public String getFormat() - { - return "RAW"; - } - public byte[] getEncoded() - { - return (byte[])keyBytes.clone(); - } - } - - - public G2CookieGen() { - try { - cipher = Cipher.getInstance(desecb); - } catch (Throwable t) { - System.err.println(t.toString()); - return; - } - } - - - public static String getClient1MacId() { - return G2_CLIENT_MEC_ID_1; - } - - public static String getClient2MacId() { - return G2_CLIENT_MEC_ID_2; - } - - public static String toHexStringFromByteArray(byte[] bytes) - { - StringBuilder retString = new StringBuilder(); - for (int i = 0; i < bytes.length; ++i) { - retString.append(Integer.toHexString(0x0100 + (bytes[i] & 0x00FF)).substring(1)); - } - return retString.toString(); - } - - public static byte[] toByteArrayFromHexString(String hexStr) - { - byte[] bts = new byte[hexStr.length() / 2]; - for (int i = 0; i < bts.length; i++) { - bts[i] = (byte) Integer.parseInt(hexStr.substring(2*i, 2*i+2), 16); - } - return bts; - } - - public byte[] encryptData(String sData) - { - try { - byte[] data = sData.getBytes(); - //System.out.println("Original data : " + new String(data)); - if (key == null) setKey(G2_ENCRYPT_KEY); - cipher.init(Cipher.ENCRYPT_MODE, key); - byte[] result = cipher.doFinal(data); - return result; - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - public String decryptData(byte[] sData) - { - try { - cipher.init(Cipher.DECRYPT_MODE, key); - byte[] result = cipher.doFinal(sData); - return new String(result); - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - public String constructCookie(String mechId) { - return mechId + ":" + System.currentTimeMillis(); - } - - public void setKey(String g2EncryptKey) { - key = new G2WSSKey(this.alg, g2EncryptKey.getBytes()); - } - - public String getEncryptedCookie(String mechId, String g2EncryptKey) { - setKey(g2EncryptKey); - String tmp = constructCookie(mechId); - byte[] byteArray = this.encryptData(tmp); - return this.toHexStringFromByteArray(byteArray); - } - - public long getTimeMillisFromCookie(String cookie) { - StringTokenizer tkn = new StringTokenizer(cookie,":"); - String tmStr = null; - while (tkn.hasMoreTokens()) { - tmStr = tkn.nextToken(); - } - Long tmLong = new Long(tmStr); - return tmLong.longValue(); - } - - public boolean isValid(long tm) { - long ctm = System.currentTimeMillis(); -System.out.println("Current Time="+ctm); -System.out.println("G2_TM_DELTA_IN_MILLISECONDS="+G2_TM_DELTA_IN_MILLISECONDS); - if ( Math.abs(ctm - tm) <= G2_TM_DELTA_IN_MILLISECONDS ) { - return true; - } - return false; - } - - - public static void main(String argv[]) { - try { - if (argv.length > 0) { -System.out.println("using Client MACID="+argv[0]); - G2_CLIENT_MEC_ID_1 = argv[0]; - - } - - if (argv.length > 1) { - if (argv[1].length() == 8) { -System.out.println("using Key="+argv[1]); - G2_ENCRYPT_KEY = argv[1]; - } - } - - if (argv.length > 2) { -System.out.println("using Epoch Time (in seconds) ="+argv[2]); - G2_EPOCH_TM_STR = argv[2]; - } - - - G2CookieGen wssc = new G2CookieGen(); - -// System.out.println("tz_diff="+G2_CLIENT_TM_ZONE_TO_PDT_IN_MILLISECONDS); -System.out.println("macid="+G2_CLIENT_MEC_ID_1); - - String cookie = wssc.constructCookie(G2_EPOCH_TM_STR); -System.out.println("original cookie="+cookie); - - byte[] byteArrary = wssc.encryptData(cookie); - String hexString = wssc.toHexStringFromByteArray(byteArrary); -System.out.println("encrypted cookie="+hexString); - System.exit(0); - - } catch (Exception e) { - System.err.println("Error: " + e); - System.exit(1); - } - } /* main */ -} diff --git a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Hostname.java b/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Hostname.java deleted file mode 100644 index 4385c01..0000000 --- a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Hostname.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this code 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.dcae.cdf.util.common; - -import java.net.InetAddress; - -public class Hostname { - - /** - * Hostname FQDN - */ - public static String getHostName() { - return getHostName("unknown.unknown"); - } - - /** - * Hostname FQDN - */ - public static String getHostName(String def) { - return (uname == null) ? def : hostName; - } - - /** - * uname, the 1st portion of the hostname FQDN - */ - public static String getUname() { - return getUname("unknown"); - } - - /** - * uname, the 1st portion of the hostname FQDN - */ - public static String getUname(String def) { - return (uname == null) ? def : uname; - } - - /** - * Get an IP address for this machine - */ - public static String getLocalIP() { - return defaultLocalIP; - } - /** - * Get an IP address for this machine - */ - public static String getLocalIPinHex() { - return defaultLocalIPinHex; - } - /** - * Get a host name for this machine - */ - public static String getCanonicalHostName() { - return defaultCanonicalHostName; - } - - /** - * Value returned by getLocalIP() method - */ - private static String defaultLocalIP; - private static String defaultLocalIPinHex; - private static String defaultCanonicalHostName; - private static String hostName = null; // Hostname FQDN - private static String uname = null; // Hostname 1st part - - static { - try { - InetAddress ia = InetAddress.getLocalHost(); - defaultLocalIP = ia.getHostAddress(); - byte b[] = ia.getAddress(); - defaultLocalIPinHex = Convert.toHexString(b); - defaultCanonicalHostName = ia.getCanonicalHostName(); - } catch (Exception e) { - defaultLocalIP = "127.0.0.1"; - defaultLocalIPinHex = "7F000001"; - defaultCanonicalHostName = "localhost"; - } - - try { - hostName = InetAddress.getLocalHost().getHostName(); - String hostNameParts[] = hostName.split("\\."); - uname = hostNameParts[0]; - } catch (Exception ex) { - } - int dotInHostname = hostName.indexOf('.'); - if (dotInHostname > -1) hostName = hostName.substring(0, dotInHostname); - } - - public static void main(String args[]) { - System.out.println("getHostName() = '" + getHostName() + "'"); - System.out.println("getUname() = '" + getUname() + "'"); - System.out.println("getLocalIP() = '" + getLocalIP() + "'"); - System.out.println("getLocalIPinHex() = '" + getLocalIPinHex() + "'"); - System.out.println("getCanonicalHostName() = '" + getCanonicalHostName() + "'"); - } -} diff --git a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Pair.java b/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Pair.java deleted file mode 100644 index 5a2a3f4..0000000 --- a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Pair.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this code 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.dcae.cdf.util.common; - -public final class Pair<L,R> { - public final L left; - public final R right; - public Pair(L l, R r) { left = l; right = r; } - - @Override - public boolean equals(Object obj) { - Pair<L,R> o = (Pair<L,R>)obj; - return left.equals(o.left) && right.equals(o.right); - } - @Override - public String toString() { - return "(" + left + "," + right + ")"; - } - @Override - public int hashCode() { - return left.hashCode() + right.hashCode(); - } - - public static <L,R> Pair<L,R> of(L l, R r) { - return new Pair<L,R>(l, r); - } -} diff --git a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Pid.java b/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Pid.java deleted file mode 100644 index c405c44..0000000 --- a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Pid.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this code 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.dcae.cdf.util.common; -import java.io.File; - -public class Pid { - /** - * Return the pid. - */ - public static int getPid() { return pid; } - public static String getPidStr() { return pidStr; } - - private static int pid; - private static String pidStr; - static { - try { - pid = Integer.parseInt( ( new File("/proc/self")).getCanonicalFile().getName() ); - pidStr = Integer.toString(pid); - } catch (java.io.IOException e) { - pid = -1; - pidStr = "-1"; - } - } -} diff --git a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Popen.java b/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Popen.java deleted file mode 100644 index d7f5846..0000000 --- a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Popen.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this code 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.dcae.cdf.util.common; - -import java.io.InputStream; -import java.io.OutputStream; -import java.io.IOException; - -public class Popen { - public static class Results { - public final String stdout, stderr; - public final int exitValue; - public Results(String so, String se, int e) { - stdout = so; stderr = se; exitValue = e; - } - } - - public static Results popen(String cmd) throws java.io.IOException, java.lang.InterruptedException { - return popen(cmd, null); - } - - public static Results popen(String cmd, String stdin) throws java.io.IOException, java.lang.InterruptedException { - Process process = Runtime.getRuntime().exec(cmd); - return proc(process, stdin); - } - - public static Results popen(String[] args) throws java.io.IOException, java.lang.InterruptedException { - return popen(args, null); - } - - public static Results popen(String[] args, String stdin) throws java.io.IOException, java.lang.InterruptedException { - Process process = Runtime.getRuntime().exec(args); - return proc(process, stdin); - } - - private static Results proc(Process process, String stdin) throws java.io.IOException, java.lang.InterruptedException { - OutputStream pinput = process.getOutputStream(); - InputStream poutput = process.getInputStream(); - InputStream perror = process.getErrorStream(); - - if (stdin != null) - pinput.write(stdin.getBytes()); - pinput.close(); - - String stdout = captureStream(poutput); - poutput.close(); - String stderr = captureStream(perror); - perror.close(); - process.waitFor(); - // System.out.println("stdin=\nnvvvvvvvvvvvvvvvv\n"); - // System.out.println(stdin); - // System.out.println("^^^^^^^^^^^^^^^^"); - // System.out.println("stdout=\nvvvvvvvvvvvvvvvv\n"); - // System.out.println(stdout); - // System.out.println("^^^^^^^^^^^^^^^^"); - // System.out.println("stderr=\nvvvvvvvvvvvvvvvv\n"); - // System.out.println(stderr); - // System.out.println("^^^^^^^^^^^^^^^^"); - return new Results(stdout, stderr, process.exitValue()); - } - - private static String captureStream(InputStream inp) throws java.io.IOException { - byte[] buf = new byte[8192]; - StringBuffer out = new StringBuffer(); - int b; - while ((b = inp.read(buf)) > 0) { - out.append(new String(buf, 0, b)); - } - return out.toString(); - } -} diff --git a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Tuple2.java b/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Tuple2.java deleted file mode 100644 index f4fd441..0000000 --- a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Tuple2.java +++ /dev/null @@ -1,25 +0,0 @@ -// -*- indent-tabs-mode: nil -*- -/* - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this code 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.dcae.cdf.util.common; - -public class Tuple2<T1,T2> { - public Tuple2(T1 n1, T2 n2) { - t1 = n1; t2 = n2; - } - public final T1 t1; - public final T2 t2; -} diff --git a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Tuple3.java b/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Tuple3.java deleted file mode 100644 index 566f910..0000000 --- a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Tuple3.java +++ /dev/null @@ -1,29 +0,0 @@ -// -*- indent-tabs-mode: nil -*- -/* - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this code 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.dcae.cdf.util.common; - -public class Tuple3<T1,T2,T3> extends Tuple2<T1,T2> { - public Tuple3(T1 n1, T2 n2, T3 n3) { - super(n1, n2); - t3 = n3; - } - public Tuple3(Tuple3<T1,T2,T3> t) { - super(t.t1, t.t2); - t3 = t.t3; - } - public final T3 t3; -} diff --git a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Tuple4.java b/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Tuple4.java deleted file mode 100644 index c41d64c..0000000 --- a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Tuple4.java +++ /dev/null @@ -1,29 +0,0 @@ -// -*- indent-tabs-mode: nil -*- -/* - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this code 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.dcae.cdf.util.common; - -public class Tuple4<T1,T2,T3,T4> extends Tuple3<T1,T2,T3> { - public Tuple4(T1 n1, T2 n2, T3 n3, T4 n4) { - super(n1, n2, n3); - t4 = n4; - } - public Tuple4(Tuple4<T1,T2,T3,T4> t) { - super(t.t1, t.t2, t.t3); - t4 = t.t4; - } - public final T4 t4; -} diff --git a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Uid.java b/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Uid.java deleted file mode 100644 index 99feeb3..0000000 --- a/cdf/src/cdf-prop-value/cdf-util/src/main/java/org/onap/dcae/cdf/util/common/Uid.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this code 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.dcae.cdf.util.common; -import java.io.File; -import java.io.BufferedReader; -import java.io.FileReader; -import org.onap.dcae.cdf.util.common.Popen; - -public class Uid { - /** - * Return the uid. - */ - public static int getUid() { return uid; } - public static String getUidStr() { return uidStr; } - - private static int uid = -1; - private static String uidStr = ""; - static { - try { - uid = getUidFromProcSelfStatus(); - if (uid == -1) uid = getUidFromIdU(); - uidStr = Integer.toString(uid); - } catch (java.io.IOException e) { - uid = -1; - uidStr = "-1"; - System.err.println("Exception: " + e); - } catch (Exception e) { - System.err.println("Exception: " + e); - } - - } - - private static int getUidFromProcSelfStatus() throws java.io.IOException { - int uid = -1; - if (true) return -1; - BufferedReader br = new BufferedReader(new FileReader(new File("/proc/self/status"))); - String thisLine = null; - while ((thisLine = br.readLine()) != null) { - if (thisLine.startsWith("Uid:")) { - String[] uids = thisLine.split("[: \t]+"); - if (uids.length > 1) { - uid = Integer.parseInt(uids[1]); - break; - } - } - } - br.close(); - return uid; - } - - private static int getUidFromIdU() throws java.io.IOException, java.lang.InterruptedException { - Popen.Results results = Popen.popen("/usr/bin/id -u"); - uid = Integer.parseInt(results.stdout.trim()); - return uid; - } -} |