ce494b3ab375e9aedfcf0c793d40e41df604bf07
[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
49     // ToscaXmlModelParser());
50     if (isAriaParser()) {
51       pkgType2ParseMap.put(EnumPackageFormat.TOSCA_YAML, new AriaModelParser());
52     } else {
53       pkgType2ParseMap.put(EnumPackageFormat.TOSCA_YAML, new ToscaYamlModelParser());
54     }
55   }
56
57   /**
58    * @return
59    */
60   private boolean isAriaParser() {
61     return "aria".equalsIgnoreCase(Config.getConfigration().getParserType());
62   }
63
64   /**
65    * parse package.
66    * @param packageId package id
67    * @param fileLocation package location
68    * @param format package format
69    * @return service template id 
70    * @throws CatalogResourceException e
71    */
72   public String parse(String packageId, String fileLocation, EnumPackageFormat format)
73       throws CatalogResourceException {
74     if (pkgType2ParseMap.get(format) == null) {
75       throw new CatalogResourceException("Can't find its parser. package type = "
76           + format.toString());
77     }
78
79     return pkgType2ParseMap.get(format).parse(packageId, fileLocation);
80   }
81 }