Support Resource Recipe Common Model 07/33507/1
authorc00149107 <chenchuanyu@huawei.com>
Thu, 1 Mar 2018 02:20:31 +0000 (10:20 +0800)
committerc00149107 <chenchuanyu@huawei.com>
Thu, 1 Mar 2018 02:20:31 +0000 (10:20 +0800)
Support Resource Recipe Common Model

Change-Id: I5df1602fb9d43b85d92a1a549e012ccae47760dc
Issue-ID: SO-452
Signed-off-by: c00149107 <chenchuanyu@huawei.com>
bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/recipe/BpmnParam.java [new file with mode: 0644]
bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/recipe/BpmnRestClient.java [new file with mode: 0644]
bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/recipe/ResourceRecipeRequest.java [new file with mode: 0644]

diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/recipe/BpmnParam.java b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/recipe/BpmnParam.java
new file mode 100644 (file)
index 0000000..f4ebd06
--- /dev/null
@@ -0,0 +1,54 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - SO
+ * ================================================================================
+ * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.mso.bpmn.common.recipe;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+/**
+ *  The bpmn workflow input param object
+ */
+public class BpmnParam {
+       
+       @JsonProperty("value")
+       private String value;
+       @JsonProperty("type")
+       private String type = "String";
+
+
+       public BpmnParam() {
+               /* Empty constructor */
+       }
+       
+       @JsonProperty("value")
+       public String getValue() {
+               return value;
+       }
+       
+       @JsonProperty("type")
+       public void setValue(String value) {
+               this.value = value;
+       }
+
+       @Override
+       public String toString() {
+               return "CamundaInput [value=" + value + ", type=" + type + "]";
+       }       
+       
+}
diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/recipe/BpmnRestClient.java b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/recipe/BpmnRestClient.java
new file mode 100644 (file)
index 0000000..446de10
--- /dev/null
@@ -0,0 +1,219 @@
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ * ONAP - SO\r
+ * ================================================================================\r
+ * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.openecomp.mso.bpmn.common.recipe;\r
+\r
+import java.io.IOException;\r
+import java.security.GeneralSecurityException;\r
+\r
+import javax.xml.bind.DatatypeConverter;\r
+\r
+import org.apache.http.HttpResponse;\r
+import org.apache.http.client.ClientProtocolException;\r
+import org.apache.http.client.HttpClient;\r
+import org.apache.http.client.config.RequestConfig;\r
+import org.apache.http.client.methods.HttpPost;\r
+import org.apache.http.entity.StringEntity;\r
+import org.apache.http.impl.client.HttpClientBuilder;\r
+import org.openecomp.mso.logger.MessageEnum;\r
+import org.openecomp.mso.logger.MsoLogger;\r
+import org.openecomp.mso.properties.MsoJavaProperties;\r
+import org.openecomp.mso.properties.MsoPropertiesFactory;\r
+import org.openecomp.mso.utils.CryptoUtils;\r
+\r
+/**\r
+ * Support to call resource recipes from the BPMN workflow.\r
+ * Such as call resource recipe in service workflow.\r
+ * <br>\r
+ * <p>\r
+ * </p>\r
+ * \r
+ * @author\r
+ * @version ONAP Beijing Release 2018-02-27\r
+ */\r
+public class BpmnRestClient {\r
+\r
+    public static final String DEFAULT_BPEL_AUTH = "admin:admin";\r
+\r
+    public static final String ENCRYPTION_KEY = "aa3871669d893c7fb8abbcda31b88b4f";\r
+\r
+    public static final String CONTENT_TYPE_JSON = "application/json";\r
+\r
+    public static final String CAMUNDA_AUTH = "camundaAuth";\r
+\r
+    private final static String MSO_PROP_APIHANDLER_INFRA = "MSO_PROP_APIHANDLER_INFRA";\r
+\r
+    private static MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();\r
+\r
+    private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);\r
+\r
+    private static boolean noProperties = true;\r
+\r
+    public synchronized static MsoJavaProperties loadMsoProperties() {\r
+        MsoJavaProperties msoProperties;\r
+        try {\r
+            msoProperties = msoPropertiesFactory.getMsoJavaProperties(MSO_PROP_APIHANDLER_INFRA);\r
+        } catch(Exception e) {\r
+            msoLogger.error(MessageEnum.APIH_LOAD_PROPERTIES_FAIL, MSO_PROP_APIHANDLER_INFRA, "", "", MsoLogger.ErrorCode.DataError,\r
+                    "Exception when loading MSO Properties", e);\r
+            return null;\r
+        }\r
+\r
+        if(msoProperties != null && msoProperties.size() > 0) {\r
+            noProperties = false;\r
+            msoLogger.info(MessageEnum.APIH_PROPERTY_LOAD_SUC, "", "");\r
+            return msoProperties;\r
+        } else {\r
+            msoLogger.error(MessageEnum.APIH_NO_PROPERTIES, MSO_PROP_APIHANDLER_INFRA, "", "", MsoLogger.ErrorCode.DataError,\r
+                    "No MSO APIH_INFRA Properties found");\r
+            return null;\r
+        }\r
+    }\r
+\r
+    public synchronized static final boolean getNoPropertiesState() {\r
+        return noProperties;\r
+    }\r
+\r
+    /**\r
+     * post the recipe Uri\r
+     * <br>\r
+     * \r
+     * @param recipeUri The request recipe uri\r
+     * @param requestId the request id\r
+     * @param recipeTimeout The recipe time out\r
+     * @param requestAction The request action\r
+     * @param serviceInstanceId The service instance id\r
+     * @param serviceType The service Type\r
+     * @param requestDetails The request Details, these information is from runtime\r
+     * @param recipeParamXsd The recipe params, its from recipe design\r
+     * @return The response of the recipe.\r
+     * @throws ClientProtocolException\r
+     * @throws IOException\r
+     * @since ONAP Beijing Release\r
+     */\r
+    public static HttpResponse post(String recipeUri, String requestId, int recipeTimeout, String requestAction, String serviceInstanceId, String serviceType,\r
+            String requestDetails, String recipeParamXsd) throws ClientProtocolException, IOException {\r
+\r
+        HttpClient client = HttpClientBuilder.create().build();\r
+\r
+        HttpPost post = new HttpPost(recipeUri);\r
+        MsoJavaProperties props = loadMsoProperties();\r
+        RequestConfig requestConfig =\r
+                RequestConfig.custom().setSocketTimeout(recipeTimeout).setConnectTimeout(recipeTimeout).setConnectionRequestTimeout(recipeTimeout).build();\r
+        post.setConfig(requestConfig);\r
+        msoLogger.debug("call the bpmn,  url:" + recipeUri);\r
+        String jsonReq = wrapResourceRequest(requestId, recipeTimeout, requestAction, serviceInstanceId, serviceType, requestDetails, recipeParamXsd);\r
+\r
+        StringEntity input = new StringEntity(jsonReq);\r
+        input.setContentType(CONTENT_TYPE_JSON);\r
+        String encryptedCredentials;\r
+        if(props != null) {\r
+            encryptedCredentials = props.getProperty(CAMUNDA_AUTH, null);\r
+            if(encryptedCredentials != null) {\r
+                String userCredentials = getEncryptedPropValue(encryptedCredentials, DEFAULT_BPEL_AUTH, ENCRYPTION_KEY);\r
+                if(userCredentials != null) {\r
+                    post.addHeader("Authorization", "Basic " + new String(DatatypeConverter.printBase64Binary(userCredentials.getBytes())));\r
+                }\r
+            }\r
+        }\r
+        post.setEntity(input);\r
+        return client.execute(post);\r
+    }\r
+\r
+    /**\r
+     * prepare the resource recipe bpmn request.\r
+     * <br>\r
+     * \r
+     * @param requestId\r
+     * @param recipeTimeout\r
+     * @param requestAction\r
+     * @param serviceInstanceId\r
+     * @param serviceType\r
+     * @param requestDetails\r
+     * @param recipeParams\r
+     * @return\r
+     * @since ONAP Beijing Release\r
+     */\r
+    private static String wrapResourceRequest(String requestId, int recipeTimeout, String requestAction, String serviceInstanceId, String serviceType,\r
+            String requestDetails, String recipeParams) {\r
+        String jsonReq = null;\r
+        if(requestId == null) {\r
+            requestId = "";\r
+        }\r
+        if(requestAction == null) {\r
+            requestAction = "";\r
+        }\r
+        if(serviceInstanceId == null) {\r
+            serviceInstanceId = "";\r
+        }\r
+\r
+        if(requestDetails == null) {\r
+            requestDetails = "";\r
+        }\r
+\r
+        try {\r
+            ResourceRecipeRequest recipeRequest = new ResourceRecipeRequest();\r
+            BpmnParam resourceInput = new BpmnParam();\r
+            BpmnParam host = new BpmnParam();\r
+            BpmnParam requestIdInput = new BpmnParam();\r
+            BpmnParam requestActionInput = new BpmnParam();\r
+            BpmnParam serviceInstanceIdInput = new BpmnParam();\r
+            BpmnParam serviceTypeInput = new BpmnParam();\r
+            BpmnParam recipeParamsInput = new BpmnParam();\r
+            // host.setValue(parseURL());\r
+            requestIdInput.setValue(requestId);\r
+            requestActionInput.setValue(requestAction);\r
+            serviceInstanceIdInput.setValue(serviceInstanceId);\r
+            recipeParamsInput.setValue(recipeParams);\r
+            resourceInput.setValue(requestDetails);\r
+            recipeRequest.setHost(host);\r
+            recipeRequest.setRequestId(requestIdInput);\r
+            recipeRequest.setRequestAction(requestActionInput);\r
+            recipeRequest.setServiceInstanceId(serviceInstanceIdInput);\r
+            recipeRequest.setServiceType(serviceTypeInput);\r
+            recipeRequest.setRecipeParams(recipeParamsInput);\r
+            jsonReq = recipeRequest.toString();\r
+            msoLogger.debug("request body is " + jsonReq);\r
+        } catch(Exception e) {\r
+            msoLogger.error(MessageEnum.APIH_WARP_REQUEST, "Camunda", "wrapVIDRequest", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH Warp request",\r
+                    e);\r
+        }\r
+        return jsonReq;\r
+    }\r
+\r
+    /**\r
+     * <br>\r
+     * \r
+     * @param prop\r
+     * @param defaultValue\r
+     * @param encryptionKey\r
+     * @return\r
+     * @since ONAP Beijing Release\r
+     */\r
+    protected static String getEncryptedPropValue(String prop, String defaultValue, String encryptionKey) {\r
+        try {\r
+            return CryptoUtils.decrypt(prop, encryptionKey);\r
+        } catch(GeneralSecurityException e) {\r
+            msoLogger.debug("Security exception", e);\r
+        }\r
+        return defaultValue;\r
+    }\r
+\r
+}\r
diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/recipe/ResourceRecipeRequest.java b/bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/bpmn/common/recipe/ResourceRecipeRequest.java
new file mode 100644 (file)
index 0000000..5bf5a02
--- /dev/null
@@ -0,0 +1,143 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - SO
+ * ================================================================================
+ * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.mso.bpmn.common.recipe;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonPropertyOrder;
+import com.fasterxml.jackson.annotation.JsonRootName;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+
+/**
+ * java object of the resource recipe , it
+ * will be passed to the Camunda process
+ */
+@JsonPropertyOrder({"resourceInput", "host", "requestId", "requestAction", "serviceInstanceId", "serviceType", "recipeParams"})
+@JsonRootName("variables")
+public class ResourceRecipeRequest {
+
+    @JsonProperty("resourceInput")
+    private BpmnParam resourceInput;
+
+    @JsonProperty("host")
+    private BpmnParam host;
+
+    @JsonProperty("requestId")
+    private BpmnParam requestId;
+
+    @JsonProperty("requestAction")
+    private BpmnParam requestAction;
+
+    @JsonProperty("serviceInstanceId")
+    private BpmnParam serviceInstanceId;
+
+    @JsonProperty("serviceType")
+    private BpmnParam serviceType;
+
+    @JsonProperty("recipeParams")
+    private BpmnParam recipeParams;
+
+    @JsonProperty("resourceInput")
+    public BpmnParam getResourceInput() {
+        return resourceInput;
+    }
+
+    @JsonProperty("resourceInput")
+    public void setResourceInput(BpmnParam resourceInput) {
+        this.resourceInput = resourceInput;
+    }
+
+    @JsonProperty("host")
+    public BpmnParam getHost() {
+        return host;
+    }
+
+    @JsonProperty("host")
+    public void setHost(BpmnParam host) {
+        this.host = host;
+    }
+
+    @JsonProperty("requestId")
+    public BpmnParam getRequestId() {
+        return requestId;
+    }
+
+    @JsonProperty("requestId")
+    public void setRequestId(BpmnParam requestId) {
+        this.requestId = requestId;
+    }
+
+    @JsonProperty("requestAction")
+    public BpmnParam getRequestAction() {
+        return requestAction;
+    }
+
+    @JsonProperty("requestAction")
+    public void setRequestAction(BpmnParam requestAction) {
+        this.requestAction = requestAction;
+    }
+
+    @JsonProperty("serviceInstanceId")
+    public BpmnParam getServiceInstanceId() {
+        return serviceInstanceId;
+    }
+
+    @JsonProperty("serviceInstanceId")
+    public void setServiceInstanceId(BpmnParam serviceInstanceId) {
+        this.serviceInstanceId = serviceInstanceId;
+    }
+
+    @JsonProperty("serviceType")
+    public BpmnParam getServiceType() {
+        return serviceType;
+    }
+
+    @JsonProperty("serviceType")
+    public void setServiceType(BpmnParam serviceType) {
+        this.serviceType = serviceType;
+    }
+
+    @JsonProperty("recipeParams")
+    public BpmnParam getRecipeParams() {
+        return recipeParams;
+    }
+
+    @JsonProperty("recipeParams")
+    public void setRecipeParams(BpmnParam recipeParams) {
+        this.recipeParams = recipeParams;
+    }
+
+    @Override
+    public String toString() {
+        ObjectMapper mapper = new ObjectMapper();
+        mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true);
+        String jsonStr = "ResourceRecipeRequest";
+        try {
+            jsonStr = mapper.writeValueAsString(this);
+        } catch(JsonProcessingException e) {
+
+            e.printStackTrace();
+        }
+        return jsonStr;
+    }
+
+}