DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_catalog / asdc / src / main / java / org / onap / sdc / dcae / catalog / asdc / Blueprinter.java
1 package org.onap.sdc.dcae.catalog.asdc;
2
3 import java.net.URI;
4
5 import java.util.Collections;
6
7 import org.json.JSONObject;
8 import org.onap.sdc.common.onaplog.OnapLoggerDebug;
9 import org.onap.sdc.common.onaplog.Enums.LogLevel;
10 import org.onap.sdc.dcae.catalog.commons.Action;
11 import org.onap.sdc.dcae.catalog.commons.Future;
12 import org.onap.sdc.dcae.catalog.commons.Http;
13 import org.json.JSONArray;
14
15 import org.springframework.util.Base64Utils;
16
17 import org.springframework.http.MediaType;
18 import org.springframework.http.HttpHeaders;
19 import org.springframework.http.HttpMethod;
20 import org.springframework.http.HttpEntity;
21 import org.springframework.stereotype.Component;
22 import org.springframework.context.annotation.Scope;
23 import org.springframework.boot.context.properties.ConfigurationProperties;
24
25 @Component("blueprinter")
26 @Scope("singleton")
27 @ConfigurationProperties(prefix="blueprinter")
28 public class Blueprinter {
29
30
31         private URI     serviceUri;
32         private OnapLoggerDebug debugLogger = OnapLoggerDebug.getInstance();
33
34
35         public Blueprinter() {
36         }
37
38         public void setUri(URI theUri) {
39                 this.serviceUri = theUri;
40         }
41
42         public BlueprintAction generateBlueprint() {
43                 return new BlueprintAction();
44         }
45
46         public class BlueprintAction implements Action<String> {
47
48                 private JSONObject      body = new JSONObject();
49
50
51                 protected BlueprintAction() {
52                 }
53
54                 public BlueprintAction withModelData(byte[] theSchema, byte[] theTemplate, byte[] theTranslation) {
55                         return this;
56                 }
57
58                 public BlueprintAction withModelInfo(JSONObject theModelInfo) {
59                         body.append("models", theModelInfo);
60                         return this;
61                 }
62
63                 public BlueprintAction withTemplateData(byte[] theData) {
64                         body.put("template", Base64Utils.encodeToString(theData));
65                         return this;
66                 }
67
68                 public Future<String> execute() {
69                         debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Blueprinter::execute() | PAYLOAD to TOSCA_LAB={}", body.toString());
70                         HttpHeaders headers = new HttpHeaders();
71                         headers.setContentType(MediaType.APPLICATION_JSON);
72                         headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
73                         return Http.exchange(serviceUri.toString(), HttpMethod.POST, new HttpEntity<String>(body.toString(), headers), String.class);
74                 }
75         }
76 }