1 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar;
3 import com.google.common.collect.ImmutableList;
4 import com.google.common.collect.ImmutableMap;
5 import org.apache.commons.lang.StringUtils;
6 import org.openecomp.sdc.common.errors.Messages;
7 import org.openecomp.sdc.logging.api.Logger;
8 import org.openecomp.sdc.logging.api.LoggerFactory;
10 import java.io.BufferedReader;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.InputStreamReader;
14 import java.io.Reader;
15 import java.nio.charset.StandardCharsets;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.List;
22 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
23 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.METADATA_MF_ATTRIBUTE;
24 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.SEPERATOR_MF_ATTRIBUTE;
25 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.SOURCE_MF_ATTRIBUTE;
27 public class OnboardingManifest {
28 private static final Logger logger = LoggerFactory.getLogger(OnboardingManifest.class);
29 private Map<String, String> metadata;
30 private List<String> sources;
31 private List<String> errors;
34 Start, ProcessMetadata, ProcessSources, Error
37 public OnboardingManifest(InputStream is) {
38 errors = new ArrayList<>();
39 sources = new ArrayList<>();
40 metadata = new HashMap<>();
44 private void parseManifest(InputStream is) {
46 ImmutableList<String> lines = readAllLines(is);
49 for (String line : lines) {
51 if (!StringUtils.isEmpty(line.trim())) {
52 state = processLine(state, line);
55 if (errors.isEmpty()) {
56 if (metadata.isEmpty()) {
57 errors.add(Messages.MANIFEST_NO_METADATA.getErrorMessage());
59 if (sources.isEmpty()) {
60 errors.add(Messages.MANIFEST_NO_SOURCES.getErrorMessage());
63 } catch (IOException e){
64 logger.error(e.getMessage(),e);
65 errors.add(Messages.MANIFEST_PARSER_INTERNAL.getErrorMessage());
69 private State processLine(State state, String line) {
72 if (line.trim().equals(METADATA_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE)) {
73 state = State.ProcessMetadata;
79 String[] metaSplit = line.split(SEPERATOR_MF_ATTRIBUTE);
80 if (metaSplit.length < 2){
84 if (!metaSplit[0].equals(SOURCE_MF_ATTRIBUTE)){
85 String value = line.replace(metaSplit[0] + SEPERATOR_MF_ATTRIBUTE, "").trim();
86 metadata.put(metaSplit[0],value);
88 state = State.ProcessSources;
89 processSourceLine(line);
93 processSourceLine(line);
102 private void processSourceLine(String line) {
103 if (line.startsWith(SOURCE_MF_ATTRIBUTE+SEPERATOR_MF_ATTRIBUTE)){
104 String value = line.replaceAll(SOURCE_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE, "").trim();
111 private void reportError(String line) {
112 errors.add(getErrorWithParameters(Messages.MANIFEST_INVALID_LINE.getErrorMessage(), line));
116 private ImmutableList<String> readAllLines(InputStream is) throws IOException {
117 ImmutableList.Builder<String> builder = ImmutableList.<String> builder();
118 try (Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8.newDecoder());
119 BufferedReader bufferedReader = new BufferedReader(reader);) {
121 String line = bufferedReader.readLine();
127 return builder.build();
130 public Map<String, String> getMetadata() {
132 return Collections.EMPTY_MAP;
134 return ImmutableMap.copyOf(metadata);
137 public List<String> getSources() {
139 return Collections.EMPTY_LIST;
141 return ImmutableList.copyOf(sources);
144 public List<String> getErrors() {
145 return ImmutableList.copyOf(errors);
148 public boolean isValid() {
149 return errors.isEmpty();