Create new VSP, onboard from TOSCA file - UI
[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.*;
11 import java.nio.charset.StandardCharsets;
12 import java.util.*;
13
14 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
15 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.*;
16
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;
22     private State state;
23     private enum State {
24         Start, ProcessMetadata, ProcessSources, Error
25     }
26
27     public OnboardingManifest(InputStream is) {
28         errors = new ArrayList<>();
29         sources = new ArrayList<>();
30         metadata = new HashMap<>();
31         parseManifest(is);
32     }
33
34     private void parseManifest(InputStream is) {
35         try {
36             ImmutableList<String> lines = readAllLines(is);
37             state = State.Start;
38
39             for (String line : lines) {
40                 line = line.trim();
41                 if (!StringUtils.isEmpty(line.trim())) {
42                     state = processLine(state, line);
43                 }
44             }
45             if (errors.isEmpty()) {
46                 if (metadata.isEmpty()) {
47                     errors.add(Messages.MANIFEST_NO_METADATA.getErrorMessage());
48                 }
49                 if (sources.isEmpty()) {
50                     errors.add(Messages.MANIFEST_NO_SOURCES.getErrorMessage());
51                 }
52             }
53         } catch (IOException e){
54             logger.error(e.getMessage(),e);
55             errors.add(Messages.MANIFEST_PARSER_INTERNAL.getErrorMessage());
56         }
57     }
58
59     private State processLine(State state, String line) {
60         switch (state) {
61             case Start:
62                 if (line.trim().equals(METADATA_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE)) {
63                     state = State.ProcessMetadata;
64                 } else {
65                     reportError(line);
66                 }
67                 break;
68             case ProcessMetadata:
69                 String[] metaSplit = line.split(SEPERATOR_MF_ATTRIBUTE);
70                 if (metaSplit.length < 2){
71                     reportError(line);
72                     break;
73                 }
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);
77                 } else {
78                     state = State.ProcessSources;
79                     processSourceLine(line);
80                 }
81                 break;
82             case ProcessSources:
83                 processSourceLine(line);
84
85                 break;
86             case Error:
87                 break;
88
89         } return state;
90     }
91
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();
95             sources.add(value);
96         }else {
97             reportError(line);
98         }
99     }
100
101     private void reportError(String line) {
102         errors.add(getErrorWithParameters(Messages.MANIFEST_INVALID_LINE.getErrorMessage(), line));
103         state = State.Error;
104     }
105
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);) {
110             for (; ; ) {
111                 String line = bufferedReader.readLine();
112                 if (line == null)
113                     break;
114                 builder.add(line);
115             }
116         }
117         return builder.build();
118     }
119
120     public Map<String, String> getMetadata() {
121         if (!isValid()){
122             return Collections.EMPTY_MAP;
123         }
124         return ImmutableMap.copyOf(metadata);
125     }
126
127     public List<String> getSources() {
128         if (!isValid()){
129             return Collections.EMPTY_LIST;
130         }
131         return ImmutableList.copyOf(sources);
132     }
133
134     public List<String> getErrors() {
135         return  ImmutableList.copyOf(errors);
136     }
137
138     public boolean isValid() {
139         return errors.isEmpty();
140     }
141 }