summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcae/ci/utilities/DcaeRestClient.java
blob: 88fbd4d38dd478a41353c8cc9c0767402a595b19 (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
package org.onap.dcae.ci.utilities;

import com.aventstack.extentreports.Status;
import com.google.common.net.UrlEscapers;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.commons.collections4.map.SingletonMap;
import org.json.simple.JSONObject;
import org.onap.dcae.ci.config.Configuration;
import org.onap.dcae.ci.entities.RestResponse;
import org.onap.dcae.ci.enums.HttpHeaderEnum;
import org.onap.dcae.ci.report.Report;
import org.onap.sdc.dcae.composition.restmodels.CreateVFCMTRequest;

import java.io.IOException;

public class DcaeRestClient extends BaseRestUtils {

	private static String designerDefaultId = "cs0008";
	private static String designer2UserId = "me0009";
	private static String adminDefaultId = "jh0003";
	private static String testerDefaultId = "jm0007";
	private static Configuration configuration = ConfigurationReader.getConfiguration();

    public static String getDefaultUserId() {
        return designerDefaultId;
    }

	public static String getDesigner2UserId() {
		return designer2UserId;
	}

	public static String getDefaultAdminId() {
    	return adminDefaultId;
	}

	public static String getDefaultTesterId() {
    	return testerDefaultId;
	}

    protected static String getApiUrl(String path) {

        String dcaeBePort = configuration.getDcaeBePort();
        String dcaeBeHost = configuration.getDcaeBeHost();
        String apiPath = configuration.getApiPath();
        if(System.getProperty("dcaeBeHost")!=null){
            dcaeBeHost = System.getProperty("dcaeBeHost");
            System.out.println("dcaeBeHost was configured via system property: "+dcaeBeHost);
        }
        if(System.getProperty("dcaeBePort")!=null){
            dcaeBePort = System.getProperty("dcaeBePort");
            System.out.println("dcaeBePort was configured via system property: "+dcaeBePort);
        }
        if(System.getProperty("apiPath")!=null){
            apiPath = System.getProperty("apiPath");
            System.out.println("apiPath was configured via system property: "+apiPath);
        }

        return String.format("%s:%s%s%s", dcaeBeHost, dcaeBePort, apiPath, path);
    }

    /* HealthCheck */

    public static RestResponse getHealthcheck() throws IOException {
        return sendGet(getApiUrl("/healthCheck"), null);
    }

    /* VFCMT */

    public static RestResponse getAllVfcmts() throws IOException {
        return sendGet(getApiUrl("/getResourcesByCategory"), designerDefaultId);
    }

    public static RestResponse getAllMonitoringTemplatesVfcmts() throws IOException {
        return sendGet(getApiUrl("/getResourcesByMonitoringTemplateCategory"), designerDefaultId);
    }

    public static RestResponse getVfcmtsForMigration(String contextType,String serviceUuid, String serviceVersion) throws IOException{

        return sendGet(getApiUrl("/" + contextType + "/" + serviceUuid + "/" + serviceVersion + "/getVfcmtsForMigration"), designerDefaultId);
    }

    public static RestResponse createVfcmt(String name, String description, String userId) throws IOException {
        JSONObject newVfcmtJSON = newVfcmtJSON(name, description);
        return sendPost(getApiUrl("/createVFCMT"), newVfcmtJSON.toString(), userId, "application/json");
    }

    public static RestResponse createMc(String request) throws IOException {
        return sendPost(getApiUrl("/createMC"), request, designerDefaultId, "application/json");
    }

    public static RestResponse createMc(String request,String userId) throws IOException {
        return sendPost(getApiUrl("/createMC"), request, userId, "application/json");
    }

    public static RestResponse createVfcmt(String name, String description) throws IOException{
        return createVfcmt(name, description, designerDefaultId);
    }

    public static RestResponse importMc(String request) throws IOException {
        return sendPost(getApiUrl("/importMC"), request, designerDefaultId, "application/json");
    }

    public static RestResponse getAttachedService(String vfcmtUuid) throws IOException {
        Report.log(Status.INFO, "getAttachedService for VFCMT uuid="+vfcmtUuid);
        RestResponse res = sendGet(getApiUrl("/" + vfcmtUuid + "/attachment"), designerDefaultId);
        Report.log(Status.INFO, "getAttachedService result=%s", res);
        return res;
    }

    public static RestResponse getServiceExternalReferences(String serviceUuid, String version) throws IOException {
        Report.log(Status.INFO, "getServiceExternalReferences for service uuid="+serviceUuid);
        RestResponse res = sendGet(getApiUrl("/SERVICE/" + serviceUuid + "/" + version + "/monitoringComponents"), designerDefaultId);
        Report.log(Status.INFO, "getServiceExternalReferences result=%s", res);
        return res;
    }
    /* VF */

    public static RestResponse getServices(String VFCMTId, String userId) throws IOException{
        return sendGet(getApiUrl("/services/"+VFCMTId), userId);
    }

    public static RestResponse getServicesInstance(String uuid) throws IOException{
        return sendGet(getApiUrl("/service/"+uuid), designerDefaultId);
    }

    public static RestResponse attachVfiRef(String vfcmtUuid, String serviceId, String vfiName) throws IOException{
        Report.log(Status.INFO, "attachVfiRef start");
        JSONObject jsonAttachObj = new JSONObject();
        jsonAttachObj.put("serviceUuid", serviceId);
        jsonAttachObj.put("instanceName", vfiName);

        return sendPost(getApiUrl("/" + vfcmtUuid + "/attachment"), jsonAttachObj.toString(), designerDefaultId, "application/json");
    }

    public static RestResponse getResourceDetails(String componentId) throws IOException{
        return sendGet(getApiUrl("/resource/"+ componentId), designerDefaultId);
    }

	public static RestResponse getCatalog() throws IOException{
		return sendGet(getApiUrl("/catalog"), designerDefaultId);
	}

    public static RestResponse getItemModel(String elementId) throws IOException{
        return sendGet(getApiUrl("/"+ elementId +"/model"), designerDefaultId);
    }
    public static RestResponse getItemType(String elementId, String type) throws IOException{
        return sendGet(getApiUrl("/"+ elementId +"/type/"+ type +"/"), designerDefaultId);
    }
    public static RestResponse saveComposition(String componentId, String userId) throws IOException{
        JsonObject json = generateCdumpInput(componentId);
        return saveComposition(componentId, userId, json.toString());
    }

    // edit composition new flow - service context
	public static RestResponse saveComposition(String serviceUuid, String vfiName, String vfcmtUuid, String body) throws IOException{
		return sendPost(getApiUrl(String.format("/service/%s/%s/saveComposition/%s", serviceUuid, UrlEscapers.urlFragmentEscaper().escape(vfiName), vfcmtUuid)), body, designerDefaultId, "application/json");
	}

	// submit composition new flow - service context
	public static RestResponse submitComposition(String serviceUuid, String vfiName, String vfcmtUuid) throws IOException {
		return sendPost(getApiUrl(String.format("/service/createBluePrint/%s/%s/%s", vfcmtUuid, serviceUuid, UrlEscapers.urlFragmentEscaper().escape(vfiName))), "", designerDefaultId, "application/json");
	}

    public static RestResponse saveComposition(String componentId, String userId, String body) throws IOException{
        return sendPost(getApiUrl("/saveComposition/"+componentId), body, userId, "application/json");
    }

    public static RestResponse getComposition(String componentId) throws IOException{
        return sendGet(getApiUrl("/getComposition/"+ componentId), designerDefaultId);
    }

    public static RestResponse submitComposition(String componentId, String serviceUuid, String vnfiName, String monitoringType) throws IOException{
        return sendPost(getApiUrl("/createBluePrint/"+ componentId +"/"+ serviceUuid +"/"+ UrlEscapers.urlFragmentEscaper().escape(vnfiName) + "/"+  UrlEscapers.urlFragmentEscaper().escape(monitoringType)), "" ,designerDefaultId, "application/json");
    }

	public static RestResponse revertToSubmittedComposition(String serviceUuid, String vnfiName, String vfcmtUuid, String submittedUuid) throws IOException{
		return sendPost(getApiUrl("/service/" + serviceUuid +"/"+ UrlEscapers.urlFragmentEscaper().escape(vnfiName) + "/"+  vfcmtUuid + "/revert/" + submittedUuid), "" ,designerDefaultId, "application/json");
	}

	public static RestResponse deleteMcReferenceAndBP(String mcName, String serviceUuid, String vnfiName, String vfcmtUuid) throws IOException{
		return sendDelete(getApiUrl("/service/" + mcName + "/" + serviceUuid +"/"+ UrlEscapers.urlFragmentEscaper().escape(vnfiName) + "/"+  vfcmtUuid + "/deleteVfcmtReference"), designerDefaultId);
	}

	public static RestResponse deleteMcReferenceAndBP(String mcName, String serviceUuid, String vnfiName, String vfcmtUuid, String submittedUuid) throws IOException{
		return sendDelete(getApiUrl(String.format("/service/%s/%s/%s/%s/deleteVfcmtReference/%s", mcName, serviceUuid, UrlEscapers.urlFragmentEscaper().escape(vnfiName), vfcmtUuid, submittedUuid)), designerDefaultId);
	}


    /* Life Cycle */

    public static RestResponse checkinVfcmt(String vfcmtUuid, String userId) throws IOException {
        return checkinGeneral("vfcmt", vfcmtUuid, userId);
    }

    public static RestResponse checkinGeneral(String assetType, String vfcmtUuid, String userId) throws IOException {
        return sendPut(getApiUrl(String.format("/checkin/%s/%s", assetType, vfcmtUuid)), null, userId, null);
    }

    public static RestResponse checkoutVfcmt(String vfcmtUuid, String userId) throws IOException {
        return checkoutGeneral("vfcmt", vfcmtUuid, userId);
    }

    private static RestResponse checkoutGeneral(String assetType, String vfcmtUuid, String userId) throws IOException {
        return sendPut(getApiUrl(String.format("/checkout/%s/%s", assetType, vfcmtUuid)), null, userId, null);
    }

    public static RestResponse certifyVfcmt(String vfcmtUuid, String userId) throws IOException {
        return sendPut(getApiUrl(String.format("/certify/vfcmt/%s", vfcmtUuid)), null,  userId, null);
    }

    /* Rule Editor */

    public static RestResponse getVesEventTypes() throws IOException {
        return sendGet(getApiUrl("/rule-editor/list-events-by-versions"), designerDefaultId);
    }

    public static RestResponse getVesDataTypes(String vesVersion, String eventType) throws IOException {
        return sendGet(getApiUrl(String.format("/rule-editor/definition/%s/%s", vesVersion, eventType)), designerDefaultId);
    }

    public static RestResponse saveRule(String vfcmtUid, String dcaeCompName, String nid, String configParam, String body) throws IOException {
        return sendPost(getApiUrl(String.format("/rule-editor/rule/%s/%s/%s/%s", vfcmtUid, dcaeCompName, nid, configParam)), body, designerDefaultId, "application/json" );
    }

    public static RestResponse getRules(String vfcmtUid, String dcaeCompName, String nid, String configParam) throws IOException {
        return sendGet(getApiUrl(String.format("/rule-editor/rule/%s/%s/%s/%s", vfcmtUid, dcaeCompName, nid, configParam)), designerDefaultId);
    }

    public static RestResponse deleteRule(String vfcmtUid, String dcaeCompName, String nid, String configParam, String ruleUid) throws IOException {
        return sendDelete(getApiUrl(String.format("/rule-editor/rule/%s/%s/%s/%s/%s", vfcmtUid, dcaeCompName, nid, configParam, ruleUid)), designerDefaultId);
    }

	public static RestResponse deleteGroupOfRules(String vfcmtUid, String dcaeCompName, String nid, String configParam, String groupId) throws IOException {
		return sendDelete(getApiUrl(String.format("/rule-editor/group/%s/%s/%s/%s/%s", vfcmtUid, dcaeCompName, nid, configParam, groupId)), designerDefaultId);
	}

	public static RestResponse translateRules(String request) throws IOException {
		return sendPost(getApiUrl("/rule-editor/rule/translate"), request, designerDefaultId, "application/json");
	}

    public static RestResponse getExistingRuleTargets(String vfcmtUuid, String dcaeCompName, String nid) throws IOException {
        String url = getApiUrl(String.format("/rule-editor/getExistingRuleTargets/%s/%s/%s", vfcmtUuid, dcaeCompName, nid));
        return sendGet(url, designerDefaultId);
    }

	public static RestResponse exportRules(String vfcmtUuid, String dcaeCompName, String nid, String configParam) throws IOException {
		String url = getApiUrl(String.format("/rule-editor/export/%s/%s/%s/%s", vfcmtUuid, dcaeCompName, nid, configParam));
		return sendGet(url, designerDefaultId, new SingletonMap<>(HttpHeaderEnum.ACCEPT.getValue(), "application/octet-stream"));
	}

	public static RestResponse importRules(String vfcmtUuid, String dcaeCompName, String nid, String configParam, String request, boolean supportGroups) throws IOException {
		String url = getApiUrl(String.format("/rule-editor/import/%s/%s/%s/%s/%s", vfcmtUuid, dcaeCompName, nid, configParam, supportGroups));
		return sendPost(url, request, designerDefaultId, "application/json");
	}

	public static RestResponse importPhase(String request) throws IOException {
		String url = getApiUrl("/rule-editor/importPhase");
		return sendPost(url, request, designerDefaultId, "application/json");
	}

	public static RestResponse applyFilter(String request) throws IOException {
		String url = getApiUrl("/rule-editor/applyFilter");
		return sendPost(url, request, designerDefaultId, "application/json");
	}

	public static RestResponse deleteFilter(String request) throws IOException {
		String url = getApiUrl("/rule-editor/deleteFilter");
		return sendPost(url, request, designerDefaultId, "application/json");
	}

	public static RestResponse getLatestMcUuid(String contextType, String serviceUuid, String vfiName, String vfcmtUuid) throws IOException {
    	return sendGet(getApiUrl(String.format("/%s/%s/%s/%s/getLatestMcUuid", contextType, serviceUuid, UrlEscapers.urlFragmentEscaper().escape(vfiName), vfcmtUuid)), designerDefaultId);
	}

	// Configuration

	public static RestResponse getConfiguredFlowTypes() throws IOException {
		return sendGet(getApiUrl("/conf/composition"), designerDefaultId);
	}

	public static RestResponse getConfiguredPhasesByFlowType(String flowType) throws IOException {
		return sendGet(getApiUrl(String.format("/conf/getPhases/%s", flowType)), designerDefaultId);
	}

    // TOSCA LAB //

	public static RestResponse getToscaLabHealthCheck() throws IOException {
		return new HttpRequest().httpSendGet(configuration.getToscaLabUrl().concat("/healthcheck"), null);
	}

	public static RestResponse translateModelToBlueprint(String payload) throws IOException {
		return new HttpRequest().httpSendPost(configuration.getToscaLabUrl().concat("/translate"), payload);
	}

	private static JSONObject newVfcmtJSON(String name, String description) {
		JSONObject json = new JSONObject();
		json.put("name", name);
		json.put("description", description);
		return json;
	}

	public static <T extends CreateVFCMTRequest> void fillCreateMcRequestMandatoryFields(T request) {
		request.setFlowType("flowType_xxx");
		request.setContextType("SERVICE");
		request.setName(StringUtils.randomString("CI-", 20));
		request.setDescription("create test vfcmt");
		if(null == request.getVfiName()) {
			request.setVfiName("whatsInAName");
		}
		if(null == request.getServiceUuid()) {
			request.setServiceUuid("service5659860");
		}
	}

	public static JsonObject generateCdumpInput(String componentId) {
		JsonObject json = new JsonObject();
		json.addProperty("cid", componentId);
		json.addProperty("version", 0);
		json.add("nodes", new JsonArray());
		json.add("relations", new JsonArray());
		json.add("inputs", new JsonArray());
		json.add("outputs", new JsonArray());
		return json;

	}
}