72cee0ed1c169d9daf5df5658b54cd32e6f15f1d
[vfc/nfvo/catalog.git] /
1 /**
2  * Copyright 2016 [ZTE] and others.
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
17 package org.openo.commontosca.catalog.model.parser.yaml.aria.service;
18
19 import org.glassfish.jersey.client.ClientConfig;
20 import org.openo.commontosca.catalog.common.Config;
21 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
22 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserExceptionResult;
23 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserRequest;
24 import org.openo.commontosca.catalog.model.parser.yaml.aria.entity.AriaParserResult;
25
26 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;
27 import com.google.gson.Gson;
28
29
30 public class AriaParserServiceConsumer {
31   public static AriaParserResult parseCsarPackage(String uri) throws CatalogResourceException {
32     return parseCsarPackage(new AriaParserRequest(uri, null));
33   }
34   /**
35    * parse csar package via aria parser.
36    * 
37    * @param request parse yaml request
38    * @return parase yaml result
39    * @throws CatalogResourceException e
40    */
41   public static AriaParserResult parseCsarPackage(AriaParserRequest request)
42       throws CatalogResourceException {
43     try {
44       IAriaParserRest parseProxy =
45           ConsumerFactory.createConsumer(
46               Config.getConfigration().getMsbServerAddr(),
47               new ClientConfig(),
48               IAriaParserRest.class);
49       String strResult = parseProxy.parse(request);
50       validateResult(strResult);
51       return new Gson().fromJson(strResult, AriaParserResult.class);
52     } catch (Exception e) {
53       throw new CatalogResourceException("Call aria parser api failed.", e);
54     }
55
56   }
57   private static void validateResult(String strResult) throws CatalogResourceException {
58     AriaParserExceptionResult except = new Gson().fromJson(strResult, AriaParserExceptionResult.class);
59     if (except.getIssues() != null && except.getIssues().length > 0) {
60       throw new CatalogResourceException("Aria parser return failure message: " + except.getIssues()[0].getMessage());
61     }
62   }
63 }