2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar;
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;
27 import java.nio.charset.StandardCharsets;
30 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
31 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.*;
33 public class OnboardingManifest {
34 private static final Logger LOGGER = LoggerFactory.getLogger(OnboardingManifest.class);
35 private Map<String, String> metadata;
36 private List<String> sources;
37 private List<String> errors;
40 START, PROCESS_METADATA, PROCESS_SOURCES, ERROR
43 public OnboardingManifest(InputStream is) {
44 errors = new ArrayList<>();
45 sources = new ArrayList<>();
46 metadata = new HashMap<>();
50 private void parseManifest(InputStream is) {
52 ImmutableList<String> lines = readAllLines(is);
55 for (String line : lines) {
57 if (!StringUtils.isEmpty(line.trim())) {
58 state = processLine(state, line);
61 if (errors.isEmpty()) {
62 if (metadata.isEmpty()) {
63 errors.add(Messages.MANIFEST_NO_METADATA.getErrorMessage());
65 if (sources.isEmpty()) {
66 errors.add(Messages.MANIFEST_NO_SOURCES.getErrorMessage());
69 } catch (IOException e){
70 LOGGER.error(e.getMessage(),e);
71 errors.add(Messages.MANIFEST_PARSER_INTERNAL.getErrorMessage());
75 private State processLine(State state, String line) {
76 State newState = state;
79 newState = startProcessLine(state, line);
81 case PROCESS_METADATA:
82 newState = processMetaData(state, line);
85 processSourceLine(line);
95 private State startProcessLine(State state, String line) {
96 State newState = state;
97 if (line.trim().equals(METADATA_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE)) {
98 newState = State.PROCESS_METADATA;
105 private State processMetaData(State state, String line) {
106 State newState = state;
107 String[] metaSplit = line.split(SEPERATOR_MF_ATTRIBUTE);
108 if (metaSplit.length < 2){
112 if (!metaSplit[0].equals(SOURCE_MF_ATTRIBUTE)){
113 String value = line.replace(metaSplit[0] + SEPERATOR_MF_ATTRIBUTE, "").trim();
114 metadata.put(metaSplit[0],value);
116 newState = State.PROCESS_SOURCES;
117 processSourceLine(line);
122 private void processSourceLine(String line) {
123 if (line.startsWith(SOURCE_MF_ATTRIBUTE+SEPERATOR_MF_ATTRIBUTE)){
124 String value = line.replaceAll(SOURCE_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE, "").trim();
131 private void reportError(String line) {
132 errors.add(getErrorWithParameters(Messages.MANIFEST_INVALID_LINE.getErrorMessage(), line));
136 private ImmutableList<String> readAllLines(InputStream is) throws IOException {
137 ImmutableList.Builder<String> builder = ImmutableList.<String> builder();
138 try (Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8.newDecoder());
139 BufferedReader bufferedReader = new BufferedReader(reader);) {
141 String line = bufferedReader.readLine();
147 return builder.build();
150 public Map<String, String> getMetadata() {
152 return Collections.emptyMap();
154 return ImmutableMap.copyOf(metadata);
157 public List<String> getSources() {
159 return Collections.emptyList();
161 return ImmutableList.copyOf(sources);
164 public List<String> getErrors() {
165 return ImmutableList.copyOf(errors);
168 public boolean isValid() {
169 return errors.isEmpty();