e210e8722053f7cf1203f0ab0ba47ce1fc749ce1
[sdc/sdc-workflow-designer.git] /
1 /**
2  * Copyright 2018 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.onap.sdc.workflowdesigner.externalservice.sdc;
17
18 import java.util.ArrayList;
19 import java.util.LinkedHashMap;
20 import java.util.List;
21 import java.util.Map;
22 import org.glassfish.jersey.client.ClientConfig;
23 import org.onap.sdc.workflowdesigner.common.ActivitySpecProxyException;
24 import org.onap.sdc.workflowdesigner.config.AppConfig;
25 import org.onap.sdc.workflowdesigner.externalservice.sdc.entity.ActivitySpec;
26 import org.onap.sdc.workflowdesigner.externalservice.sdc.entity.GenericCollectionWrapper;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;
31
32 /**
33  * 
34  */
35 public class ActivitySpecServiceProxy {
36   private static final Logger LOGGER = LoggerFactory.getLogger(ActivitySpecServiceProxy.class);
37
38   private static final String AUTHORIZATION = AppConfig.getActivitySpecServiceProxy().getAuthorization();
39
40   private static final String X_ECOMP_INSTANCE_ID = AppConfig.getActivitySpecServiceProxy().getxEcompInstanceId();
41
42   private static final String USER_ID = AppConfig.getActivitySpecServiceProxy().getUserId();
43
44   /** */
45   private static final String ACTIVITY_ROOT_PATH = "/activity-spec-api/v1.0";
46
47   private static final String ACTIVITY_SPEC_VERSION_ID_DEFAULT_VALUE = "latest";
48
49   private static String getActivityRootPath() {
50     return AppConfig.getActivitySpecServiceProxy().getServiceAddr() + ACTIVITY_ROOT_PATH;
51   }
52
53   /**
54    * @return
55    */
56   private ActivitySpecService getActivitySpecServiceProxy() {
57     ClientConfig config = new ClientConfig();
58     return ConsumerFactory.createConsumer(getActivityRootPath(), config, ActivitySpecService.class);
59   }
60
61
62   /**
63    * 
64    * @return
65    * @throws ActivitySpecProxyException
66    */
67   public ActivitySpec[] getActivitySpecs() throws ActivitySpecProxyException {
68     ActivitySpecService serviceProxy = getActivitySpecServiceProxy();
69     List<ActivitySpec> activitySpecList = new ArrayList<>();
70     try {
71       GenericCollectionWrapper activityCollection = serviceProxy.getActivitySpecs(USER_ID, X_ECOMP_INSTANCE_ID, AUTHORIZATION);
72       for (Object obj : activityCollection.getResults()) {
73         if (obj instanceof Map){
74           Map activitySpecMap = (Map) obj;
75           String activitySpecId = activitySpecMap.get("id").toString();
76           ActivitySpec activitySpec = serviceProxy.getActivitySpec(USER_ID, X_ECOMP_INSTANCE_ID, AUTHORIZATION, ACTIVITY_SPEC_VERSION_ID_DEFAULT_VALUE, activitySpecId);
77           activitySpec.setId(activitySpecId);
78           activitySpecList.add(activitySpec);
79         }
80       }
81     } catch (Exception e) {
82       LOGGER.error("Get Activity Specifications Failed.", e);
83       throw new ActivitySpecProxyException("Get Activity Specifications Failed.", e);
84     }
85     return activitySpecList.toArray(new ActivitySpec[activitySpecList.size()]);
86   }
87
88
89 }