Refactor of DecomposeJsonUtils class.

Change-Id: I72b779eb17ffb901266de605eb2eda49f13bd03e
Issue-ID: SO-374
Signed-off-by: biniek <lukasz.biniek@nokia.com>
diff --git a/bpmn/MSOCommonBPMN/src/main/groovy/org/openecomp/mso/bpmn/common/scripts/DecomposeService.groovy b/bpmn/MSOCommonBPMN/src/main/groovy/org/openecomp/mso/bpmn/common/scripts/DecomposeService.groovy
index 06cc284..2645ea3 100644
--- a/bpmn/MSOCommonBPMN/src/main/groovy/org/openecomp/mso/bpmn/common/scripts/DecomposeService.groovy
+++ b/bpmn/MSOCommonBPMN/src/main/groovy/org/openecomp/mso/bpmn/common/scripts/DecomposeService.groovy
@@ -17,7 +17,9 @@
  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
-package org.openecomp.mso.bpmn.common.scripts;
+package org.openecomp.mso.bpmn.common.scripts
+
+import org.openecomp.mso.bpmn.core.json.DecomposeJsonUtil;
 
 import static org.apache.commons.lang3.StringUtils.*;
 
@@ -96,12 +98,12 @@
 			// check for input
 			String serviceModelInvariantId = execution.getVariable("DDS_serviceModelInvariantId")
 			String modelVersion = execution.getVariable("DDS_modelVersion")
-			
+
 			utils.log("DEBUG", "serviceModelInvariantId: " + serviceModelInvariantId, isDebugEnabled)
-			utils.log("DEBUG", "modelVersion: " + modelVersion, isDebugEnabled)			
-			
+			utils.log("DEBUG", "modelVersion: " + modelVersion, isDebugEnabled)
+
 			JSONObject catalogDbResponse = null
-			
+
 			if (modelVersion != null && modelVersion.length() > 0)
 				catalogDbResponse = catalogDbUtils.getServiceResourcesByServiceModelInvariantUuidAndServiceModelVersion(execution, serviceModelInvariantId, modelVersion, "v2")
 			else
@@ -140,7 +142,7 @@
 			utils.log("DEBUG", "getting service decomposition", isDebugEnabled)
 
 			String catalogDbResponse = execution.getVariable("DDS_catalogDbResponse")
-			ServiceDecomposition serviceDecomposition = new ServiceDecomposition(catalogDbResponse, serviceInstanceId)
+			ServiceDecomposition serviceDecomposition = DecomposeJsonUtil.jsonToServiceDecomposition(catalogDbResponse, serviceInstanceId)
 
 			execution.setVariable("serviceDecomposition", serviceDecomposition)
 			execution.setVariable("serviceDecompositionString", serviceDecomposition.toJsonString())
diff --git a/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/domain/ServiceDecomposition.java b/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/domain/ServiceDecomposition.java
index 1e68c84..0e985cc 100644
--- a/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/domain/ServiceDecomposition.java
+++ b/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/domain/ServiceDecomposition.java
@@ -31,9 +31,8 @@
 import com.fasterxml.jackson.annotation.JsonProperty;

 import com.fasterxml.jackson.annotation.JsonRootName;

 

-import org.json.JSONObject;

 import org.openecomp.mso.bpmn.core.json.DecomposeJsonUtil;

-

+import org.openecomp.mso.bpmn.core.json.JsonDecomposingException;

 

 

 /**

@@ -50,7 +49,6 @@
 public class ServiceDecomposition extends JsonWrapper implements Serializable {

 

 	private static final long serialVersionUID = 1L;

-	DecomposeJsonUtil jsonUtils = new DecomposeJsonUtil();

 

 	@JsonProperty("modelInfo")

 	private ModelInfo modelInfo;

@@ -67,48 +65,6 @@
 	private List <AllottedResource>  allottedResources;

 

 	public ServiceDecomposition () {

-		super();

-	}

-

-	public ServiceDecomposition (String catalogRestOutput) {

-

-		ServiceDecomposition serviceDecomposition = DecomposeJsonUtil.JsonToServiceDecomposition(catalogRestOutput);

-		this.modelInfo = serviceDecomposition.getModelInfo();

-		this.vnfResources = serviceDecomposition.getServiceVnfs();

-		this.allottedResources = serviceDecomposition.getServiceAllottedResources();

-		this.networkResources = serviceDecomposition.getServiceNetworks();

-		this.serviceRole = serviceDecomposition.getServiceRole();

-		this.serviceType = serviceDecomposition.getServiceType();

-	}

-

-	/**

-	 * Constructor taking Catalog DB Adapter REST output (serviceResources model) + service Instance ID

-	 * @param catalogRestOutput

-	 * @param serviceInstanceId

-	 */

-	public ServiceDecomposition (String catalogRestOutput, String serviceInstanceId) {

-

-		ServiceDecomposition serviceDecomposition = DecomposeJsonUtil.JsonToServiceDecomposition(catalogRestOutput);

-		this.modelInfo = serviceDecomposition.getModelInfo();

-		this.vnfResources = serviceDecomposition.getServiceVnfs();

-		this.allottedResources = serviceDecomposition.getServiceAllottedResources();

-		this.networkResources = serviceDecomposition.getServiceNetworks();

-

-		this.serviceRole = serviceDecomposition.getServiceRole();

-		this.serviceType = serviceDecomposition.getServiceType();

-		

-		this.serviceInstance = new ServiceInstance();

-		this.serviceInstance.setInstanceId(serviceInstanceId);

-	}

-

-	/**

-	 * Constructor taking a Service Decomposition JSON serialization

-	 * @param catalogRestOutput

-	 * @param serviceInstanceId

-	 */

-	public ServiceDecomposition (JSONObject jsonServiceDecomposition, String serviceInstanceId) {

-		//TODO provide constructor implementation

-

 	}

 

 	//*****

@@ -278,7 +234,7 @@
 	 * Add resource to the list

 	 * Given a ResourceDecomposition (subclass) object, add it to the Service Decomposition (in the appropriate category, e.g. as a VNF, Network, or Allotted Resource).

 	 * As dependencies are not currently supported, add it to the end of any ordered lists.

-	 * @param Resource

+	 * @param resource

 	 */

 	public void addResource(Resource resource) {

 		//create resource based upon type

@@ -299,36 +255,36 @@
 

 	/**

 	 * Add resource to the list

-	 * @param Resource

+	 * @param jsonResource

 	 */

-	public void addVnfResource(String jsonResource) {

+	public void addVnfResource(String jsonResource) throws JsonDecomposingException {

 		VnfResource vnfResource = null;

-		vnfResource = DecomposeJsonUtil.JsonToVnfResource(jsonResource);

+		vnfResource = DecomposeJsonUtil.jsonToVnfResource(jsonResource);

 		this.addVnfResource(vnfResource);

 	}

 	/**

 	 * Add resource to the list

-	 * @param Resource

+	 * @param jsonResource

 	 */

-	public void addNetworkResource(String jsonResource) {

+	public void addNetworkResource(String jsonResource) throws JsonDecomposingException {

 		NetworkResource networkResource = null;

-		networkResource = DecomposeJsonUtil.JsonToNetworkResource(jsonResource);

+		networkResource = DecomposeJsonUtil.jsonToNetworkResource(jsonResource);

 		this.addVnfResource(networkResource);

 	}

 	/**

 	 * Add resource to the list

-	 * @param Resource

+	 * @param jsonResource

 	 */

-	public void addAllottedResource(String jsonResource) {

+	public void addAllottedResource(String jsonResource) throws JsonDecomposingException {

 		AllottedResource allottedResource = null;

-		allottedResource = DecomposeJsonUtil.JsonToAllottedResource(jsonResource);

+		allottedResource = DecomposeJsonUtil.jsonToAllottedResource(jsonResource);

 		this.addVnfResource(allottedResource);

 	}

 

 	/**

 	 * Given a ResourceDecomposition (subclass) object, locate it in the Service Decomposition by its unique ID, and replace the current version with the new one.

 	 * This method should support concurrency control via an auto-incrementing field in the ResourceDecomposition class.

-	 * @param Resource

+	 * @param newResource

 	 * @return TRUE if replacement was a success

 	 */

 	public boolean replaceResource(Resource newResource){

@@ -365,7 +321,7 @@
 

 	/**

 	 *  Given a resource object ID, locate it in the Service Decomposition by its unique ID, and delete it.

-	 * @param Resource

+	 * @param resource

 	 * @return TRUE if delete was a success

 	 */

 	public boolean deleteResource(Resource resource){

diff --git a/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/json/DecomposeJsonUtil.java b/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/json/DecomposeJsonUtil.java
index 71ef09c..dcd9e3b 100644
--- a/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/json/DecomposeJsonUtil.java
+++ b/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/json/DecomposeJsonUtil.java
@@ -20,139 +20,102 @@
 

 package org.openecomp.mso.bpmn.core.json;

 

-import java.io.Serializable;

+import com.fasterxml.jackson.databind.DeserializationFeature;

+import com.fasterxml.jackson.databind.ObjectMapper;

 import java.io.IOException;

-

-

 import org.openecomp.mso.bpmn.core.domain.AllottedResource;

 import org.openecomp.mso.bpmn.core.domain.NetworkResource;

 import org.openecomp.mso.bpmn.core.domain.ServiceDecomposition;

+import org.openecomp.mso.bpmn.core.domain.ServiceInstance;

 import org.openecomp.mso.bpmn.core.domain.VnfResource;

 

-import com.fasterxml.jackson.core.JsonParseException;

-import com.fasterxml.jackson.databind.DeserializationFeature;

-import com.fasterxml.jackson.databind.JsonMappingException;

-import com.fasterxml.jackson.databind.ObjectMapper;

+public class DecomposeJsonUtil {

 

-import org.openecomp.mso.logger.MsoLogger;

+    private static final ObjectMapper OBJECT_MAPPER = createObjectMapper();

 

-public class DecomposeJsonUtil implements Serializable {

-	private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);

-	/**

-	 * 

-	 */

-	private static final long serialVersionUID = 1L;

+    private DecomposeJsonUtil() {

+    }

 

-	/**

-	 * Method to construct Service Decomposition object converting

-	 * JSON structure

-	 * 

-	 * @param jsonString - input in JSON format confirming ServiceDecomposition

-	 * @return - ServiceDecomposition object

-	 * @throws IOException 

-	 * @throws JsonMappingException 

-	 * @throws JsonParseException 

-	 */

-	public static ServiceDecomposition JsonToServiceDecomposition(String jsonString) {

-        

-        ServiceDecomposition serviceDecomposition = new ServiceDecomposition();

+    private static ObjectMapper createObjectMapper() {

         ObjectMapper om = new ObjectMapper();

         om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);

-       

-		try {

-			serviceDecomposition = om.readValue(jsonString, ServiceDecomposition.class);

-		} catch (JsonParseException e) {

-			LOGGER.debug("JsonParseException :",e);

-		} catch (JsonMappingException e) {

-			LOGGER.debug("JsonMappingException :",e);

-		} catch (IOException e) {

-			LOGGER.debug("IOException :",e);

-		}

-		

-		return serviceDecomposition;

-	}

-	

-	/**

-	 * Method to construct Resource Decomposition object converting

-	 * JSON structure

-	 * 

-	 * @param jsonString - input in JSON format confirming ResourceDecomposition

-	 * @return - ServiceDecomposition object

-	 * @throws IOException 

-	 * @throws JsonMappingException 

-	 * @throws JsonParseException 

-	 */

-	public static VnfResource JsonToVnfResource(String jsonString) {

-        

-        VnfResource vnfResource = new VnfResource();

-        ObjectMapper om = new ObjectMapper();

-        om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);

-       

-		try {

-			vnfResource = om.readValue(jsonString, VnfResource.class);

-		} catch (JsonParseException e) {

-			LOGGER.debug("JsonParseException :",e);

-		} catch (JsonMappingException e) {

-			LOGGER.debug("JsonMappingException :",e);

-		} catch (IOException e) {

-			LOGGER.debug("IOException :",e);

-		}

-		return vnfResource;

-	}

-	

-	/**

-	 * Method to construct Resource Decomposition object converting

-	 * JSON structure

-	 * 

-	 * @param jsonString - input in JSON format confirming ResourceDecomposition

-	 * @return - ServiceDecomposition object

-	 * @throws IOException 

-	 * @throws JsonMappingException 

-	 * @throws JsonParseException 

-	 */

-	public static NetworkResource JsonToNetworkResource(String jsonString) {

-        

-        NetworkResource networkResource = new NetworkResource();

-        ObjectMapper om = new ObjectMapper();

-        om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);

-       

-		try {

-			networkResource = om.readValue(jsonString, NetworkResource.class);

-		} catch (JsonParseException e) {

-			LOGGER.debug("Exception :",e);

-		} catch (JsonMappingException e) {

-			LOGGER.debug("Exception :",e);

-		} catch (IOException e) {

-			LOGGER.debug("Exception :",e);

-		}

-		return networkResource;

-	}

-	

-	/**

-	 * Method to construct Resource Decomposition object converting

-	 * JSON structure

-	 * 

-	 * @param jsonString - input in JSON format confirming ResourceDecomposition

-	 * @return - ServiceDecomposition object

-	 * @throws IOException 

-	 * @throws JsonMappingException 

-	 * @throws JsonParseException 

-	 */

-	public static AllottedResource JsonToAllottedResource(String jsonString) {

-        

-		AllottedResource allottedResource = new AllottedResource();

-        ObjectMapper om = new ObjectMapper();

-        om.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);

-       

-		try {

-			allottedResource = om.readValue(jsonString, AllottedResource.class);

-		} catch (JsonParseException e) {

-			LOGGER.debug("Exception :",e);

-		} catch (JsonMappingException e) {

-			LOGGER.debug("Exception :",e);

-		} catch (IOException e) {

-			LOGGER.debug("Exception :",e);

-		}

-		return allottedResource;

-	}

+        return om;

+    }

+

+    /**

+     * Method to construct Service Decomposition object converting JSON structure

+     *

+     * @param jsonString input in JSON format confirming ServiceDecomposition

+     * @return decoded object

+     * @throws JsonDecomposingException thrown when decoding json fails

+     */

+    public static ServiceDecomposition jsonToServiceDecomposition(String jsonString) throws JsonDecomposingException {

+        try {

+            return OBJECT_MAPPER.readValue(jsonString, ServiceDecomposition.class);

+        } catch (IOException e) {

+            throw new JsonDecomposingException("Exception while converting json to service decomposition", e);

+        }

+    }

+

+    /**

+     * Method to construct Service Decomposition object converting JSON structure

+     *

+     * @param jsonString input in JSON format confirming ServiceDecomposition

+     * @param serviceInstanceId service instance id to be put in decoded ServiceDecomposition

+     * @return decoded object

+     * @throws JsonDecomposingException thrown when decoding json fails

+     */

+    public static ServiceDecomposition jsonToServiceDecomposition(String jsonString, String serviceInstanceId)

+            throws JsonDecomposingException {

+        ServiceDecomposition serviceDecomposition = jsonToServiceDecomposition(jsonString);

+        ServiceInstance serviceInstance = new ServiceInstance();

+        serviceInstance.setInstanceId(serviceInstanceId);

+        serviceDecomposition.setServiceInstance(serviceInstance);

+        return serviceDecomposition;

+    }

+

+    /**

+     * Method to construct Resource Decomposition object converting JSON structure

+     *

+     * @param jsonString input in JSON format confirming ResourceDecomposition

+     * @return decoded object

+     * @throws JsonDecomposingException thrown when decoding json fails

+     */

+    public static VnfResource jsonToVnfResource(String jsonString) throws JsonDecomposingException {

+        try {

+            return OBJECT_MAPPER.readValue(jsonString, VnfResource.class);

+        } catch (IOException e) {

+            throw new JsonDecomposingException("Exception while converting json to vnf resource", e);

+        }

+    }

+

+    /**

+     * Method to construct Resource Decomposition object converting JSON structure

+     *

+     * @param jsonString input in JSON format confirming ResourceDecomposition

+     * @return decoded object

+     * @throws JsonDecomposingException thrown when decoding json fails

+     */

+    public static NetworkResource jsonToNetworkResource(String jsonString) throws JsonDecomposingException {

+        try {

+            return OBJECT_MAPPER.readValue(jsonString, NetworkResource.class);

+        } catch (IOException e) {

+            throw new JsonDecomposingException("Exception while converting json to network resource", e);

+        }

+    }

+

+    /**

+     * Method to construct Resource Decomposition object converting JSON structure

+     *

+     * @param jsonString - input in JSON format confirming ResourceDecomposition

+     * @return decoded object

+     * @throws JsonDecomposingException thrown when decoding json fails

+     */

+    public static AllottedResource jsonToAllottedResource(String jsonString) throws JsonDecomposingException {

+        try {

+            return OBJECT_MAPPER.readValue(jsonString, AllottedResource.class);

+        } catch (IOException e) {

+            throw new JsonDecomposingException("Exception while converting json to allotted resource", e);

+        }

+    }

 }
\ No newline at end of file
diff --git a/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/json/JsonDecomposingException.java b/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/json/JsonDecomposingException.java
new file mode 100644
index 0000000..352979e
--- /dev/null
+++ b/bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/json/JsonDecomposingException.java
@@ -0,0 +1,30 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - SO
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. 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.core.json;
+
+import java.io.IOException;
+
+public class JsonDecomposingException extends IOException {
+
+    public JsonDecomposingException(String message, Throwable cause) {
+        super(message, cause);
+    }
+}
diff --git a/bpmn/MSOInfrastructureBPMN/src/main/groovy/org/openecomp/mso/bpmn/infrastructure/scripts/DoCreateVnfAndModules.groovy b/bpmn/MSOInfrastructureBPMN/src/main/groovy/org/openecomp/mso/bpmn/infrastructure/scripts/DoCreateVnfAndModules.groovy
index 862c46a..3088340 100644
--- a/bpmn/MSOInfrastructureBPMN/src/main/groovy/org/openecomp/mso/bpmn/infrastructure/scripts/DoCreateVnfAndModules.groovy
+++ b/bpmn/MSOInfrastructureBPMN/src/main/groovy/org/openecomp/mso/bpmn/infrastructure/scripts/DoCreateVnfAndModules.groovy
@@ -54,7 +54,6 @@
    JsonUtils jsonUtil = new JsonUtils()
    VidUtils vidUtils = new VidUtils(this)
    CatalogDbUtils cutils = new CatalogDbUtils()
-   DecomposeJsonUtil decomposeJsonUtil = new DecomposeJsonUtil()
 
    /**
 	* This method gets and validates the incoming
@@ -185,7 +184,7 @@
 			   utils.log("DEBUG", "obtained VNF list")
 			   // Only one match here
 			   JSONObject vnfObject = vnfs[0]
-			   vnf = decomposeJsonUtil.JsonToVnfResource(vnfObject.toString())			   
+			   vnf = DecomposeJsonUtil.jsonToVnfResource(vnfObject.toString())
 		   }
 		   utils.log("DEBUG", "Read vnfResource", isDebugLogEnabled)
 		   if (vnf == null) {