diff options
author | Zhaoxing <meng.zhaoxing1@zte.com.cn> | 2017-09-28 16:44:06 +0800 |
---|---|---|
committer | Fu Jinhua <fu.jinhua@zte.com.cn> | 2017-09-28 08:58:17 +0000 |
commit | ca57c4bc2ce48c52f10bf6ef816492cf044430e0 (patch) | |
tree | af1f05a4eb9d89e200296f82dfaf8efde42de606 /activiti-extension/src/main | |
parent | 4d813ced540b70cefd212f9b082bce474b3c2977 (diff) |
Add unit test for vfc-nfvo-wfengine
Change-Id: Ib6c9151f5aeae55e7f9055a8d5e33fb0b51a8474
Issue-id: VFC-454
Signed-off-by: Zhaoxing <meng.zhaoxing1@zte.com.cn>
Diffstat (limited to 'activiti-extension/src/main')
20 files changed, 1001 insertions, 497 deletions
diff --git a/activiti-extension/src/main/java/org/activiti/engine/impl/pvm/runtime/AtomicOperationProcessStart.java b/activiti-extension/src/main/java/org/activiti/engine/impl/pvm/runtime/AtomicOperationProcessStart.java new file mode 100644 index 0000000..2fb8c9a --- /dev/null +++ b/activiti-extension/src/main/java/org/activiti/engine/impl/pvm/runtime/AtomicOperationProcessStart.java @@ -0,0 +1,71 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.activiti.engine.impl.pvm.runtime; + +import java.util.List; +import java.util.Map; + +import org.activiti.engine.delegate.event.ActivitiEventType; +import org.activiti.engine.delegate.event.impl.ActivitiEventBuilder; +import org.activiti.engine.impl.context.Context; +import org.activiti.engine.impl.pvm.process.ActivityImpl; +import org.activiti.engine.impl.pvm.process.ProcessDefinitionImpl; +import org.activiti.engine.impl.pvm.process.ScopeImpl; + +public class AtomicOperationProcessStart extends AbstractEventAtomicOperation { + @Override + public boolean isAsync(InterpretableExecution execution) { + return true; + } + + protected ScopeImpl getScope(InterpretableExecution execution) { + return execution.getProcessDefinition(); + } + + protected String getEventName() { + return "start"; + } + + protected void eventNotificationsCompleted(InterpretableExecution execution) { + if ((Context.getProcessEngineConfiguration() != null) + && (Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled())) { + Map variablesMap = null; + try { + variablesMap = execution.getVariables(); + } catch (Throwable localThrowable) { + } + Context + .getProcessEngineConfiguration() + .getEventDispatcher() + .dispatchEvent( + ActivitiEventBuilder.createEntityWithVariablesEvent( + ActivitiEventType.ENTITY_INITIALIZED, execution, variablesMap, false)); + + Context + .getProcessEngineConfiguration() + .getEventDispatcher() + .dispatchEvent( + ActivitiEventBuilder.createProcessStartedEvent(execution, variablesMap, false)); + } + + ProcessDefinitionImpl processDefinition = execution.getProcessDefinition(); + StartingExecution startingExecution = execution.getStartingExecution(); + List initialActivityStack = + processDefinition.getInitialActivityStack(startingExecution.getInitial()); + execution.setActivity((ActivityImpl) initialActivityStack.get(0)); + execution.performOperation(PROCESS_START_INITIAL); + } +} diff --git a/activiti-extension/src/main/java/org/activiti/rest/conf/init/InitBaseProcessDefinition.java b/activiti-extension/src/main/java/org/activiti/rest/conf/init/InitBaseProcessDefinition.java new file mode 100644 index 0000000..e1e7c18 --- /dev/null +++ b/activiti-extension/src/main/java/org/activiti/rest/conf/init/InitBaseProcessDefinition.java @@ -0,0 +1,84 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.activiti.rest.conf.init; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.util.List; + +import javax.annotation.PostConstruct; + +import org.activiti.engine.RepositoryService; +import org.activiti.engine.repository.DeploymentBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; + +@Configuration +public class InitBaseProcessDefinition { + + protected static final Logger LOGGER = LoggerFactory.getLogger(InitBaseProcessDefinition.class); + + @Autowired + protected RepositoryService repositoryService; + + @Autowired + protected Environment environment; + + @PostConstruct + public void init() { + + if (Boolean.valueOf(this.environment.getProperty("init.process.definitions", "true")) + .booleanValue()) { + LOGGER.info("Initializing base process definitions"); + initBaseProcessDefinitions(); + } + } + + protected void initBaseProcessDefinitions() { + String deploymentName = "base definitions"; + List deploymentList = + this.repositoryService.createDeploymentQuery().deploymentName(deploymentName).list(); + + if ((deploymentList == null) || (deploymentList.isEmpty())) { + + String classPath = this.getClass().getClassLoader().getResource("/").getPath(); + classPath = classPath.replaceAll("WEB-INF/classes", "baseProccessDefinition"); + + File files = new File(classPath); + DeploymentBuilder builder = this.repositoryService.createDeployment().name(deploymentName); + File flist[] = files.listFiles(); + if (flist != null && flist.length != 0) { + for (File f : flist) { + + String fileName = f.getName(); + if (fileName != null && fileName.endsWith("bpmn20.xml")) { + try { + builder.addInputStream(fileName, new FileInputStream(f)); + LOGGER.info("deploy success: {}", fileName); + } catch (FileNotFoundException e) { + LOGGER.info("deploy failed: {}", fileName); + } + } + } + builder.deploy(); + } + } + } +} diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/ActivitiExtApp.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/ActivitiExtApp.java deleted file mode 100644 index dd56610..0000000 --- a/activiti-extension/src/main/java/org/onap/workflow/activitiext/ActivitiExtApp.java +++ /dev/null @@ -1,41 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onap.workflow.activitiext; - -import org.glassfish.jersey.media.multipart.MultiPartFeature; - -import io.dropwizard.Application; -import io.dropwizard.setup.Environment; - -public class ActivitiExtApp extends Application<ActivitiExtAppConfig> { - - public static void main(String[] args) throws Exception { - new ActivitiExtApp().run(args); - } - - @Override - public String getName() { - return " Activiti Ext APP "; - } - - @Override - public void run(ActivitiExtAppConfig configuration, Environment environment) throws Exception { - - environment.jersey().register(MultiPartFeature.class); - } -} - diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/ActivitiExtAppConfig.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/ActivitiExtAppConfig.java deleted file mode 100644 index ffe3c9e..0000000 --- a/activiti-extension/src/main/java/org/onap/workflow/activitiext/ActivitiExtAppConfig.java +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2017 ZTE Corporation. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onap.workflow.activitiext; - -import io.dropwizard.Configuration; - -import javax.validation.Valid; - -import org.hibernate.validator.constraints.NotEmpty; -import org.onap.workflow.activitiext.common.MsbClientConfig; - -import com.fasterxml.jackson.annotation.JsonProperty; - -public class ActivitiExtAppConfig extends Configuration { - - @NotEmpty - private String defaultName = "WORKFLOW-ACTIVITI-EXT"; - @NotEmpty - @JsonProperty - private String apidescription = "ZTE workflow-activiti-ext rest API"; - - @JsonProperty - @Valid - private MsbClientConfig msbClientConfig; - - public MsbClientConfig getMsbClientConfig() { - return msbClientConfig; - } -} diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/common/Config.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/common/Config.java deleted file mode 100644 index 62ee72a..0000000 --- a/activiti-extension/src/main/java/org/onap/workflow/activitiext/common/Config.java +++ /dev/null @@ -1,39 +0,0 @@ -/**
- * Copyright 2017 ZTE Corporation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.onap.workflow.activitiext.common;
-
-import org.onap.workflow.activitiext.ActivitiExtAppConfig;
-
-public class Config {
-
- private static Config instance = new Config();
-
- private ActivitiExtAppConfig appconfig;
-
- public static Config getInstance(){
- return instance;
- }
-
- public ActivitiExtAppConfig getAppconfig() {
- return appconfig;
- }
-
- public void setAppconfig(ActivitiExtAppConfig config) {
- this.appconfig = config;
- }
-
-}
diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/ConstString.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/common/ConstString.java index 371d8d7..17f715e 100644 --- a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/ConstString.java +++ b/activiti-extension/src/main/java/org/onap/workflow/activitiext/common/ConstString.java @@ -1,70 +1,99 @@ -/**
- * Copyright 2016-2017 ZTE Corporation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.onap.workflow.activitiext.restservicetask;
-
-/**
- *
- * @author 10222158
- *
- */
-public class ConstString {
-
- /**
- * http method type : get
- */
- public static final String HTTP_GET = "GET";
-
- /**
- * http method type : post
- */
- public static final String HTTP_POST = "POST";
-
- /**
- * http method type : delete
- */
- public static final String HTTP_DELETE = "DELETE";
-
- /**
- * http method type : put
- */
- public static final String HTTP_PUT = "PUT";
-
- /**
- * parameter position : in the uri path
- */
- public static final String PARAMETER_POSITION_PATH = "path";
-
- /**
- * parameter positoin : after the uri
- */
- public static final String PARAMETER_POSITION_QUERY = "query";
-
- /**
- * parameter positoin : in the request body
- */
- public static final String PARAMETER_POSITION_BODY = "body";
-
- /**
- * parameter type : string
- */
- public static final String PARAMETER_TYPE_STRING = "string";
-
- /**
- * parameter type : acitviti expression
- */
- public static final String PARAMETER_TYPE_EXPRESSION = "expression";
-
-}
+/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.workflow.activitiext.common; + +/** + * + * @author 10222158 + * + */ +public class ConstString { + + /** + * http method type : get + */ + public static final String HTTP_GET = "GET"; + + /** + * http method type : post + */ + public static final String HTTP_POST = "POST"; + + /** + * http method type : delete + */ + public static final String HTTP_DELETE = "DELETE"; + + /** + * http method type : put + */ + public static final String HTTP_PUT = "PUT"; + + /** + * parameter position : in the uri path + */ + public static final String PARAMETER_POSITION_PATH = "path"; + + /** + * parameter positoin : after the uri + */ + public static final String PARAMETER_POSITION_QUERY = "query"; + + /** + * parameter positoin : in the request body + */ + public static final String PARAMETER_POSITION_BODY = "body"; + + /** + * parameter type : string + */ + public static final String PARAMETER_VALUE_SOURCE_STRING = "String"; + + /** + * parameter type : plan + */ + public static final String PARAMETER_VALUE_SOURCE_PLAN = "Plan"; + + /** + * parameter type : Topology + */ + public static final String PARAMETER_VALUE_SOURCE_TOPOLOGY = "Topology"; + + /** + * parameter type : Variable + */ + public static final String PARAMETER_VALUE_SOURCE_VARIABLE = "Variable"; + + /** + * parameter type : Definition + */ + public static final String PARAMETER_VALUE_SOURCE_DEFINITION = "Definition"; + + /** + * + */ + public static final String PARAMETER_VALUE = "value"; + + /** + * + */ + public static final String PARAMETER_VALUESOURCE = "valueSource"; + + /** + * + */ + public static final String CSARID_EXPRESSION = "csarId"; +} diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/common/EnumModuleUrl.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/common/EnumModuleUrl.java new file mode 100644 index 0000000..efc4dff --- /dev/null +++ b/activiti-extension/src/main/java/org/onap/workflow/activitiext/common/EnumModuleUrl.java @@ -0,0 +1,56 @@ +/** + * Copyright 2017 [ZTE] and others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.workflow.activitiext.common; + +import org.apache.commons.lang3.StringUtils; +import org.onap.workflow.activitiext.restservicetask.PropertyUtil; +import org.onap.workflow.utils.MsbUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + */ +public class EnumModuleUrl { + + private static final Logger logger = LoggerFactory.getLogger(EnumModuleUrl.class); + + public static String getApiRootDomain() { + return "/openoapi/catalog/v1"; + } + + public static String getBaseUrl(ServiceType type) { + String baseUrl = ""; + String publishUrl =""; + try { + publishUrl = MsbUtils.getServiceAddress("catalog", "v1"); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + if(StringUtils.isNotEmpty(publishUrl)) + { + baseUrl = publishUrl; + }else{ + + baseUrl = PropertyUtil.getBasePath() + getApiRootDomain(); + } + logger.info("baseUrl is" + baseUrl); + + return baseUrl; + } +} diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/common/Parameter.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/common/Parameter.java new file mode 100644 index 0000000..6aa97a0 --- /dev/null +++ b/activiti-extension/src/main/java/org/onap/workflow/activitiext/common/Parameter.java @@ -0,0 +1,33 @@ +/** + * Copyright 2017 [ZTE] and others. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.onap.workflow.activitiext.common; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@JsonIgnoreProperties(ignoreUnknown = true) +public class Parameter { + + private String position; + private String type; + private String name; + private String value; + private String valueSource; +} diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/StartProcessRequest.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/common/RestInfo.java index 9d712f2..18c376b 100644 --- a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/StartProcessRequest.java +++ b/activiti-extension/src/main/java/org/onap/workflow/activitiext/common/RestInfo.java @@ -13,21 +13,28 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.onap.workflow.activitiext.restservicetask; +package org.onap.workflow.activitiext.common; -import java.util.Map; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; -import com.fasterxml.jackson.annotation.JsonIgnoreProperties; - @Data @NoArgsConstructor @AllArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true) -public class StartProcessRequest { - private String processId; - private Map<String, String> params; +public class RestInfo { + + private String name; + private String version; + private String url; + private String path; + private String method; + private String accept; + private String contentType; + private String requestBody; + private String realUri; + } diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/common/MsbClientConfig.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/common/ServiceType.java index 338a1ff..c6c3d20 100644 --- a/activiti-extension/src/main/java/org/onap/workflow/activitiext/common/MsbClientConfig.java +++ b/activiti-extension/src/main/java/org/onap/workflow/activitiext/common/ServiceType.java @@ -1,26 +1,24 @@ -/**
- * Copyright 2017 ZTE Corporation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.onap.workflow.activitiext.common;
-
-import lombok.Data;
-
-@Data
-public class MsbClientConfig {
-
- private String msbSvrIp;
- private Integer msbSvrPort;
-}
+/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.workflow.activitiext.common; + +/** + * @author 10175158 + * + */ +public enum ServiceType { + catalog +} diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/CatalogServiceConsumer.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/CatalogServiceConsumer.java new file mode 100644 index 0000000..64152d3 --- /dev/null +++ b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/CatalogServiceConsumer.java @@ -0,0 +1,50 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.workflow.activitiext.restservicetask; + +import org.glassfish.jersey.client.ClientConfig; +import org.onap.workflow.activitiext.common.EnumModuleUrl; +import org.onap.workflow.activitiext.common.ServiceType; + +import com.eclipsesource.jaxrs.consumer.ConsumerFactory; + +/** + * + * @author 10090474 + * + */ +public class CatalogServiceConsumer { + + private static ICatalogService activitiServiceProxy = null; + + private static ICatalogService getCatalogService() { + if (activitiServiceProxy == null) { + ClientConfig config = new ClientConfig(); + activitiServiceProxy = ConsumerFactory.createConsumer( + EnumModuleUrl.getBaseUrl(ServiceType.catalog), config, ICatalogService.class); + + } + return activitiServiceProxy; + } + + public static String getTopologyJson(String csarId, String nodeName) { + + getCatalogService(); + String result = activitiServiceProxy.getTopologyJson(csarId, nodeName); + + return result; + } +} diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/GsonUtil.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/GsonUtil.java new file mode 100644 index 0000000..2e0aa77 --- /dev/null +++ b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/GsonUtil.java @@ -0,0 +1,117 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.workflow.activitiext.restservicetask; + +import java.util.Map.Entry; + +import org.activiti.engine.delegate.DelegateExecution; +import org.onap.workflow.activitiext.common.ConstString; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.gson.JsonArray; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; + +public class GsonUtil { + + private static final Logger logger = LoggerFactory.getLogger(GsonUtil.class); + + public static JsonObject formatJsonObject(String key, JsonObject obj, DelegateExecution execution) { + + JsonObject res = new JsonObject(); + + for (Entry<String, JsonElement> entry : obj.entrySet()) { + + if (entry.getValue() instanceof JsonObject) { + + JsonObject tempObj = (JsonObject) entry.getValue(); + JsonElement valueSource = tempObj.get(ConstString.PARAMETER_VALUESOURCE); + if(valueSource == null || valueSource.getAsString().equals(ConstString.PARAMETER_VALUE_SOURCE_DEFINITION)) + { + res.add(entry.getKey(), formatJsonObject(entry.getKey(), entry.getValue().getAsJsonObject(), execution)); + }else{ + res.addProperty(entry.getKey(), getValue(tempObj, execution)); + } + + } else if (entry.getValue() instanceof JsonArray) { + + JsonArray objArray = entry.getValue().getAsJsonArray(); + JsonArray resArray = formatJsonArray(objArray, execution); + res.add(entry.getKey(), resArray); + } + } + + return res; + } + + public static JsonArray formatJsonArray(JsonArray objArray, DelegateExecution execution) { + + JsonArray array = new JsonArray(); + + for (JsonElement element : objArray) { + + if (element.isJsonObject()) { + + JsonObject tempObj = (JsonObject) element; + JsonElement valueSource = tempObj.get(ConstString.PARAMETER_VALUESOURCE); + if(valueSource == null || valueSource.getAsString().equals(ConstString.PARAMETER_VALUE_SOURCE_DEFINITION)) + { + array.add(formatJsonObject(null, element.getAsJsonObject(), execution)); + }else{ + array.add(new JsonPrimitive(getValue(tempObj, execution))); + } + + } else if (element.isJsonArray()) { + + array = formatJsonArray(element.getAsJsonArray(), execution); + } + } + + return array; + } + + /** + * obtain jsonObject value + * @param obj + * @param execution + * @return + */ + public static String getValue(JsonObject obj, DelegateExecution execution) + { + String result = new String(); + try { + String valueSource = obj.get(ConstString.PARAMETER_VALUESOURCE).getAsString(); + String value = obj.get(ConstString.PARAMETER_VALUE).getAsString(); + if(ConstString.PARAMETER_VALUE_SOURCE_STRING.equals(valueSource)) + { + result = value; + }else if(ConstString.PARAMETER_VALUE_SOURCE_PLAN.equals(valueSource)) + { + result = HttpUtil.parsePlanExpression(value, execution); + }else if(ConstString.PARAMETER_VALUE_SOURCE_TOPOLOGY.equals(valueSource)) + { + result = HttpUtil.parseTopologyExpression(value, execution); + } + } catch (Exception e) { + logger.error("jsonObject {} is illegal!", obj.toString(), e); + throw e; + } + + return result; + } +} diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HighLevelRestApi.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HighLevelRestApi.java index 5dd2071..cd8517b 100644 --- a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HighLevelRestApi.java +++ b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HighLevelRestApi.java @@ -1,5 +1,5 @@ /** - * Copyright 2016-2017 ZTE Corporation. + * Copyright 2017 ZTE Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +23,8 @@ import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.PutMethod; import org.apache.commons.lang3.StringUtils; +import org.onap.workflow.activitiext.common.ConstString; +import org.onap.workflow.activitiext.common.RestInfo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,41 +39,43 @@ public class HighLevelRestApi { /** * invoke http service * - * @param methodValue + * @param method * @param uri - * @param requestPayload - * @param acceptValue - * @param contentTypeValue + * @param requestBody + * @param accept + * @param contentType * @return */ - public static HttpResponseMessage invoke(String methodValue, String uri, String requestPayload, String acceptValue, - String contentTypeValue) throws Exception { - - // complete uri - uri = completeUri(uri); - - logger.info("uri: " + uri); - logger.info("method: " + methodValue); - logger.info("requestbody: " + requestPayload); - logger.info("accept: " + acceptValue); - logger.info("content-type: " + contentTypeValue); - + public static HttpResponseMessage invoke(RestInfo restInfo) throws Exception { + + String realUri = restInfo.getRealUri(); + String method = restInfo.getMethod(); + String requestBody = restInfo.getRequestBody(); + String accept = restInfo.getAccept(); + String contentType = restInfo.getContentType(); + + logger.info("uri: " + realUri); + logger.info("method: " + method); + logger.info("requestbody: " + requestBody); + logger.info("accept: " + accept); + logger.info("content-type: " + contentType); + HttpResponseMessage msg = new HttpResponseMessage(); - switch (methodValue.toUpperCase()) { + switch (method.toUpperCase()) { case ConstString.HTTP_GET: - msg = HighLevelRestApi.Get(uri, acceptValue, contentTypeValue); + msg = HighLevelRestApi.Get(realUri, accept, contentType); break; case ConstString.HTTP_POST: - msg = HighLevelRestApi.Post(uri, requestPayload, acceptValue, contentTypeValue); + msg = HighLevelRestApi.Post(realUri, requestBody, accept, contentType); break; case ConstString.HTTP_DELETE: - msg = HighLevelRestApi.Delete(uri, acceptValue, contentTypeValue); + msg = HighLevelRestApi.Delete(realUri, accept, contentType); break; case ConstString.HTTP_PUT: - msg = HighLevelRestApi.Put(uri, requestPayload, acceptValue, contentTypeValue); + msg = HighLevelRestApi.Put(realUri, requestBody, accept, contentType); break; default: - throw new ActivitiException("can not find the http method type '" + methodValue + "'!"); + throw new ActivitiException("can not find the http method type '" + method + "'!"); } return msg; } @@ -81,19 +85,19 @@ public class HighLevelRestApi { * * @param uri * Resource URI - * @param requestPayload + * @param requestBody * Content which has to be put into the Resource * @return ResponseCode of HTTP Interaction */ @SuppressWarnings("deprecation") - public static HttpResponseMessage Put(String uri, String requestPayload, String acceptValue, - String contentTypeValue) { + public static HttpResponseMessage Put(String uri, String requestBody, String accept, + String contentType) throws Exception { PutMethod method = new PutMethod(uri); - HighLevelRestApi.setAcceptHeader(method, acceptValue); - HighLevelRestApi.setContentTypeHeader(method, contentTypeValue); - method.setRequestBody(requestPayload); + HighLevelRestApi.setAcceptHeader(method, accept); + HighLevelRestApi.setContentTypeHeader(method, contentType); + method.setRequestBody(requestBody); HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method); @@ -105,13 +109,13 @@ public class HighLevelRestApi { * * @param uri * Resource URI - * @param requestPayload + * @param requestBody * Content which has to be posted into the Resource * @return ResponseCode of HTTP Interaction */ @SuppressWarnings("deprecation") - public static HttpResponseMessage Post(String uri, String requestPayload, String acceptValue, - String contentTypeValue) { + public static HttpResponseMessage Post(String uri, String requestBody, String accept, + String contentType) throws Exception { PostMethod method = null; if (uri.contains("?")) { @@ -121,9 +125,9 @@ public class HighLevelRestApi { } else { method = new PostMethod(uri); } - method.setRequestBody(requestPayload); - HighLevelRestApi.setAcceptHeader(method, acceptValue); - HighLevelRestApi.setContentTypeHeader(method, contentTypeValue); + method.setRequestBody(requestBody); + HighLevelRestApi.setAcceptHeader(method, accept); + HighLevelRestApi.setContentTypeHeader(method, contentType); HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method); return responseMessage; } @@ -135,7 +139,7 @@ public class HighLevelRestApi { * Resource URI * @return Content represented by the Resource URI */ - public static HttpResponseMessage Get(String uri, String acceptValue, String contentTypeValue) { + public static HttpResponseMessage Get(String uri, String accept, String contentType) throws Exception { GetMethod method = null; if (uri.contains("?")) { String[] split = uri.split("\\?"); @@ -145,13 +149,13 @@ public class HighLevelRestApi { method = new GetMethod(uri); } - HighLevelRestApi.setAcceptHeader(method, acceptValue); - HighLevelRestApi.setContentTypeHeader(method, contentTypeValue); + HighLevelRestApi.setAcceptHeader(method, accept); + HighLevelRestApi.setContentTypeHeader(method, contentType); HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method); return responseMessage; } - private static NameValuePair[] createNameValuePairArrayFromQuery(String query) { + static NameValuePair[] createNameValuePairArrayFromQuery(String query) { String[] pairs = query.trim().split("&"); NameValuePair[] nameValuePairArray = new NameValuePair[pairs.length]; int count = 0; @@ -173,38 +177,32 @@ public class HighLevelRestApi { * Resource URI * @return ResponseCode of HTTP Interaction */ - public static HttpResponseMessage Delete(String uri, String acceptValue, String contentTypeValue) { + public static HttpResponseMessage Delete(String uri, String accept, String contentType) throws Exception { DeleteMethod method = new DeleteMethod(uri); - HighLevelRestApi.setAcceptHeader(method, acceptValue); - HighLevelRestApi.setContentTypeHeader(method, contentTypeValue); + HighLevelRestApi.setAcceptHeader(method, accept); + HighLevelRestApi.setContentTypeHeader(method, contentType); HttpResponseMessage responseMessage = LowLevelRestApi.executeHttpMethod(method); return responseMessage; } private static void setAcceptHeader(HttpMethodBase method, String value) { - if (!StringUtils.isEmpty(value)) { + if (StringUtils.isNotEmpty(value)) { + if(value.startsWith("[")&&value.endsWith("]")){ + value = value.substring(1, value.length()-1); + } + method.setRequestHeader("Accept", value); - } else { - method.setRequestHeader("Accept", "application/xml"); } } private static void setContentTypeHeader(HttpMethodBase method, String value) { - if (!StringUtils.isEmpty(value)) { + if (StringUtils.isNotEmpty(value)) { + if(value.startsWith("[")&&value.endsWith("]")){ + value = value.substring(1, value.length()-1); + } + method.setRequestHeader("content-type", value); - } else { - method.setRequestHeader("content-type", "application/json"); } } - - private static String completeUri(String uri) { - - String urlReg = "^http[\\s\\S]*"; - if (uri != null && !uri.matches(urlReg)) { - uri = PropertyUtil.getBasePath() + uri; - } - - return uri; - } } diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HttpResponseMessage.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HttpResponseMessage.java index 2128c18..ac80b50 100644 --- a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HttpResponseMessage.java +++ b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HttpResponseMessage.java @@ -1,28 +1,28 @@ /** - * Copyright 2017 ZTE Corporation. + * Copyright 2017 [ZTE] and others. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. */ package org.onap.workflow.activitiext.restservicetask; import java.io.Serializable; +import com.alibaba.fastjson.JSON; + /** */ public class HttpResponseMessage implements Serializable { private int statusCode; - private String responseBody; + private JSON responseBody; /** @@ -42,15 +42,20 @@ public class HttpResponseMessage implements Serializable { /** * @return the responseBody */ - public String getResponseBody() { + public JSON getResponseBody() { return this.responseBody; } /** * @param responseBody the responseBody to set */ - protected void setResponseBody(String responseBody) { + protected void setResponseBody(JSON responseBody) { this.responseBody = responseBody; } + + @Override + public String toString() { + return "{statusCode=" + statusCode + ", responseBody=" + responseBody.toJSONString() + "}"; + } } diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HttpUtil.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HttpUtil.java index 75a6b2f..0acab5f 100644 --- a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HttpUtil.java +++ b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/HttpUtil.java @@ -1,5 +1,5 @@ /**
- * Copyright 2016-2017 ZTE Corporation.
+ * Copyright 2017 ZTE Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,9 +15,11 @@ */
package org.onap.workflow.activitiext.restservicetask;
-import java.util.HashMap;
+import java.util.LinkedList;
import java.util.List;
-import java.util.Map;
+import java.util.Queue;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.delegate.BpmnError;
@@ -26,11 +28,18 @@ import org.activiti.engine.delegate.Expression; import org.activiti.engine.delegate.JavaDelegate;
import org.activiti.engine.impl.context.Context;
import org.apache.commons.lang3.StringUtils;
+import org.onap.workflow.activitiext.common.ConstString;
+import org.onap.workflow.activitiext.common.Parameter;
+import org.onap.workflow.activitiext.common.RestInfo;
+import org.onap.workflow.utils.MsbUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.google.gson.JsonPrimitive;
/**
* rest service
@@ -42,7 +51,10 @@ public class HttpUtil implements JavaDelegate { private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);
- private Expression uri;
+ private Expression name;
+ private Expression version;
+ private Expression url;
+ private Expression path;
private Expression method;
private Expression accept;
private Expression contentType;
@@ -54,7 +66,6 @@ public class HttpUtil implements JavaDelegate { try {
this.executeMethod(execution);
} catch (Exception e) {
-
logger.error("Invoke rest service failed!", e);
throw new BpmnError(e.getMessage());
}
@@ -65,40 +76,38 @@ public class HttpUtil implements JavaDelegate { *
* @param execution
*/
- public boolean executeMethod(DelegateExecution execution) throws Exception {
+ public void executeMethod(DelegateExecution execution) throws Exception {
+
+ // get rest task information
+ RestInfo restInfo = new RestInfo();
+ restInfo.setName(getValue(name, execution));
+ restInfo.setVersion(getValue(version, execution));
+ restInfo.setUrl(getValue(url, execution));
+ restInfo.setPath(getValue(path, execution));
+ restInfo.setMethod(getValue(method, execution));
+ restInfo.setAccept(getValue(accept, execution));
+ restInfo.setContentType(getValue(contentType, execution));
- String uriValue = getValue(uri, execution);
- String methodValue = getValue(method, execution);
- String acceptValue = getValue(accept, execution);
- String contentTypeValue = getValue(contentType, execution);
String parametersValue = getValue(parameters, execution);
- Map<String, String> requestBody = new HashMap<String, String>();
+ // real uri
+ compeleteUri(restInfo);
if (!StringUtils.isEmpty(parametersValue)) {
-
// Parse the parameter into Object List
List<Parameter> parameters = JSONArray.parseArray(parametersValue, Parameter.class);
for (Parameter param : parameters) {
- handleParam(execution, param, requestBody, uriValue);
+ restInfo = handleParam(execution, param, restInfo);
}
}
- String requestPayload = JSON.toJSONString(requestBody);
-
// invoke http service
- HttpResponseMessage msg = HighLevelRestApi.invoke(methodValue, uriValue, requestPayload, acceptValue,
- contentTypeValue);
+ HttpResponseMessage msg = HighLevelRestApi.invoke(restInfo);
// inject the result to variable
execution.setVariable(execution.getCurrentActivityId(), msg);
-
- logger.info("statusCode: " + msg.getStatusCode());
- logger.info("responseBody: " + msg.getResponseBody());
-
- return true;
}
/**
@@ -108,37 +117,58 @@ public class HttpUtil implements JavaDelegate { * @param requestBody
* @param uriValue
*/
- public void handleParam(DelegateExecution execution, Parameter param, Map<String, String> requestBody,
- String uriValue) throws ActivitiException {
+ public RestInfo handleParam(DelegateExecution execution, Parameter param, RestInfo restInfo)
+ throws ActivitiException {
- if (ConstString.PARAMETER_TYPE_EXPRESSION.equals(param.getType())) {
- handleExpression(execution, param);
+ String realUri = restInfo.getRealUri();
+
+ if (ConstString.PARAMETER_VALUE_SOURCE_PLAN.equals(param.getValueSource())) {
+
+ String planValue = parsePlanExpression(param.getValue(), execution);
+ param.setValue(planValue);
+ } else if (ConstString.PARAMETER_VALUE_SOURCE_TOPOLOGY.equals(param.getValueSource())) {
+
+ String topValue = parseTopologyExpression(param.getValue(), execution);
+ param.setValue(topValue);
+ }else if(ConstString.PARAMETER_VALUE_SOURCE_VARIABLE.equals(param.getValueSource())) {
+
+ String varValue = parseVariableExpression(param.getValue(), execution);
+ param.setValue(varValue);
}
switch (param.getPosition()) {
case ConstString.PARAMETER_POSITION_PATH:
// replace the path parameter
- uriValue = uriValue.replaceAll("\\{+" + param.getName() + "+\\}", param.getValue());
+ realUri = realUri.replaceAll("\\{+" + param.getName() + "+\\}", param.getValue());
+ restInfo.setRealUri(realUri);
break;
case ConstString.PARAMETER_POSITION_QUERY:
// add the query parameter
- if (!uriValue.contains("?")) {
- uriValue = uriValue + "?" + param.getName() + "=" + param.getValue();
+ if (!realUri.contains("?")) {
+ realUri = realUri + "?" + param.getName() + "=" + param.getValue();
} else {
- uriValue = uriValue + "&" + param.getName() + "=" + param.getValue();
+ realUri = realUri + "&" + param.getName() + "=" + param.getValue();
}
+ restInfo.setRealUri(realUri);
break;
case ConstString.PARAMETER_POSITION_BODY:
// add parameter to request body
- requestBody.put(param.getName(), param.getValue());
+ String value = param.getValue();
+
+ JsonObject obj = new JsonParser().parse(value).getAsJsonObject();
+
+ JsonObject res = GsonUtil.formatJsonObject(null, obj, execution);
+
+ restInfo.setRequestBody(res.toString());
break;
default:
- throw new ActivitiException(
- "The position '" + param.getPosition() + "' is illegal, please check your input!");
+ logger.info("The position {} is illegal, please check your input!",param.getPosition());
}
+
+ return restInfo;
}
/**
@@ -150,27 +180,176 @@ public class HttpUtil implements JavaDelegate { */
public String getValue(Expression expression, DelegateExecution execution) {
- String value = new String();
+ String result = new String();
if (expression != null) {
- value = (String) expression.getValue(execution);
+ result = (String) expression.getValue(execution);
}
- return value;
+ return result;
}
/**
- * parse expression in the parameter
+ * parse plan expression
*
* @param execution
- * @param param
+ * @param expStr
+ * @return
*/
- public void handleExpression(DelegateExecution execution, Parameter param) {
+ public static String parsePlanExpression(String expStr, DelegateExecution execution) {
- Expression expression = Context.getProcessEngineConfiguration().getExpressionManager()
- .createExpression(param.getValue());
+ String result = "";
+ String key = "";
+ try {
- String value = String.valueOf(expression.getValue(execution));
+ Queue<String> planQueue = parseExpressionToQueue(expStr);
+ Object planObj = execution.getVariable(planQueue.poll());
+
+ if (planQueue.isEmpty()) {
+ result = String.valueOf(planObj);
+ } else if(planObj instanceof HttpResponseMessage) {
+
+ JsonObject jsonObj = new JsonParser().parse(planObj.toString()).getAsJsonObject();
+ while (!planQueue.isEmpty()) {
+
+ key = planQueue.poll();
+ Object obj = jsonObj.get(key);
+ if (obj instanceof JsonArray) {
+
+ break;
+ }else if(obj instanceof JsonObject){
+
+ jsonObj = (JsonObject) obj;
+ }else if(obj instanceof JsonPrimitive){
+
+ JsonPrimitive jsonPri = (JsonPrimitive)obj;
+ result = jsonPri.getAsString();
+ }
+ }
+ }
+ } catch (Exception e) {
+ logger.error("expression {} is illegal, parse key {} failed!", expStr, key, e);
+ throw e;
+ }
- param.setValue(value);
+ return result;
}
-}
\ No newline at end of file +
+ /**
+ * parse topology expression
+ *
+ * @param expStr
+ * @param csarId
+ * @return
+ */
+ public static String parseTopologyExpression(String expStr, DelegateExecution execution) {
+
+ String csarId = getCsarId(execution);
+ Queue<String> topQueue = parseExpressionToQueue(expStr);
+
+ // parse topology expression
+ if (topQueue.size() != 2) {
+ logger.error("topology data {} is illegal!", expStr);
+ return new String();
+ }
+
+ // obtain topology json data
+ String topJsonStr = CatalogServiceConsumer.getTopologyJson(csarId, topQueue.poll());
+ JsonObject topJsonObj = new JsonParser().parse(topJsonStr).getAsJsonObject();
+
+ // obtain topology value
+ String topValue = topJsonObj.get(topQueue.poll()).getAsString();
+
+ logger.info("topology expression {} : {}", expStr, topValue);
+
+ return topValue;
+ }
+
+ /**
+ * parse variable expression
+ *
+ * @param execution
+ * @param expStr
+ * @return
+ */
+ public static String parseVariableExpression(String expStr, DelegateExecution execution) {
+
+ String result = "";
+
+ try {
+ Expression expression = Context.getProcessEngineConfiguration().getExpressionManager()
+ .createExpression(expStr);
+
+ result = String.valueOf(expression.getValue(execution));
+ } catch (Exception e) {
+ logger.error("variable expression illegal!",e);
+ }
+
+ return result;
+ }
+
+ /**
+ * obtain csar id
+ *
+ * @param execution
+ */
+ private static String getCsarId(DelegateExecution execution) {
+
+ String csarId = "";
+ csarId = String.valueOf(execution.getVariable(ConstString.CSARID_EXPRESSION));
+
+ return csarId;
+ }
+
+ /**
+ * compelete uri
+ *
+ * @param restInfo
+ * @return
+ */
+ private static String compeleteUri(RestInfo restInfo) {
+
+ String publishUrl = "";
+ try {
+ publishUrl = new MsbUtils().getServiceAddress(restInfo.getName(), restInfo.getVersion());
+ } catch (Exception e) {
+ logger.error("get service publish address error!", e);
+ }
+ String realUri = null;
+ if (StringUtils.isNotEmpty(publishUrl)) {
+ realUri = publishUrl + restInfo.getPath();
+ } else {
+ realUri = PropertyUtil.getBasePath() + restInfo.getUrl() + restInfo.getPath();
+ }
+
+ logger.info("realUri is " + realUri);
+ restInfo.setRealUri(realUri);
+
+ return realUri;
+ }
+
+ /**
+ * prase expression as a queue
+ *
+ * @param expStr
+ * @return
+ */
+ private static Queue<String> parseExpressionToQueue(String expStr) {
+
+ Queue<String> queue = new LinkedList<String>();
+
+ if (StringUtils.isNotEmpty(expStr) && expStr.startsWith("[")) {
+
+ Pattern p = Pattern.compile("[\\[]+[^]]+[\\]]");
+ Matcher m = p.matcher(expStr);
+ String key = "";
+ while (m.find()) {
+ key = m.group();
+ key = key.substring(1, key.length() - 1);
+ queue.offer(key);
+ }
+ }
+
+ return queue;
+ }
+
+}
diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/ActivitiExtAppInit.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/ICatalogService.java index 0291e3b..81081b5 100644 --- a/activiti-extension/src/main/java/org/onap/workflow/activitiext/ActivitiExtAppInit.java +++ b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/ICatalogService.java @@ -13,27 +13,21 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package org.onap.workflow.activitiext.restservicetask; -package org.onap.workflow.activitiext; +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; -import javax.annotation.PostConstruct; -import javax.inject.Inject; - -import org.jvnet.hk2.annotations.Service; -import org.onap.workflow.activitiext.common.Config; - - -@Service -public class ActivitiExtAppInit { - @Inject - private ActivitiExtAppConfig appconfig; - - @PostConstruct - private void run() { - runConfigComponent(); - } - - private void runConfigComponent() { - Config.getInstance().setAppconfig(appconfig); - } +@Path("/") +public interface ICatalogService { + + @Path("csars/{csarId}/tosca/nodes/{nodeName}/properties") + @GET + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + String getTopologyJson(@PathParam("csarId") String csarId, @PathParam("nodeName") String nodeName); } diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/LowLevelRestApi.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/LowLevelRestApi.java index 9171c31..25dc873 100644 --- a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/LowLevelRestApi.java +++ b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/LowLevelRestApi.java @@ -1,79 +1,89 @@ /** - * Copyright 2017 ZTE Corporation. + * Copyright 2017 [ZTE] and others. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. */ package org.onap.workflow.activitiext.restservicetask; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.alibaba.fastjson.JSON; /** - * This static-class eases HTTP-method execution by self-managed fault-handling and automated - * Response-information processing + * This static-class eases HTTP-method execution by self-managed fault-handling + * and automated Response-information processing */ public class LowLevelRestApi { - // Local HttpClient used for every communication (Singleton implementation) - private static HttpClient httpClient = new HttpClient(); - - - /** - * Executes a passed HttpMethod (Method type is either PUT, POST, GET or DELETE) and returns a - * HttpResponseMessage - * - * @param method Method to execute - * @return HttpResponseMessage which contains all information about the execution - */ - public static HttpResponseMessage executeHttpMethod(HttpMethod method) { + private static final Logger logger = LoggerFactory.getLogger(LowLevelRestApi.class); - HttpResponseMessage responseMessage = null; + // Local HttpClient used for every communication (Singleton implementation) + private static HttpClient httpClient = new HttpClient(); - try { + /** + * Executes a passed HttpMethod (Method type is either PUT, POST, GET or + * DELETE) and returns a HttpResponseMessage + * + * @param method + * Method to execute + * @return HttpResponseMessage which contains all information about the + * execution + */ + public static HttpResponseMessage executeHttpMethod(HttpMethod method) throws Exception { - // Execute Request - LowLevelRestApi.httpClient.executeMethod(method); - responseMessage = LowLevelRestApi.extractResponseInformation(method); + HttpResponseMessage responseMessage = null; - } catch (Exception e) { - e.printStackTrace(); - } finally { + try { - // Release Connection anyway - method.releaseConnection(); - } + // Execute Request + LowLevelRestApi.httpClient.executeMethod(method); + responseMessage = LowLevelRestApi.extractResponseInformation(method); + + logger.info("statusCode: {}", method.getStatusCode()); + logger.info("responseBody: {}", method.getResponseBodyAsString()); + } catch (Exception e) { + logger.error("visit interface failed!", e); + throw e; + } finally { - // Extract response information and return - return responseMessage; - } + // Release Connection anyway + method.releaseConnection(); + } - /** - * Extracts the response information from an executed HttpMethod - * - * @param method Executed Method - * @return Packaged response information - */ - private static HttpResponseMessage extractResponseInformation(HttpMethod method) { - // Create and return HttpResponseMethod - HttpResponseMessage responseMessage = new HttpResponseMessage(); - responseMessage.setStatusCode(method.getStatusCode()); - try { - responseMessage.setResponseBody(method.getResponseBodyAsString()); - } catch (Exception e) { - e.printStackTrace(); - } - return responseMessage; + // Extract response information and return + return responseMessage; + } - } + /** + * Extracts the response information from an executed HttpMethod + * + * @param method + * Executed Method + * @return Packaged response information + */ + private static HttpResponseMessage extractResponseInformation(HttpMethod method) { + // Create and return HttpResponseMethod + HttpResponseMessage responseMessage = new HttpResponseMessage(); + responseMessage.setStatusCode(method.getStatusCode()); + try { + JSON json = (JSON) JSON.parse(method.getResponseBodyAsString()); + responseMessage.setResponseBody(json); + json.toJSONString(); + } catch (Exception e) { + logger.info("response is not a json!"); + } + return responseMessage; + } } diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/Parameter.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/Parameter.java deleted file mode 100644 index 9397d33..0000000 --- a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/Parameter.java +++ /dev/null @@ -1,56 +0,0 @@ -/**
- * Copyright 2017 ZTE Corporation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.onap.workflow.activitiext.restservicetask;
-
-public class Parameter {
-
- private String position;
- private String type;
- private String name;
- private String value;
-
- public String getPosition() {
- return position;
- }
-
- public void setPosition(String position) {
- this.position = position;
- }
-
- public String getType() {
- return type;
- }
-
- public void setType(String type) {
- this.type = type;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public String getValue() {
- return value;
- }
-
- public void setValue(String value) {
- this.value = value;
- }
-}
diff --git a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/PropertyUtil.java b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/PropertyUtil.java index 5391757..48e2ab6 100644 --- a/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/PropertyUtil.java +++ b/activiti-extension/src/main/java/org/onap/workflow/activitiext/restservicetask/PropertyUtil.java @@ -1,46 +1,47 @@ /**
- * Copyright 2017 ZTE Corporation.
+ * Copyright 2017 [ZTE] and others.
*
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
+ * in compliance with the License. You may obtain a copy of the License at
*
- * http://www.apache.org/licenses/LICENSE-2.0
+ * http://www.apache.org/licenses/LICENSE-2.0
*
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the License for the specific language governing permissions and limitations under
+ * the License.
*/
-
package org.onap.workflow.activitiext.restservicetask;
import java.util.ResourceBundle;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
public class PropertyUtil {
+ private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class);
+
public static final String REST_PROPERTY_LOCATION = "config";
public static final String BASE_URL = "baseurl";
- private static ResourceBundle resource;
-
- static{
- resource = ResourceBundle.getBundle(REST_PROPERTY_LOCATION);
- }
-
public static String findByKey(String key){
- String value = resource.getString(key);
-
- return value;
+ try {
+ ResourceBundle resource = ResourceBundle.getBundle(REST_PROPERTY_LOCATION);
+
+ String value = resource.getString(key);
+
+ return value;
+ } catch (Exception e) {
+ logger.error("read resource failed!", e);
+ throw e;
+ }
}
public static String getBasePath(){
- String basepath = findByKey(BASE_URL);
-
- return basepath;
+ return findByKey(BASE_URL);
}
}
diff --git a/activiti-extension/src/main/java/org/onap/workflow/utils/MsbUtils.java b/activiti-extension/src/main/java/org/onap/workflow/utils/MsbUtils.java new file mode 100644 index 0000000..4757237 --- /dev/null +++ b/activiti-extension/src/main/java/org/onap/workflow/utils/MsbUtils.java @@ -0,0 +1,51 @@ +/** + * Copyright 2017 ZTE Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.workflow.utils; + +import org.apache.commons.lang3.StringUtils; +import org.jvnet.hk2.annotations.Service; +import org.onap.msb.sdk.discovery.MSBService; +import org.onap.msb.sdk.discovery.common.RouteException; +import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo; +import org.onap.workflow.activitiext.restservicetask.PropertyUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Service +public class MsbUtils { + + private static final Logger LOG = LoggerFactory.getLogger(MsbUtils.class); + + public static String getServiceAddress(String serviceName, String version) throws RouteException { + + String baseUrl = ""; + String msbIp = System.getenv("OPENPALETTE_MSB_IP"); + String msbPort = System.getenv("OPENPALETTE_MSB_PORT"); + + if(StringUtils.isNotEmpty(msbIp) && StringUtils.isNotEmpty(msbPort)) + { + baseUrl = "http://" + msbIp + ":" + msbPort; + }else{ + baseUrl = PropertyUtil.getBasePath(); + } + + MicroServiceFullInfo msbInfo = new MSBService().queryMicroServiceInfo(baseUrl, serviceName, version); + + LOG.info("publishUrl: {}", msbInfo.getUrl()); + + return msbInfo.getUrl(); + } +} |