summaryrefslogtreecommitdiffstats
path: root/sdc-tosca-parser/src/main/java/org/openecomp/sdc/tosca/parser/impl/SdcToscaParserFactory.java
blob: 14c332c81616462b709568235154d7167b083710 (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
package org.openecomp.sdc.tosca.parser.impl;

import org.openecomp.sdc.tosca.parser.api.ConformanceLevel;
import org.openecomp.sdc.tosca.parser.api.ISdcCsarHelper;
import org.openecomp.sdc.tosca.parser.config.Configuration;
import org.openecomp.sdc.tosca.parser.config.ConfigurationManager;
import org.openecomp.sdc.tosca.parser.exceptions.SdcToscaParserException;
import org.openecomp.sdc.tosca.parser.utils.GeneralUtility;
import org.openecomp.sdc.toscaparser.api.ToscaTemplate;
import org.openecomp.sdc.toscaparser.api.common.JToscaException;

public class SdcToscaParserFactory {

    private static volatile SdcToscaParserFactory instance;
    private static Configuration configuration;

    private SdcToscaParserFactory() {

    }

    /**
     * Get an SdcToscaParserFactory instance.
     * @return SdcToscaParserFactory instance.
     */
    public static SdcToscaParserFactory getInstance() {
        if (instance == null) {
            synchronized (SdcToscaParserFactory.class) {
                if (instance == null) {
                    instance = new SdcToscaParserFactory();
                    configuration = ConfigurationManager.getInstance().getConfiguration();
                }
            }
        }
        return instance;
    }

    /**
     * Get an ISdcCsarHelper object for this CSAR file.
     *
     * @param csarPath - the absolute path to CSAR file.
     * @return ISdcCsarHelper object.
     * @throws SdcToscaParserException - in case the path or CSAR are invalid.
     * @throws JToscaException - in case the path or CSAR are invalid.
     */
    public ISdcCsarHelper getSdcCsarHelper(String csarPath) throws JToscaException, SdcToscaParserException {
        synchronized (SdcToscaParserFactory.class) {
            ToscaTemplate tosca = new ToscaTemplate(csarPath, null, true, null);
            SdcCsarHelperImpl sdcCsarHelperImpl = new SdcCsarHelperImpl(tosca);
            validateCsarVersion(sdcCsarHelperImpl.getConformanceLevel());
            return sdcCsarHelperImpl;
        }
    }

    private void validateCsarVersion(String cSarVersion) throws SdcToscaParserException {
        ConformanceLevel level = configuration.getConformanceLevel();
        String minVersion = level.getMinVersion();
        String maxVersion = level.getMaxVersion();
        if (cSarVersion != null) {
            if ((GeneralUtility.conformanceLevelCompare(cSarVersion, minVersion) < 0) || (GeneralUtility.conformanceLevelCompare(cSarVersion, maxVersion) > 0)) {
                throw new SdcToscaParserException("Model is not supported. Parser supports versions " + minVersion + " to " + maxVersion);
            }
        } else {
            throw new SdcToscaParserException("Model is not supported. Parser supports versions " + minVersion + " to " + maxVersion);
        }
    }

}