From ba18c19740050c9ac7e2b0e1ba457a83160e9003 Mon Sep 17 00:00:00 2001 From: Srinivasan Ramanathan Date: Tue, 10 Mar 2020 20:27:10 +0000 Subject: Changes for MDONS usecase Template handling and SDNC request sending for Optical services. Change-Id: I52838076ea119237758dfdc804f2ee1f55f3c33c Issue-ID: SO-2460 Signed-off-by: Srinivasan Ramanathan --- .../org/onap/so/adapters/sdnc/impl/Constants.java | 1 + .../onap/so/adapters/sdnc/impl/SDNCRestClient.java | 2 + .../java/org/onap/so/adapters/sdnc/impl/Utils.java | 82 ++++++++++++++++++++++ .../scripts/CreateSDNCNetworkResource.groovy | 33 ++++++++- 4 files changed, 117 insertions(+), 1 deletion(-) 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 294dccdb90..da0fcd3d04 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 interface Constants { 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 f3bf8c2108..b8977f05d8 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 @@ public class SDNCRestClient { 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 2f6ec00283..f52ebcab9b 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.Document; 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 @@ public class Utils { 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("()", "$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 3da4161953..8b9726c2b7 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 @@ public class CreateSDNCNetworkResource extends AbstractServiceTaskProcessor { String key = iterator.next() HashMap 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>> paramMap = new HashMap() @@ -345,6 +349,32 @@ public class CreateSDNCNetworkResource extends AbstractServiceTaskProcessor { switch (modelType) { case "VNF" : + if(modelName.contains("UNI") && "MDONS_OTN".equals(serviceType)){ + source = "SO" + sdncTopologyCreateRequest = """ + + ${msoUtils.xmlEscape(hdrRequestId)} + ${msoUtils.xmlEscape(serviceInstanceId)} + ${msoUtils.xmlEscape(sdnc_svcAction)} + optical-service-create + sdncCallback + opticalservice + + + ${msoUtils.xmlEscape(hdrRequestId)} + ${msoUtils.xmlEscape(globalCustomerId)} + ${msoUtils.xmlEscape(serviceType)} + sdncCallback + ${msoUtils.xmlEscape(source)} + ${msoUtils.xmlEscape(serviceInstanceId)} + + $netowrkInputParameters + + + """.trim() + } else{ sdncTopologyCreateRequest = """ @@ -399,6 +429,7 @@ public class CreateSDNCNetworkResource extends AbstractServiceTaskProcessor { """.trim() + } break case "GROUP" : String vnfid = resourceInputObj.getVnfId() -- cgit 1.2.3-korg