1701c60f8c3291ec7f5769b9076cdbbef7d6ee98
[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.yaml.aria.service;
17
18 import org.glassfish.jersey.client.ClientConfig;
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.entity.AriaParserRequest;
22 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;
27 import com.google.gson.Gson;
28
29
30 public class AriaParserServiceConsumer {
31   private static final Logger logger = LoggerFactory.getLogger(AriaParserServiceConsumer.class);
32   
33   public static AriaParserResult parseCsarPackage(String uri) throws CatalogResourceException {
34     logger.info("parseCsarPackage uri = " + uri);
35     return parseCsarPackage(new AriaParserRequest(uri, null));
36   }
37   /**
38    * parse csar package via aria parser.
39    * 
40    * @param request parse yaml request
41    * @return parase yaml result
42    * @throws CatalogResourceException e
43    */
44   public static AriaParserResult parseCsarPackage(AriaParserRequest request)
45       throws CatalogResourceException {
46     try {
47       IAriaParserRest parseProxy =
48           ConsumerFactory.createConsumer(
49               Config.getConfigration().getMsbServerAddr(),
50               new ClientConfig(),
51               IAriaParserRest.class);
52       String strResult = parseProxy.parse(request);
53       AriaParserResult result = new Gson().fromJson(strResult, AriaParserResult.class);
54       result.setRawData(strResult);
55       validateResult(result, strResult);
56       return result;
57     } catch (Exception e) {
58       throw new CatalogResourceException("Call aria parser api failed.", e);
59     }
60
61   }
62   private static void validateResult(AriaParserResult result, String strResult) throws CatalogResourceException {
63     if (result.getIssues() != null && result.getIssues().length > 0) {
64       logger.error("Aria parser return failure message: " + strResult);
65       throw new CatalogResourceException("Aria parser return failure message: " + strResult);
66     }
67   }
68 }