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;
11 import java.nio.charset.StandardCharsets;
14 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
15 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.*;
17 public class OnboardingManifest {
18 private static final Logger logger = LoggerFactory.getLogger(OnboardingManifest.class);
19 private Map<String, String> metadata;
20 private List<String> sources;
21 private List<String> errors;
24 Start, ProcessMetadata, ProcessSources, Error
27 public OnboardingManifest(InputStream is) {
28 errors = new ArrayList<>();
29 sources = new ArrayList<>();
30 metadata = new HashMap<>();
34 private void parseManifest(InputStream is) {
36 ImmutableList<String> lines = readAllLines(is);
39 for (String line : lines) {
41 if (!StringUtils.isEmpty(line.trim())) {
42 state = processLine(state, line);
45 if (errors.isEmpty()) {
46 if (metadata.isEmpty()) {
47 errors.add(Messages.MANIFEST_NO_METADATA.getErrorMessage());
49 if (sources.isEmpty()) {
50 errors.add(Messages.MANIFEST_NO_SOURCES.getErrorMessage());
53 } catch (IOException e){
54 logger.error(e.getMessage(),e);
55 errors.add(Messages.MANIFEST_PARSER_INTERNAL.getErrorMessage());
59 private State processLine(State state, String line) {
62 if (line.trim().equals(METADATA_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE)) {
63 state = State.ProcessMetadata;
69 String[] metaSplit = line.split(SEPERATOR_MF_ATTRIBUTE);
70 if (metaSplit.length < 2){
74 if (!metaSplit[0].equals(SOURCE_MF_ATTRIBUTE)){
75 String value = line.replace(metaSplit[0] + SEPERATOR_MF_ATTRIBUTE, "").trim();
76 metadata.put(metaSplit[0],value);
78 state = State.ProcessSources;
79 processSourceLine(line);
83 processSourceLine(line);
92 private void processSourceLine(String line) {
93 if (line.startsWith(SOURCE_MF_ATTRIBUTE+SEPERATOR_MF_ATTRIBUTE)){
94 String value = line.replaceAll(SOURCE_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE, "").trim();
101 private void reportError(String line) {
102 errors.add(getErrorWithParameters(Messages.MANIFEST_INVALID_LINE.getErrorMessage(), line));
106 private ImmutableList<String> readAllLines(InputStream is) throws IOException {
107 ImmutableList.Builder<String> builder = ImmutableList.<String> builder();
108 try (Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8.newDecoder());
109 BufferedReader bufferedReader = new BufferedReader(reader);) {
111 String line = bufferedReader.readLine();
117 return builder.build();
120 public Map<String, String> getMetadata() {
122 return Collections.EMPTY_MAP;
124 return ImmutableMap.copyOf(metadata);
127 public List<String> getSources() {
129 return Collections.EMPTY_LIST;
131 return ImmutableList.copyOf(sources);
134 public List<String> getErrors() {
135 return ImmutableList.copyOf(errors);
138 public boolean isValid() {
139 return errors.isEmpty();