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