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.aai.util;
23 import java.io.UnsupportedEncodingException;
24 import java.net.MalformedURLException;
26 import java.net.URISyntaxException;
28 import java.net.URLEncoder;
29 import java.security.KeyManagementException;
30 import java.text.DateFormat;
31 import java.text.SimpleDateFormat;
32 import java.util.Base64;
33 import java.util.Date;
34 import java.util.UUID;
36 import javax.ws.rs.client.Client;
37 import javax.ws.rs.client.Entity;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.Response;
41 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
42 import org.openecomp.portalsdk.core.util.SystemProperties;
43 import org.openecomp.vid.encryption.EncryptedPropValue;
46 * The Class AAIRestInterface.
48 public class AAIRestInterface {
51 EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(AAIRestInterface.class);
53 /** The Constant dateFormat. */
54 final static DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss:SSSS");
57 private static Client client = null;
59 /** The rest srvr base URL. */
60 private String restSrvrBaseURL;
62 /** The certificate path. */
63 public String certificatePath = "";
66 * Instantiates a new AAI rest interface.
68 * @param certPath the cert path
70 public AAIRestInterface(String certPath)
72 certificatePath = certPath;
78 * @param nodeKey the node key
80 * @throws UnsupportedEncodingException the unsupported encoding exception
82 public String encodeURL (String nodeKey) throws UnsupportedEncodingException {
83 return URLEncoder.encode(nodeKey, "UTF-8").replaceAll("\\+", "%20");
87 * Inits the rest client.
89 private void initRestClient()
91 String methodName = "initRestClient";
95 client = HttpsAuthClient.getClient(certificatePath);
97 catch (KeyManagementException e){
98 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== KeyManagementException in " + methodName + e.toString());
99 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== KeyManagementException in " + methodName + e.toString());
100 } catch (Exception e) {
101 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== Exception in REST call to DB in initRestClient" + e.toString());
102 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== Exception in REST call to DB : " + e.toString());
108 * Sets the rest srvr base URL.
110 * @param baseURL the base URL
112 public void SetRestSrvrBaseURL(String baseURL)
116 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== REST Server base URL cannot be null.");
117 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== REST Server base URL cannot be null.");
120 restSrvrBaseURL = baseURL;
124 * Gets the rest srvr base URL.
126 * @return the rest srvr base URL
128 public String getRestSrvrBaseURL()
130 return restSrvrBaseURL;
137 * @param fromAppId the from app id
138 * @param transId the trans id
139 * @param requestUri the request uri
143 public String RestGet(String fromAppId, String transId, String requestUri, boolean xml) throws UnsupportedEncodingException {
144 String methodName = "RestGet";
146 String responseType = "application/json";
148 responseType = "application/xml";
153 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
155 url = SystemProperties.getProperty(AAIProperties.AAI_SERVER_URL) + requestUri;
158 // what is the point of this, just to check syntax??
159 URL urlObj= new URL(url);
160 URI uri = new URI(urlObj.getProtocol(), urlObj.getUserInfo(), urlObj.getHost(), urlObj.getPort(), urlObj.getPath(), urlObj.getQuery(), urlObj.getRef());
161 url = uri.toASCIIString();
162 } catch (URISyntaxException | MalformedURLException e) {
163 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " bad URL");
166 logger.debug(dateFormat.format(new Date()) + "<== " + url + " for the get REST API");
168 String vidUsername = SystemProperties.getProperty(AAIProperties.AAI_VID_USERNAME);
169 String vidPassword = EncryptedPropValue.decryptTriple(SystemProperties.getProperty(AAIProperties.AAI_VID_PASSWD_X));
170 String encodeThis = vidUsername + ":" + vidPassword;
172 final Response cres = client.target(url)
174 .accept(responseType)
175 .header("X-TransactionId", transId)
176 .header("X-FromAppId", fromAppId)
177 .header("Authorization", "Basic " + Base64.getEncoder().encodeToString(encodeThis.getBytes("utf-8")))
178 .header("Content-Type", "application/json")
181 if (cres.getStatus() == 200) {
182 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " REST api GET was successful!");
183 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + " REST api GET was successful!");
185 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName +" with status="+cres.getStatus()+", url="+url);
188 return cres.readEntity(String.class);
195 * @param sourceID the source ID
196 * @param transId the trans id
197 * @param path the path
198 * @return true, if successful
200 public boolean Delete(String sourceID, String transId, String path) throws UnsupportedEncodingException {
201 String methodName = "Delete";
203 transId += ":" + UUID.randomUUID().toString();
204 logger.debug(dateFormat.format(new Date()) + "<== " + methodName + " start");
207 String request = "{}";
208 url = SystemProperties.getProperty(AAIProperties.AAI_SERVER_URL) + path;
210 String vidUsername = SystemProperties.getProperty(AAIProperties.AAI_VID_USERNAME);
211 String vidPassword = EncryptedPropValue.decryptTriple(SystemProperties.getProperty(AAIProperties.AAI_VID_PASSWD_X));
212 String encodeThis = vidUsername + ":" + vidPassword;
214 final Response cres = client.target(url)
216 .accept("application/json")
217 .header("X-TransactionId", transId)
218 .header("X-FromAppId", sourceID)
219 .header("Authorization", "Basic " + Base64.getEncoder().encodeToString(encodeThis.getBytes("utf-8")))
223 if (cres.getStatus() == 404) { // resource not found
224 String msg = "Resource does not exist...: " + cres.getStatus()
225 + ":" + cres.readEntity(String.class);
226 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + msg);
228 } else if (cres.getStatus() == 200 || cres.getStatus() == 204){
229 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + "Resource " + url + " deleted");
232 String msg = "Deleting Resource failed: " + cres.getStatus()
233 + ":" + cres.readEntity(String.class);
234 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + msg);
244 * @param fromAppId the from app id
245 * @param transId the trans id
246 * @param path the path
247 * @param payload the payload
251 public String RestPost(String fromAppId, String transId, String path, String payload, boolean xml) throws UnsupportedEncodingException {
252 String methodName = "RestPost";
254 transId = UUID.randomUUID().toString();
255 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " start");
259 String responseType = "application/json";
261 responseType = "application/xml";
265 url = SystemProperties.getProperty(AAIProperties.AAI_SERVER_URL_BASE) + path;
267 String vidUsername = SystemProperties.getProperty(AAIProperties.AAI_VID_USERNAME);
268 String vidPassword = EncryptedPropValue.decryptTriple(SystemProperties.getProperty(AAIProperties.AAI_VID_PASSWD_X));
269 String encodeThis = vidUsername + ":" + vidPassword;
271 final Response cres = client.target(url)
273 .accept(responseType)
274 .header("X-TransactionId", transId)
275 .header("X-FromAppId", fromAppId)
276 .header("Authorization", "Basic " + Base64.getEncoder().encodeToString(encodeThis.getBytes("utf-8")))
277 .post(Entity.entity(payload, MediaType.APPLICATION_JSON));
279 int statuscode = cres.getStatus();
280 if ( statuscode >= 200 && statuscode <= 299 ) {
281 logger.info(EELFLoggerDelegate.errorLogger, dateFormat.format(new Date()) + "<== " + methodName + " REST api POST was successful!");
282 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " REST api POST was successful!");
283 return cres.readEntity(String.class);
285 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " with status="+statuscode+", url="+url + ", msg=" + cres.readEntity(String.class));
289 } catch (Exception e)
291 logger.debug(EELFLoggerDelegate.debugLogger, dateFormat.format(new Date()) + "<== " + methodName + " with url="+url+ ", Exception: " + e.toString());