2 * Copyright © 2016-2017 European Support Limited
3 * Modification Copyright (C) 2019 Nordix Foundation.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.openecomp.sdc.tosca.csar;
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.collect.ImmutableMap;
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 import java.io.BufferedReader;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.InputStreamReader;
29 import java.nio.charset.StandardCharsets;
30 import java.util.ArrayList;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.Iterator;
34 import java.util.List;
37 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
38 import static org.openecomp.sdc.tosca.csar.CSARConstants.METADATA_MF_ATTRIBUTE;
39 import static org.openecomp.sdc.tosca.csar.CSARConstants.SEPERATOR_MF_ATTRIBUTE;
41 abstract class AbstractOnboardingManifest implements Manifest{
43 private static final Logger LOGGER = LoggerFactory.getLogger(AbstractOnboardingManifest.class);
44 protected Map<String, String> metadata;
45 protected List<String> sources;
46 protected List<String> errors;
47 protected Map<String, List<String>> nonManoSources;
49 protected AbstractOnboardingManifest() {
50 errors = new ArrayList<>();
51 sources = new ArrayList<>();
52 metadata = new HashMap<>();
53 nonManoSources = new HashMap<>();
57 public void parse(InputStream is) {
59 ImmutableList<String> lines = readAllLines(is);
60 processManifest(lines);
61 } catch (IOException e){
62 LOGGER.error(e.getMessage(),e);
63 errors.add(Messages.MANIFEST_PARSER_INTERNAL.getErrorMessage());
67 protected void processManifest(ImmutableList<String> lines) {
68 if (isEmptyManifest(lines)){
71 Iterator<String> iterator = lines.iterator();
72 //SOL004 #4.3.2: The manifest file shall start with the package metadata
73 String line = iterator.next();
74 if (!isMetadata(line)) {
78 processMetadata(iterator);
79 if (errors.isEmpty() && metadata.isEmpty()) {
80 errors.add(Messages.MANIFEST_NO_METADATA.getErrorMessage());
84 protected abstract void processMetadata(Iterator<String> iterator);
86 protected boolean isEmptyLine(Iterator<String> iterator, String line) {
88 processMetadata(iterator);
94 protected boolean isInvalidLine(String line, String[] metaSplit) {
95 if (metaSplit.length < 2){
102 protected boolean isMetadata(String line) {
103 if(line.trim().equals(METADATA_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE)){
110 protected boolean isEmptyManifest(ImmutableList<String> lines) {
111 if(lines == null || lines.isEmpty()){
112 errors.add(Messages.MANIFEST_EMPTY.getErrorMessage());
118 protected void reportError(String line) {
119 errors.add(getErrorWithParameters(Messages.MANIFEST_INVALID_LINE.getErrorMessage(), line));
122 protected ImmutableList<String> readAllLines(InputStream is) throws IOException {
124 throw new IOException("Input Stream cannot be null!");
126 ImmutableList.Builder<String> builder = ImmutableList.<String> builder();
127 try (BufferedReader bufferedReader = new BufferedReader(
128 new InputStreamReader(is, StandardCharsets.UTF_8.newDecoder()))) {
129 bufferedReader.lines().forEach(builder::add);
131 return builder.build();
134 public Map<String, String> getMetadata() {
136 return Collections.emptyMap();
138 return ImmutableMap.copyOf(metadata);
141 public List<String> getSources() {
143 return Collections.emptyList();
145 return ImmutableList.copyOf(sources);
148 public List<String> getErrors() {
149 return ImmutableList.copyOf(errors);
152 public boolean isValid() {
153 return errors.isEmpty();
156 public Map<String, List<String>> getNonManoSources() {
158 return Collections.emptyMap();
160 return ImmutableMap.copyOf(nonManoSources);