Merge "resolve Activation/Deactivation DB data modification problem"
diff --git a/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/impl/Constants.java b/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/impl/Constants.java
index 294dccd..da0fcd3 100644
--- a/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/impl/Constants.java
+++ b/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/impl/Constants.java
@@ -42,6 +42,7 @@
     public static final String REQUEST_TUNABLES = "org.onap.so.adapters.sdnc";
 
     public static final String MSO_ACTION_LCM = "lcm";
+    public static final String MSO_ACTION_MDONS = "opticalservice";
     public static final String LCM_API_VER = "2.00";
     public static final int LCM_FLAGS_TTL = 65000;
     public static final String LCM_ORIGINATOR_ID = "MSO";
diff --git a/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/impl/SDNCRestClient.java b/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/impl/SDNCRestClient.java
index f3bf8c2..b8977f0 100644
--- a/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/impl/SDNCRestClient.java
+++ b/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/impl/SDNCRestClient.java
@@ -112,6 +112,8 @@
             Document reqDoc = node.getOwnerDocument();
             if (Constants.MSO_ACTION_LCM.equals(msoAction)) {
                 sdncReqBody = Utils.genSdncLcmReq(reqDoc, rt);
+            } else if (Constants.MSO_ACTION_MDONS.equals(msoAction)) {
+                sdncReqBody = Utils.genOpticalSdncReq(reqDoc, rt);
             } else {
                 sdncReqBody = Utils.genSdncReq(reqDoc, rt);
             }
diff --git a/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/impl/Utils.java b/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/impl/Utils.java
index 2f6ec00..f52ebca 100644
--- a/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/impl/Utils.java
+++ b/adapters/mso-sdnc-adapter/src/main/java/org/onap/so/adapters/sdnc/impl/Utils.java
@@ -42,10 +42,13 @@
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
+import org.json.XML;
+import org.json.JSONObject;
 
 public class Utils {
 
     private static Logger logger = LoggerFactory.getLogger(Utils.class);
+    private static int MSOJsonIndentFactor = 3;
 
     private Utils() {}
 
@@ -230,6 +233,85 @@
         return null;
     }
 
+    public static String genOpticalSdncReq(Document reqDoc, RequestTunables rt) {
+        try {
+            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+            DocumentBuilder db = dbf.newDocumentBuilder();
+
+            // NewDoc for output
+            // Root
+            Document newdoc = db.newDocument();
+            Element root = newdoc.createElementNS(rt.getNamespace(), "input");
+            newdoc.appendChild(root);
+            // RequestData
+            NodeList nodes = reqDoc.getDocumentElement().getChildNodes();
+            for (int i = 0; i < nodes.getLength(); i++) {
+                Node n = nodes.item(i);
+                Node newNode = newdoc.importNode(n, true);
+                root.appendChild(newNode);
+            }
+            String req = domToStr(newdoc);
+            String s = xml2json(req, true);
+            logger.debug("Formatted SdncReq:\n", s);
+            return s;
+
+        } catch (Exception e) {
+            logger.error(LoggingAnchor.FOUR, MessageEnum.RA_ERROR_CREATE_SDNC_REQUEST.toString(), "SDNC",
+                    ErrorCode.BusinessProcessError.getValue(), "Exception in genOpticalSdncReq", e);
+        }
+        return null;
+    }
+
+    /**
+     * Uses the JSONObject static method to convert a XML doc to JSON.
+     *
+     * @param xml String containing the XML doc
+     * @param pretty flag to determine if the output should be formatted
+     * @return String containing the JSON translation
+     */
+    public static String xml2json(String xml, boolean pretty) {
+        try {
+            // name spaces cause problems, so just remove them
+            JSONObject jsonObj = XML.toJSONObject(removeNamespaces(xml));
+            if (!pretty) {
+                return jsonObj.toString();
+            } else {
+                // add an indent to make it 'pretty'
+                return jsonObj.toString(MSOJsonIndentFactor);
+            }
+        } catch (Exception e) {
+            logger.debug("xml2json(): unable to parse xml and convert to json. Exception was: {}", e.toString(), e);
+            return null;
+        }
+    }
+
+    /**
+     * Removes namespaces and namespace declarations from an XML document.
+     *
+     * @param xml the XML document
+     * @return a possibly modified document
+     */
+    public static String removeNamespaces(Object xml) {
+        if (xml == null) {
+            logger.debug("removeNamespaces input object is null , returning null");
+            return null;
+        }
+
+        String text = String.valueOf(xml);
+
+        // remove xmlns declaration
+        text = text.replaceAll("xmlns.*?(\"|\').*?(\"|\')", "");
+        // remove opening tag prefix
+        text = text.replaceAll("(<)(\\w+:)(.*?>)", "$1$3");
+        // remove closing tags prefix
+        text = text.replaceAll("(</)(\\w+:)(.*?>)", "$1$3");
+        // remove extra spaces left when xmlns declarations are removed
+        text = text.replaceAll("\\s+>", ">");
+
+        return text;
+    }
+
+
     public static String genMsoFailResp(SDNCResponse resp) {
         try {
 
diff --git a/bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/CreateSDNCNetworkResource.groovy b/bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/CreateSDNCNetworkResource.groovy
index 3da4161..8b9726c 100644
--- a/bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/CreateSDNCNetworkResource.groovy
+++ b/bpmn/so-bpmn-infrastructure-common/src/main/groovy/org/onap/so/bpmn/infrastructure/scripts/CreateSDNCNetworkResource.groovy
@@ -127,7 +127,11 @@
             String key = iterator.next()
             HashMap<String, String> hashMap = new HashMap()
             hashMap.put("name", key)
-            hashMap.put("value", jsonObject.get(key))
+            if(jsonObject.get(key)==null){
+                 hashMap.put("value", "")
+             }else{
+                 hashMap.put("value", jsonObject.get(key))
+             }
             paramList.add(hashMap)
         }
         Map<String, List<Map<String, Object>>> paramMap = new HashMap()
@@ -345,6 +349,32 @@
 
             switch (modelType) {
                 case "VNF" :
+					if(modelName.contains("UNI") && "MDONS_OTN".equals(serviceType)){
+						source = "SO"
+						sdncTopologyCreateRequest = """<aetgt:SDNCAdapterWorkflowRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
+                                                              xmlns:sdncadapter="http://org.onap.so/workflow/sdnc/adapter/schema/v1"
+                                                              xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1">
+                                 <sdncadapter:RequestHeader>
+                                    <sdncadapter:RequestId>${msoUtils.xmlEscape(hdrRequestId)}</sdncadapter:RequestId>
+                                    <sdncadapter:SvcInstanceId>${msoUtils.xmlEscape(serviceInstanceId)}</sdncadapter:SvcInstanceId>
+                                    <sdncadapter:SvcAction>${msoUtils.xmlEscape(sdnc_svcAction)}</sdncadapter:SvcAction>
+                                    <sdncadapter:SvcOperation>optical-service-create</sdncadapter:SvcOperation>
+                                    <sdncadapter:CallbackUrl>sdncCallback</sdncadapter:CallbackUrl>
+                                    <sdncadapter:MsoAction>opticalservice</sdncadapter:MsoAction>
+                                 </sdncadapter:RequestHeader>
+                                 <sdncadapterworkflow:SDNCRequestData>
+                                    <request-id>${msoUtils.xmlEscape(hdrRequestId)}</request-id>
+                                    <global-customer-id>${msoUtils.xmlEscape(globalCustomerId)}</global-customer-id>
+                                    <service-type>${msoUtils.xmlEscape(serviceType)}</service-type>
+                                    <notification-url>sdncCallback</notification-url>
+                                    <source>${msoUtils.xmlEscape(source)}</source>
+                                    <service-id>${msoUtils.xmlEscape(serviceInstanceId)}</service-id>
+                                     <payload>
+                                         $netowrkInputParameters
+                                     </payload>
+                                </sdncadapterworkflow:SDNCRequestData>
+                             </aetgt:SDNCAdapterWorkflowRequest>""".trim()
+					} else{
                     sdncTopologyCreateRequest = """<aetgt:SDNCAdapterWorkflowRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
                                                               xmlns:sdncadapter="http://org.onap.so/workflow/sdnc/adapter/schema/v1"
                                                               xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1">
@@ -399,6 +429,7 @@
                                       </vnf-request-input>
                                 </sdncadapterworkflow:SDNCRequestData>
                              </aetgt:SDNCAdapterWorkflowRequest>""".trim()
+					}
                     break
                 case "GROUP" :
                     String vnfid = resourceInputObj.getVnfId()