2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.vid.mso;
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.Collections;
27 import java.util.Date;
29 import javax.ws.rs.client.Client;
30 import javax.ws.rs.client.Entity;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.MultivaluedHashMap;
33 import javax.ws.rs.core.Response;
35 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
36 import org.openecomp.portalsdk.core.util.SystemProperties;
38 import org.apache.commons.codec.binary.Base64;
39 import org.openecomp.vid.client.HttpBasicClient;
40 import org.openecomp.vid.client.HttpsBasicClient;
41 import org.openecomp.vid.encryption.EncryptedPropValue;
42 import org.openecomp.vid.mso.rest.RequestDetails;
45 * The Class MsoRestInterface.
47 public class MsoRestInterface extends MsoRestInt implements MsoRestInterfaceIfc {
50 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MsoRestInterface.class);
52 /** The Constant dateFormat. */
53 final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
56 private static Client client = null;
58 /** The common headers. */
59 private MultivaluedHashMap<String, Object> commonHeaders;
62 * Instantiates a new mso rest interface.
64 public MsoRestInterface() {
69 * @see org.openecomp.vid.mso.MsoRestInterfaceIfc#initRestClient()
71 public void initRestClient()
73 final String methodname = "initRestClient()";
75 final String username = SystemProperties.getProperty(MsoProperties.MSO_USER_NAME);
76 final String password = SystemProperties.getProperty(MsoProperties.MSO_PASSWORD);
77 final String mso_url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL);
78 final String decrypted_password = EncryptedPropValue.decryptTriple(password);
80 String authString = username + ":" + decrypted_password;
82 byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
83 String authStringEnc = new String(authEncBytes);
85 commonHeaders = new MultivaluedHashMap<String, Object> ();
86 commonHeaders.put("Authorization", Collections.singletonList((Object) ("Basic " + authStringEnc)));
88 boolean use_ssl = true;
89 if ( (mso_url != null) && ( !(mso_url.isEmpty()) ) ) {
90 if ( mso_url.startsWith("https")) {
101 //logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + methodname + " getting HttpsBasicClient with username=" + username
102 // + " password=" + password);
103 client = HttpsBasicClient.getClient();
106 //logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + methodname + " getting HttpsBasicClient with username=" + username
107 // + " password=" + password);
108 client = HttpBasicClient.getClient();
110 } catch (Exception e) {
111 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + methodname + " Unable to get the SSL client");
117 * @see org.openecomp.vid.mso.MsoRestInterfaceIfc#Get(java.lang.Object, java.lang.String, java.lang.String, org.openecomp.vid.mso.RestObject)
119 @SuppressWarnings("unchecked")
120 public <T> void Get (T t, String sourceId, String path, RestObject<T> restObject ) throws Exception {
121 String methodName = "Get";
123 logger.debug(EELFLoggerDelegate.debugLogger, methodName + " start");
128 url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
129 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " sending request to url= " + url);
133 final Response cres = client.target(url)
135 .accept("application/json")
136 .headers(commonHeaders)
139 int status = cres.getStatus();
140 restObject.setStatusCode (status);
143 t = (T) cres.readEntity(t.getClass());
145 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + methodName + " REST api was successfull!");
148 throw new Exception(methodName + " with status="+ status + ", url= " + url );
151 logger.debug(EELFLoggerDelegate.debugLogger,methodName + " received status=" + status );
157 * @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)
159 @SuppressWarnings("unchecked")
160 public <T> void Delete(T t, RequestDetails r, String sourceID, String path, RestObject<T> restObject) {
162 String methodName = "Delete";
164 Response cres = null;
166 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " start");
172 url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
173 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + " methodName sending request to: " + url);
175 cres = client.target(url)
177 .accept("application/json")
178 .headers(commonHeaders)
180 .build("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON)).invoke();
181 // .method("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON));
182 //.delete(Entity.entity(r, MediaType.APPLICATION_JSON));
184 int status = cres.getStatus();
185 restObject.setStatusCode (status);
187 if (status == 404) { // resource not found
188 String msg = "Resource does not exist...: " + cres.getStatus();
189 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);
190 } else if (status == 200 || status == 204){
191 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + "Resource " + url + " deleted");
192 } else if (status == 202) {
193 String msg = "Delete in progress: " + status;
194 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);
197 String msg = "Deleting Resource failed: " + status;
198 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + msg);
202 t = (T) cres.readEntity(t.getClass());
205 catch ( Exception e ) {
206 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " No response entity, this is probably ok, e="
213 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with url="+url+ ", Exception: " + e.toString());
220 * @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)
222 @SuppressWarnings("unchecked")
223 public <T> void Post(T t, RequestDetails r, String sourceID, String path, RestObject<T> restObject) throws Exception {
225 String methodName = "Post";
228 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " start");
235 url = SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL) + path;
236 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " sending request to url= " + url);
237 // Change the content length
238 final Response cres = client.target(url)
240 .accept("application/json")
241 .headers(commonHeaders)
242 //.header("content-length", 201)
243 //.header("X-FromAppId", sourceID)
244 .post(Entity.entity(r, MediaType.APPLICATION_JSON));
247 t = (T) cres.readEntity(t.getClass());
250 catch ( Exception e ) {
251 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " No response entity, this is probably ok, e="
255 int status = cres.getStatus();
256 restObject.setStatusCode (status);
258 if ( status >= 200 && status <= 299 ) {
259 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + " REST api POST was successful!");
260 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " REST api POST was successful!");
263 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with status="+status+", url="+url);
266 } catch (Exception e)
268 logger.debug(EELFLoggerDelegate.debugLogger,dateFormat.format(new Date()) + "<== " + methodName + " with url="+url+ ", Exception: " + e.toString());
276 * Gets the single instance of MsoRestInterface.
278 * @param <T> the generic type
279 * @param clazz the clazz
280 * @return single instance of MsoRestInterface
281 * @throws IllegalAccessException the illegal access exception
282 * @throws InstantiationException the instantiation exception
284 public <T> T getInstance(Class<T> clazz) throws IllegalAccessException, InstantiationException
286 return clazz.newInstance();