d72d417c1c46f99f868f2568ad834d1318be13e7
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar;
18
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.ImmutableMap;
21 import org.apache.commons.lang.StringUtils;
22 import org.openecomp.sdc.common.errors.Messages;
23 import org.openecomp.sdc.logging.api.Logger;
24 import org.openecomp.sdc.logging.api.LoggerFactory;
25
26 import java.io.BufferedReader;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.InputStreamReader;
30 import java.io.Reader;
31 import java.nio.charset.StandardCharsets;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37
38 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
39 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.METADATA_MF_ATTRIBUTE;
40 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.SEPERATOR_MF_ATTRIBUTE;
41 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.SOURCE_MF_ATTRIBUTE;
42
43 public class OnboardingManifest {
44     private static final Logger LOGGER = LoggerFactory.getLogger(OnboardingManifest.class);
45     private Map<String, String> metadata;
46     private List<String> sources;
47     private List<String> errors;
48     private State state;
49     private enum State {
50         START, PROCESS_METADATA, PROCESS_SOURCES, ERROR
51     }
52
53     public OnboardingManifest(InputStream is) {
54         errors = new ArrayList<>();
55         sources = new ArrayList<>();
56         metadata = new HashMap<>();
57         parseManifest(is);
58     }
59
60     private void parseManifest(InputStream is) {
61         try {
62             ImmutableList<String> lines = readAllLines(is);
63             state = State.START;
64
65             for (String line : lines) {
66                 line = line.trim();
67                 if (!StringUtils.isEmpty(line.trim())) {
68                     state = processLine(state, line);
69                 }
70             }
71             if (errors.isEmpty()) {
72                 if (metadata.isEmpty()) {
73                     errors.add(Messages.MANIFEST_NO_METADATA.getErrorMessage());
74                 }
75                 if (sources.isEmpty()) {
76                     errors.add(Messages.MANIFEST_NO_SOURCES.getErrorMessage());
77                 }
78             }
79         } catch (IOException e){
80             LOGGER.error(e.getMessage(),e);
81             errors.add(Messages.MANIFEST_PARSER_INTERNAL.getErrorMessage());
82         }
83     }
84
85     private State processLine(State state, String line) {
86         State newState = state;
87         switch (state) {
88             case START:
89                 newState = startProcessLine(state, line);
90                 break;
91             case PROCESS_METADATA:
92                 newState = processMetaData(state, line);
93                 break;
94             case PROCESS_SOURCES:
95                 processSourceLine(line);
96
97                 break;
98             case ERROR:
99                 break;
100             default:
101         }
102         return newState;
103     }
104
105     private State startProcessLine(State state, String line) {
106         State newState = state;
107         if (line.trim().equals(METADATA_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE)) {
108             newState = State.PROCESS_METADATA;
109         } else {
110             reportError(line);
111         }
112         return newState;
113     }
114
115     private State processMetaData(State state, String line) {
116         State newState = state;
117         String[] metaSplit = line.split(SEPERATOR_MF_ATTRIBUTE);
118         if (metaSplit.length < 2){
119             reportError(line);
120             return state;
121         }
122         if (!metaSplit[0].equals(SOURCE_MF_ATTRIBUTE)){
123             String value = line.replace(metaSplit[0] + SEPERATOR_MF_ATTRIBUTE, "").trim();
124             metadata.put(metaSplit[0],value);
125         } else {
126             newState = State.PROCESS_SOURCES;
127             processSourceLine(line);
128         }
129         return newState;
130     }
131
132     private void processSourceLine(String line) {
133         if (line.startsWith(SOURCE_MF_ATTRIBUTE+SEPERATOR_MF_ATTRIBUTE)){
134             String value = line.replaceAll(SOURCE_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE, "").trim();
135             sources.add(value);
136         }else {
137             reportError(line);
138         }
139     }
140
141     private void reportError(String line) {
142         errors.add(getErrorWithParameters(Messages.MANIFEST_INVALID_LINE.getErrorMessage(), line));
143         state = State.ERROR;
144     }
145
146     private ImmutableList<String> readAllLines(InputStream is) throws IOException {
147         ImmutableList.Builder<String> builder = ImmutableList.<String> builder();
148         try (Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8.newDecoder());
149              BufferedReader bufferedReader = new BufferedReader(reader);) {
150             for (; ; ) {
151                 String line = bufferedReader.readLine();
152                 if (line == null)
153                     break;
154                 builder.add(line);
155             }
156         }
157         return builder.build();
158     }
159
160     public Map<String, String> getMetadata() {
161         if (!isValid()){
162             return Collections.emptyMap();
163         }
164         return ImmutableMap.copyOf(metadata);
165     }
166
167     public List<String> getSources() {
168         if (!isValid()){
169             return Collections.emptyList();
170         }
171         return ImmutableList.copyOf(sources);
172     }
173
174     public List<String> getErrors() {
175         return  ImmutableList.copyOf(errors);
176     }
177
178     public boolean isValid() {
179         return errors.isEmpty();
180     }
181 }