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;
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;
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;
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;
50 START, PROCESS_METADATA, PROCESS_SOURCES, ERROR
53 public OnboardingManifest(InputStream is) {
54 errors = new ArrayList<>();
55 sources = new ArrayList<>();
56 metadata = new HashMap<>();
60 private void parseManifest(InputStream is) {
62 ImmutableList<String> lines = readAllLines(is);
65 for (String line : lines) {
67 if (!StringUtils.isEmpty(line.trim())) {
68 state = processLine(state, line);
71 if (errors.isEmpty()) {
72 if (metadata.isEmpty()) {
73 errors.add(Messages.MANIFEST_NO_METADATA.getErrorMessage());
75 if (sources.isEmpty()) {
76 errors.add(Messages.MANIFEST_NO_SOURCES.getErrorMessage());
79 } catch (IOException e){
80 LOGGER.error(e.getMessage(),e);
81 errors.add(Messages.MANIFEST_PARSER_INTERNAL.getErrorMessage());
85 private State processLine(State state, String line) {
86 State newState = state;
89 newState = startProcessLine(state, line);
91 case PROCESS_METADATA:
92 newState = processMetaData(state, line);
95 processSourceLine(line);
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;
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){
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);
126 newState = State.PROCESS_SOURCES;
127 processSourceLine(line);
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();
141 private void reportError(String line) {
142 errors.add(getErrorWithParameters(Messages.MANIFEST_INVALID_LINE.getErrorMessage(), line));
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);) {
151 String line = bufferedReader.readLine();
157 return builder.build();
160 public Map<String, String> getMetadata() {
162 return Collections.emptyMap();
164 return ImmutableMap.copyOf(metadata);
167 public List<String> getSources() {
169 return Collections.emptyList();
171 return ImmutableList.copyOf(sources);
174 public List<String> getErrors() {
175 return ImmutableList.copyOf(errors);
178 public boolean isValid() {
179 return errors.isEmpty();