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.be.datatypes.enums.ResourceTypeEnum;
23 import org.openecomp.sdc.common.errors.Messages;
24 import org.openecomp.sdc.logging.api.Logger;
25 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.nio.charset.StandardCharsets;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
37 import java.util.Optional;
39 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
40 import static org.openecomp.sdc.tosca.csar.CSARConstants.MANIFEST_PNF_METADATA;
41 import static org.openecomp.sdc.tosca.csar.CSARConstants.METADATA_MF_ATTRIBUTE;
42 import static org.openecomp.sdc.tosca.csar.CSARConstants.SEPERATOR_MF_ATTRIBUTE;
44 abstract class AbstractOnboardingManifest implements Manifest{
46 private static final Logger LOGGER = LoggerFactory.getLogger(AbstractOnboardingManifest.class);
47 private static final int MAX_ALLOWED_MANIFEST_META_ENTRIES = 4;
48 protected Map<String, String> metadata;
49 protected List<String> sources;
50 protected List<String> errors;
51 protected Map<String, List<String>> nonManoSources;
52 protected ResourceTypeEnum type;
54 protected AbstractOnboardingManifest() {
55 errors = new ArrayList<>();
56 sources = new ArrayList<>();
57 metadata = new HashMap<>();
58 nonManoSources = new HashMap<>();
62 public Optional<ResourceTypeEnum> getType(){
63 if(errors.isEmpty() && !metadata.isEmpty() && metadata.size() == MAX_ALLOWED_MANIFEST_META_ENTRIES) {
64 for (String key : metadata.keySet()) {
65 if (MANIFEST_PNF_METADATA.stream().anyMatch(key::equals)) {
66 return Optional.of(ResourceTypeEnum.PNF);
68 return Optional.of(ResourceTypeEnum.VF);
71 return Optional.empty();
75 public void parse(InputStream is) {
77 ImmutableList<String> lines = readAllLines(is);
78 processManifest(lines);
79 } catch (IOException e){
80 LOGGER.error(e.getMessage(),e);
81 errors.add(Messages.MANIFEST_PARSER_INTERNAL.getErrorMessage());
85 protected void processManifest(ImmutableList<String> lines) {
86 if (isEmptyManifest(lines)){
89 Iterator<String> iterator = lines.iterator();
90 //SOL004 #4.3.2: The manifest file shall start with the package metadata
91 String line = iterator.next();
92 if (!isMetadata(line)) {
96 processMetadata(iterator);
97 if (errors.isEmpty() && metadata.isEmpty()) {
98 errors.add(Messages.MANIFEST_NO_METADATA.getErrorMessage());
102 protected abstract void processMetadata(Iterator<String> iterator);
104 protected boolean isEmptyLine(Iterator<String> iterator, String line) {
106 processMetadata(iterator);
112 protected boolean isInvalidLine(String line, String[] metaSplit) {
113 if (metaSplit.length < 2){
120 protected boolean isMetadata(String line) {
121 if(line.trim().equals(METADATA_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE)){
128 protected boolean isEmptyManifest(ImmutableList<String> lines) {
129 if(lines == null || lines.isEmpty()){
130 errors.add(Messages.MANIFEST_EMPTY.getErrorMessage());
136 protected void reportError(String line) {
137 errors.add(getErrorWithParameters(Messages.MANIFEST_INVALID_LINE.getErrorMessage(), line));
140 protected ImmutableList<String> readAllLines(InputStream is) throws IOException {
142 throw new IOException("Input Stream cannot be null!");
144 ImmutableList.Builder<String> builder = ImmutableList.<String> builder();
145 try (BufferedReader bufferedReader = new BufferedReader(
146 new InputStreamReader(is, StandardCharsets.UTF_8.newDecoder()))) {
147 bufferedReader.lines().forEach(builder::add);
149 return builder.build();
152 public Map<String, String> getMetadata() {
154 return Collections.emptyMap();
156 return ImmutableMap.copyOf(metadata);
159 public List<String> getSources() {
161 return Collections.emptyList();
163 return ImmutableList.copyOf(sources);
166 public List<String> getErrors() {
167 return ImmutableList.copyOf(errors);
170 public boolean isValid() {
171 return errors.isEmpty();
174 public Map<String, List<String>> getNonManoSources() {
176 return Collections.emptyMap();
178 return ImmutableMap.copyOf(nonManoSources);