2 * Copyright 2016-2017, Nokia Corporation
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.driver.vnfm.svnfm.nokia.util;
18 import com.google.common.io.ByteStreams;
19 import org.apache.http.impl.client.CloseableHttpClient;
20 import org.apache.http.impl.client.HttpClients;
22 import java.io.InputStream;
23 import java.io.PrintStream;
26 * Wrapper class for static method calls to core or core libraries.
27 * Calls to static methods in core or core libraries are wrapped to be able to test
28 * the classes that uses static calls.
30 public class SystemFunctions {
31 private static SystemFunctions singletonInstance;
34 * @return singleton instance
36 public static SystemFunctions systemFunctions() {
37 if (singletonInstance != null) {
38 return singletonInstance;
40 synchronized (SystemFunctions.class) {
41 singletonInstance = new SystemFunctions();
43 return singletonInstance;
48 * Causes the currently executing thread to sleep (temporarily cease
49 * execution) for the specified number of milliseconds, subject to
50 * the precision and accuracy of system timers and schedulers. The thread
51 * does not lose ownership of any monitors.
53 * @param millis the length of time to sleep in milliseconds
55 public void sleep(long millis) {
58 } catch (InterruptedException e) {
59 Thread.currentThread().interrupt();
60 throw new UserInvisibleError("Interrupted while sleep", e);
65 * Returns the current time in milliseconds. Note that
66 * while the unit of time of the return value is a millisecond,
67 * the granularity of the value depends on the underlying
68 * operating system and may be larger. For example, many
69 * operating systems measure time in units of tens of
72 * <p> See the description of the class <code>Date</code> for
73 * a discussion of slight discrepancies that may arise between
74 * Unable to load /unittests/missing * "computer time" and coordinated universal time (UTC).
76 * @return the difference, measured in milliseconds, between
77 * the current time and midnight, January 1, 1970 UTC.
80 public long currentTimeMillis() {
81 return System.currentTimeMillis();
85 * Loads a file from the class path
87 * @param url the URL of the file
88 * @return the content of the file
90 public byte[] loadFile(String url) {
92 InputStream stream = SystemFunctions.class.getClassLoader().getResourceAsStream(url);
93 return ByteStreams.toByteArray(stream);
94 } catch (Exception e) {
95 throw new UserVisibleError("Unable to load " + url, e);
100 * The "standard" error output stream. This stream is already
101 * open and ready to accept output data.
103 * Typically this stream corresponds to display output or another
104 * output destination specified by the host environment or user. By
105 * convention, this output stream is used to display error messages
106 * or other information that should come to the immediate attention
107 * of a user even if the principal output stream, the value of the
108 * variable <code>out</code>, has been redirected to a file or other
109 * destination that is typically not continuously monitored.
111 @SuppressWarnings("squid:S106") // (intentional wrapping of system err)
112 public PrintStream err() {
117 * The "standard" output stream. This stream is already
118 * open and ready to accept output data. Typically this stream
119 * corresponds to display output or another output destination
120 * specified by the host environment or user.
122 * For simple stand-alone Java applications, a typical way to write
123 * a line of output data is:
125 * System.out.println(data)
126 * </pre></blockquote>
128 * See the <code>println</code> methods in class <code>PrintStream</code>.
130 * @see java.io.PrintStream#println()
131 * @see java.io.PrintStream#println(boolean)
132 * @see java.io.PrintStream#println(char)
133 * @see java.io.PrintStream#println(char[])
134 * @see java.io.PrintStream#println(double)
135 * @see java.io.PrintStream#println(float)
136 * @see java.io.PrintStream#println(int)
137 * @see java.io.PrintStream#println(long)
138 * @see java.io.PrintStream#println(java.lang.Object)
139 * @see java.io.PrintStream#println(java.lang.String)
141 @SuppressWarnings("squid:S106") // (intentional wrapping of system err)
142 public PrintStream out() {
147 * The "standard" input stream. This stream is already
148 * open and ready to supply input data. Typically this stream
149 * corresponds to keyboard input or another input source specified by
150 * the host environment or user.
152 public InputStream in() {
153 return System.in; //NO SONAR (intentional wrapping of system in)
157 * Wraps the static call (required for being able to test)
159 * @return the default HTTP client
161 public CloseableHttpClient getHttpClient() {
162 return HttpClients.createDefault();