aboutsummaryrefslogtreecommitdiffstats
path: root/appc-adapters/appc-rest-adapter/appc-rest-adapter-bundle/src/main/java/org/openecomp/appc/adapter/rest/impl/RestAdapterImpl.java
blob: 82aad7d6f96e34b04c005115de7d0f03d4b39f77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : APP-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 						reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.appc.adapter.rest.impl;

import java.net.URI;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Pattern;

import org.openecomp.appc.Constants;
import org.openecomp.appc.adapter.rest.RestAdapter;
import org.openecomp.appc.configuration.Configuration;
import org.openecomp.appc.configuration.ConfigurationFactory;
import org.openecomp.appc.exceptions.APPCException;
import org.openecomp.appc.exceptions.UnknownProviderException;
import org.openecomp.appc.i18n.Msg;
import org.openecomp.appc.pool.Pool;
import org.openecomp.appc.pool.PoolExtensionException;
import org.openecomp.appc.util.StructuredPropertyHelper;
import org.openecomp.appc.util.StructuredPropertyHelper.Node;
import com.att.cdp.exceptions.ContextConnectionException;
import com.att.cdp.exceptions.ResourceNotFoundException;
import com.att.cdp.exceptions.TimeoutException;
import com.att.cdp.exceptions.ZoneException;
import com.att.cdp.pal.util.StringHelper;
import com.att.cdp.zones.ComputeService;
import com.att.cdp.zones.Context;
import com.att.cdp.zones.ImageService;
import com.att.cdp.zones.Provider;
import com.att.cdp.zones.model.Image;
import com.att.cdp.zones.model.Server;
import com.att.cdp.zones.model.Server.Status;
import com.att.cdp.zones.model.ServerBootSource;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.att.eelf.i18n.EELFResourceManager;
import org.openecomp.sdnc.sli.SvcLogicContext;

import org.glassfish.grizzly.http.util.HttpStatus;
import org.slf4j.MDC;

import java.net.InetAddress;
import java.util.Locale;
import java.util.UUID;
import static com.att.eelf.configuration.Configuration.*;

import org.apache.http.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import org.apache.http.entity.StringEntity;

import java.net.InetAddress;
import org.json.*;

/**
 * This class implements the {@link RestAdapter} interface. This interface
 * defines the behaviors that our service provides.
 */
public class RestAdapterImpl implements RestAdapter {

	/**
	 * The constant used to define the adapter name in the mapped diagnostic
	 * context
	 */
	

	@SuppressWarnings("nls")
	public static final String MDC_ADAPTER = "adapter";

	/**
	 * The constant used to define the service name in the mapped diagnostic
	 * context
	 */
	@SuppressWarnings("nls")
	public static final String MDC_SERVICE = "service";

	/**
	 * The constant for the status code for a failed outcome
	 */
	@SuppressWarnings("nls")
	public static final String OUTCOME_FAILURE = "failure";

	/**
	 * The constant for the status code for a successful outcome
	 */
	@SuppressWarnings("nls")
	public static final String OUTCOME_SUCCESS = "success";

	/**
	 * A constant for the property token "provider" used in the structured
	 * property specifications
	 */
	@SuppressWarnings("nls")
	public static final String PROPERTY_PROVIDER = "provider";

	/**
	 * A constant for the property token "identity" used in the structured
	 * property specifications
	 */
	@SuppressWarnings("nls")
	public static final String PROPERTY_PROVIDER_IDENTITY = "identity";

	/**
	 * A constant for the property token "name" used in the structured property
	 * specifications
	 */
	@SuppressWarnings("nls")
	public static final String PROPERTY_PROVIDER_NAME = "name";

	/**
	 * A constant for the property token "tenant" used in the structured
	 * property specifications
	 */
	@SuppressWarnings("nls")
	public static final String PROPERTY_PROVIDER_TENANT = "tenant";

	/**
	 * A constant for the property token "tenant name" used in the structured
	 * property specifications
	 */
	@SuppressWarnings("nls")
	public static final String PROPERTY_PROVIDER_TENANT_NAME = "name";

	/**
	 * A constant for the property token "password" used in the structured
	 * property specifications
	 */
	@SuppressWarnings("nls")
	public static final String PROPERTY_PROVIDER_TENANT_PASSWORD = "password"; // NOSONAR

	/**
	 * A constant for the property token "userid" used in the structured
	 * property specifications
	 */
	@SuppressWarnings("nls")
	public static final String PROPERTY_PROVIDER_TENANT_USERID = "userid";

	/**
	 * A constant for the property token "type" used in the structured property
	 * specifications
	 */
	@SuppressWarnings("nls")
	public static final String PROPERTY_PROVIDER_TYPE = "type";

	/**
	 * The name of the service to restart a server
	 */
	@SuppressWarnings("nls")
	public static final String PING_SERVICE = "pingServer";

	/**
	 * The logger to be used
	 */
	private static final EELFLogger logger = EELFManager.getInstance().getLogger(RestAdapterImpl.class);

	/**
	 * The constant for a left parenthesis
	 */
	private static final char LPAREN = '(';

	/**
	 * The constant for a new line control code
	 */
	private static final char NL = '\n';

	/**
	 * The constant for a single quote
	 */
	private static final char QUOTE = '\'';

	/**
	 * The constant for a right parenthesis
	 */
	private static final char RPAREN = ')';

	/**
	 * The constant for a space
	 */
	private static final char SPACE = ' ';

	/**
	 * A reference to the adapter configuration object.
	 */
	private Configuration configuration;

	/**
	 * A cache of providers that are predefined.
	 */
	// private Map<String /* provider name */, ProviderCache> providerCache;

	/**
	 * This default constructor is used as a work around because the activator
	 * wasnt getting called
	 */
	/**
	 * A cache of providers that are predefined.
	 */
	// private Map<String /* provider name */, ProviderCache> providerCache;

	/**
	 * This default constructor is used as a work around because the activator
	 * wasnt getting called
	 */
	public RestAdapterImpl() {
		initialize();

	}

	/**
	 * This constructor is used primarily in the test cases to bypass
	 * initialization of the adapter for isolated, disconnected testing
	 * 
	 * @param initialize
	 *            True if the adapter is to be initialized, can false if not
	 */
	public RestAdapterImpl(boolean initialize) {
		configuration = ConfigurationFactory.getConfiguration();
		if (initialize) {
			initialize();

		}
	}

	/**
	 * @param props
	 *            not used
	 */
	public RestAdapterImpl(Properties props) {
		initialize();

	}

	/**
	 * Returns the symbolic name of the adapter
	 * 
	 * @return The adapter name
	 * @see org.openecomp.appc.adapter.rest.RestAdapter#getAdapterName()
	 */
	@Override
	public String getAdapterName() {
		return configuration.getProperty(Constants.PROPERTY_ADAPTER_NAME);
	}

	public HttpRequestBase addHeaders(HttpRequestBase method,String headers){
		if(headers.length()==0)
			{
				return method;
			}else{
				JSONObject JsonHeaders= new JSONObject(headers);
				Iterator keys = JsonHeaders.keys();
				while(keys.hasNext()) {
			        String String1 = (String)keys.next();
			        String String2 = JsonHeaders.getString(String1);
			        method.addHeader(String1,String2);
			    }
				return method;
			}
	}
	
	
	public void commonGet(Map<String, String> params, SvcLogicContext ctx) {
		logger.info("Run get method");
		String haveHeader="false";
		String tUrl=params.get("org.openecomp.appc.instance.URI");
		haveHeader=params.get("org.openecomp.appc.instance.haveHeader");
		String headers=params.get("org.openecomp.appc.instance.headers");
		RequestContext rc = new RequestContext(ctx);
		rc.isAlive();
	
		try {
			HttpGet httpGet = new HttpGet(tUrl);
			
			if(haveHeader.equals("true"))
			{
				JSONObject JsonHeaders= new JSONObject(headers);
				Iterator keys = JsonHeaders.keys();
				while(keys.hasNext()) {
			        String String1 = (String)keys.next();
			        String String2 = JsonHeaders.getString(String1);
			        httpGet.addHeader(String1,String2);
			    }

			}
			
			HttpClient httpClient = HttpClients.createDefault();
			HttpResponse response = null;
			response = httpClient.execute(httpGet);
			int responseCode=response.getStatusLine().getStatusCode();
			HttpEntity entity = response.getEntity();
			String responseOutput=EntityUtils.toString(entity);
			doSuccess(rc,responseCode,responseOutput);
		} catch (Exception ex) {
			doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
		}
	}

	
	public void commonDelete(Map<String, String> params, SvcLogicContext ctx) {
		logger.info("Run Delete method");
		String haveHeader="false";
		String tUrl=params.get("org.openecomp.appc.instance.URI");
		haveHeader=params.get("org.openecomp.appc.instance.haveHeader");
		String headers=params.get("org.openecomp.appc.instance.headers");
		RequestContext rc = new RequestContext(ctx);
		rc.isAlive();

		try {
			HttpDelete httpDelete = new HttpDelete(tUrl);
			if(haveHeader.equals("true"))
			{
				JSONObject JsonHeaders= new JSONObject(headers);
				Iterator keys = JsonHeaders.keys();
				while(keys.hasNext()) {
			        String String1 = (String)keys.next();
			        String String2 = JsonHeaders.getString(String1);
			        httpDelete.addHeader(String1,String2);
			    }

			}
			HttpClient httpClient = HttpClients.createDefault();
			HttpResponse response = null;
			response = httpClient.execute(httpDelete);
			int responseCode=response.getStatusLine().getStatusCode();
			HttpEntity entity = response.getEntity();
			String responseOutput=EntityUtils.toString(entity);
			doSuccess(rc,responseCode,responseOutput);
		} catch (Exception ex) {
			doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
		}
	}
	
	
	
	public void commonPost(Map<String, String> params, SvcLogicContext ctx) {
		logger.info("Run post method");
		String haveHeader="false";
		String tUrl=params.get("org.openecomp.appc.instance.URI");
		String body=params.get("org.openecomp.appc.instance.requestBody");
		haveHeader=params.get("org.openecomp.appc.instance.haveHeader");
		String headers=params.get("org.openecomp.appc.instance.headers");
		RequestContext rc = new RequestContext(ctx);
		rc.isAlive();
		
		try {
			HttpPost httpPost = new HttpPost(tUrl);
			if(haveHeader.equals("true"))
			{
				JSONObject JsonHeaders= new JSONObject(headers);
				Iterator keys = JsonHeaders.keys();
				while(keys.hasNext()) {
			        String String1 = (String)keys.next();
			        String String2 = JsonHeaders.getString(String1);
			        httpPost.addHeader(String1,String2);
			    }

			}
			StringEntity bodyParams =new StringEntity (body,"UTF-8");
			httpPost.setEntity(bodyParams);
			HttpClient httpClient = HttpClients.createDefault();
			HttpResponse response = null;
			response = httpClient.execute(httpPost);
			int responseCode=response.getStatusLine().getStatusCode();
			HttpEntity entity = response.getEntity();
			String responseOutput=EntityUtils.toString(entity);
			doSuccess(rc,responseCode,responseOutput);
		} catch (Exception ex) {
			doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
		}
	}
	
	public void commonPut(Map<String, String> params, SvcLogicContext ctx) {
		logger.info("Run put method");
		String haveHeader="false";
		String tUrl=params.get("org.openecomp.appc.instance.URI");
		String body=params.get("org.openecomp.appc.instance.requestBody");
		haveHeader=params.get("org.openecomp.appc.instance.haveHeader");
		String headers=params.get("org.openecomp.appc.instance.headers");
		RequestContext rc = new RequestContext(ctx);
		rc.isAlive();
		
	try {
			HttpPut httpPut = new HttpPut(tUrl);
			if(haveHeader.equals("true"))
			{
				JSONObject JsonHeaders= new JSONObject(headers);
				Iterator keys = JsonHeaders.keys();
				while(keys.hasNext()) {
			        String String1 = (String)keys.next();
			        String String2 = JsonHeaders.getString(String1);
			        httpPut.addHeader(String1,String2);
			    }

			}
			StringEntity bodyParams =new StringEntity (body,"UTF-8");
			httpPut.setEntity(bodyParams);
			HttpClient httpClient = HttpClients.createDefault();
			HttpResponse response = null;
			response = httpClient.execute(httpPut);
			int responseCode=response.getStatusLine().getStatusCode();
			HttpEntity entity = response.getEntity();
			String responseOutput=EntityUtils.toString(entity);
			if(responseCode == 200){
			doSuccess(rc,responseCode,responseOutput);
			} else {
				doFailure(rc, HttpStatus.getHttpStatus(responseCode), response.getStatusLine().getReasonPhrase());
			}
			} 
			catch (Exception ex) {
				doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.toString());
			}
/*		} catch (Exception ex) {
			doFailure(rc, HttpStatus.INTERNAL_SERVER_ERROR_500, ex.getMessage());
		}*/
	}
	
	@SuppressWarnings("static-method")
	private void doFailure(RequestContext rc, HttpStatus code, String message) {
		SvcLogicContext svcLogic = rc.getSvcLogicContext();
		String msg = (message == null) ? code.getReasonPhrase() : message;
		if (msg.contains("\n")) {
			msg = msg.substring(msg.indexOf("\n"));
		}

		String status;
		try {
			status = Integer.toString(code.getStatusCode());
		} catch (Exception e) {
			status = "500";
		}
		svcLogic.setStatus(OUTCOME_FAILURE);
		svcLogic.setAttribute(Constants.ATTRIBUTE_ERROR_CODE, status);
        svcLogic.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, msg);
		svcLogic.setAttribute("org.openecomp.rest.result.code", status);
		svcLogic.setAttribute("org.openecomp.rest.result.message", msg);
	}
	

	/**
	 * @param rc
	 *            The request context that manages the state and recovery of the
	 *            request for the life of its processing.
	 */
	@SuppressWarnings("static-method")
	private void doSuccess(RequestContext rc, int code, String message) {
		SvcLogicContext svcLogic = rc.getSvcLogicContext();
		svcLogic.setStatus(OUTCOME_SUCCESS);
		svcLogic.setAttribute(Constants.ATTRIBUTE_ERROR_CODE, Integer.toString(HttpStatus.OK_200.getStatusCode()));
        svcLogic.setAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE, message);
		svcLogic.setAttribute("org.openecomp.rest.agent.result.code",Integer.toString(code));
		svcLogic.setAttribute("org.openecomp.rest.agent.result.message",message);
		svcLogic.setAttribute("org.openecomp.rest.result.code",Integer.toString(HttpStatus.OK_200.getStatusCode()));
	}
	

	/**
	 * initialize the provider adapter by building the context cache
	 */
	private void initialize() {
		configuration = ConfigurationFactory.getConfiguration();
	
		logger.info("init rest adapter!!!!!");
	}

}