00eb46183ad20fbd8b7dd9fc78e5677bfc8262aa
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  * Modification Copyright (C) 2019 Nordix Foundation.
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.openecomp.sdc.tosca.csar;
19
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;
36 import java.util.Map;
37 import java.util.Optional;
38
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;
43
44  abstract class AbstractOnboardingManifest implements Manifest{
45
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;
53
54     protected AbstractOnboardingManifest() {
55         errors = new ArrayList<>();
56         sources = new ArrayList<>();
57         metadata = new HashMap<>();
58         nonManoSources = new HashMap<>();
59     }
60
61     @Override
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);
67                 }
68                 return Optional.of(ResourceTypeEnum.VF);
69             }
70         }
71         return Optional.empty();
72     }
73
74     @Override
75     public void parse(InputStream is) {
76         try {
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());
82         }
83     }
84
85     protected void processManifest(ImmutableList<String> lines) {
86         if (isEmptyManifest(lines)){
87             return;
88         }
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)) {
93             return;
94         }
95         //handle metadata
96         processMetadata(iterator);
97         if (errors.isEmpty() && metadata.isEmpty()) {
98                 errors.add(Messages.MANIFEST_NO_METADATA.getErrorMessage());
99         }
100     }
101
102     protected abstract void processMetadata(Iterator<String> iterator);
103
104      protected boolean isEmptyLine(Iterator<String> iterator, String line) {
105          if(line.isEmpty()){
106              processMetadata(iterator);
107              return true;
108          }
109          return false;
110      }
111
112      protected boolean isInvalidLine(String line, String[] metaSplit) {
113          if (metaSplit.length < 2){
114              reportError(line);
115              return true;
116          }
117          return false;
118      }
119
120      protected boolean isMetadata(String line) {
121          if(line.trim().equals(METADATA_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE)){
122              return true;
123          }
124          reportError(line);
125          return false;
126      }
127
128      protected boolean isEmptyManifest(ImmutableList<String> lines) {
129          if(lines == null || lines.isEmpty()){
130              errors.add(Messages.MANIFEST_EMPTY.getErrorMessage());
131              return true;
132          }
133          return false;
134      }
135
136     protected void reportError(String line) {
137         errors.add(getErrorWithParameters(Messages.MANIFEST_INVALID_LINE.getErrorMessage(), line));
138     }
139
140     protected ImmutableList<String> readAllLines(InputStream is) throws IOException {
141         if(is == null){
142             throw new IOException("Input Stream cannot be null!");
143         }
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);
148         }
149         return builder.build();
150     }
151
152     public Map<String, String> getMetadata() {
153         if (!isValid()){
154             return Collections.emptyMap();
155         }
156         return ImmutableMap.copyOf(metadata);
157     }
158
159     public List<String> getSources() {
160         if (!isValid()){
161             return Collections.emptyList();
162         }
163         return ImmutableList.copyOf(sources);
164     }
165
166     public List<String> getErrors() {
167         return  ImmutableList.copyOf(errors);
168     }
169
170     public boolean isValid() {
171         return errors.isEmpty();
172     }
173
174     public Map<String, List<String>> getNonManoSources() {
175         if (!isValid()){
176             return Collections.emptyMap();
177         }
178         return ImmutableMap.copyOf(nonManoSources);
179     }
180 }