2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package org.openecomp.vid.mso;
\r
24 import java.text.DateFormat;
\r
25 import java.text.SimpleDateFormat;
\r
26 import java.util.Collections;
\r
27 import java.util.Date;
\r
29 import javax.ws.rs.client.Client;
\r
30 import javax.ws.rs.client.Entity;
\r
31 import javax.ws.rs.core.MediaType;
\r
32 import javax.ws.rs.core.MultivaluedHashMap;
\r
33 import javax.ws.rs.core.Response;
\r
35 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
\r
36 import org.openecomp.portalsdk.core.util.SystemProperties;
\r
38 import org.apache.commons.codec.binary.Base64;
\r
39 import org.eclipse.jetty.util.security.Password;
\r
40 import org.openecomp.vid.changeManagement.ChangeManagementRequest;
\r
41 import org.openecomp.vid.client.HttpBasicClient;
\r
42 import org.openecomp.vid.client.HttpsBasicClient;
\r
43 import org.openecomp.vid.mso.rest.RequestDetails;
\r
46 * The Class MsoRestInterface.
\r
48 public class MsoRestInterface extends MsoRestInt implements MsoRestInterfaceIfc {
\r
51 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MsoRestInterface.class);
\r
53 /** The Constant dateFormat. */
\r
54 final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
\r
57 private static Client client = null;
\r
59 /** The common headers. */
\r
60 private MultivaluedHashMap<String, Object> commonHeaders;
\r
63 * Instantiates a new mso rest interface.
\r
65 public MsoRestInterface() {
\r
70 * @see org.openecomp.vid.mso.MsoRestInterfaceIfc#initRestClient()
\r
72 public void initMsoClient()
\r
74 final String methodname = "initRestClient()";
\r
76 final String username = SystemProperties.getProperty(MsoProperties.MSO_USER_NAME);
\r
77 final String password = SystemProperties.getProperty(MsoProperties.MSO_PASSWORD);
\r
78 final String mso_url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL);
\r
79 final String decrypted_password = Password.deobfuscate(password);
\r
81 String authString = username + ":" + decrypted_password;
\r
83 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
\r
84 String authStringEnc = new String(authEncBytes);
\r
86 commonHeaders = new MultivaluedHashMap<String, Object> ();
\r
87 commonHeaders.put("Authorization", Collections.singletonList((Object) ("Basic " + authStringEnc)));
\r
88 //Pass calling application identifier to SO
\r
89 commonHeaders.put("X-FromAppId",
\r
90 Collections.singletonList(SystemProperties.getProperty(SystemProperties.APP_DISPLAY_NAME)));
\r
92 boolean use_ssl = true;
\r
93 if ( (mso_url != null) && ( !(mso_url.isEmpty()) ) ) {
\r
94 if ( mso_url.startsWith("https")) {
\r
101 if (client == null) {
\r
105 //logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + methodname + " getting HttpsBasicClient with username=" + username
\r
106 // + " password=" + password);
\r
107 client = HttpsBasicClient.getClient();
\r
110 //logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + methodname + " getting HttpsBasicClient with username=" + username
\r
111 // + " password=" + password);
\r
112 client = HttpBasicClient.getClient();
\r
114 } catch (Exception e) {
\r
115 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + methodname + " Unable to get the SSL client");
\r
121 * @see org.openecomp.vid.mso.MsoRestInterfaceIfc#Get(java.lang.Object, java.lang.String, java.lang.String, org.openecomp.vid.mso.RestObject)
\r
123 @SuppressWarnings("unchecked")
\r
124 public <T> void Get (T t, String sourceId, String path, RestObject<T> restObject ) throws Exception {
\r
125 String methodName = "Get";
\r
127 logger.debug(EELFLoggerDelegate.debugLogger, methodName + " start");
\r
132 url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
\r
133 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " sending request to url= " + url);
\r
137 final Response cres = client.target(url)
\r
139 .accept("application/json")
\r
140 .headers(commonHeaders)
\r
143 int status = cres.getStatus();
\r
144 restObject.setStatusCode (status);
\r
146 if (status == 200) {
\r
147 t = (T) cres.readEntity(t.getClass());
\r
149 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " REST api was successfull!");
\r
152 throw new Exception(methodName + " with status="+ status + ", url= " + url );
\r
155 logger.debug(EELFLoggerDelegate.debugLogger,methodName + " received status=" + status );
\r
161 * @see org.openecomp.vid.mso.MsoRestInterfaceIfc#Delete(java.lang.Object, org.openecomp.vid.mso.rest.RequestDetails, java.lang.String, java.lang.String, org.openecomp.vid.mso.RestObject)
\r
163 @SuppressWarnings("unchecked")
\r
164 public <T> void Delete(T t, RequestDetails r, String sourceID, String path, RestObject<T> restObject) {
\r
166 String methodName = "Delete";
\r
168 Response cres = null;
\r
170 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " start");
\r
176 url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
\r
177 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + " methodName sending request to: " + url);
\r
179 cres = client.target(url)
\r
181 .accept("application/json")
\r
182 .headers(commonHeaders)
\r
184 .build("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON)).invoke();
\r
185 // .method("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON));
\r
186 //.delete(Entity.entity(r, MediaType.APPLICATION_JSON));
\r
188 int status = cres.getStatus();
\r
189 restObject.setStatusCode (status);
\r
191 if (status == 404) { // resource not found
\r
192 String msg = "Resource does not exist...: " + cres.getStatus();
\r
193 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);
\r
194 } else if (status == 200 || status == 204){
\r
195 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + "Resource " + url + " deleted");
\r
196 } else if (status == 202) {
\r
197 String msg = "Delete in progress: " + status;
\r
198 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);
\r
201 String msg = "Deleting Resource failed: " + status;
\r
202 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);
\r
206 t = (T) cres.readEntity(t.getClass());
\r
209 catch ( Exception e ) {
\r
210 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " No response entity, this is probably ok, e="
\r
215 catch (Exception e)
\r
217 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with url="+url+ ", Exception: " + e.toString());
\r
224 * @see org.openecomp.vid.mso.MsoRestInterfaceIfc#Post(java.lang.Object, org.openecomp.vid.mso.rest.RequestDetails, java.lang.String, java.lang.String, org.openecomp.vid.mso.RestObject)
\r
226 @SuppressWarnings("unchecked")
\r
227 public <T> void Post(T t, RequestDetails r, String sourceID, String path, RestObject<T> restObject) throws Exception {
\r
229 String methodName = "Post";
\r
232 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " start");
\r
239 url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
\r
240 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " sending request to url= " + url);
\r
241 // Change the content length
\r
242 final Response cres = client.target(url)
\r
244 .accept("application/json")
\r
245 .headers(commonHeaders)
\r
246 //.header("content-length", 201)
\r
247 //.header("X-FromAppId", sourceID)
\r
248 .post(Entity.entity(r, MediaType.APPLICATION_JSON));
\r
251 t = (T) cres.readEntity(t.getClass());
\r
254 catch ( Exception e ) {
\r
255 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " No response entity, this is probably ok, e="
\r
259 int status = cres.getStatus();
\r
260 restObject.setStatusCode (status);
\r
262 if ( status >= 200 && status <= 299 ) {
\r
263 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + " REST api POST was successful!");
\r
264 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " REST api POST was successful!");
\r
267 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with status="+status+", url="+url);
\r
270 } catch (Exception e)
\r
272 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with url="+url+ ", Exception: " + e.toString());
\r
280 * @see org.openecomp.vid.mso.MsoRestInterfaceIfc#Put(java.lang.Object, org.openecomp.vid.mso.rest.RequestDetails, java.lang.String, java.lang.String, org.openecomp.vid.mso.RestObject)
\r
282 @SuppressWarnings("unchecked")
\r
283 public <T> void Put(T t, ChangeManagementRequest r, String sourceID, String path, RestObject<T> restObject) throws Exception {
\r
285 String methodName = "Put";
\r
288 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " start");
\r
295 url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
\r
296 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " sending request to url= " + url);
\r
297 // Change the content length
\r
298 final Response cres = client.target(url)
\r
300 .accept("application/json")
\r
301 .headers(commonHeaders)
\r
302 //.header("content-length", 201)
\r
303 //.header("X-FromAppId", sourceID)
\r
304 .put(Entity.entity(r, MediaType.APPLICATION_JSON));
\r
307 t = (T) cres.readEntity(t.getClass());
\r
310 catch ( Exception e ) {
\r
311 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " No response entity, this is probably ok, e="
\r
315 int status = cres.getStatus();
\r
316 restObject.setStatusCode (status);
\r
318 if ( status >= 200 && status <= 299 ) {
\r
319 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + " REST api POST was successful!");
\r
320 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " REST api POST was successful!");
\r
323 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with status="+status+", url="+url);
\r
326 } catch (Exception e)
\r
328 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with url="+url+ ", Exception: " + e.toString());
\r
336 * Gets the single instance of MsoRestInterface.
\r
338 * @param <T> the generic type
\r
339 * @param clazz the clazz
\r
340 * @return single instance of MsoRestInterface
\r
341 * @throws IllegalAccessException the illegal access exception
\r
342 * @throws InstantiationException the instantiation exception
\r
344 public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
\r
346 return clazz.newInstance();
\r