2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.adapter.rest.impl;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
30 import java.util.Properties;
32 import java.util.regex.Pattern;
34 import org.openecomp.appc.Constants;
35 import org.openecomp.appc.adapter.rest.RestAdapter;
36 import org.openecomp.appc.configuration.Configuration;
37 import org.openecomp.appc.configuration.ConfigurationFactory;
38 import org.openecomp.appc.exceptions.APPCException;
39 import org.openecomp.appc.exceptions.UnknownProviderException;
40 import org.openecomp.appc.i18n.Msg;
41 import org.openecomp.appc.pool.Pool;
42 import org.openecomp.appc.pool.PoolExtensionException;
43 import org.openecomp.appc.util.StructuredPropertyHelper;
44 import org.openecomp.appc.util.StructuredPropertyHelper.Node;
45 import com.att.cdp.exceptions.ContextConnectionException;
46 import com.att.cdp.exceptions.ResourceNotFoundException;
47 import com.att.cdp.exceptions.TimeoutException;
48 import com.att.cdp.exceptions.ZoneException;
49 import com.att.cdp.pal.util.StringHelper;
50 import com.att.cdp.zones.ComputeService;
51 import com.att.cdp.zones.Context;
52 import com.att.cdp.zones.ImageService;
53 import com.att.cdp.zones.Provider;
54 import com.att.cdp.zones.model.Image;
55 import com.att.cdp.zones.model.Server;
56 import com.att.cdp.zones.model.Server.Status;
57 import com.att.cdp.zones.model.ServerBootSource;
58 import com.att.eelf.configuration.EELFLogger;
59 import com.att.eelf.configuration.EELFManager;
60 import com.att.eelf.i18n.EELFResourceManager;
61 import org.openecomp.sdnc.sli.SvcLogicContext;
63 import org.glassfish.grizzly.http.util.HttpStatus;
66 import java.net.InetAddress;
67 import java.util.Locale;
68 import java.util.UUID;
69 import static com.att.eelf.configuration.Configuration.*;
71 import org.apache.http.*;
72 import org.apache.http.client.*;
73 import org.apache.http.client.methods.*;
74 import org.apache.http.impl.client.*;
75 import org.apache.http.util.EntityUtils;
76 import java.io.IOException;
77 import org.apache.http.entity.StringEntity;
79 import java.net.InetAddress;
83 * This class implements the {@link RestAdapter} interface. This interface
84 * defines the behaviors that our service provides.
86 public class RestAdapterImpl implements RestAdapter {
89 * The constant used to define the adapter name in the mapped diagnostic
94 @SuppressWarnings("nls")
95 public static final String MDC_ADAPTER = "adapter";
98 * The constant used to define the service name in the mapped diagnostic
101 @SuppressWarnings("nls")
102 public static final String MDC_SERVICE = "service";
105 * The constant for the status code for a failed outcome
107 @SuppressWarnings("nls")
108 public static final String OUTCOME_FAILURE = "failure";
111 * The constant for the status code for a successful outcome
113 @SuppressWarnings("nls")
114 public static final String OUTCOME_SUCCESS = "success";
117 * A constant for the property token "provider" used in the structured
118 * property specifications
120 @SuppressWarnings("nls")
121 public static final String PROPERTY_PROVIDER = "provider";
124 * A constant for the property token "identity" used in the structured
125 * property specifications
127 @SuppressWarnings("nls")
128 public static final String PROPERTY_PROVIDER_IDENTITY = "identity";
131 * A constant for the property token "name" used in the structured property
134 @SuppressWarnings("nls")
135 public static final String PROPERTY_PROVIDER_NAME = "name";
138 * A constant for the property token "tenant" used in the structured
139 * property specifications
141 @SuppressWarnings("nls")
142 public static final String PROPERTY_PROVIDER_TENANT = "tenant";
145 * A constant for the property token "tenant name" used in the structured
146 * property specifications
148 @SuppressWarnings("nls")
149 public static final String PROPERTY_PROVIDER_TENANT_NAME = "name";
152 * A constant for the property token "password" used in the structured
153 * property specifications
155 @SuppressWarnings("nls")
156 public static final String PROPERTY_PROVIDER_TENANT_PASSWORD = "password"; // NOSONAR
159 * A constant for the property token "userid" used in the structured
160 * property specifications
162 @SuppressWarnings("nls")
163 public static final String PROPERTY_PROVIDER_TENANT_USERID = "userid";
166 * A constant for the property token "type" used in the structured property
169 @SuppressWarnings("nls")
170 public static final String PROPERTY_PROVIDER_TYPE = "type";
173 * The name of the service to restart a server
175 @SuppressWarnings("nls")
176 public static final String PING_SERVICE = "pingServer";
179 * The logger to be used
181 private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestAdapterImpl.class);
184 * The constant for a left parenthesis
186 private static final char LPAREN = '(';
189 * The constant for a new line control code
191 private static final char NL = '\n';
194 * The constant for a single quote
196 private static final char QUOTE = '\'';
199 * The constant for a right parenthesis
201 private static final char RPAREN = ')';
204 * The constant for a space
206 private static final char SPACE = ' ';
209 * A reference to the adapter configuration object.
211 private Configuration configuration;
214 * A cache of providers that are predefined.
216 // private Map<String /* provider name */, ProviderCache> providerCache;
219 * This default constructor is used as a work around because the activator
220 * wasnt getting called
223 * A cache of providers that are predefined.
225 // private Map<String /* provider name */, ProviderCache> providerCache;
228 * This default constructor is used as a work around because the activator
229 * wasnt getting called
231 public RestAdapterImpl() {
237 * This constructor is used primarily in the test cases to bypass
238 * initialization of the adapter for isolated, disconnected testing
241 * True if the adapter is to be initialized, can false if not
243 public RestAdapterImpl(boolean initialize) {
244 configuration = ConfigurationFactory.getConfiguration();
255 public RestAdapterImpl(Properties props) {
261 * Returns the symbolic name of the adapter
263 * @return The adapter name
264 * @see org.openecomp.appc.adapter.rest.RestAdapter#getAdapterName()
267 public String getAdapterName() {
268 return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
271 public HttpRequestBase addHeaders(HttpRequestBase method,String headers){
272 if(headers.length()==0)
276 JSONObject JsonHeaders= new JSONObject(headers);
277 Iterator keys = JsonHeaders.keys();
278 while(keys.hasNext()) {
279 String String1 = (String)keys.next();
280 String String2 = JsonHeaders.getString(String1);
281 method.addHeader(String1,String2);
288 public void commonGet(Map<String, String> params, SvcLogicContext ctx) {
289 logger.info("Run get method");
290 String haveHeader="false";
291 String tUrl=params.get("org.openecomp.appc.instance.URI");
292 haveHeader=params.get("org.openecomp.appc.instance.haveHeader");
293 String headers=params.get("org.openecomp.appc.instance.headers");
294 RequestContext rc = new RequestContext(ctx);
298 HttpGet httpGet = new HttpGet(tUrl);
300 if(haveHeader.equals("true"))
302 JSONObject JsonHeaders= new JSONObject(headers);
303 Iterator keys = JsonHeaders.keys();
304 while(keys.hasNext()) {
305 String String1 = (String)keys.next();
306 String String2 = JsonHeaders.getString(String1);
307 httpGet.addHeader(String1,String2);
312 HttpClient httpClient = HttpClients.createDefault();
313 HttpResponse response = null;
314 response = httpClient.execute(httpGet);
315 int responseCode=response.getStatusLine().getStatusCode();
316 HttpEntity entity = response.getEntity();
317 String responseOutput=EntityUtils.toString(entity);
318 doSuccess(rc,responseCode,responseOutput);
319 } catch (Exception ex) {
320 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
325 public void commonDelete(Map<String, String> params, SvcLogicContext ctx) {
326 logger.info("Run Delete method");
327 String haveHeader="false";
328 String tUrl=params.get("org.openecomp.appc.instance.URI");
329 haveHeader=params.get("org.openecomp.appc.instance.haveHeader");
330 String headers=params.get("org.openecomp.appc.instance.headers");
331 RequestContext rc = new RequestContext(ctx);
335 HttpDelete httpDelete = new HttpDelete(tUrl);
336 if(haveHeader.equals("true"))
338 JSONObject JsonHeaders= new JSONObject(headers);
339 Iterator keys = JsonHeaders.keys();
340 while(keys.hasNext()) {
341 String String1 = (String)keys.next();
342 String String2 = JsonHeaders.getString(String1);
343 httpDelete.addHeader(String1,String2);
347 HttpClient httpClient = HttpClients.createDefault();
348 HttpResponse response = null;
349 response = httpClient.execute(httpDelete);
350 int responseCode=response.getStatusLine().getStatusCode();
351 HttpEntity entity = response.getEntity();
352 String responseOutput=EntityUtils.toString(entity);
353 doSuccess(rc,responseCode,responseOutput);
354 } catch (Exception ex) {
355 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
361 public void commonPost(Map<String, String> params, SvcLogicContext ctx) {
362 logger.info("Run post method");
363 String haveHeader="false";
364 String tUrl=params.get("org.openecomp.appc.instance.URI");
365 String body=params.get("org.openecomp.appc.instance.requestBody");
366 haveHeader=params.get("org.openecomp.appc.instance.haveHeader");
367 String headers=params.get("org.openecomp.appc.instance.headers");
368 RequestContext rc = new RequestContext(ctx);
372 HttpPost httpPost = new HttpPost(tUrl);
373 if(haveHeader.equals("true"))
375 JSONObject JsonHeaders= new JSONObject(headers);
376 Iterator keys = JsonHeaders.keys();
377 while(keys.hasNext()) {
378 String String1 = (String)keys.next();
379 String String2 = JsonHeaders.getString(String1);
380 httpPost.addHeader(String1,String2);
384 StringEntity bodyParams =new StringEntity (body,"UTF-8");
385 httpPost.setEntity(bodyParams);
386 HttpClient httpClient = HttpClients.createDefault();
387 HttpResponse response = null;
388 response = httpClient.execute(httpPost);
389 int responseCode=response.getStatusLine().getStatusCode();
390 HttpEntity entity = response.getEntity();
391 String responseOutput=EntityUtils.toString(entity);
392 doSuccess(rc,responseCode,responseOutput);
393 } catch (Exception ex) {
394 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
398 public void commonPut(Map<String, String> params, SvcLogicContext ctx) {
399 logger.info("Run put method");
400 String haveHeader="false";
401 String tUrl=params.get("org.openecomp.appc.instance.URI");
402 String body=params.get("org.openecomp.appc.instance.requestBody");
403 haveHeader=params.get("org.openecomp.appc.instance.haveHeader");
404 String headers=params.get("org.openecomp.appc.instance.headers");
405 RequestContext rc = new RequestContext(ctx);
409 HttpPut httpPut = new HttpPut(tUrl);
410 if(haveHeader.equals("true"))
412 JSONObject JsonHeaders= new JSONObject(headers);
413 Iterator keys = JsonHeaders.keys();
414 while(keys.hasNext()) {
415 String String1 = (String)keys.next();
416 String String2 = JsonHeaders.getString(String1);
417 httpPut.addHeader(String1,String2);
421 StringEntity bodyParams =new StringEntity (body,"UTF-8");
422 httpPut.setEntity(bodyParams);
423 HttpClient httpClient = HttpClients.createDefault();
424 HttpResponse response = null;
425 response = httpClient.execute(httpPut);
426 int responseCode=response.getStatusLine().getStatusCode();
427 HttpEntity entity = response.getEntity();
428 String responseOutput=EntityUtils.toString(entity);
429 if(responseCode == 200){
430 doSuccess(rc,responseCode,responseOutput);
432 doFailure(rc, HttpStatus.getHttpStatus(responseCode), response.getStatusLine().getReasonPhrase());
435 catch (Exception ex) {
436 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
438 /* } catch (Exception ex) {
439 doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.getMessage());
443 @SuppressWarnings("static-method")
444 private void doFailure(RequestContext rc, HttpStatus code, String message) {
445 SvcLogicContext svcLogic = rc.getSvcLogicContext();
446 String msg = (message == null) ? code.getReasonPhrase() : message;
447 if (msg.contains("\n")) {
448 msg = msg.substring(msg.indexOf("\n"));
453 status = Integer.toString(code.getStatusCode());
454 } catch (Exception e) {
457 svcLogic.setStatus(OUTCOME_FAILURE);
458 svcLogic.setAttribute(Constants.ATTRIBUTE_ERROR_CODE, status);
459 svcLogic.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
460 svcLogic.setAttribute("org.openecomp.rest.result.code", status);
461 svcLogic.setAttribute("org.openecomp.rest.result.message", msg);
467 * The request context that manages the state and recovery of the
468 * request for the life of its processing.
470 @SuppressWarnings("static-method")
471 private void doSuccess(RequestContext rc, int code, String message) {
472 SvcLogicContext svcLogic = rc.getSvcLogicContext();
473 svcLogic.setStatus(OUTCOME_SUCCESS);
474 svcLogic.setAttribute(Constants.ATTRIBUTE_ERROR_CODE, Integer.toString(HttpStatus.OK_200.getStatusCode()));
475 svcLogic.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, message);
476 svcLogic.setAttribute("org.openecomp.rest.agent.result.code",Integer.toString(code));
477 svcLogic.setAttribute("org.openecomp.rest.agent.result.message",message);
478 svcLogic.setAttribute("org.openecomp.rest.result.code",Integer.toString(HttpStatus.OK_200.getStatusCode()));
483 * initialize the provider adapter by building the context cache
485 private void initialize() {
486 configuration = ConfigurationFactory.getConfiguration();
488 logger.info("init rest adapter!!!!!");