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
|
/*-
* ============LICENSE_START=======================================================
* SDC
* ================================================================================
* Copyright (C) 2019 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.sdc.asdctool.impl.internal.tool;
import java.io.IOException;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Scanner;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.openecomp.sdc.asdctool.utils.ConsoleWriter;
import org.openecomp.sdc.be.dao.cassandra.ArtifactCassandraDao;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
import org.openecomp.sdc.be.model.ArtifactDefinition;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.LifecycleStateEnum;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
import org.openecomp.sdc.be.resources.data.DAOArtifactData;
import org.openecomp.sdc.be.tosca.CsarUtils;
import org.openecomp.sdc.be.tosca.ToscaExportHandler;
import org.openecomp.sdc.be.tosca.ToscaRepresentation;
import org.openecomp.sdc.common.api.ArtifactTypeEnum;
import org.openecomp.sdc.common.util.GeneralUtility;
import org.springframework.beans.factory.annotation.Autowired;
@org.springframework.stereotype.Component("csarGenerator")
public class CsarGenerator extends CommonInternalTool {
private JanusGraphDao janusGraphDao;
private CsarUtils csarUtils;
private ToscaOperationFacade toscaOperationFacade;
private ArtifactCassandraDao artifactCassandraDao;
private ToscaExportHandler toscaExportHandler;
@Autowired
public CsarGenerator(JanusGraphDao janusGraphDao, CsarUtils csarUtils,
ToscaOperationFacade toscaOperationFacade,
ArtifactCassandraDao artifactCassandraDao, ToscaExportHandler toscaExportHandler) {
super("generate");
this.janusGraphDao = janusGraphDao;
this.csarUtils = csarUtils;
this.toscaOperationFacade = toscaOperationFacade;
this.artifactCassandraDao = artifactCassandraDao;
this.toscaExportHandler = toscaExportHandler;
}
public void generateCsar(String uuid, Scanner scanner) {
JanusGraphOperationStatus status = JanusGraphOperationStatus.OK;
Map<GraphPropertyEnum, Object> props = new EnumMap<>(GraphPropertyEnum.class);
props.put(GraphPropertyEnum.UUID, uuid);
props.put(GraphPropertyEnum.STATE, LifecycleStateEnum.CERTIFIED.name());
props.put(GraphPropertyEnum.COMPONENT_TYPE, ComponentTypeEnum.SERVICE.name());
List<GraphVertex> byCriteria = janusGraphDao
.getByCriteria(VertexTypeEnum.TOPOLOGY_TEMPLATE, props).either(l -> l, r -> null);
if (byCriteria != null && !byCriteria.isEmpty()) {
if (byCriteria.size() > 1) {
ConsoleWriter.dataLine("Warning ! More that 1 certified service with uuid", uuid);
// TBD
} else {
GraphVertex metadataV = byCriteria.get(0);
printComponentInfo(metadataV.getMetadataProperties());
ConsoleWriter.dataLine("\nGenerate CSAR (yes/no)?");
String input = scanner.nextLine();
if (input.equalsIgnoreCase("yes")) {
status = handleService(metadataV, uuid);
}
}
} else {
ConsoleWriter.dataLine("No certified service with UUID ", uuid);
}
if (status == JanusGraphOperationStatus.OK) {
janusGraphDao.commit();
} else {
janusGraphDao.rollback();
}
}
private JanusGraphOperationStatus handleService(GraphVertex metadataV, String uuid) {
JanusGraphOperationStatus status = JanusGraphOperationStatus.OK;
org.openecomp.sdc.be.model.Component component = toscaOperationFacade.getToscaFullElement(metadataV.getUniqueId()).either(l -> l, r -> null);
if (component != null) {
Supplier<byte[]> supplier = () -> generateToscaPayload(component);
generateArtifact(component, ArtifactTypeEnum.TOSCA_TEMPLATE, supplier);
supplier = () -> generateCsarPayload(component);
generateArtifact(component, ArtifactTypeEnum.TOSCA_CSAR, supplier);
GraphVertex toscaArtifactV = janusGraphDao
.getChildVertex(metadataV, EdgeLabelEnum.TOSCA_ARTIFACTS, JsonParseFlagEnum.ParseJson).either(l -> l, r -> null);
if (toscaArtifactV != null) {
Map<String, ArtifactDataDefinition> copy = component.getToscaArtifacts().entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue())));
toscaArtifactV.setJson(copy);
janusGraphDao.updateVertex(toscaArtifactV);
}
} else {
ConsoleWriter.dataLine("Failed to fetch certified service with UUID ", uuid);
}
return status;
}
private JanusGraphOperationStatus generateArtifact(Component component, ArtifactTypeEnum artifactType, Supplier<byte[]> supplier) {
JanusGraphOperationStatus status = JanusGraphOperationStatus.GENERAL_ERROR;
ArtifactDefinition csarArtifact;
Optional<ArtifactDefinition> op = component.getToscaArtifacts().values().stream()
.filter(p -> p.getArtifactType().equals(artifactType.getType())).findAny();
if (op.isPresent()) {
csarArtifact = op.get();
status = savePayload(component, csarArtifact, supplier);
}
return status;
}
private byte[] generateCsarPayload(org.openecomp.sdc.be.model.Component component) {
return csarUtils.createCsar(component, true, true).either(l -> l, r -> null);
}
private byte[] generateToscaPayload(Component component) {
return toscaExportHandler.exportComponent(component).either(ToscaRepresentation::getMainYaml, r -> null);
}
private JanusGraphOperationStatus savePayload(org.openecomp.sdc.be.model.Component component, ArtifactDefinition csarArtifact,
Supplier<byte[]> supplier) {
byte[] payload = supplier.get();
if (payload == null) {
ConsoleWriter.dataLine("create artifact failed ", csarArtifact.getArtifactLabel());
return JanusGraphOperationStatus.GENERAL_ERROR;
}
ConsoleWriter.dataLine("create artifact success ", csarArtifact.getArtifactLabel());
csarArtifact.setPayload(payload);
byte[] decodedPayload = csarArtifact.getPayloadData();
String uniqueId = UniqueIdBuilder.buildPropertyUniqueId(component.getUniqueId(), csarArtifact.getArtifactLabel());
csarArtifact.setUniqueId(uniqueId);
csarArtifact.setEsId(csarArtifact.getUniqueId());
ConsoleWriter.dataLine("create artifact unique id ", uniqueId);
csarArtifact.setArtifactChecksum(GeneralUtility.calculateMD5Base64EncodedByByteArray(decodedPayload));
DAOArtifactData artifactData = new DAOArtifactData(csarArtifact.getEsId(), decodedPayload);
artifactCassandraDao.saveArtifact(artifactData);
ConsoleWriter.dataLine("Artifact generated and saved into Cassandra ", csarArtifact.getArtifactLabel());
report(component, csarArtifact);
return JanusGraphOperationStatus.OK;
}
private void report(org.openecomp.sdc.be.model.Component component, ArtifactDefinition csarArtifact) {
Map<String, Object> dataToPrint = new HashMap<>();
dataToPrint.put("name", component.getName());
dataToPrint.put("type", component.getComponentType());
dataToPrint.put("version", component.getVersion());
dataToPrint.put("UUID", component.getUUID());
dataToPrint.put("state", component.getLifecycleState());
dataToPrint.put("archive", component.isArchived());
dataToPrint.put("id", component.getUniqueId());
dataToPrint.put("artifact name", csarArtifact.getArtifactLabel());
dataToPrint.put("artifact id", csarArtifact.getUniqueId());
dataToPrint.put("csar es id", csarArtifact.getEsId());
dataToPrint.put("artifact checksum", csarArtifact.getArtifactChecksum());
try {
getReportWriter().report(dataToPrint);
} catch (IOException e) {
ConsoleWriter.dataLine("\nFailed to created report file.");
}
}
}
|