Add collaboration feature
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / main / java / org / openecomp / sdc / vendorsoftwareproduct / impl / orchestration / csar / OnboardingManifest.java
1 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar;
2
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;
9
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;
20 import java.util.Map;
21
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;
26
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;
32     private State state;
33     private enum State {
34         Start, ProcessMetadata, ProcessSources, Error
35     }
36
37     public OnboardingManifest(InputStream is) {
38         errors = new ArrayList<>();
39         sources = new ArrayList<>();
40         metadata = new HashMap<>();
41         parseManifest(is);
42     }
43
44     private void parseManifest(InputStream is) {
45         try {
46             ImmutableList<String> lines = readAllLines(is);
47             state = State.Start;
48
49             for (String line : lines) {
50                 line = line.trim();
51                 if (!StringUtils.isEmpty(line.trim())) {
52                     state = processLine(state, line);
53                 }
54             }
55             if (errors.isEmpty()) {
56                 if (metadata.isEmpty()) {
57                     errors.add(Messages.MANIFEST_NO_METADATA.getErrorMessage());
58                 }
59                 if (sources.isEmpty()) {
60                     errors.add(Messages.MANIFEST_NO_SOURCES.getErrorMessage());
61                 }
62             }
63         } catch (IOException e){
64             logger.error(e.getMessage(),e);
65             errors.add(Messages.MANIFEST_PARSER_INTERNAL.getErrorMessage());
66         }
67     }
68
69     private State processLine(State state, String line) {
70         switch (state) {
71             case Start:
72                 if (line.trim().equals(METADATA_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE)) {
73                     state = State.ProcessMetadata;
74                 } else {
75                     reportError(line);
76                 }
77                 break;
78             case ProcessMetadata:
79                 String[] metaSplit = line.split(SEPERATOR_MF_ATTRIBUTE);
80                 if (metaSplit.length < 2){
81                     reportError(line);
82                     break;
83                 }
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);
87                 } else {
88                     state = State.ProcessSources;
89                     processSourceLine(line);
90                 }
91                 break;
92             case ProcessSources:
93                 processSourceLine(line);
94
95                 break;
96             case Error:
97                 break;
98
99         } return state;
100     }
101
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();
105             sources.add(value);
106         }else {
107             reportError(line);
108         }
109     }
110
111     private void reportError(String line) {
112         errors.add(getErrorWithParameters(Messages.MANIFEST_INVALID_LINE.getErrorMessage(), line));
113         state = State.Error;
114     }
115
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);) {
120             for (; ; ) {
121                 String line = bufferedReader.readLine();
122                 if (line == null)
123                     break;
124                 builder.add(line);
125             }
126         }
127         return builder.build();
128     }
129
130     public Map<String, String> getMetadata() {
131         if (!isValid()){
132             return Collections.EMPTY_MAP;
133         }
134         return ImmutableMap.copyOf(metadata);
135     }
136
137     public List<String> getSources() {
138         if (!isValid()){
139             return Collections.EMPTY_LIST;
140         }
141         return ImmutableList.copyOf(sources);
142     }
143
144     public List<String> getErrors() {
145         return  ImmutableList.copyOf(errors);
146     }
147
148     public boolean isValid() {
149         return errors.isEmpty();
150     }
151 }