50c2c106ff5588f7e761a90f72daad11660baf54
[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.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;
35 import java.util.Map;
36
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;
40
41  abstract class AbstractOnboardingManifest implements Manifest{
42
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;
48
49     protected AbstractOnboardingManifest() {
50         errors = new ArrayList<>();
51         sources = new ArrayList<>();
52         metadata = new HashMap<>();
53         nonManoSources = new HashMap<>();
54     }
55
56     @Override
57     public void parse(InputStream is) {
58         try {
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());
64         }
65     }
66
67     protected void processManifest(ImmutableList<String> lines) {
68         if (isEmptyManifest(lines)){
69             return;
70         }
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)) {
75             return;
76         }
77         //handle metadata
78         processMetadata(iterator);
79         if (errors.isEmpty() && metadata.isEmpty()) {
80                 errors.add(Messages.MANIFEST_NO_METADATA.getErrorMessage());
81         }
82     }
83
84     protected abstract void processMetadata(Iterator<String> iterator);
85
86      protected boolean isEmptyLine(Iterator<String> iterator, String line) {
87          if(line.isEmpty()){
88              processMetadata(iterator);
89              return true;
90          }
91          return false;
92      }
93
94      protected boolean isInvalidLine(String line, String[] metaSplit) {
95          if (metaSplit.length < 2){
96              reportError(line);
97              return true;
98          }
99          return false;
100      }
101
102      protected boolean isMetadata(String line) {
103          if(line.trim().equals(METADATA_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE)){
104              return true;
105          }
106          reportError(line);
107          return false;
108      }
109
110      protected boolean isEmptyManifest(ImmutableList<String> lines) {
111          if(lines == null || lines.isEmpty()){
112              errors.add(Messages.MANIFEST_EMPTY.getErrorMessage());
113              return true;
114          }
115          return false;
116      }
117
118     protected void reportError(String line) {
119         errors.add(getErrorWithParameters(Messages.MANIFEST_INVALID_LINE.getErrorMessage(), line));
120     }
121
122     protected ImmutableList<String> readAllLines(InputStream is) throws IOException {
123         if(is == null){
124             throw new IOException("Input Stream cannot be null!");
125         }
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);
130         }
131         return builder.build();
132     }
133
134     public Map<String, String> getMetadata() {
135         if (!isValid()){
136             return Collections.emptyMap();
137         }
138         return ImmutableMap.copyOf(metadata);
139     }
140
141     public List<String> getSources() {
142         if (!isValid()){
143             return Collections.emptyList();
144         }
145         return ImmutableList.copyOf(sources);
146     }
147
148     public List<String> getErrors() {
149         return  ImmutableList.copyOf(errors);
150     }
151
152     public boolean isValid() {
153         return errors.isEmpty();
154     }
155
156     public Map<String, List<String>> getNonManoSources() {
157         if (!isValid()){
158             return Collections.emptyMap();
159         }
160         return ImmutableMap.copyOf(nonManoSources);
161     }
162 }