ec08966c5a4d998aaf5e3485ed765a20b3beda8c
[vfc/nfvo/catalog.git] /
1 /**
2  * Copyright 2016 [ZTE] and others.
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.openo.commontosca.catalog.model.parser;
18
19 import org.openo.commontosca.catalog.common.Config;
20 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
21 import org.openo.commontosca.catalog.model.parser.yaml.aria.AriaModelParser;
22 import org.openo.commontosca.catalog.model.parser.yaml.zte.ToscaYamlModelParser;
23
24 import java.util.HashMap;
25 import java.util.Map;
26
27
28 public class ModelParserFactory {
29   private static final ModelParserFactory instance = new ModelParserFactory();
30
31   public static ModelParserFactory getInstance() {
32     return instance;
33   }
34
35   private Map<EnumPackageFormat, AbstractModelParser> pkgType2ParseMap =
36       new HashMap<EnumPackageFormat, AbstractModelParser>();
37
38   /**
39    * @param format
40    * @param parse
41    */
42   public void put(EnumPackageFormat format, AbstractModelParser parse) {
43     if (parse != null) {
44       pkgType2ParseMap.put(format, parse);
45     }
46   }
47   
48   private ModelParserFactory() {
49     // PackageParseMap.put(EnumPackageFormat.TOSCA_XML, new
50     // ToscaXmlModelParser());
51     if (isAriaParser()) {
52       pkgType2ParseMap.put(EnumPackageFormat.TOSCA_YAML, new AriaModelParser());
53     } else {
54       pkgType2ParseMap.put(EnumPackageFormat.TOSCA_YAML, new ToscaYamlModelParser());
55     }
56   }
57
58   /**
59    * @return
60    */
61   private boolean isAriaParser() {
62     return "aria".equalsIgnoreCase(Config.getConfigration().getParserType());
63   }
64
65   /**
66    * parse package.
67    * @param packageId package id
68    * @param fileLocation package location
69    * @param format package format
70    * @return service template id 
71    * @throws CatalogResourceException e
72    */
73   public String parse(String packageId, String fileLocation, EnumPackageFormat format)
74       throws CatalogResourceException {
75     if (pkgType2ParseMap.get(format) == null) {
76       throw new CatalogResourceException("Can't find its parser. package type = "
77           + format.toString());
78     }
79
80     return pkgType2ParseMap.get(format).parse(packageId, fileLocation);
81   }
82 }