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