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