Auto-resolve edge type
[aai/gizmo.git] / src / main / java / org / onap / crud / service / AaiResourceService.java
index afabe7e..c2e0338 100644 (file)
@@ -22,6 +22,7 @@ package org.onap.crud.service;
 \r
 import java.security.cert.X509Certificate;\r
 import java.util.AbstractMap;\r
+import java.util.Arrays;\r
 import java.util.HashSet;\r
 import java.util.Map;\r
 import java.util.Map.Entry;\r
@@ -54,10 +55,11 @@ import org.onap.aaiauth.auth.Auth;
 import org.onap.crud.exception.CrudException;\r
 import org.onap.crud.logging.CrudServiceMsgs;\r
 import org.onap.crud.logging.LoggingUtil;\r
+import org.onap.crud.parser.EdgePayload;\r
+import org.onap.crud.parser.util.EdgePayloadUtil;\r
 import org.onap.crud.service.CrudRestService.Action;\r
 import org.onap.crud.util.CrudServiceConstants;\r
 import org.onap.schema.EdgeRulesLoader;\r
-import org.onap.schema.RelationshipSchemaValidator;\r
 import org.slf4j.MDC;\r
 import com.google.gson.Gson;\r
 import com.google.gson.JsonElement;\r
@@ -167,8 +169,9 @@ public class AaiResourceService {
         ImmutablePair<EntityTag, String> result = graphDataService.addEdge(EdgeRulesLoader.getLatestSchemaVersion(), type, payload);\r
         response = Response.status(Status.CREATED).entity(result.getValue()).tag(result.getKey()).type(mediaType).build();\r
 \r
-      } catch (CrudException e) {\r
-\r
+      } catch (CrudException ce) {\r
+          response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();\r
+      } catch (Exception e) {\r
         response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();\r
       }\r
     }\r
@@ -397,16 +400,15 @@ public class AaiResourceService {
   public EdgePayload applyEdgeRulesToPayload(EdgePayload payload) throws CrudException {\r
 \r
     // Extract the types for both the source and target vertices.\r
-    String srcType = RelationshipSchemaValidator.vertexTypeFromUri(payload.getSource());\r
-    String tgtType = RelationshipSchemaValidator.vertexTypeFromUri(payload.getTarget());\r
-\r
-      // Now, get the default properties for this edge based on the edge rules definition...\r
-      Map<EdgeProperty, String> props = getEdgeRuleProperties(srcType, tgtType);\r
+    String srcType = EdgePayloadUtil.getVertexNodeType(payload.getSource());\r
+    String tgtType = EdgePayloadUtil.getVertexNodeType(payload.getTarget());\r
 \r
-      // ...and merge them with any custom properties provided in the request.\r
-      JsonElement mergedProperties = mergeProperties(payload.getProperties(), props);\r
-      payload.setProperties(mergedProperties);\r
+    // Now, get the default properties for this edge based on the edge rules definition...\r
+    Map<EdgeProperty, String> props = getEdgeRuleProperties(srcType, tgtType);\r
 \r
+    // ...and merge them with any custom properties provided in the request.\r
+    JsonElement mergedProperties = mergeProperties(payload.getProperties(), props);\r
+    payload.setProperties(mergedProperties);\r
 \r
     if(logger.isDebugEnabled()) {\r
       logger.debug("Edge properties after applying rules for '" + srcType + " -> " + tgtType + "': " + mergedProperties);\r
@@ -430,6 +432,7 @@ public class AaiResourceService {
    *\r
    * @throws CrudException\r
    */\r
+  @SuppressWarnings("unchecked")\r
   public JsonElement mergeProperties(JsonElement propertiesFromRequest, Map<EdgeProperty, String> propertyDefaults) throws CrudException {\r
 \r
     // Convert the properties from the edge payload into something we can\r
@@ -505,7 +508,7 @@ public class AaiResourceService {
         authUser = subjectDn.toString();\r
       }\r
 \r
-      return this.auth.validateRequest(authUser.toLowerCase(), action.toString() + ":" + authPolicyFunctionName);\r
+      return this.auth.validateRequest(authUser!=null ? authUser.toLowerCase():"", action.toString() + ":" + authPolicyFunctionName);\r
 \r
     } catch (Exception e) {\r
       logResult(action, uri, e);\r
@@ -517,8 +520,7 @@ public class AaiResourceService {
 \r
     logger.error(CrudServiceMsgs.EXCEPTION_DURING_METHOD_CALL,\r
                  op.toString(),\r
-                 uri,\r
-                 e.getStackTrace().toString());\r
+                 uri, Arrays.toString(e.getStackTrace()));\r
 \r
     // Clear the MDC context so that no other transaction inadvertently\r
     // uses our transaction id.\r