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