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