From 70ff098fd488850655ad7ac98ad06712f0340552 Mon Sep 17 00:00:00 2001 From: "k.kazak" Date: Mon, 8 Apr 2019 11:48:16 +0200 Subject: fix sonar bugs in ServicePluginFactory Fixed potential NullPointerException in lines: 123, 286, 480 and 607 - uuiObject is nullable here Change-Id: I01b30d090add7faccf3958f2fa37c7905d4bd5a3 Issue-ID: SO-1490 Signed-off-by: k.kazak --- .../workflow/service/ServicePluginFactory.java | 255 +++++++++++---------- 1 file changed, 139 insertions(+), 116 deletions(-) (limited to 'bpmn/so-bpmn-infrastructure-common/src/main/java/org') diff --git a/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/workflow/service/ServicePluginFactory.java b/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/workflow/service/ServicePluginFactory.java index 113f05d3ca..9b878afb7c 100644 --- a/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/workflow/service/ServicePluginFactory.java +++ b/bpmn/so-bpmn-infrastructure-common/src/main/java/org/onap/so/bpmn/infrastructure/workflow/service/ServicePluginFactory.java @@ -22,15 +22,18 @@ package org.onap.so.bpmn.infrastructure.workflow.service; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import java.io.IOException; import java.net.SocketTimeoutException; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Optional; - import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpResponse; import org.apache.http.ParseException; @@ -68,17 +71,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.util.UriUtils; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; - public class ServicePluginFactory { // SOTN calculate route public static final String OOF_DEFAULT_ENDPOINT = "http://192.168.1.223:8443/oof/sotncalc"; public static final String THIRD_SP_DEFAULT_ENDPOINT = "http://192.168.1.223:8443/sp/resourcemgr/querytps"; - + public static final String INVENTORY_OSS_DEFAULT_ENDPOINT = "http://192.168.1.199:8443/oss/inventory"; private static final int DEFAULT_TIME_OUT = 60000; @@ -88,7 +87,7 @@ public class ServicePluginFactory { private static Logger logger = LoggerFactory.getLogger(ServicePluginFactory.class); private static ServicePluginFactory instance; - + public static synchronized ServicePluginFactory getInstance() { if (null == instance) { @@ -100,11 +99,11 @@ public class ServicePluginFactory { private ServicePluginFactory() { } - + private String getInventoryOSSEndPoint(){ return UrnPropertiesReader.getVariable("mso.service-plugin.inventory-oss-endpoint", INVENTORY_OSS_DEFAULT_ENDPOINT); } - + private String getThirdSPEndPoint(){ return UrnPropertiesReader.getVariable("mso.service-plugin.third-sp-endpoint", THIRD_SP_DEFAULT_ENDPOINT); } @@ -118,18 +117,23 @@ public class ServicePluginFactory { if(!isNeedProcessSite(uuiRequest)) { return uuiRequest; } - - Map uuiObject = getJsonObject(uuiRequest, Map.class); - Map serviceObject = (Map) uuiObject.get("service"); - Map serviceParametersObject = (Map) serviceObject.get("parameters"); - Map serviceRequestInputs = (Map) serviceParametersObject.get("requestInputs"); - List resources = (List) serviceParametersObject.get("resources"); - - if (isSiteLocationLocal(serviceRequestInputs, resources)) { - // resources changed : added TP info - String newRequest = getJsonString(uuiObject); - return newRequest; - } + + Map uuiObject = getJsonObject(uuiRequest, Map.class); + if (uuiObject == null) { + return uuiRequest; + } + Map serviceObject = (Map) uuiObject + .getOrDefault("service", Collections.emptyMap()); + Map serviceParametersObject = (Map) serviceObject + .getOrDefault("parameters", Collections.emptyMap()); + Map serviceRequestInputs = (Map) serviceParametersObject + .getOrDefault("requestInputs", Collections.emptyMap()); + List resources = (List) serviceParametersObject.getOrDefault("resources", Collections.emptyList()); + + if (isSiteLocationLocal(serviceRequestInputs, resources)) { + // resources changed : added TP info + return getJsonString(uuiObject); + } List addResourceList = new ArrayList<>(); addResourceList.addAll(serviceDecomposition.getServiceResources()); @@ -155,9 +159,9 @@ public class ServicePluginFactory { } @SuppressWarnings("unchecked") - private boolean isSiteLocationLocal(Map serviceRequestInputs, List resources) { - Map tpInfoMap = getTPforVPNAttachment(serviceRequestInputs); - + private boolean isSiteLocationLocal(Map serviceRequestInputs, List resources) { + Map tpInfoMap = getTPforVPNAttachment(serviceRequestInputs); + if(tpInfoMap.isEmpty()) { return true; } @@ -166,7 +170,7 @@ public class ServicePluginFactory { if (!host.isEmpty()) { return false; } - + Map accessTPInfo = new HashMap(); accessTPInfo.put("access-provider-id", tpInfoMap.get("access-provider-id")); accessTPInfo.put("access-client-id", tpInfoMap.get("access-client-id")); @@ -180,7 +184,7 @@ public class ServicePluginFactory { Map resource = (Map)curResource; String curResourceName = (String) resource.get("resourceName"); curResourceName = curResourceName.replaceAll(" ", ""); - if(resourceName.equalsIgnoreCase(curResourceName)) { + if(resourceName.equalsIgnoreCase(curResourceName)) { putResourceRequestInputs(resource, accessTPInfo); break; } @@ -188,7 +192,7 @@ public class ServicePluginFactory { return true; } - + @SuppressWarnings("unchecked") private Map getTPforVPNAttachment(Map serviceRequestInputs) { Object location = null; @@ -199,24 +203,24 @@ public class ServicePluginFactory { // logic for R2 uuiRequest params in service level for (Entry entry : serviceRequestInputs.entrySet()) { String key = entry.getKey(); - if (key.toLowerCase().contains("site_address")) { + if (key.toLowerCase().contains("site_address")) { location = entry.getValue(); - } - if (key.toLowerCase().contains("sotncondition_clientsignal")) { + } + if (key.toLowerCase().contains("sotncondition_clientsignal")) { clientSignal = entry.getValue(); vpnAttachmentResourceName = key.substring(0, key.indexOf("_")); } } Map tpInfoMap = new HashMap(); - + // Site resource has location param and SOTNAttachment resource has clientSignal param if(location == null || clientSignal == null ) { return tpInfoMap; } - - // Query terminal points from InventoryOSS system by location. - String locationAddress = (String) location; + + // Query terminal points from InventoryOSS system by location. + String locationAddress = (String) location; List locationTPList = queryAccessTPbyLocationFromInventoryOSS(locationAddress); if(locationTPList != null && !locationTPList.isEmpty()) { for(Object tp: locationTPList) { @@ -232,10 +236,10 @@ public class ServicePluginFactory { logger.debug("Get Terminal TP from InventoryOSS"); return tpInfoMap; } - + return tpInfoMap; } - + @SuppressWarnings("unchecked") private List queryAccessTPbyLocationFromInventoryOSS(String locationAddress) { String url = getInventoryOSSEndPoint(); @@ -247,7 +251,7 @@ public class ServicePluginFactory { } return accessTPs; } - + @SuppressWarnings("unchecked") private void putResourceRequestInputs(Map resource, Map resourceInputs) { Map resourceParametersObject = new HashMap<>(); @@ -260,7 +264,7 @@ public class ServicePluginFactory { if(resParametersObject.containsKey("requestInputs")) { Map resRequestInputs = (Map) resourceRequestInputs.get("requestInputs"); Map oldRequestInputs = (Map) resParametersObject.get("requestInputs"); - if(oldRequestInputs != null) { + if(oldRequestInputs != null) { oldRequestInputs.putAll(resRequestInputs); } else { @@ -268,7 +272,7 @@ public class ServicePluginFactory { } } else { - resParametersObject.putAll(resourceRequestInputs); + resParametersObject.putAll(resourceRequestInputs); } } else { @@ -277,24 +281,29 @@ public class ServicePluginFactory { return; } - - + + @SuppressWarnings("unchecked") - public String doTPResourcesAllocation(DelegateExecution execution, String uuiRequest) { - Map uuiObject = getJsonObject(uuiRequest, Map.class); - Map serviceObject = (Map) uuiObject.get("service"); - Map serviceParametersObject = (Map) serviceObject.get("parameters"); - Map serviceRequestInputs = (Map) serviceParametersObject.get("requestInputs"); - - if(!isNeedAllocateCrossTPResources(serviceRequestInputs)) { - return uuiRequest; - } - - allocateCrossTPResources(execution, serviceRequestInputs); - String newRequest = getJsonString(uuiObject); - return newRequest; - } + public String doTPResourcesAllocation(DelegateExecution execution, String uuiRequest) { + Map uuiObject = getJsonObject(uuiRequest, Map.class); + if (uuiObject == null) { + return uuiRequest; + } + Map serviceObject = (Map) uuiObject + .getOrDefault("service", Collections.emptyMap()); + Map serviceParametersObject = (Map) serviceObject + .getOrDefault("parameters", Collections.emptyMap()); + Map serviceRequestInputs = (Map) serviceParametersObject + .getOrDefault("requestInputs", Collections.emptyMap()); + + if (!isNeedAllocateCrossTPResources(serviceRequestInputs)) { + return uuiRequest; + } + + allocateCrossTPResources(execution, serviceRequestInputs); + return getJsonString(uuiObject); + } @SuppressWarnings("unchecked") private boolean isNeedAllocateCrossTPResources(Map serviceRequestInputs) { @@ -303,7 +312,7 @@ public class ServicePluginFactory { String callSource = (String) serviceRequestInputs.get("CallSource"); if("ExternalAPI".equalsIgnoreCase(callSource)) { return false; - } + } } for (String input : serviceRequestInputs.keySet()) { @@ -313,12 +322,12 @@ public class ServicePluginFactory { } return false; } - + @SuppressWarnings("unchecked") private void allocateCrossTPResources(DelegateExecution execution, Map serviceRequestInputs) { Map crossTPs = this.getTPsfromAAI(); - + if(crossTPs == null || crossTPs.isEmpty()) { serviceRequestInputs.put("local-access-provider-id", ""); serviceRequestInputs.put("local-access-client-id", ""); @@ -329,7 +338,7 @@ public class ServicePluginFactory { serviceRequestInputs.put("remote-access-client-id", ""); serviceRequestInputs.put("remote-access-topology-id", ""); serviceRequestInputs.put("remote-access-node-id", ""); - serviceRequestInputs.put("remote-access-ltp-id", ""); + serviceRequestInputs.put("remote-access-ltp-id", ""); } else { serviceRequestInputs.put("local-access-provider-id", crossTPs.get("local-access-provider-id")); @@ -343,24 +352,24 @@ public class ServicePluginFactory { serviceRequestInputs.put("remote-access-node-id", crossTPs.get("remote-node-id")); serviceRequestInputs.put("remote-access-ltp-id", crossTPs.get("remote-ltp-id")); } - + return; } - // This method returns Local and remote TPs information from AAI + // This method returns Local and remote TPs information from AAI public Map getTPsfromAAI() { Map tpInfo = new HashMap<>(); - + AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectPlurals.LOGICAL_LINK); AAIResourcesClient client = new AAIResourcesClient(); Optional result = client.get(LogicalLinks.class, uri); - + if (result.isPresent()) { LogicalLinks links = result.get(); boolean isRemoteLink = false; - + links.getLogicalLink(); - + for (LogicalLink link : links.getLogicalLink()) { AAIResultWrapper wrapper = new AAIResultWrapper(link); Optional optRelationships = wrapper.getRelationships(); @@ -372,7 +381,7 @@ public class ServicePluginFactory { } pInterfaces.addAll(relationships.getRelatedAAIUris(AAIObjectType.P_INTERFACE)); } - + if (isRemoteLink) { // find remote p interface AAIResourceUri localTP = null; @@ -393,7 +402,7 @@ public class ServicePluginFactory { String tpUrl = localTP.build().toString(); PInterface intfLocal = client.get(PInterface.class, localTP).get(); tpInfo.put("local-access-node-id", tpUrl.split("/")[6]); - + String[] networkRef = intfLocal.getNetworkRef().split("/"); if (networkRef.length == 6) { tpInfo.put("local-access-provider-id", networkRef[1]); @@ -404,7 +413,7 @@ public class ServicePluginFactory { if (ltpIdStr.contains("-")) { tpInfo.put("local-access-ltp-id", ltpIdStr.substring(ltpIdStr.lastIndexOf("-") + 1)); } - + // give remote tp tpUrl = remoteTP.build().toString(); PInterface intfRemote = client.get(PInterface.class, remoteTP).get(); @@ -431,7 +440,7 @@ public class ServicePluginFactory { // this method check if pInterface is remote private boolean isRemotePInterface(AAIResourcesClient client, AAIResourceUri uri) { - + String uriString = uri.build().toString(); if (uriString != null) { @@ -447,7 +456,7 @@ public class ServicePluginFactory { return !relationships.getRelatedAAIUris(AAIObjectType.EXT_AAI_NETWORK).isEmpty(); } } - + return false; } @@ -475,12 +484,19 @@ public class ServicePluginFactory { return uuiRequest.contains("clientSignal") && uuiRequest.contains("vpnType"); } + @SuppressWarnings("unchecked") private String preProcessSOTNService(ServiceDecomposition serviceDecomposition, String uuiRequest) { Map uuiObject = getJsonObject(uuiRequest, Map.class); - Map serviceObject = (Map) uuiObject.get("service"); - Map serviceParametersObject = (Map) serviceObject.get("parameters"); - Map serviceRequestInputs = (Map) serviceParametersObject.get("requestInputs"); - List resources = (List) serviceParametersObject.get("resources"); + if (uuiObject == null) { + return uuiRequest; + } + Map serviceObject = (Map) uuiObject + .getOrDefault("service", Collections.emptyMap()); + Map serviceParametersObject = (Map) serviceObject + .getOrDefault("parameters", Collections.emptyMap()); + Map serviceRequestInputs = (Map) serviceParametersObject + .getOrDefault("requestInputs", Collections.emptyMap()); + List resources = (List) serviceParametersObject.getOrDefault("resources", Collections.emptyList()); // This is a logic for demo , it could not be finalized to community. String srcLocation = ""; String dstLocation = ""; @@ -534,7 +550,7 @@ public class ServicePluginFactory { vpnRequestInputs.put("src-client-signal", srcClientSignal); vpnRequestInputs.put("dst-client-signal", dstClientSignal); } - + // Now we need to query terminal points from SP resourcemgr system. List locationTerminalPointList = queryTerminalPointsFromServiceProviderSystem(srcLocation, dstLocation); @@ -589,54 +605,61 @@ public class ServicePluginFactory { } return null; } - + public static void main(String args[]){ String str = "restconf/config/GENERIC-RESOURCE-API:services/service/eca7e542-12ba-48de-8544-fac59303b14e/service-data/networks/network/aec07806-1671-4af2-b722-53c8e320a633/network-data/"; - + int index1 = str.indexOf("/network/"); int index2 = str.indexOf("/network-data"); - + String str1 = str.substring(index1 + "/network/".length(), index2); System.out.println(str1); - + } - private String doSOTNServiceHoming(ServiceDecomposition serviceDecomposition, String uuiRequest) { - // query the route for the service. - Map uuiObject = getJsonObject(uuiRequest, Map.class); - Map serviceObject = (Map) uuiObject.get("service"); - Map serviceParametersObject = (Map) serviceObject.get("parameters"); - Map serviceRequestInputs = (Map) serviceParametersObject.get("requestInputs"); - Map oofQueryObject = new HashMap<>(); - List resources = (List) serviceParametersObject.get("resources"); - oofQueryObject.put("src-access-provider-id", serviceRequestInputs.get("inner-src-access-provider-id")); - oofQueryObject.put("src-access-client-id", serviceRequestInputs.get("inner-src-access-client-id")); - oofQueryObject.put("src-access-topology-id", serviceRequestInputs.get("inner-src-access-topology-id")); - oofQueryObject.put("src-access-node-id", serviceRequestInputs.get("inner-src-access-node-id")); - oofQueryObject.put("src-access-ltp-id", serviceRequestInputs.get("inner-src-access-ltp-id")); - oofQueryObject.put("dst-access-provider-id", serviceRequestInputs.get("inner-dst-access-provider-id")); - oofQueryObject.put("dst-access-client-id", serviceRequestInputs.get("inner-dst-access-client-id")); - oofQueryObject.put("dst-access-topology-id", serviceRequestInputs.get("inner-dst-access-topology-id")); - oofQueryObject.put("dst-access-node-id", serviceRequestInputs.get("inner-dst-access-node-id")); - oofQueryObject.put("dst-access-ltp-id", serviceRequestInputs.get("inner-dst-access-ltp-id")); - String oofRequestReq = getJsonString(oofQueryObject); - String url = getOOFCalcEndPoint(); - String responseContent = sendRequest(url, "POST", oofRequestReq); + @SuppressWarnings("unchecked") + private String doSOTNServiceHoming(ServiceDecomposition serviceDecomposition, String uuiRequest) { + // query the route for the service. + Map uuiObject = getJsonObject(uuiRequest, Map.class); + if (uuiObject == null) { + return uuiRequest; + } + Map serviceObject = (Map) uuiObject + .getOrDefault("service", Collections.emptyMap()); + Map serviceParametersObject = (Map) serviceObject + .getOrDefault("parameters", Collections.emptyMap()); + Map serviceRequestInputs = (Map) serviceParametersObject + .getOrDefault("requestInputs", Collections.emptyMap()); + Map oofQueryObject = new HashMap<>(); + List resources = (List) serviceParametersObject + .getOrDefault("resources", Collections.emptyList()); + oofQueryObject.put("src-access-provider-id", serviceRequestInputs.get("inner-src-access-provider-id")); + oofQueryObject.put("src-access-client-id", serviceRequestInputs.get("inner-src-access-client-id")); + oofQueryObject.put("src-access-topology-id", serviceRequestInputs.get("inner-src-access-topology-id")); + oofQueryObject.put("src-access-node-id", serviceRequestInputs.get("inner-src-access-node-id")); + oofQueryObject.put("src-access-ltp-id", serviceRequestInputs.get("inner-src-access-ltp-id")); + oofQueryObject.put("dst-access-provider-id", serviceRequestInputs.get("inner-dst-access-provider-id")); + oofQueryObject.put("dst-access-client-id", serviceRequestInputs.get("inner-dst-access-client-id")); + oofQueryObject.put("dst-access-topology-id", serviceRequestInputs.get("inner-dst-access-topology-id")); + oofQueryObject.put("dst-access-node-id", serviceRequestInputs.get("inner-dst-access-node-id")); + oofQueryObject.put("dst-access-ltp-id", serviceRequestInputs.get("inner-dst-access-ltp-id")); + String oofRequestReq = getJsonString(oofQueryObject); + String url = getOOFCalcEndPoint(); + String responseContent = sendRequest(url, "POST", oofRequestReq); + + List returnList = new ArrayList<>(); + if (null != responseContent) { + returnList = getJsonObject(responseContent, List.class); + } + // in demo we have only one VPN. no cross VPNs, so get first item. + Map returnRoute = getReturnRoute(returnList); + Map vpnRequestInputs = getVPNResourceRequestInputs(resources); + if (null != vpnRequestInputs) { + vpnRequestInputs.putAll(returnRoute); + } + return getJsonString(uuiObject); + } - List returnList = new ArrayList<>(); - if (null != responseContent) { - returnList = getJsonObject(responseContent, List.class); - } - // in demo we have only one VPN. no cross VPNs, so get first item. - Map returnRoute = getReturnRoute(returnList); - Map vpnRequestInputs = getVPNResourceRequestInputs(resources); - if(null!=vpnRequestInputs) { - vpnRequestInputs.putAll(returnRoute); - } - String newRequest = getJsonString(uuiObject); - return newRequest; - } - private Map getReturnRoute(List returnList){ Map returnRoute = new HashMap<>(); for(Object returnVpn :returnList){ @@ -681,7 +704,7 @@ public class ServicePluginFactory { return resourceInputsFromUuiMap; } - public static T getJsonObject(String jsonstr, Class type) { + private static T getJsonObject(String jsonstr, Class type) { ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, true); try { @@ -706,7 +729,7 @@ public class ServicePluginFactory { } private static String sendRequest(String url, String methodType, String content) { - + String msbUrl = url; HttpRequestBase method = null; HttpResponse httpResponse = null; -- cgit 1.2.3-korg