b951e952ec64a0c2ac5890bf2e5de653b85d469b
[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.externalservice.ro;
18
19 import com.google.gson.Gson;
20
21 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;
22
23 import org.openo.commontosca.catalog.common.MsbUtil;
24 import org.openo.commontosca.catalog.common.ToolUtil;
25 import org.openo.commontosca.catalog.model.externalservice.entity.ro.ResourceResponseEntity;
26 import org.openo.commontosca.catalog.model.externalservice.entity.ro.VimEntity;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31 /**
32  * The roc resource service.
33  * 
34  * @author 10189609
35  * 
36  */
37 public class ResourceServiceConsumer {
38   private static final Logger LOG = LoggerFactory.getLogger(ResourceServiceConsumer.class);
39
40   private static final String RESOURCE_REST_RESULT = "SUCCESS";
41
42   /**
43    * get vim entity from roc by vimid.
44    * 
45    * @param vimId id
46    * @return vim entity
47    */
48   public static VimEntity getResourceVim(String vimId) {
49     LOG.info("begin query vim info from roc,vimId:" + vimId);
50     IResourceServiceRest resourceserviceproxy =
51         ConsumerFactory.createConsumer(MsbUtil.getRocBaseUrl(), IResourceServiceRest.class);
52     String result = "";
53     try {
54       result = resourceserviceproxy.getResourceVim(vimId);
55     } catch (Exception e1) {
56       LOG.error("query vim info faild.", e1);
57       return null;
58     }
59     if (ToolUtil.isEmptyString(result)) {
60       LOG.error("query vim info faild, vim info is null, vimId:" + vimId);
61       return null;
62     }
63
64     Gson gson = new Gson();
65     ResourceResponseEntity responseEntity = gson.fromJson(result, ResourceResponseEntity.class);
66     if (!RESOURCE_REST_RESULT.equalsIgnoreCase(responseEntity.getOperationResult())) {
67       LOG.error("query vim info faild.vimId:" + vimId);
68       return null;
69     }
70     if (responseEntity.getData().size() <= 0) {
71       LOG.error("query vim info faild, vim info is empty, vimId:" + vimId);
72       return null;
73     }
74
75     LOG.info("end query vim info from roc.");
76     return responseEntity.getData().get(0);
77   }
78 }