summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/att/ecomp/dcae/ci/utilities/SdcInternalApiClient.java
blob: 4adade1ac8a0b89c1b1d81158bcc5e752fa1e06e (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
package com.att.ecomp.dcae.ci.utilities;

import com.att.ecomp.dcae.ci.entities.composition.services.Vfi;
import com.att.ecomp.dcae.ci.entities.sdc.SdcComponent;
import com.att.ecomp.dcae.ci.entities.sdc.SdcComponentMetadata;
import com.att.ecomp.dcae.ci.ui.setup.DcaeConfiguration;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import org.openecomp.d2.ci.api.BaseRestUtils;
import org.openecomp.d2.ci.api.ElementFactory;
import org.openecomp.d2.ci.api.ServiceRestUtils;
import org.openecomp.d2.ci.api.VfRestUtils;
import org.openecomp.d2.ci.datatypes.*;
import org.openecomp.d2.ci.datatypes.http.RestResponse;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SdcInternalApiClient extends BaseRestUtils {
	
	private static Gson gson = (new GsonBuilder()).create();
	private static User defaultUser = DcaeRestClient.getDefaultUser();
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public static ServiceReqDetails createService() throws Exception {
		ServiceReqDetails defaultService = ElementFactory.getDefaultService();
		RestResponse response = ServiceRestUtils.createService(defaultService, defaultUser);
		Report.logDebug("Response:", response);
		if (response.getStatusCode() != 201) {
			throw new Exception("Unable to create service.\nResponse: " + response.toString());
		}
		return gson.fromJson(response.getResponse(), ServiceReqDetails.class);
	}
	
	/**
	 * 
	 * @return
	 * @throws Exception
	 */
	public static ResourceReqDetails createVf() throws Exception {
		ResourceReqDetails defaultVf = ElementFactory.getDefaultResource(ResourceTypeEnum.VF);
		RestResponse response = VfRestUtils.createResource(defaultVf, defaultUser);
		Report.logDebug("Response:", response);
		if (response.getStatusCode() != 201) {
			throw new Exception("Unable to create vf.\nResponse: " + response.toString());
		}
		return gson.fromJson(response.getResponse(), ResourceReqDetails.class);
	}
	
	/**
	 * 
	 * @param vf
	 * @return
	 * @throws Exception
	 */
	public static ResourceReqDetails checkinVf(ResourceReqDetails vf) throws Exception {
		RestResponse response = VfRestUtils.changeResourceState(vf, defaultUser, LifeCycleStatesEnum.CHECKIN);
		Report.logDebug("Response:", response);
		if (response.getStatusCode() != 200) {
			throw new Exception("Unable to checkin vf.\nResponse: " + response.toString());
		}
		return gson.fromJson(response.getResponse(), ResourceReqDetails.class);
	}
	
	/**
	 * 
	 * @param service
	 * @param vf
	 * @return
	 * @throws Exception
	 */
	public static Vfi createVfi(ServiceReqDetails service, ResourceReqDetails vf) throws Exception {
		RestResponse response = ServiceRestUtils.createComponentInstance(service, vf, defaultUser, ComponentTypeEnum.RESOURCE, true);
		Report.logDebug("Response:", response);
		if (response.getStatusCode() != 201) {
			throw new Exception("Unable to create vfi.\nResponse: " + response.toString());
		}
		try {
			JsonObject resBody = new JsonParser().parse(response.getResponse()).getAsJsonObject();
			String vfiName = resBody.get("name").getAsString();
			return new Vfi(vfiName, service);
		} catch (Exception err) {
			throw new Exception(String.format("Unable to parse vfi name\nResponse: %s\n", response), err);
		}
	}

	// DELETE - Clean up //

	public static RestResponse deleteAssetFromSdc(String context, String uniqueId) throws IOException {
		DcaeConfiguration configuration = ConfigurationReader.getConfiguration();
		String url = String.format("%s:%s/sdc2/rest/v1/catalog/%s/%s", configuration.getBeHost(), configuration.getBePort(), context, uniqueId);
		return sendDelete(url, ElementFactory.getDefaultUser(UserRoleEnum.ADMIN).getUserId());
	}

	public static RestResponse deleteMarkedResources() throws IOException {
		DcaeConfiguration configuration = ConfigurationReader.getConfiguration();
		String url = String.format("%s:%s/sdc2/rest/v1/inactiveComponents/resource", configuration.getBeHost(), configuration.getBePort());
		return sendDelete(url, ElementFactory.getDefaultUser(UserRoleEnum.ADMIN).getUserId());
	}

	public static RestResponse deleteMarkedServices() throws IOException {
		DcaeConfiguration configuration = ConfigurationReader.getConfiguration();
		String url = String.format("%s:%s/sdc2/rest/v1/inactiveComponents/service", configuration.getBeHost(), configuration.getBePort());
		return sendDelete(url, ElementFactory.getDefaultUser(UserRoleEnum.ADMIN).getUserId());
	}

	public static Map<String, List<SdcComponent>> getAssetsByUser(String userId) throws IOException {
		DcaeConfiguration configuration = ConfigurationReader.getConfiguration();
		String url = String.format("%s:%s/sdc2/rest/v1/followed", configuration.getBeHost(), configuration.getBePort());
		RestResponse restResponse = sendGet(url, userId);
		return 200 == restResponse.getStatusCode() ? gson.fromJson(restResponse.getResponse(), new TypeToken<Map<String, List<SdcComponent>>>(){}.getType()) : new HashMap<>();
	}


	public static SdcComponentMetadata getAssetMetadata(String context, String uniqueId, String userId) throws IOException {
		DcaeConfiguration configuration = ConfigurationReader.getConfiguration();
		String url = String.format("%s:%s/sdc2/rest/v1/catalog/%s/%s/filteredDataByParams?include=metadata", configuration.getBeHost(), configuration.getBePort(), context, uniqueId);
		RestResponse restResponse = sendGet(url, userId);
		return gson.fromJson(restResponse.getResponse(), SdcComponentMetadata.class);
	}

}