aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/backend/openecomp-sdc-vendor-software-product-manager/src/main/java/org/openecomp/sdc/vendorsoftwareproduct/impl/orchestration/csar/OnboardingManifest.java
blob: d88d883e671b018ee3a953707946e7de329353f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang.StringUtils;
import org.openecomp.sdc.common.errors.Messages;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;

import static org.openecomp.core.validation.errors.ErrorMessagesFormatBuilder.getErrorWithParameters;
import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.CSARConstants.*;

public class OnboardingManifest {
    private static final Logger logger = LoggerFactory.getLogger(OnboardingManifest.class);
    private Map<String, String> metadata;
    private List<String> sources;
    private List<String> errors;
    private State state;
    private enum State {
        Start, ProcessMetadata, ProcessSources, Error
    }

    public OnboardingManifest(InputStream is) {
        errors = new ArrayList<>();
        sources = new ArrayList<>();
        metadata = new HashMap<>();
        parseManifest(is);
    }

    private void parseManifest(InputStream is) {
        try {
            ImmutableList<String> lines = readAllLines(is);
            state = State.Start;

            for (String line : lines) {
                line = line.trim();
                if (!StringUtils.isEmpty(line.trim())) {
                    state = processLine(state, line);
                }
            }
            if (errors.isEmpty()) {
                if (metadata.isEmpty()) {
                    errors.add(Messages.MANIFEST_NO_METADATA.getErrorMessage());
                }
                if (sources.isEmpty()) {
                    errors.add(Messages.MANIFEST_NO_SOURCES.getErrorMessage());
                }
            }
        } catch (IOException e){
            logger.error(e.getMessage(),e);
            errors.add(Messages.MANIFEST_PARSER_INTERNAL.getErrorMessage());
        }
    }

    private State processLine(State state, String line) {
        switch (state) {
            case Start:
                if (line.trim().equals(METADATA_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE)) {
                    state = State.ProcessMetadata;
                } else {
                    reportError(line);
                }
                break;
            case ProcessMetadata:
                String[] metaSplit = line.split(SEPERATOR_MF_ATTRIBUTE);
                if (metaSplit.length < 2){
                    reportError(line);
                    break;
                }
                if (!metaSplit[0].equals(SOURCE_MF_ATTRIBUTE)){
                    String value = line.replace(metaSplit[0] + SEPERATOR_MF_ATTRIBUTE, "").trim();
                    metadata.put(metaSplit[0],value);
                } else {
                    state = State.ProcessSources;
                    processSourceLine(line);
                }
                break;
            case ProcessSources:
                processSourceLine(line);

                break;
            case Error:
                break;

        } return state;
    }

    private void processSourceLine(String line) {
        if (line.startsWith(SOURCE_MF_ATTRIBUTE+SEPERATOR_MF_ATTRIBUTE)){
            String value = line.replaceAll(SOURCE_MF_ATTRIBUTE + SEPERATOR_MF_ATTRIBUTE, "").trim();
            sources.add(value);
        }else {
            reportError(line);
        }
    }

    private void reportError(String line) {
        errors.add(getErrorWithParameters(Messages.MANIFEST_INVALID_LINE.getErrorMessage(), line));
        state = State.Error;
    }

    private ImmutableList<String> readAllLines(InputStream is) throws IOException {
        ImmutableList.Builder<String> builder = ImmutableList.<String> builder();
        try (Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8.newDecoder());
             BufferedReader bufferedReader = new BufferedReader(reader);) {
            for (; ; ) {
                String line = bufferedReader.readLine();
                if (line == null)
                    break;
                builder.add(line);
            }
        }
        return builder.build();
    }

    public Map<String, String> getMetadata() {
        if (!isValid()){
            return Collections.EMPTY_MAP;
        }
        return ImmutableMap.copyOf(metadata);
    }

    public List<String> getSources() {
        if (!isValid()){
            return Collections.EMPTY_LIST;
        }
        return ImmutableList.copyOf(sources);
    }

    public List<String> getErrors() {
        return  ImmutableList.copyOf(errors);
    }

    public boolean isValid() {
        return errors.isEmpty();
    }
}