fd3d3c43f23b0c384396f5a1b705401729897943
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar;
18
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;
25
26 import java.io.*;
27 import java.nio.charset.StandardCharsets;
28 import java.util.*;
29
30 import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
31 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.*;
32
33 public class OnboardingManifest {
34     private static final Logger LOGGER = LoggerFactory.getLogger(OnboardingManifest.class);
35     private Map<String, String> metadata;
36     private List<String> sources;
37     private List<String> errors;
38     private State state;
39     private enum State {
40         START, PROCESS_METADATA, PROCESS_SOURCES, ERROR
41     }
42
43     public OnboardingManifest(InputStream is) {
44         errors = new ArrayList<>();
45         sources = new ArrayList<>();
46         metadata = new HashMap<>();
47         parseManifest(is);
48     }
49
50     private void parseManifest(InputStream is) {
51         try {
52             ImmutableList<String> lines = readAllLines(is);
53             state = State.START;
54
55             for (String line : lines) {
56                 line = line.trim();
57                 if (!StringUtils.isEmpty(line.trim())) {
58                     state = processLine(state, line);
59                 }
60             }
61             if (errors.isEmpty()) {
62                 if (metadata.isEmpty()) {
63                     errors.add(Messages.MANIFEST_NO_METADATA.getErrorMessage());
64                 }
65                 if (sources.isEmpty()) {
66                     errors.add(Messages.MANIFEST_NO_SOURCES.getErrorMessage());
67                 }
68             }
69         } catch (IOException e){
70             LOGGER.error(e.getMessage(),e);
71             errors.add(Messages.MANIFEST_PARSER_INTERNAL.getErrorMessage());
72         }
73     }
74
75     private State processLine(State state, String line) {
76         State newState = state;
77         switch (state) {
78             case START:
79                 newState = startProcessLine(state, line);
80                 break;
81             case PROCESS_METADATA:
82                 newState = processMetaData(state, line);
83                 break;
84             case PROCESS_SOURCES:
85                 processSourceLine(line);
86
87                 break;
88             case ERROR:
89                 break;
90             default:
91         }
92         return newState;
93     }
94
95     private State startProcessLine(State state, String line) {
96         State newState = state;
97         if (line.trim().equals(METADATA_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE)) {
98             newState = State.PROCESS_METADATA;
99         } else {
100             reportError(line);
101         }
102         return newState;
103     }
104
105     private State processMetaData(State state, String line) {
106         State newState = state;
107         String[] metaSplit = line.split(SEPERATOR_MF_ATTRIBUTE);
108         if (metaSplit.length < 2){
109             reportError(line);
110             return state;
111         }
112         if (!metaSplit[0].equals(SOURCE_MF_ATTRIBUTE)){
113             String value = line.replace(metaSplit[0] + SEPERATOR_MF_ATTRIBUTE, "").trim();
114             metadata.put(metaSplit[0],value);
115         } else {
116             newState = State.PROCESS_SOURCES;
117             processSourceLine(line);
118         }
119         return newState;
120     }
121
122     private void processSourceLine(String line) {
123         if (line.startsWith(SOURCE_MF_ATTRIBUTE+SEPERATOR_MF_ATTRIBUTE)){
124             String value = line.replaceAll(SOURCE_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE, "").trim();
125             sources.add(value);
126         }else {
127             reportError(line);
128         }
129     }
130
131     private void reportError(String line) {
132         errors.add(getErrorWithParameters(Messages.MANIFEST_INVALID_LINE.getErrorMessage(), line));
133         state = State.ERROR;
134     }
135
136     private ImmutableList<String> readAllLines(InputStream is) throws IOException {
137         ImmutableList.Builder<String> builder = ImmutableList.<String> builder();
138         try (Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8.newDecoder());
139              BufferedReader bufferedReader = new BufferedReader(reader);) {
140             for (; ; ) {
141                 String line = bufferedReader.readLine();
142                 if (line == null)
143                     break;
144                 builder.add(line);
145             }
146         }
147         return builder.build();
148     }
149
150     public Map<String, String> getMetadata() {
151         if (!isValid()){
152             return Collections.emptyMap();
153         }
154         return ImmutableMap.copyOf(metadata);
155     }
156
157     public List<String> getSources() {
158         if (!isValid()){
159             return Collections.emptyList();
160         }
161         return ImmutableList.copyOf(sources);
162     }
163
164     public List<String> getErrors() {
165         return  ImmutableList.copyOf(errors);
166     }
167
168     public boolean isValid() {
169         return errors.isEmpty();
170     }
171 }