diff options
author | 2017-06-27 09:19:22 -0400 | |
---|---|---|
committer | 2017-06-27 13:50:15 -0400 | |
commit | 323c3c56919bf25a3df9bffffd9bf5bc66fd5017 (patch) | |
tree | 57809bcebe23c3e62315b5fec78792f32c0b631d /aai-core/src/main/java/org/openecomp | |
parent | eff9232ffe36022d63e6a70df7a3f0165015bbc8 (diff) |
[AAI-12] Add the swagger documentation for onap
Change-Id: If2ebc7acd38c3a268c1461b05f1b7ff1bc0c8548
Signed-off-by: Venkata Harish K Kajur <vk250x@att.com>
Diffstat (limited to 'aai-core/src/main/java/org/openecomp')
15 files changed, 1386 insertions, 739 deletions
diff --git a/aai-core/src/main/java/org/openecomp/aai/dbgen/DataGrooming.java b/aai-core/src/main/java/org/openecomp/aai/dbgen/DataGrooming.java index d1480d15..2acfe904 100644 --- a/aai-core/src/main/java/org/openecomp/aai/dbgen/DataGrooming.java +++ b/aai-core/src/main/java/org/openecomp/aai/dbgen/DataGrooming.java @@ -39,6 +39,8 @@ import java.util.Set; import java.util.UUID; import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; +import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__; import org.apache.tinkerpop.gremlin.structure.Direction; import org.apache.tinkerpop.gremlin.structure.Edge; import org.apache.tinkerpop.gremlin.structure.Property; @@ -53,6 +55,8 @@ import org.openecomp.aai.introspection.LoaderFactory; import org.openecomp.aai.introspection.ModelType; import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException; import org.openecomp.aai.logging.ErrorLogHelper; +import org.openecomp.aai.serialization.db.EdgeProperties; +import org.openecomp.aai.serialization.db.EdgeProperty; import org.openecomp.aai.util.AAIConfig; import org.openecomp.aai.util.AAIConstants; import org.openecomp.aai.util.FormatDate; @@ -64,7 +68,6 @@ import com.thinkaurelius.titan.core.TitanEdge; import com.thinkaurelius.titan.core.TitanFactory; import com.thinkaurelius.titan.core.TitanGraph; import com.thinkaurelius.titan.core.TitanTransaction; -import com.thinkaurelius.titan.core.TitanVertex; public class DataGrooming { @@ -199,6 +202,7 @@ public class DataGrooming { System.exit(1); } + try { if (!prevFileName.equals("")) { // They are trying to fix some data based on a data in a @@ -380,16 +384,16 @@ public class DataGrooming { String emsg = "null graphTransaction object in DataGrooming\n"; throw new AAIException("AAI_6101", emsg); } - + GraphTraversalSource source1 = g.traversal(); ArrayList<String> errArr = new ArrayList<>(); int totalNodeCount = 0; HashMap<String, String> misMatchedHash = new HashMap<String, String>(); - HashMap<String, TitanVertex> orphanNodeHash = new HashMap<String, TitanVertex>(); - HashMap<String, TitanVertex> missingDepNodeHash = new HashMap<String, TitanVertex>(); + HashMap<String, Vertex> orphanNodeHash = new HashMap<String, Vertex>(); + HashMap<String, Vertex> missingDepNodeHash = new HashMap<String, Vertex>(); HashMap<String, Edge> oneArmedEdgeHash = new HashMap<String, Edge>(); HashMap<String, String> emptyVertexHash = new HashMap<String, String>(); - HashMap<String, TitanVertex> ghostNodeHash = new HashMap<String, TitanVertex>(); + HashMap<String, Vertex> ghostNodeHash = new HashMap<String, Vertex>(); ArrayList<String> dupeGroups = new ArrayList<>(); Loader loader = LoaderFactory.createLoaderForVersion(ModelType.MOXY, AAIProperties.LATEST); @@ -419,15 +423,14 @@ public class DataGrooming { // Loop through all the nodes of this Node type int lastShownForNt = 0; - ArrayList <TitanVertex> tmpList = new ArrayList <> (); - Iterable <?> verts = g.query().has("aai-node-type",nType).vertices(); - Iterator<?> iterv = verts.iterator(); + ArrayList <Vertex> tmpList = new ArrayList <> (); + Iterator <Vertex> iterv = source1.V().has("aai-node-type",nType); while (iterv.hasNext()) { // We put the nodes into an ArrayList because the graph.query iterator can time out - tmpList.add((TitanVertex)iterv.next()); + tmpList.add(iterv.next()); } - Iterator <?> iter = tmpList.iterator(); + Iterator <Vertex> iter = tmpList.iterator(); while (iter.hasNext()) { try { thisNtCount++; @@ -435,14 +438,14 @@ public class DataGrooming { lastShownForNt = thisNtCount; LOGGER.debug("count for " + nType + " so far = " + thisNtCount ); } - TitanVertex thisVtx = (TitanVertex) iter.next(); + Vertex thisVtx = iter.next(); String thisVid = thisVtx.id().toString(); if (processedVertices.contains(thisVid)) { LOGGER.debug("skipping already processed vertex: " + thisVid); continue; } totalNodeCount++; - List <TitanVertex> secondGetList = new ArrayList <> (); + List <Vertex> secondGetList = new ArrayList <> (); // ----------------------------------------------------------------------- // For each vertex of this nodeType, we want to: // a) make sure that it can be retrieved using it's AAI defined key @@ -470,19 +473,18 @@ public class DataGrooming { if( depNodeTypes.isEmpty() ){ // This kind of node is not dependent on any other. // Make sure we can get it back using it's key properties and that we only get one. - secondGetList = getNodeJustUsingKeyParams( TRANSID, FROMAPPID, g, nType, + secondGetList = getNodeJustUsingKeyParams( TRANSID, FROMAPPID, source1, nType, propHashWithKeys, version ); } else { // This kind of node is dependent on another for uniqueness. // Start at it's parent (the dependent vertex) and make sure we can get it // back using it's key properties and that we only get one. - Iterable <?> verts2 = thisVtx.query().direction(Direction.IN).has("isParent",true).vertices(); - Iterator <?> vertI2 = verts2.iterator(); - TitanVertex parentVtx = null; + Iterator <Vertex> vertI2 = source1.V(thisVtx).union(__.inE().has(EdgeProperties.out(EdgeProperty.IS_PARENT), true).outV(), __.outE().has(EdgeProperties.in(EdgeProperty.IS_PARENT)).inV()); + Vertex parentVtx = null; int pCount = 0; while( vertI2 != null && vertI2.hasNext() ){ - parentVtx = (TitanVertex) vertI2.next(); + parentVtx = vertI2.next(); pCount++; } if( pCount <= 0 ){ @@ -543,12 +545,12 @@ public class DataGrooming { // We found the parent - so use it to do the second-look. // NOTE --- We're just going to do the same check from the other direction - because // there could be duplicates or the pointer going the other way could be broken - ArrayList <TitanVertex> tmpListSec = new ArrayList <> (); + ArrayList <Vertex> tmpListSec = new ArrayList <> (); - tmpListSec = getConnectedChildrenOfOneType( g, parentVtx, nType ) ; - Iterator<TitanVertex> vIter = tmpListSec.iterator(); + tmpListSec = getConnectedChildrenOfOneType( source1, parentVtx, nType ) ; + Iterator<Vertex> vIter = tmpListSec.iterator(); while (vIter.hasNext()) { - TitanVertex tmpV = vIter.next(); + Vertex tmpV = vIter.next(); if( vertexHasTheseKeys(tmpV, propHashWithKeys) ){ secondGetList.add(tmpV); } @@ -580,7 +582,7 @@ public class DataGrooming { // Found some DUPLICATES - need to process them LOGGER.info(" - now check Dupes for this guy - "); List<String> tmpDupeGroups = checkAndProcessDupes( - TRANSID, FROMAPPID, g, version, + TRANSID, FROMAPPID, g, source1, version, nType, secondGetList, dupeFixOn, deleteCandidateList, singleCommits, dupeGroups, loader); Iterator<String> dIter = tmpDupeGroups.iterator(); @@ -710,7 +712,7 @@ public class DataGrooming { } String vNtI = ""; String vIdI = ""; - TitanVertex ghost2 = null; + Vertex ghost2 = null; Boolean keysMissing = true; Boolean cantGetUsingVid = false; @@ -729,7 +731,7 @@ public class DataGrooming { } if( ! ghost2CheckOff ){ - TitanVertex connectedVert = g2.getVertex(vIdLong); + Vertex connectedVert = g2.getVertex(vIdLong); if( connectedVert == null ) { LOGGER.warn( "GHOST2 -- got NULL when doing getVertex for vid = " + vIdLong); cantGetUsingVid = true; @@ -842,7 +844,7 @@ public class DataGrooming { } if( ! ghost2CheckOff ){ - TitanVertex connectedVert = g2.getVertex(vIdLong); + Vertex connectedVert = g2.getVertex(vIdLong); if( connectedVert == null ) { cantGetUsingVid = true; LOGGER.info( "GHOST2 -- got NULL when doing getVertex for vid = " + vIdLong); @@ -972,13 +974,13 @@ public class DataGrooming { + misMatchedHash.size() + "\n"); bw.write("\n ------------- Delete Candidates ---------\n"); - for (Map.Entry<String, TitanVertex> entry : ghostNodeHash + for (Map.Entry<String, Vertex> entry : ghostNodeHash .entrySet()) { String vid = entry.getKey(); bw.write("DeleteCandidate: Phantom Vid = [" + vid + "]\n"); cleanupCandidateCount++; } - for (Map.Entry<String, TitanVertex> entry : orphanNodeHash + for (Map.Entry<String, Vertex> entry : orphanNodeHash .entrySet()) { String vid = entry.getKey(); bw.write("DeleteCandidate: OrphanDepNode Vid = [" + vid + "]\n"); @@ -991,7 +993,7 @@ public class DataGrooming { bw.write("DeleteCandidate: Bad EDGE Edge-id = [" + eid + "]\n"); cleanupCandidateCount++; } - for (Map.Entry<String, TitanVertex> entry : missingDepNodeHash + for (Map.Entry<String, Vertex> entry : missingDepNodeHash .entrySet()) { String vid = entry.getKey(); bw.write("DeleteCandidate: (maybe) missingDepNode Vid = [" @@ -1001,7 +1003,7 @@ public class DataGrooming { bw.write("\n-- NOTE - To see DeleteCandidates for Duplicates, you need to look in the Duplicates Detail section below.\n"); bw.write("\n ------------- GHOST NODES - detail "); - for (Map.Entry<String, TitanVertex> entry : ghostNodeHash + for (Map.Entry<String, Vertex> entry : ghostNodeHash .entrySet()) { try { String vid = entry.getKey(); @@ -1023,7 +1025,7 @@ public class DataGrooming { } bw.write("\n ------------- Missing Dependent Edge ORPHAN NODES - detail: "); - for (Map.Entry<String, TitanVertex> entry : orphanNodeHash + for (Map.Entry<String, Vertex> entry : orphanNodeHash .entrySet()) { try { String vid = entry.getKey(); @@ -1045,7 +1047,7 @@ public class DataGrooming { } bw.write("\n ------------- Missing Dependent Edge (but not orphan) NODES: "); - for (Map.Entry<String, TitanVertex> entry : missingDepNodeHash + for (Map.Entry<String, Vertex> entry : missingDepNodeHash .entrySet()) { try { String vid = entry.getKey(); @@ -1113,9 +1115,9 @@ public class DataGrooming { idArr.add(vidString); long longVertId = Long.parseLong(vidString); Iterator<Vertex> vtxIterator = g.vertices(longVertId); - TitanVertex vtx = null; + Vertex vtx = null; if (vtxIterator.hasNext()) { - vtx = (TitanVertex)vtxIterator.next(); + vtx = vtxIterator.next(); } ArrayList<String> retArr = showPropertiesForNode(TRANSID, FROMAPPID, vtx); for (String info : retArr) { @@ -1269,7 +1271,7 @@ public class DataGrooming { * @param propHashWithKeys the prop hash with keys * @return the boolean */ - private static Boolean vertexHasTheseKeys( TitanVertex tmpV, HashMap <String, Object> propHashWithKeys) { + private static Boolean vertexHasTheseKeys( Vertex tmpV, HashMap <String, Object> propHashWithKeys) { Iterator <?> it = propHashWithKeys.entrySet().iterator(); while( it.hasNext() ){ String propName = ""; @@ -1398,19 +1400,19 @@ public class DataGrooming { * @param g the g * @param dupeVertexList the dupe vertex list * @param ver the ver - * @return TitanVertex + * @return Vertex * @throws AAIException the AAI exception */ - public static TitanVertex getPreferredDupe(String transId, - String fromAppId, TitanTransaction g, - ArrayList<TitanVertex> dupeVertexList, String ver, Loader loader) + public static Vertex getPreferredDupe(String transId, + String fromAppId, GraphTraversalSource g, + ArrayList<Vertex> dupeVertexList, String ver, Loader loader) throws AAIException { // This method assumes that it is being passed a List of vertex objects // which // violate our uniqueness constraints. - TitanVertex nullVtx = null; + Vertex nullVtx = null; if (dupeVertexList == null) { return nullVtx; @@ -1420,13 +1422,13 @@ public class DataGrooming { return nullVtx; } if (listSize == 1) { - return ((TitanVertex) dupeVertexList.get(0)); + return (dupeVertexList.get(0)); } - TitanVertex vtxPreferred = null; - TitanVertex currentFaveVtx = (TitanVertex) dupeVertexList.get(0); + Vertex vtxPreferred = null; + Vertex currentFaveVtx = dupeVertexList.get(0); for (int i = 1; i < listSize; i++) { - TitanVertex vtxB = (TitanVertex) dupeVertexList.get(i); + Vertex vtxB = dupeVertexList.get(i); vtxPreferred = pickOneOfTwoDupes(transId, fromAppId, g, currentFaveVtx, vtxB, ver, loader); if (vtxPreferred == null) { @@ -1450,15 +1452,15 @@ public class DataGrooming { * @param vtxA the vtx A * @param vtxB the vtx B * @param ver the ver - * @return TitanVertex + * @return Vertex * @throws AAIException the AAI exception */ - public static TitanVertex pickOneOfTwoDupes(String transId, - String fromAppId, TitanTransaction g, TitanVertex vtxA, - TitanVertex vtxB, String ver, Loader loader) throws AAIException { + public static Vertex pickOneOfTwoDupes(String transId, + String fromAppId, GraphTraversalSource g, Vertex vtxA, + Vertex vtxB, String ver, Loader loader) throws AAIException { - TitanVertex nullVtx = null; - TitanVertex preferredVtx = null; + Vertex nullVtx = null; + Vertex preferredVtx = null; Long vidA = new Long(vtxA.id().toString()); Long vidB = new Long(vtxB.id().toString()); @@ -1520,11 +1522,11 @@ public class DataGrooming { HashMap<String, String> nodeTypesConn2A = new HashMap<>(); HashMap<String, String> nodeTypesConn2B = new HashMap<>(); - ArrayList<TitanVertex> vertListA = getConnectedNodes( g, vtxA ); + ArrayList<Vertex> vertListA = getConnectedNodes( g, vtxA ); if (vertListA != null) { - Iterator<TitanVertex> iter = vertListA.iterator(); + Iterator<Vertex> iter = vertListA.iterator(); while (iter.hasNext()) { - TitanVertex tvCon = iter.next(); + Vertex tvCon = iter.next(); String conVid = tvCon.id().toString(); String nt = ""; objType = tvCon.<Object>property("aai-node-type").orElse(null); @@ -1536,11 +1538,11 @@ public class DataGrooming { } } - ArrayList<TitanVertex> vertListB = getConnectedNodes( g, vtxB ); + ArrayList<Vertex> vertListB = getConnectedNodes( g, vtxB ); if (vertListB != null) { - Iterator<TitanVertex> iter = vertListB.iterator(); + Iterator<Vertex> iter = vertListB.iterator(); while (iter.hasNext()) { - TitanVertex tvCon = iter.next(); + Vertex tvCon = iter.next(); String conVid = tvCon.id().toString(); String nt = ""; objType = tvCon.<Object>property("aai-node-type").orElse(null); @@ -1663,13 +1665,13 @@ public class DataGrooming { * @return the array list */ private static List<String> checkAndProcessDupes(String transId, - String fromAppId, TitanTransaction g, String version, String nType, - List<TitanVertex> passedVertList, Boolean dupeFixOn, + String fromAppId, TitanTransaction g, GraphTraversalSource source, String version, String nType, + List<Vertex> passedVertList, Boolean dupeFixOn, Set<String> deleteCandidateList, Boolean singleCommits, ArrayList<String> alreadyFoundDupeGroups, Loader loader ) { ArrayList<String> returnList = new ArrayList<>(); - ArrayList<TitanVertex> checkVertList = new ArrayList<>(); + ArrayList<Vertex> checkVertList = new ArrayList<>(); ArrayList<String> alreadyFoundDupeVidArr = new ArrayList<>(); Boolean noFilterList = true; Iterator<String> afItr = alreadyFoundDupeGroups.iterator(); @@ -1710,9 +1712,9 @@ public class DataGrooming { // not process any vertices that we've already seen. try { - Iterator<TitanVertex> pItr = passedVertList.iterator(); + Iterator<Vertex> pItr = passedVertList.iterator(); while (pItr.hasNext()) { - TitanVertex tvx = (TitanVertex) pItr.next(); + Vertex tvx = pItr.next(); String passedId = tvx.id().toString(); if (noFilterList || !alreadyFoundDupeVidArr.contains(passedId)) { // We haven't seen this one before - so we should check it. @@ -1733,12 +1735,12 @@ public class DataGrooming { String dupesStr = ""; for (int i = 0; i < checkVertList.size(); i++) { dupesStr = dupesStr - + ((TitanVertex) (checkVertList.get(i))).id() + + ((checkVertList.get(i))).id() .toString() + "|"; } if (dupesStr != "") { - TitanVertex prefV = getPreferredDupe(transId, fromAppId, - g, checkVertList, version, loader); + Vertex prefV = getPreferredDupe(transId, fromAppId, + source, checkVertList, version, loader); if (prefV == null) { // We could not determine which duplicate to keep dupesStr = dupesStr + "KeepVid=UNDETERMINED"; @@ -1767,12 +1769,12 @@ public class DataGrooming { // are also pointing at the same 'parent' node. // Note: for a given set of key data, it is possible that there // could be more than one set of duplicates. - HashMap<String, ArrayList<TitanVertex>> vertsGroupedByParentHash = groupVertsByDepNodes( - transId, fromAppId, g, version, nType, + HashMap<String, ArrayList<Vertex>> vertsGroupedByParentHash = groupVertsByDepNodes( + transId, fromAppId, source, version, nType, checkVertList, loader); - for (Map.Entry<String, ArrayList<TitanVertex>> entry : vertsGroupedByParentHash + for (Map.Entry<String, ArrayList<Vertex>> entry : vertsGroupedByParentHash .entrySet()) { - ArrayList<TitanVertex> thisParentsVertList = entry + ArrayList<Vertex> thisParentsVertList = entry .getValue(); if (thisParentsVertList.size() > 1) { // More than one vertex found with the same key info @@ -1780,12 +1782,12 @@ public class DataGrooming { String dupesStr = ""; for (int i = 0; i < thisParentsVertList.size(); i++) { dupesStr = dupesStr - + ((TitanVertex) (thisParentsVertList + + ((thisParentsVertList .get(i))).id() + "|"; } if (dupesStr != "") { - TitanVertex prefV = getPreferredDupe(transId, - fromAppId, g, thisParentsVertList, + Vertex prefV = getPreferredDupe(transId, + fromAppId, source, thisParentsVertList, version, loader); if (prefV == null) { @@ -1835,9 +1837,9 @@ public class DataGrooming { * @return the hash map * @throws AAIException the AAI exception */ - private static HashMap<String, ArrayList<TitanVertex>> groupVertsByDepNodes( - String transId, String fromAppId, TitanTransaction g, String version, - String nType, ArrayList<TitanVertex> passedVertList, Loader loader) + private static HashMap<String, ArrayList<Vertex>> groupVertsByDepNodes( + String transId, String fromAppId, GraphTraversalSource g, String version, + String nType, ArrayList<Vertex> passedVertList, Loader loader) throws AAIException { // Given a list of Titan Vertices of one nodeType (see AAI-8956), group // them together by the parent node they depend on. @@ -1847,7 +1849,7 @@ public class DataGrooming { // we're trying to find duplicates - so we // allow for the case where more than one is under the same parent node. - HashMap<String, ArrayList<TitanVertex>> retHash = new HashMap<String, ArrayList<TitanVertex>>(); + HashMap<String, ArrayList<Vertex>> retHash = new HashMap<String, ArrayList<Vertex>>(); if (loader.introspectorFromName(nType).isTopLevel()) { // This method really should not have been called if this is not the // kind of node @@ -1866,10 +1868,10 @@ public class DataGrooming { // For each vertex, we want find its depended-on/parent vertex so we // can track what other vertexes that are dependent on that same guy. if (passedVertList != null) { - Iterator<TitanVertex> iter = passedVertList.iterator(); + Iterator<Vertex> iter = passedVertList.iterator(); while (iter.hasNext()) { - TitanVertex thisVert = iter.next(); - TitanVertex tmpParentVtx = getConnectedParent( g, thisVert ); + Vertex thisVert = iter.next(); + Vertex tmpParentVtx = getConnectedParent( g, thisVert ); if( tmpParentVtx != null ) { String parentNt = null; Object obj = tmpParentVtx.<Object>property("aai-node-type").orElse(null); @@ -1884,7 +1886,7 @@ public class DataGrooming { retHash.get(parentVid).add(thisVert); } else { // This is the first one we found on this parent - ArrayList<TitanVertex> vList = new ArrayList<>(); + ArrayList<Vertex> vList = new ArrayList<>(); vList.add(thisVert); retHash.put(parentVid, vList); } @@ -1960,7 +1962,7 @@ public class DataGrooming { try { long longVertId = Long .parseLong(thisVid); - TitanVertex vtx = g + Vertex vtx = g .getVertex(longVertId); vtx.remove(); if (singleCommits) { @@ -2005,10 +2007,10 @@ public class DataGrooming { * @return the node just using key params * @throws AAIException the AAI exception */ - public static List <TitanVertex> getNodeJustUsingKeyParams( String transId, String fromAppId, TitanTransaction graph, String nodeType, + public static List <Vertex> getNodeJustUsingKeyParams( String transId, String fromAppId, GraphTraversalSource graph, String nodeType, HashMap<String,Object> keyPropsHash, String apiVersion ) throws AAIException{ - List <TitanVertex> retVertList = new ArrayList <> (); + List <Vertex> retVertList = new ArrayList <> (); // We assume that all NodeTypes have at least one key-property defined. // Note - instead of key-properties (the primary key properties), a user could pass @@ -2026,32 +2028,32 @@ public class DataGrooming { kVal.add(i, entry.getValue()); } int topPropIndex = i; - TitanVertex tiV = null; + Vertex tiV = null; String propsAndValuesForMsg = ""; - Iterable <?> verts = null; + Iterator <Vertex> verts = null; try { if( topPropIndex == 0 ){ propsAndValuesForMsg = " (" + kName.get(0) + " = " + kVal.get(0) + ") "; - verts= graph.query().has(kName.get(0),kVal.get(0)).has("aai-node-type",nodeType).vertices(); + verts= graph.V().has(kName.get(0),kVal.get(0)).has("aai-node-type",nodeType); } else if( topPropIndex == 1 ){ propsAndValuesForMsg = " (" + kName.get(0) + " = " + kVal.get(0) + ", " + kName.get(1) + " = " + kVal.get(1) + ") "; - verts = graph.query().has(kName.get(0),kVal.get(0)).has(kName.get(1),kVal.get(1)).has("aai-node-type",nodeType).vertices(); + verts = graph.V().has(kName.get(0),kVal.get(0)).has(kName.get(1),kVal.get(1)).has("aai-node-type",nodeType); } else if( topPropIndex == 2 ){ propsAndValuesForMsg = " (" + kName.get(0) + " = " + kVal.get(0) + ", " + kName.get(1) + " = " + kVal.get(1) + ", " + kName.get(2) + " = " + kVal.get(2) + ") "; - verts= graph.query().has(kName.get(0),kVal.get(0)).has(kName.get(1),kVal.get(1)).has(kName.get(2),kVal.get(2)).has("aai-node-type",nodeType).vertices(); + verts= graph.V().has(kName.get(0),kVal.get(0)).has(kName.get(1),kVal.get(1)).has(kName.get(2),kVal.get(2)).has("aai-node-type",nodeType); } else if( topPropIndex == 3 ){ propsAndValuesForMsg = " (" + kName.get(0) + " = " + kVal.get(0) + ", " + kName.get(1) + " = " + kVal.get(1) + ", " + kName.get(2) + " = " + kVal.get(2) + ", " + kName.get(3) + " = " + kVal.get(3) + ") "; - verts= graph.query().has(kName.get(0),kVal.get(0)).has(kName.get(1),kVal.get(1)).has(kName.get(2),kVal.get(2)).has(kName.get(3),kVal.get(3)).has("aai-node-type",nodeType).vertices(); + verts= graph.V().has(kName.get(0),kVal.get(0)).has(kName.get(1),kVal.get(1)).has(kName.get(2),kVal.get(2)).has(kName.get(3),kVal.get(3)).has("aai-node-type",nodeType); } else { throw new AAIException("AAI_6114", " We only support 4 keys per nodeType for now \n"); @@ -2062,9 +2064,8 @@ public class DataGrooming { } if( verts != null ){ - Iterator <?> vertI = verts.iterator(); - while( vertI.hasNext() ){ - tiV = (TitanVertex) vertI.next(); + while( verts.hasNext() ){ + tiV = verts.next(); retVertList.add(tiV); } } @@ -2086,7 +2087,7 @@ public class DataGrooming { * @param tVert the t vert * @return the array list */ - private static ArrayList <String> showAllEdgesForNode( String transId, String fromAppId, TitanVertex tVert ){ + private static ArrayList <String> showAllEdgesForNode( String transId, String fromAppId, Vertex tVert ){ ArrayList <String> retArr = new ArrayList <> (); Iterator <Edge> eI = tVert.edges(Direction.IN); @@ -2096,7 +2097,7 @@ public class DataGrooming { while( eI.hasNext() ){ TitanEdge ed = (TitanEdge) eI.next(); String lab = ed.label(); - TitanVertex vtx = (TitanVertex) ed.otherVertex(tVert); + Vertex vtx = ed.otherVertex(tVert); if( vtx == null ){ retArr.add(" >>> COULD NOT FIND VERTEX on the other side of this edge edgeId = " + ed.id() + " <<< "); } @@ -2115,7 +2116,7 @@ public class DataGrooming { while( eI.hasNext() ){ TitanEdge ed = (TitanEdge) eI.next(); String lab = ed.label(); - TitanVertex vtx = (TitanVertex) ed.otherVertex(tVert); + Vertex vtx = ed.otherVertex(tVert); if( vtx == null ){ retArr.add(" >>> COULD NOT FIND VERTEX on the other side of this edge edgeId = " + ed.id() + " <<< "); } @@ -2137,7 +2138,7 @@ public class DataGrooming { * @param tVert the t vert * @return the array list */ - private static ArrayList <String> showPropertiesForNode( String transId, String fromAppId, TitanVertex tVert ){ + private static ArrayList <String> showPropertiesForNode( String transId, String fromAppId, Vertex tVert ){ ArrayList <String> retArr = new ArrayList <> (); if( tVert == null ){ @@ -2166,19 +2167,19 @@ public class DataGrooming { } - private static ArrayList <TitanVertex> getConnectedNodes(TitanTransaction g, TitanVertex startVtx ) + private static ArrayList <Vertex> getConnectedNodes(GraphTraversalSource g, Vertex startVtx ) throws AAIException { - ArrayList <TitanVertex> retArr = new ArrayList <> (); + ArrayList <Vertex> retArr = new ArrayList <> (); if( startVtx == null ){ return retArr; } else { GraphTraversal<Vertex, Vertex> modPipe = null; - modPipe = g.traversal().V(startVtx).both(); + modPipe = g.V(startVtx).both(); if( modPipe != null && modPipe.hasNext() ){ while( modPipe.hasNext() ){ - TitanVertex conVert = (TitanVertex) modPipe.next(); + Vertex conVert = modPipe.next(); retArr.add(conVert); } } @@ -2188,15 +2189,14 @@ public class DataGrooming { }// End of getConnectedNodes() - private static ArrayList <TitanVertex> getConnectedChildrenOfOneType( TitanTransaction graph, - TitanVertex startVtx, String childNType ) throws AAIException{ + private static ArrayList <Vertex> getConnectedChildrenOfOneType( GraphTraversalSource g, + Vertex startVtx, String childNType ) throws AAIException{ - ArrayList <TitanVertex> childList = new ArrayList <> (); - Iterable <?> verts = startVtx.query().direction(Direction.OUT).has("isParent",true).vertices(); - Iterator <?> vertI = verts.iterator(); - TitanVertex tmpVtx = null; + ArrayList <Vertex> childList = new ArrayList <> (); + Iterator <Vertex> vertI = g.V(startVtx).union(__.outE().has(EdgeProperties.out(EdgeProperty.IS_PARENT), true), __.inE().has(EdgeProperties.in(EdgeProperty.IS_PARENT), true)).bothV(); + Vertex tmpVtx = null; while( vertI != null && vertI.hasNext() ){ - tmpVtx = (TitanVertex) vertI.next(); + tmpVtx = vertI.next(); Object ob = tmpVtx.<Object>property("aai-node-type").orElse(null); if (ob != null) { String tmpNt = ob.toString(); @@ -2211,15 +2211,14 @@ public class DataGrooming { }// End of getConnectedChildrenOfOneType() - private static TitanVertex getConnectedParent( TitanTransaction graph, - TitanVertex startVtx ) throws AAIException{ + private static Vertex getConnectedParent( GraphTraversalSource g, + Vertex startVtx ) throws AAIException{ - TitanVertex parentVtx = null; - Iterable <?> verts = startVtx.query().direction(Direction.IN).has("isParent",true).vertices(); - Iterator <?> vertI = verts.iterator(); + Vertex parentVtx = null; + Iterator <Vertex> vertI = g.V(startVtx).union(__.inE().has(EdgeProperties.out(EdgeProperty.IS_PARENT), true), __.outE().has(EdgeProperties.in(EdgeProperty.IS_PARENT), true)).bothV(); while( vertI != null && vertI.hasNext() ){ // Note - there better only be one! - parentVtx = (TitanVertex) vertI.next(); + parentVtx = vertI.next(); } return parentVtx; diff --git a/aai-core/src/main/java/org/openecomp/aai/introspection/generator/CreateExample.java b/aai-core/src/main/java/org/openecomp/aai/introspection/generator/CreateExample.java index a94d5406..fd258460 100644 --- a/aai-core/src/main/java/org/openecomp/aai/introspection/generator/CreateExample.java +++ b/aai-core/src/main/java/org/openecomp/aai/introspection/generator/CreateExample.java @@ -20,9 +20,9 @@ package org.openecomp.aai.introspection.generator; +import java.security.SecureRandom; import java.util.ArrayList; import java.util.List; -import java.util.Random; import org.openecomp.aai.exceptions.AAIException; import org.openecomp.aai.introspection.Introspector; @@ -33,7 +33,7 @@ import org.openecomp.aai.introspection.Wanderer; public class CreateExample implements Wanderer { - private Random rand = new Random(); + private SecureRandom rand = new SecureRandom(); private final long range = 100000000L; private Loader loader = null; private Introspector result = null; diff --git a/aai-core/src/main/java/org/openecomp/aai/restcore/RESTAPI.java b/aai-core/src/main/java/org/openecomp/aai/restcore/RESTAPI.java index f1f53a4d..18aef073 100644 --- a/aai-core/src/main/java/org/openecomp/aai/restcore/RESTAPI.java +++ b/aai-core/src/main/java/org/openecomp/aai/restcore/RESTAPI.java @@ -44,8 +44,6 @@ import org.openecomp.aai.introspection.tools.RemoveNonVisibleProperty; import org.openecomp.aai.logging.ErrorLogHelper; import org.openecomp.aai.logging.LoggingContext; import org.openecomp.aai.util.AAIConfig; -import org.openecomp.aai.util.AAIConstants; -import org.openecomp.aai.util.AAITxnLog; import org.openecomp.aai.util.FormatDate; import com.att.eelf.configuration.EELFLogger; @@ -74,9 +72,6 @@ public class RESTAPI { GET, PUT, POST, DELETE }; - - public AAITxnLog txn = null; - /** * Gets the from app id. * @@ -140,7 +135,7 @@ public class RESTAPI { * @return the string */ protected String genDate() { - FormatDate fd = new FormatDate(AAIConfig.get(AAIConstants.HBASE_TABLE_TIMESTAMP_FORMAT, "YYMMdd-HH:mm:ss:SSS")); + FormatDate fd = new FormatDate( "YYMMdd-HH:mm:ss:SSS"); return fd.getDateTime(); } @@ -162,67 +157,6 @@ public class RESTAPI { } - /** - * Log transaction. - * - * @param appId the app id - * @param tId the t id - * @param action the action - * @param input the input - * @param rqstTm the rqst tm - * @param respTm the resp tm - * @param request the request - * @param response the response - */ - /* ---------------- Log Transaction into HBase --------------------- */ - public void logTransaction( String appId, String tId, String action, - String input, String rqstTm, String respTm, String request, Response response) { - String respBuf = ""; - int status = 0; - - if (response != null && response.getEntity() != null) { - respBuf = response.getEntity().toString(); - status = response.getStatus(); - } - logTransaction(appId, tId, action, input, rqstTm, respTm, request, respBuf, String.valueOf(status)); - } - - /** - * Log transaction. - * - * @param appId the app id - * @param tId the t id - * @param action the action - * @param input the input - * @param rqstTm the rqst tm - * @param respTm the resp tm - * @param request the request - * @param respBuf the resp buf - * @param status the status - * @param logline the logline - */ - public void logTransaction( String appId, String tId, String action, - String input, String rqstTm, String respTm, String request, String respBuf, String status) { - try { - // we only run this way if we're not doing it in the CXF interceptor - if (!AAIConfig.get(AAIConstants.AAI_LOGGING_HBASE_INTERCEPTOR).equalsIgnoreCase("true")) { - if (AAIConfig.get(AAIConstants.AAI_LOGGING_HBASE_ENABLED).equalsIgnoreCase("true")) { - txn = new AAITxnLog(tId, appId); - // tid, status, rqstTm, respTm, srcId, rsrcId, rsrcType, rqstBuf, respBuf - String hbtid = txn.put(tId, status, - rqstTm, respTm, appId, input, action, request, respBuf); - - LOGGER.debug("HbTransId={}",hbtid); - LOGGER.debug("action={}", action); - LOGGER.debug("urlin={}", input); - } - - } - } catch (AAIException e) { - // i think we do nothing - } - } - /* ----------helpers for common consumer actions ----------- */ /** diff --git a/aai-core/src/main/java/org/openecomp/aai/serialization/db/DBSerializer.java b/aai-core/src/main/java/org/openecomp/aai/serialization/db/DBSerializer.java index 4e261060..49bcfd5a 100644 --- a/aai-core/src/main/java/org/openecomp/aai/serialization/db/DBSerializer.java +++ b/aai-core/src/main/java/org/openecomp/aai/serialization/db/DBSerializer.java @@ -142,6 +142,13 @@ public class DBSerializer { } + private void touchStandardVertexProperties(String nodeType, Vertex v, boolean isNewVertex) { + + v.property(AAIProperties.NODE_TYPE, nodeType); + touchStandardVertexProperties(v, isNewVertex); + + } + /** @@ -156,8 +163,7 @@ public class DBSerializer { Vertex v = this.engine.tx().addVertex(); - v.property(AAIProperties.NODE_TYPE, wrappedObject.getDbName()); - touchStandardVertexProperties(v, true); + touchStandardVertexProperties(wrappedObject.getDbName(), v, true); return v; } @@ -272,6 +278,9 @@ public class DBSerializer { List<Vertex> processedVertexes = new ArrayList<>(); boolean isComplexType = false; boolean isListType = false; + if (!obj.isContainer()) { + this.touchStandardVertexProperties(obj.getDbName(), v, false); + } this.executePreSideEffects(obj, v); for (String property : properties) { Object value = null; @@ -1156,7 +1165,7 @@ public class DBSerializer { e = this.getEdgeBetween(EdgeType.COUSIN, inputVertex, relatedVertex); if (e == null) { edgeRules.addEdge(this.engine.asAdmin().getTraversalSource(), inputVertex, relatedVertex); - + } else { //attempted to link two vertexes already linked } @@ -1358,7 +1367,12 @@ public class DBSerializer { //are there any cousin edges? long children = 0; for (Edge e : inEdges) { - if (e.<Boolean>property("isParent").orElse(false)) { + if (e.<Boolean>property(EdgeProperties.out(EdgeProperty.IS_PARENT)).orElse(false)) { + children++; + } + } + for (Edge e : outEdges) { + if (e.<Boolean>property(EdgeProperties.in(EdgeProperty.IS_PARENT)).orElse(false)) { children++; } } @@ -1376,7 +1390,7 @@ public class DBSerializer { } else { result = true; for (Edge edge : outEdges) { - Object property = edge.<Boolean>property("isParent").orElse(null); + Object property = edge.<Boolean>property(EdgeProperties.out(EdgeProperty.IS_PARENT)).orElse(null); if (property != null && property.equals(Boolean.TRUE)) { Vertex v = edge.inVertex(); String vType = v.<String>property(AAIProperties.NODE_TYPE).orElse(null); @@ -1389,7 +1403,7 @@ public class DBSerializer { } for (Edge edge : inEdges) { - Object property = edge.<Boolean>property("isParent-REV").orElse(null); + Object property = edge.<Boolean>property(EdgeProperties.in(EdgeProperty.IS_PARENT)).orElse(null); if (property != null && property.equals(Boolean.TRUE)) { Vertex v = edge.outVertex(); String vType = v.<String>property(AAIProperties.NODE_TYPE).orElse(null); diff --git a/aai-core/src/main/java/org/openecomp/aai/serialization/db/EdgeProperties.java b/aai-core/src/main/java/org/openecomp/aai/serialization/db/EdgeProperties.java new file mode 100644 index 00000000..8ce1d1c9 --- /dev/null +++ b/aai-core/src/main/java/org/openecomp/aai/serialization/db/EdgeProperties.java @@ -0,0 +1,43 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * 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.aai.serialization.db; + +public class EdgeProperties { + + public static String out(EdgeProperty prop) { + + return out(prop.toString()); + } + + public static String in(EdgeProperty prop) { + return in(prop.toString()); + } + + public static String out(String prop) { + + return prop; + } + + public static String in(String prop) { + return prop + "-REV"; + } + +} diff --git a/aai-core/src/main/java/org/openecomp/aai/serialization/db/EdgeProperty.java b/aai-core/src/main/java/org/openecomp/aai/serialization/db/EdgeProperty.java new file mode 100644 index 00000000..ce21431d --- /dev/null +++ b/aai-core/src/main/java/org/openecomp/aai/serialization/db/EdgeProperty.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * 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.aai.serialization.db; + +public enum EdgeProperty { + IS_PARENT("isParent"), + USES_RESOURCE("usesResource"), + HAS_DEL_TARGET("hasDelTarget"), + SVC_INFRA("SVC-INFRA"); + private final String name; + + private EdgeProperty(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } +} diff --git a/aai-core/src/main/java/org/openecomp/aai/serialization/engines/query/GraphTraversalQueryEngine.java b/aai-core/src/main/java/org/openecomp/aai/serialization/engines/query/GraphTraversalQueryEngine.java index 7144aa69..a1967223 100644 --- a/aai-core/src/main/java/org/openecomp/aai/serialization/engines/query/GraphTraversalQueryEngine.java +++ b/aai-core/src/main/java/org/openecomp/aai/serialization/engines/query/GraphTraversalQueryEngine.java @@ -32,9 +32,10 @@ import org.apache.tinkerpop.gremlin.structure.Direction; import org.apache.tinkerpop.gremlin.structure.Edge; import org.apache.tinkerpop.gremlin.structure.Element; import org.apache.tinkerpop.gremlin.structure.Vertex; - import org.openecomp.aai.db.props.AAIProperties; import org.openecomp.aai.introspection.Loader; +import org.openecomp.aai.serialization.db.EdgeProperties; +import org.openecomp.aai.serialization.db.EdgeProperty; /* * This class needs some big explanation despite its compact size. @@ -62,7 +63,7 @@ public class GraphTraversalQueryEngine extends QueryEngine { @Override public List<Vertex> findParents(Vertex start) { - final GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).emit(v -> true).repeat(__.inE().has("isParent", true).outV()); + final GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).emit(v -> true).repeat(__.union(__.inE().has(EdgeProperties.out(EdgeProperty.IS_PARENT), true).outV(), __.outE().has(EdgeProperties.in(EdgeProperty.IS_PARENT), true).inV())); return pipe.toList(); } @@ -73,7 +74,7 @@ public class GraphTraversalQueryEngine extends QueryEngine { public List<Vertex> findAllChildren(Vertex start) { GraphTraversal<Vertex, Vertex> pipe = this.g - .V(start).emit(v -> true).repeat(__.outE().has("isParent", true).inV()); + .V(start).emit(v -> true).repeat(__.union(__.outE().has(EdgeProperties.out(EdgeProperty.IS_PARENT), true).inV(), __.inE().has(EdgeProperties.in(EdgeProperty.IS_PARENT), true).outV())); return pipe.toList(); @@ -82,8 +83,8 @@ public class GraphTraversalQueryEngine extends QueryEngine { public List<Vertex> findChildrenOfType(Vertex start, String type) { GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).union( - __.outE().has("isParent", true).inV(), - __.inE().has("isParent-REV", true).outV() + __.outE().has(EdgeProperties.out(EdgeProperty.IS_PARENT), true).inV(), + __.inE().has(EdgeProperties.in(EdgeProperty.IS_PARENT), true).outV() ).has(AAIProperties.NODE_TYPE, type).dedup(); return pipe.toList(); @@ -91,8 +92,8 @@ public class GraphTraversalQueryEngine extends QueryEngine { public List<Vertex> findChildren(Vertex start) { GraphTraversal<Vertex, Vertex> pipe = this.g.V(start).union( - __.outE().has("isParent", true), - __.inE().has("isParent-REV", true) + __.outE().has(EdgeProperties.out(EdgeProperty.IS_PARENT), true), + __.inE().has(EdgeProperties.in(EdgeProperty.IS_PARENT), true) ).otherV().dedup(); return pipe.toList(); @@ -104,9 +105,18 @@ public class GraphTraversalQueryEngine extends QueryEngine { @Override public List<Vertex> findDeletable(Vertex start) { GraphTraversal<Vertex, Vertex> pipe = this.g - .V(start).emit(v -> true).repeat(__.outE().or( - __.has("isParent", true), - __.has("hasDelTarget", true)).inV()); + .V(start).emit(v -> true).repeat( + __.union( + __.outE().or( + __.has(EdgeProperties.out(EdgeProperty.IS_PARENT), true), + __.has(EdgeProperties.out(EdgeProperty.HAS_DEL_TARGET), true) + ).inV(), + __.inE().or( + __.has(EdgeProperties.in(EdgeProperty.IS_PARENT), true), + __.has(EdgeProperties.in(EdgeProperty.HAS_DEL_TARGET), true) + ).outV() + ) + ); return pipe.toList(); } @@ -138,12 +148,15 @@ public class GraphTraversalQueryEngine extends QueryEngine { @Override public Tree<Element> findSubGraph(Vertex start, int iterations, boolean nodeOnly) { final GraphTraversal<Vertex, ?> t = this.g.V(start).emit(v -> true).times(iterations).repeat( - __.outE().has("isParent", true).inV()); + __.union( + __.outE().has(EdgeProperties.out(EdgeProperty.IS_PARENT), true).inV(), + __.inE().has(EdgeProperties.in(EdgeProperty.IS_PARENT), true).outV()) + ); if (!nodeOnly) { t.union( __.identity(), - __.bothE().has("isParent", false).dedup().otherV() + __.bothE().and(__.has(EdgeProperties.out(EdgeProperty.IS_PARENT), false), __.has(EdgeProperties.in(EdgeProperty.IS_PARENT), false)).dedup().otherV() ); } t.tree(); @@ -158,8 +171,8 @@ public class GraphTraversalQueryEngine extends QueryEngine { public List<Edge> findEdgesForVersion(Vertex start, Loader loader) { final Set<String> objects = loader.getAllObjects().keySet(); GraphTraversal<Vertex, Edge> pipeline = this.g.V(start).union( - __.inE().has("isParent", false).has("isParent-REV", false).where(__.outV().has(AAIProperties.NODE_TYPE, P.within(objects))), - __.outE().has("isParent", false).has("isParent-REV", false).where(__.inV().has(AAIProperties.NODE_TYPE, P.within(objects))) + __.inE().has(EdgeProperties.out(EdgeProperty.IS_PARENT), false).has(EdgeProperties.in(EdgeProperty.IS_PARENT), false).where(__.outV().has(AAIProperties.NODE_TYPE, P.within(objects))), + __.outE().has(EdgeProperties.out(EdgeProperty.IS_PARENT), false).has(EdgeProperties.in(EdgeProperty.IS_PARENT), false).where(__.inV().has(AAIProperties.NODE_TYPE, P.within(objects))) ).dedup(); return pipeline.toList(); @@ -169,8 +182,8 @@ public class GraphTraversalQueryEngine extends QueryEngine { @Override public List<Vertex> findCousinVertices(Vertex start) { GraphTraversal<Vertex, Vertex> pipeline = this.g.V(start).union( - __.inE().has("isParent", false).has("isParent-REV", false), - __.outE().has("isParent", false).has("isParent-REV", false)).otherV().dedup(); + __.inE().has(EdgeProperties.out(EdgeProperty.IS_PARENT), false).has(EdgeProperties.in(EdgeProperty.IS_PARENT), false), + __.outE().has(EdgeProperties.out(EdgeProperty.IS_PARENT), false).has(EdgeProperties.in(EdgeProperty.IS_PARENT), false)).otherV().dedup(); return pipeline.toList(); } diff --git a/aai-core/src/main/java/org/openecomp/aai/serialization/engines/query/GremlinQueryEngine.java b/aai-core/src/main/java/org/openecomp/aai/serialization/engines/query/GremlinQueryEngine.java new file mode 100644 index 00000000..1a6aa6d0 --- /dev/null +++ b/aai-core/src/main/java/org/openecomp/aai/serialization/engines/query/GremlinQueryEngine.java @@ -0,0 +1,175 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * 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.aai.serialization.engines.query; +// +//import java.util.List; +//import java.util.regex.Matcher; +//import java.util.regex.Pattern; +// +//import org.apache.commons.collections.IteratorUtils; +// +//import org.openecomp.aai.db.AAIProperties; +//import org.openecomp.aai.query.builder.QueryBuilder; +//import org.openecomp.aai.serialization.engines.TransactionalGraphEngine; +//import com.tinkerpop.blueprints.Direction; +//import com.tinkerpop.blueprints.Graph; +//import com.tinkerpop.blueprints.TransactionalGraph; +//import com.tinkerpop.blueprints.Vertex; +//import com.tinkerpop.gremlin.groovy.Gremlin; +//import com.tinkerpop.gremlin.java.GremlinPipeline; +//import com.tinkerpop.pipes.Pipe; +//import com.tinkerpop.pipes.util.iterators.SingleIterator; +// +//public class GremlinQueryEngine extends QueryEngine { +// +// public GremlinQueryEngine (TransactionalGraphEngine engine) { +// super(engine); +// +// } +// +// +// @Override +// public List<Vertex> executeQuery(TransactionalGraph g, QueryBuilder query) { +// List<Vertex> result = null; +// Vertex start = query.getStart(); +// if (start != null) { +// result = this.executeQuery(start, (String)query.getQuery()); +// } else { +// result = this.processGremlinQuery((String)query.getQuery()); +// } +// return result; +// +// } +// +// @Override +// public List<Vertex> executeParentQuery(TransactionalGraph g, QueryBuilder query) { +// +// List<Vertex> result = null; +// Vertex start = query.getStart(); +// if (start != null) { +// result = this.executeQuery(start, (String)query.getParentQuery()); +// } else { +// result = this.processGremlinQuery((String)query.getParentQuery()); +// } +// return result; +// } +// +// private List<Vertex> executeQuery(Vertex startVertex, String query) { +// +// return this.processGremlinQuery(startVertex, "_()" + query); +// +// } +// +// @Override +// public List<Vertex> findParents(Vertex start) { +// +// String findAllParents = ".as('x').inE.has('isParent', true).outV" +// + ".loop('x'){it.object.inE.has('isParent',true).count()==1}{true}"; +// +// List<Vertex> results = this.executeQuery(start, findAllParents); +// results.add(0, start); +// return results; +// +// } +// +// @Override +// public List<Vertex> findChildren(Vertex start) { +// String findAllChildren = ".as('x').outE.has('isParent', true).inV" +// + ".loop('x'){it.object.outE.has('isParent', true).count() >= 1}{true}"; +// +// List<Vertex> results = this.executeQuery(start, findAllChildren); +// results.add(0, start); +// return results; +// +// } +// +// @Override +// public List<Vertex> findDeletable(Vertex start) { +// String findAllChildren = ".as('x').outE.or(_().has('isParent', true), _().has('hasDelTarget', true)).inV" +// + ".loop('x'){it.object.outE.or(_().has('isParent', true), _().has('hasDelTarget', true)).count() >= 1}{true}"; +// +// List<Vertex> results = this.executeQuery(start, findAllChildren); +// results.add(0, start); +// return results; +// } +// private List<Vertex> processGremlinQuery(String query) { +// +// Pattern firstHasSet = Pattern.compile("^(\\.has\\(.*?\\))(\\.has\\(.*?\\))*(?!\\.has)"); +// Pattern p = Pattern.compile("\\.has\\('(.*?)',\\s?'(.*?)'\\)"); +// Matcher m = firstHasSet.matcher(query); +// List<Vertex> results = null; +// GremlinPipeline<Graph, Vertex> pipe = new GremlinPipeline<>(dbEngine.getGraph()); +// if (m.find()) { +// String hasSet = m.group(); +// query = query.replace(m.group(0), ""); +// m = p.matcher(hasSet); +// pipe.V(); +// while (m.find()) { +// pipe.has(m.group(1), m.group(2)); +// } +// results = processGremlinQuery(pipe.toList(), "_()" + query); +// } +// +// return results; +// +// } +// private List<Vertex> processGremlinQuery(Vertex startVertex, String query) { +// +// Pipe pipe = Gremlin.compile(query); +// pipe.setStarts(new SingleIterator<Vertex>(startVertex)); +// +// return (List<Vertex>)IteratorUtils.toList(pipe.iterator()); +// } +// private List<Vertex> processGremlinQuery(List<Vertex> list, String query) { +// +// Pipe pipe = Gremlin.compile(query); +// +// pipe.setStarts(list); +// +// return (List<Vertex>)IteratorUtils.toList(pipe.iterator()); +// } +// +// +// @Override +// public List<Vertex> findRelatedVertices(Vertex start, Direction direction, String label, String nodeType) { +// String findRelatedVertices = "_()"; +// switch (direction) { +// case OUT: +// findRelatedVertices += ".out('" + label + "')"; +// break; +// case IN: +// findRelatedVertices += ".in('" + label + "')"; +// break; +// case BOTH: +// findRelatedVertices += ".both('" + label + "')"; +// break; +// default: +// break; +// } +// findRelatedVertices += ".has('" + AAIProperties.NODE_TYPE + "', '" + nodeType + "').dedup()"; +// List<Vertex> results = this.executeQuery(start, findRelatedVertices); +// results.add(0, start); +// return results; +// } +// +//} +// diff --git a/aai-core/src/main/java/org/openecomp/aai/serialization/queryformats/utils/UrlBuilder.java b/aai-core/src/main/java/org/openecomp/aai/serialization/queryformats/utils/UrlBuilder.java index 50e50e43..817a6795 100644 --- a/aai-core/src/main/java/org/openecomp/aai/serialization/queryformats/utils/UrlBuilder.java +++ b/aai-core/src/main/java/org/openecomp/aai/serialization/queryformats/utils/UrlBuilder.java @@ -51,11 +51,11 @@ public class UrlBuilder { try { final StringBuilder result = new StringBuilder(); final URI uri = this.serializer.getURIForVertex(v); - + result.append(this.serverBase); result.append(this.version); result.append(uri.getRawPath()); - + return result.toString(); } catch (UnsupportedEncodingException | IllegalArgumentException | SecurityException e) { throw new AAIFormatVertexException(e); diff --git a/aai-core/src/main/java/org/openecomp/aai/util/AAIConstants.java b/aai-core/src/main/java/org/openecomp/aai/util/AAIConstants.java index 7d9bcccd..010f7a24 100644 --- a/aai-core/src/main/java/org/openecomp/aai/util/AAIConstants.java +++ b/aai-core/src/main/java/org/openecomp/aai/util/AAIConstants.java @@ -105,16 +105,6 @@ public final class AAIConstants { public static final String AAI_DBMODEL_FILENAME = "aai.dbmodel.filename"; public static final String AAI_RESVERSION_ENABLEFLAG = "aai.resourceversion.enableflag"; - public static final String ECM_OPENSTACK_TENANTID = "ecm.openstack.tenantid"; - - public static final String HBASE_TABLE_NAME = "hbase.table.name"; - public static final String HBASE_NOTIFICATION_TABLE_NAME = "hbase.notificationTable.name"; - public static final String HBASE_TABLE_TIMESTAMP_FORMAT = "hbase.table.timestamp.format"; - public static final String HBASE_CONFIGURATION_ZOOKEEPER_QUORUM = "hbase.zookeeper.quorum"; - public static final String HBASE_CONFIGURATION_ZOOKEEPER_CLIENTPORT = "hbase.zookeeper.property.clientPort"; - public static final String HBASE_ZOOKEEPER_ZNODE_PARENT = "hbase.zookeeper.znode.parent"; - public static final String ZOOKEEPER_ZNODE_PARENT = "zookeeper.znode.parent"; - public static final int AAI_MAX_TRANS_RETRIES = 5; public static final long AAI_TRANS_RETRY_SLEEP_MSEC = 500; @@ -129,12 +119,12 @@ public final class AAIConstants { /** Default to skipping real-time grooming unless system property aai.skiprealtime.grooming is set to "false" */ public static final String AAI_SKIPREALTIME_GROOMING = (System.getProperty("aai.skiprealtime.grooming") == null) ? "true" : System.getProperty("aai.skiprealtime.grooming"); - + /*** UEB ***/ public static final String UEB_PUB_PARTITION_AAI = "AAI"; public static final String UEB_PUB_AAI_VCE_INTERFACE_DATA_TOPIC = "ueb.pub.aai.vce.interface.data.topic"; - + /** Service description for Hosted Communications */ public static final String AAI_SERVICEDESCRIPTION_HOSTEDCOMM = "aai.servicedescription.hostedcomm"; @@ -143,7 +133,7 @@ public final class AAIConstants { /** Service description for Mobility */ public static final String AAI_SERVICEDESCRIPTION_VUSP = "aai.servicedescription.vusp"; - + /** Service description for Vvig */ public static final String AAI_SERVICEDESCRIPTION_VVIG = "aai.servicedescription.vvig"; diff --git a/aai-core/src/main/java/org/openecomp/aai/util/AAITxnLog.java b/aai-core/src/main/java/org/openecomp/aai/util/AAITxnLog.java deleted file mode 100644 index 3a90e8e9..00000000 --- a/aai-core/src/main/java/org/openecomp/aai/util/AAITxnLog.java +++ /dev/null @@ -1,499 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * org.openecomp.aai - * ================================================================================ - * 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.aai.util; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import org.apache.hadoop.hbase.HBaseConfiguration; -import org.apache.hadoop.hbase.client.Get; -import org.apache.hadoop.hbase.client.HTable; -import org.apache.hadoop.hbase.client.Put; -import org.apache.hadoop.hbase.client.Result; -import org.apache.hadoop.hbase.client.ResultScanner; -import org.apache.hadoop.hbase.client.Scan; -import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; -import org.apache.hadoop.hbase.filter.Filter; -import org.apache.hadoop.hbase.filter.FilterList; -import org.apache.hadoop.hbase.filter.RegexStringComparator; -import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; -import org.apache.hadoop.hbase.util.Bytes; -import org.openecomp.aai.domain.notificationEvent.NotificationEvent; -import org.openecomp.aai.domain.translog.TransactionLogEntries; -import org.openecomp.aai.domain.translog.TransactionLogEntry; -import org.openecomp.aai.exceptions.AAIException; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; - -public class AAITxnLog { - - private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(AAITxnLog.class); - - private final org.apache.hadoop.conf.Configuration config; - private HTable table = null; - private String tm = null; - - /** - * Instantiates a new AAI txn log. - * - * @param transId the trans id - * @param fromAppId the from app id - */ - public AAITxnLog(String transId, String fromAppId) { - /* When you create a HBaseConfiguration, it reads in whatever you've set - into your hbase-site.xml and in hbase-default.xml, as long as these can - be found on the CLASSPATH */ - - config = HBaseConfiguration.create(); - - try { - config.set(AAIConstants.ZOOKEEPER_ZNODE_PARENT, AAIConfig.get(AAIConstants.HBASE_ZOOKEEPER_ZNODE_PARENT)); - config.set(AAIConstants.HBASE_CONFIGURATION_ZOOKEEPER_QUORUM, AAIConfig.get(AAIConstants.HBASE_CONFIGURATION_ZOOKEEPER_QUORUM)); - config.set(AAIConstants.HBASE_CONFIGURATION_ZOOKEEPER_CLIENTPORT, AAIConfig.get(AAIConstants.HBASE_CONFIGURATION_ZOOKEEPER_CLIENTPORT)); - - FormatDate fd = new FormatDate(AAIConfig.get(AAIConstants.HBASE_TABLE_TIMESTAMP_FORMAT, "YYMMdd-HH:mm:ss:SSS")); - - tm = fd.getDateTime(); - } catch (AAIException e) { - LOGGER.warn("Missing configuration in AAIConfig: " + e.getMessage()); - } - } - - /** - * Put. - * - * @param status the status - * @param srcId the src id - * @param rsrcId the rsrc id - * @param rsrcType the rsrc type - * @param rqstBuf the rqst buf - * @param respBuf the resp buf - * @return the string - */ - public String put( - String status, - String srcId, - String rsrcId, - String rsrcType, - String rqstBuf, - String respBuf - ) { - return put ("",status,"","",srcId,rsrcId,rsrcType,rqstBuf,respBuf,false,new NotificationEvent()); - - } - - /** - * Put. - * - * @param tid the tid - * @param status the status - * @param srcId the src id - * @param rsrcId the rsrc id - * @param rsrcType the rsrc type - * @param rqstBuf the rqst buf - * @param respBuf the resp buf - * @return the string - */ - public String put( - String tid, - String status, - String srcId, - String rsrcId, - String rsrcType, - String rqstBuf, - String respBuf - ) { - return put (tid,status,"","",srcId,rsrcId,rsrcType,rqstBuf,respBuf,false,new NotificationEvent()); - } - - /** - * Put. - * - * @param tid the tid - * @param status the status - * @param rqstTm the rqst tm - * @param respTm the resp tm - * @param srcId the src id - * @param rsrcId the rsrc id - * @param rsrcType the rsrc type - * @param rqstBuf the rqst buf - * @param respBuf the resp buf - * @return the string - */ - public String put( - String tid, - String status, - String rqstTm, - String respTm, - String srcId, - String rsrcId, - String rsrcType, - String rqstBuf, - String respBuf - ) { - return put (tid,status,"","",srcId,rsrcId,rsrcType,rqstBuf,respBuf,false,new NotificationEvent()); - } - - /** - * Put. - * - * @param tid the tid - * @param status the status - * @param rqstTm the rqst tm - * @param respTm the resp tm - * @param srcId the src id - * @param rsrcId the rsrc id - * @param rsrcType the rsrc type - * @param rqstBuf the rqst buf - * @param respBuf the resp buf - * @param hasNotificationEvent the has notification event - * @param ne the ne - * @return the string - */ - public String put( - String tid, - String status, - String rqstTm, - String respTm, - String srcId, - String rsrcId, - String rsrcType, - String rqstBuf, - String respBuf, - boolean hasNotificationEvent, - NotificationEvent ne - ) { - - if (tid == null || "".equals(tid)) { - FormatDate fd = new FormatDate(AAIConfig.get(AAIConstants.HBASE_TABLE_TIMESTAMP_FORMAT, "YYMMdd-HH:mm:ss:SSS")); - - tm = fd.getDateTime(); - tid = tm + "-"; - } - String htid = tid; - - //need to add a prefix for better hbase logging server balancing - htid = HbaseSaltPrefixer.getInstance().prependSalt(htid); - - if (rqstTm == null || "".equals(rqstTm)) { - rqstTm = tm; - } - - if (respTm == null || "".equals(respTm)) { - respTm = tm; - } - - try { - table = new HTable(config, AAIConfig.get(AAIConstants.HBASE_TABLE_NAME)); - - Put p = new Put(Bytes.toBytes(htid)); - - p.add(Bytes.toBytes("transaction"),Bytes.toBytes("tid"),Bytes.toBytes(tid)); - p.add(Bytes.toBytes("transaction"),Bytes.toBytes("status"),Bytes.toBytes(status)); - p.add(Bytes.toBytes("transaction"),Bytes.toBytes("rqstDate"),Bytes.toBytes(rqstTm)); - p.add(Bytes.toBytes("transaction"),Bytes.toBytes("respDate"),Bytes.toBytes(respTm)); - p.add(Bytes.toBytes("transaction"),Bytes.toBytes("sourceId"),Bytes.toBytes(srcId)); - - p.add(Bytes.toBytes("resource"),Bytes.toBytes("resourceId"),Bytes.toBytes(rsrcId)); - p.add(Bytes.toBytes("resource"),Bytes.toBytes("resourceType"),Bytes.toBytes(rsrcType)); - - p.add(Bytes.toBytes("payload"),Bytes.toBytes("rqstBuf"),Bytes.toBytes(rqstBuf)); - p.add(Bytes.toBytes("payload"),Bytes.toBytes("respBuf"),Bytes.toBytes(respBuf)); - - if (hasNotificationEvent == true) { - String eventType = ne.getEventHeader().getEventType(); - String eventStatus = ne.getEventHeader().getStatus(); - - if (eventStatus == null) { - eventStatus = AAIConfig.get("aai.notificationEvent.default.status", "UNPROCESSED"); - } - if (eventType == null) { - eventType = AAIConfig.get("aai.notificationEvent.default.eventType", "AAI-EVENT"); - } - - if (ne.getEntity() != null) { - PojoUtils pu = new PojoUtils(); - p.add(Bytes.toBytes("notification"),Bytes.toBytes("notificationPayload"),Bytes.toBytes(pu.getJsonFromObject(ne))); - } - if (ne.getEventHeader().getId() != null) { - p.add(Bytes.toBytes("notification"),Bytes.toBytes("notificationId"),Bytes.toBytes(ne.getEventHeader().getId())); - } - - p.add(Bytes.toBytes("notification"),Bytes.toBytes("notificationStatus"),Bytes.toBytes(eventStatus)); - p.add(Bytes.toBytes("notification"),Bytes.toBytes("notificationTopic"),Bytes.toBytes(eventType)); - - if (ne.getEventHeader().getEntityLink() != null) { - p.add(Bytes.toBytes("notification"),Bytes.toBytes("notificationEntityLink"),Bytes.toBytes(ne.getEventHeader().getEntityLink())); - } - if (ne.getEventHeader().getAction() != null) { - p.add(Bytes.toBytes("notification"),Bytes.toBytes("notificationAction"),Bytes.toBytes(ne.getEventHeader().getAction()) ); - } - } - /* Once you've adorned your Put instance with all the updates you want to - make, to commit it do the following */ - table.put(p); - table.flushCommits(); - table.close(); - return htid; - } catch (Exception e) { - LOGGER.warn("AAITxnLog: put: Exception", e); - return htid; - } - } - - /** - * Gets the. - * - * @param htid the htid - * @return the transaction log entry - * @throws AAIException the AAI exception - */ - public TransactionLogEntry get(String htid) throws AAIException { - - LOGGER.debug("In get: searching hbase config file..."); - String tidStr = ""; - TransactionLogEntry txObj = new TransactionLogEntry(); - - try { - table = new HTable(config, AAIConfig.get(AAIConstants.HBASE_TABLE_NAME)); - - Get g = new Get(Bytes.toBytes(htid)); - - Result r = table.get(g); - byte [] tid = r.getValue(Bytes.toBytes("transaction"),Bytes.toBytes("tid")); - byte [] status = r.getValue(Bytes.toBytes("transaction"),Bytes.toBytes("status")); - byte [] rqstDate = r.getValue(Bytes.toBytes("transaction"),Bytes.toBytes("rqstDate")); - byte [] respDate = r.getValue(Bytes.toBytes("transaction"),Bytes.toBytes("respDate")); - byte [] sourceId = r.getValue(Bytes.toBytes("transaction"),Bytes.toBytes("sourceId")); - - byte [] resourceId = r.getValue(Bytes.toBytes("resource"),Bytes.toBytes("resourceId")); - byte [] resourceType = r.getValue(Bytes.toBytes("resource"),Bytes.toBytes("resourceType")); - - byte [] rqstBuf = r.getValue(Bytes.toBytes("payload"),Bytes.toBytes("rqstBuf")); - byte [] respBuf = r.getValue(Bytes.toBytes("payload"),Bytes.toBytes("respBuf")); - - byte [] notificationPayload = r.getValue(Bytes.toBytes("notification"),Bytes.toBytes("notificationPayload")); - byte [] notificationStatus = r.getValue(Bytes.toBytes("notification"),Bytes.toBytes("notificationStatus")); - byte [] notificationId = r.getValue(Bytes.toBytes("notification"),Bytes.toBytes("notificationId")); - byte [] notificationTopic = r.getValue(Bytes.toBytes("notification"),Bytes.toBytes("notificationTopic")); - byte [] notificationEntityLink = r.getValue(Bytes.toBytes("notification"),Bytes.toBytes("notificationEntityLink")); - byte [] notificationAction = r.getValue(Bytes.toBytes("notification"),Bytes.toBytes("notificationAction")); - - table.close(); - - tidStr = Bytes.toString(tid); - txObj.setTransactionLogEntryId(tidStr); - txObj.setStatus(Bytes.toString(status)); - txObj.setRqstDate(Bytes.toString(rqstDate)); - txObj.setRespDate(Bytes.toString(respDate)); - txObj.setSourceId(Bytes.toString(sourceId)); - txObj.setResourceId(Bytes.toString(resourceId)); - txObj.setResourceType(Bytes.toString(resourceType)); - txObj.setRqstBuf(Bytes.toString(rqstBuf)); - txObj.setrespBuf(Bytes.toString(respBuf)); - txObj.setNotificationPayload(Bytes.toString(notificationPayload)); - txObj.setNotificationStatus(Bytes.toString(notificationStatus)); - txObj.setNotificationId(Bytes.toString(notificationId)); - txObj.setNotificationTopic(Bytes.toString(notificationTopic)); - txObj.setNotificationEntityLink(Bytes.toString(notificationEntityLink)); - txObj.setNotificationAction(Bytes.toString(notificationAction)); - } catch (IOException e) { - LOGGER.error("IOException on hbase call", e); - throw new AAIException("AAI_4000"); - } - - return txObj; - } - - - /** - * Scan filtered. - * - * @param startMillis the start millis - * @param endMillis the end millis - * @param methodList the method list - * @param putFilter the put filter - * @param getFilter the get filter - * @param resourceFilter the resource filter - * @param fromAppIdFilter the from app id filter - * @return the transaction log entries - */ - public TransactionLogEntries scanFiltered(long startMillis, long endMillis, List<String> methodList, - String putFilter, String getFilter, String resourceFilter, String fromAppIdFilter) { - - LOGGER.debug("Starting scanFiltered()"); - - // we should have the config ready from the constructor - - TransactionLogEntries txs = new TransactionLogEntries(); - - if (config == null) { - LOGGER.debug("in scan: can't create HBase configuration"); - return txs; - } - - try { - table = new HTable(config, AAIConfig.get(AAIConstants.HBASE_TABLE_NAME)); - Scan s = new Scan(); - FilterList flMaster = new FilterList(FilterList.Operator.MUST_PASS_ALL); - FilterList methodflMaster = new FilterList(FilterList.Operator.MUST_PASS_ONE); - if (methodList != null) { - for (String method : methodList) { - Filter filt = new SingleColumnValueFilter(Bytes.toBytes("resource"), - Bytes.toBytes("resourceType"), CompareOp.EQUAL, Bytes.toBytes(method)); - methodflMaster.addFilter(filt); - } - flMaster.addFilter(methodflMaster); - } - - if (getFilter != null) { - Filter filt = new SingleColumnValueFilter(Bytes.toBytes("payload"), - Bytes.toBytes("respBuf"), CompareOp.EQUAL, new RegexStringComparator(getFilter)); - flMaster.addFilter(filt); - } - if (putFilter != null) { - Filter filt = new SingleColumnValueFilter(Bytes.toBytes("payload"), - Bytes.toBytes("rqstBuf"), CompareOp.EQUAL, new RegexStringComparator(putFilter)); - flMaster.addFilter(filt); - } - if (resourceFilter != null) { - Filter filt = new SingleColumnValueFilter(Bytes.toBytes("resource"), - Bytes.toBytes("resourceId"), CompareOp.EQUAL, new RegexStringComparator(resourceFilter)); - flMaster.addFilter(filt); - } - if (fromAppIdFilter != null) { - Filter filt = new SingleColumnValueFilter(Bytes.toBytes("transaction"), - Bytes.toBytes("sourceId"), CompareOp.EQUAL, new RegexStringComparator("^" + fromAppIdFilter)); - flMaster.addFilter(filt); - } - - if (flMaster.hasFilterRow()) { - s.setFilter(flMaster); - } - - s.setTimeRange(startMillis, endMillis); - ResultScanner scanner = table.getScanner(s); - - try { - for (Result rr = scanner.next(); rr != null; rr = scanner.next()) { - - byte [] tid = rr.getValue(Bytes.toBytes("transaction"),Bytes.toBytes("tid")); - byte [] status = rr.getValue(Bytes.toBytes("transaction"),Bytes.toBytes("status")); - byte [] rqstDate = rr.getValue(Bytes.toBytes("transaction"),Bytes.toBytes("rqstDate")); - byte [] respDate = rr.getValue(Bytes.toBytes("transaction"),Bytes.toBytes("respDate")); - byte [] sourceId = rr.getValue(Bytes.toBytes("transaction"),Bytes.toBytes("sourceId")); - - byte [] resourceId = rr.getValue(Bytes.toBytes("resource"),Bytes.toBytes("resourceId")); - byte [] resourceType = rr.getValue(Bytes.toBytes("resource"),Bytes.toBytes("resourceType")); - - byte [] rqstBuf = rr.getValue(Bytes.toBytes("payload"),Bytes.toBytes("rqstBuf")); - byte [] respBuf = rr.getValue(Bytes.toBytes("payload"),Bytes.toBytes("respBuf")); - - byte [] notificationPayload = rr.getValue(Bytes.toBytes("notification"),Bytes.toBytes("notificationPayload")); - byte [] notificationStatus = rr.getValue(Bytes.toBytes("notification"),Bytes.toBytes("notificationStatus")); - byte [] notificationId = rr.getValue(Bytes.toBytes("notification"),Bytes.toBytes("notificationId")); - byte [] notificationTopic = rr.getValue(Bytes.toBytes("notification"),Bytes.toBytes("notificationTopic")); - byte [] notificationEntityLink = rr.getValue(Bytes.toBytes("notification"),Bytes.toBytes("notificationEntityLink")); - byte [] notificationAction = rr.getValue(Bytes.toBytes("notification"),Bytes.toBytes("notificationAction")); - TransactionLogEntry txObj = new TransactionLogEntry(); - String tidStr = Bytes.toString(tid); - txObj.setTransactionLogEntryId(tidStr); - txObj.setStatus(Bytes.toString(status)); - txObj.setRqstDate(Bytes.toString(rqstDate)); - txObj.setRespDate(Bytes.toString(respDate)); - txObj.setSourceId(Bytes.toString(sourceId)); - txObj.setResourceId(Bytes.toString(resourceId)); - txObj.setResourceType(Bytes.toString(resourceType)); - txObj.setRqstBuf(Bytes.toString(rqstBuf)); - txObj.setrespBuf(Bytes.toString(respBuf)); - txObj.setNotificationPayload(Bytes.toString(notificationPayload)); - txObj.setNotificationStatus(Bytes.toString(notificationStatus)); - txObj.setNotificationId(Bytes.toString(notificationId)); - txObj.setNotificationTopic(Bytes.toString(notificationTopic)); - txObj.setNotificationEntityLink(Bytes.toString(notificationEntityLink)); - txObj.setNotificationAction(Bytes.toString(notificationAction)); - txs.getTransactionLogEntries().add(txObj); - } - } finally { - // Make sure you close your scanners when you are done! - scanner.close(); - } - table.close(); - } catch (Exception e) { - LOGGER.warn("AAITxnLog: scan: Exception=" + e.toString()); - } - - return txs; - } - - /** - * Scan. - * - * @param htid the htid - * @return the list - */ - public List<String> scan(String htid) { - - List<String> list = new ArrayList<String>(); - LOGGER.debug("In scan: searching hbase config file..."); - // we should have the config ready from the constructor - if (config == null) { - LOGGER.debug("in scan: can't create HBase configuration"); - return list; - } - - try { - table = new HTable(config, AAIConfig.get(AAIConstants.HBASE_TABLE_NAME)); - Scan s = new Scan(Bytes.toBytes(htid)); - ResultScanner scanner = table.getScanner(s); - - try { - for (Result rr = scanner.next(); rr != null; rr = scanner.next()) { - list.add(rr.toString()); - LOGGER.debug("in scan: Found row : " + rr); - - } - } finally { - // Make sure you close your scanners when you are done! - scanner.close(); - } - table.close(); - } catch (Exception e) { - - LOGGER.debug("AAITxnLog: scan: Exception=" + e.toString()); - } - return list; - } - -} - -/* -Need to implement HBase Connection Pooling in the future. -This is to reduce the 1 second delay during the first open of HConnection, and HTable instantiation. -Hbase provides the Hconnection class and the HConnectionManager class. -Both provifde the functionaltity similar to jdbc connection pooling -to share pre-existing opened connections. -Here we should be able to use the getTable() method to get a -reference to an HTable instance. - - */ diff --git a/aai-core/src/main/java/org/openecomp/aai/util/GenerateXsd.java b/aai-core/src/main/java/org/openecomp/aai/util/GenerateXsd.java index 7cc6bcf0..83fcf634 100644 --- a/aai-core/src/main/java/org/openecomp/aai/util/GenerateXsd.java +++ b/aai-core/src/main/java/org/openecomp/aai/util/GenerateXsd.java @@ -83,14 +83,14 @@ public class GenerateXsd { private static final String generateTypeXSD = "xsd"; private static final String generateTypeYAML = "yaml"; - + private static final String root = "../aai-schema/src/main/resources"; private static final String xsd_dir = root + "/aai_schema"; private static final String yaml_dir = root + "/aai_swagger_yaml"; - + private static int annotationsStartVersion = 9; // minimum version to support annotations in xsd private static int swaggerSupportStartsVersion = 7; // minimum version to support swagger documentation - + private static XPath xpath = XPathFactory.newInstance().newXPath(); @@ -229,11 +229,11 @@ public class GenerateXsd { } private static boolean validVersion(String versionToGen) { - + if ("ALL".equalsIgnoreCase(versionToGen)) { return true; } - + for (Version v : Version.values()) { if (v.name().equals(versionToGen)) { return true; @@ -242,49 +242,49 @@ public class GenerateXsd { return false; } - + private static boolean versionUsesAnnotations( String version) { if (new Integer(version.substring(1)).intValue() >= annotationsStartVersion ) { return true; } return false; } - + private static boolean versionSupportsSwagger( String version) { if (new Integer(version.substring(1)).intValue() >= swaggerSupportStartsVersion ) { return true; } return false; } - + public static void main(String[] args) throws IOException { String versionToGen = System.getProperty("gen_version").toLowerCase(); String fileTypeToGen = System.getProperty("gen_type").toLowerCase(); if ( fileTypeToGen == null ) { fileTypeToGen = generateTypeXSD; } - + if ( !fileTypeToGen.equals( generateTypeXSD ) && !fileTypeToGen.equals( generateTypeYAML )) { System.err.println("Invalid gen_type passed. " + fileTypeToGen); System.exit(1); } - - + + if ( versionToGen == null ) { System.err.println("Version is required, ie v<n> or ALL."); - System.exit(1); + System.exit(1); } - + responsesUrl = System.getProperty("yamlresponses_url"); String responsesLabel = System.getProperty("yamlresponses_label"); List<Version> versionsToGen = new ArrayList<>(); - + if (!"ALL".equalsIgnoreCase(versionToGen) && !versionToGen.matches("v\\d+") && !validVersion(versionToGen)) { System.err.println("Invalid version passed. " + versionToGen); System.exit(1); } - + if ("ALL".equalsIgnoreCase(versionToGen)) { versionsToGen = Arrays.asList(Version.values()); Collections.sort(versionsToGen); @@ -294,7 +294,7 @@ public class GenerateXsd { } if ( fileTypeToGen.equals(generateTypeYAML) ) { - if ( responsesUrl == null || responsesUrl.length() < 1 + if ( responsesUrl == null || responsesUrl.length() < 1 || responsesLabel == null || responsesLabel.length() < 1 ) { System.err.println("generating swagger yaml file requires yamlresponses_url and yamlresponses_label properties" ); System.exit(1); @@ -302,11 +302,11 @@ public class GenerateXsd { responsesUrl = "description: "+ responsesLabel+ "(" + responsesUrl + ").\n"; } String oxmPath = root + "/oxm/"; - + String outfileName; File outfile; String fileContent; - + for (Version v : versionsToGen) { apiVersion = v.toString(); System.out.println("Generating " + apiVersion + " " + fileTypeToGen); @@ -326,9 +326,9 @@ public class GenerateXsd { } outfile = new File(outfileName); File parentDir = outfile.getParentFile(); - if(! parentDir.exists()) + if(! parentDir.exists()) parentDir.mkdirs(); - + try { outfile.createNewFile(); } catch (IOException e) { @@ -347,7 +347,7 @@ public class GenerateXsd { } System.out.println( "GeneratedXSD successful, saved in " + outfileName); } - + } @@ -688,7 +688,7 @@ public class GenerateXsd { sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"); String namespace = "org.openecomp"; if ( useAnnotationsInXsd ) { - sb.append("<xs:schema elementFormDefault=\"qualified\" version=\"1.0\" targetNamespace=\"http://" + namespace + ".aai.inventory/" + sb.append("<xs:schema elementFormDefault=\"qualified\" version=\"1.0\" targetNamespace=\"http://" + namespace + ".aai.inventory/" + apiVersion + "\" xmlns:tns=\"http://" + namespace + ".aai.inventory/" + apiVersion + "\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"" + "\n" + "xmlns:jaxb=\"http://java.sun.com/xml/ns/jaxb\"\r\n" + @@ -696,7 +696,7 @@ public class GenerateXsd { " xmlns:annox=\"http://annox.dev.java.net\" \r\n" + " jaxb:extensionBindingPrefixes=\"annox\">\n\n"); } else { - sb.append("<xs:schema elementFormDefault=\"qualified\" version=\"1.0\" targetNamespace=\"http://" + namespace + ".aai.inventory/" + sb.append("<xs:schema elementFormDefault=\"qualified\" version=\"1.0\" targetNamespace=\"http://" + namespace + ".aai.inventory/" + apiVersion + "\" xmlns:tns=\"http://" + namespace + ".aai.inventory/" + apiVersion + "\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n\n"); } @@ -927,6 +927,7 @@ public class GenerateXsd { case "Network": case "ServiceDesignAndCreation": case "Business": + case "LicenseManagement": case "CloudInfrastructure": break; default: @@ -1639,6 +1640,8 @@ public class GenerateXsd { sb.append("swagger: \"2.0\"\ninfo:\n description: |\n Copyright © 2017 AT&T Intellectual Property. All rights reserved.\n\n Licensed under the Creative Commons License, Attribution 4.0 Intl. (the "License"); you may not use this documentation except in compliance with the License.\n\n You may obtain a copy of the License at\n\n (https://creativecommons.org/licenses/by/4.0/)\n\n 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.\n\n ECOMP and OpenECOMP are trademarks and service marks of AT&T Intellectual Property.\n\n This document is best viewed with Firefox or Chrome. Nodes can be found by appending /#/definitions/node-type-to-find to the path to this document. Edge definitions can be found with the node definitions.\n version: \"" + apiVersion +"\"\n"); sb.append(" title: Active and Available Inventory REST API\n"); sb.append(" license:\n name: Apache 2.0\n url: http://www.apache.org/licenses/LICENSE-2.0.html\n"); + sb.append(" contact:\n name:\n url:\n email:\n"); + sb.append("host:\nbasePath: /aai/" + apiVersion + "\n"); sb.append("schemes:\n - https\npaths:\n"); /* sb.append("responses:\n"); diff --git a/aai-core/src/main/java/org/openecomp/aai/util/swagger/Api.java b/aai-core/src/main/java/org/openecomp/aai/util/swagger/Api.java new file mode 100644 index 00000000..241be43a --- /dev/null +++ b/aai-core/src/main/java/org/openecomp/aai/util/swagger/Api.java @@ -0,0 +1,310 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * 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.aai.util.swagger; + +import java.util.List; +import java.util.Map; + +public class Api { + + private String path; + + private List<HttpVerb> httpMethods; + + private String tag; + + public List<HttpVerb> getHttpMethods() { + return httpMethods; + } + + public void setHttpMethods(List<HttpVerb> httpMethods) { + this.httpMethods = httpMethods; + } + + public String getTag(){ + + if(this.tag != null){ + return this.tag; + } + + if(this.httpMethods != null){ + if(this.httpMethods.size() != 0){ + if(this.httpMethods.get(0).getTags() != null){ + if(this.httpMethods.get(0).getTags().size() != 0){ + this.tag = this.httpMethods.get(0).getTags().get(0); + } + } + } + } + + if(this.tag == null){ + this.tag = ""; + } + + return this.tag; + } + + @Override + public String toString() { + return "Api{" + + "path='" + path + '\'' + + ", httpMethods=" + httpMethods + + '}'; + } + + public void setPath(String path) { + this.path = path; + } + + public String getPath() { + return this.path; + } + + public String getOperation(){ + + if(this.path != null){ + return this.path.replaceAll("[^a-zA-Z0-9\\-]", "-") + "-"; + } + + return ""; + } + + public static class HttpVerb { + + private List<String> tags; + + private String type; + + private String summary; + + private String operationId; + + private List<String> consumes; + + private boolean consumerEnabled; + + private List<String> produces; + + private List<Response> responses; + + private List<Map<String, Object>> parameters; + + private Map<String, Object> bodyParameters; + + private boolean bodyParametersEnabled; + + private boolean parametersEnabled; + + private String schemaLink; + + private String schemaType; + + private boolean hasReturnSchema; + + private String returnSchemaLink; + + private String returnSchemaObject; + + public void setConsumerEnabled(boolean consumerEnabled){ + this.consumerEnabled = consumerEnabled; + } + + public boolean isConsumerEnabled() { + return consumerEnabled; + } + + + public List<String> getTags() { + return tags; + } + + public void setTags(List<String> tags) { + this.tags = tags; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getSummary() { + return summary; + } + + public void setSummary(String summary) { + this.summary = summary; + } + + public String getOperationId() { + return operationId; + } + + public void setOperationId(String operationId) { + this.operationId = operationId; + } + + public List<String> getConsumes() { + return consumes; + } + + public void setConsumes(List<String> consumes) { + this.consumes = consumes; + } + + public List<String> getProduces() { + return produces; + } + + public void setProduces(List<String> produces) { + this.produces = produces; + } + + public List<Response> getResponses() { + return responses; + } + + public void setResponses(List<Response> responses) { + this.responses = responses; + } + + public List<Map<String, Object>> getParameters() { + return parameters; + } + + public void setParameters(List<Map<String, Object>> parameters) { + this.parameters = parameters; + } + + @Override + public String toString() { + return "HttpVerb{" + + "tags=" + tags + + ", type='" + type + '\'' + + ", summary='" + summary + '\'' + + ", operationId='" + operationId + '\'' + + ", consumes=" + consumes + + ", produces=" + produces + + ", responses=" + responses + + ", parameters=" + parameters + + '}'; + } + + public void setParametersEnabled(boolean b) { + this.parametersEnabled = b; + } + + public boolean isParametersEnabled() { + return parametersEnabled; + } + + public boolean isBodyParametersEnabled() { + return bodyParametersEnabled; + } + + public void setBodyParametersEnabled(boolean bodyParametersEnabled) { + this.bodyParametersEnabled = bodyParametersEnabled; + } + + public Map<String, Object> getBodyParameters() { + return bodyParameters; + } + + public void setBodyParameters(Map<String, Object> bodyParameters) { + this.bodyParameters = bodyParameters; + } + + public String getSchemaLink() { + return schemaLink; + } + + public void setSchemaLink(String schemaLink) { + this.schemaLink = schemaLink; + } + + public String getSchemaType() { + return schemaType; + } + + public void setSchemaType(String schemaType) { + this.schemaType = schemaType; + } + + public boolean isHasReturnSchema() { + return hasReturnSchema; + } + + public void setHasReturnSchema(boolean hasReturnSchema) { + this.hasReturnSchema = hasReturnSchema; + } + + public String getReturnSchemaLink() { + return returnSchemaLink; + } + + public void setReturnSchemaLink(String returnSchemaLink) { + this.returnSchemaLink = returnSchemaLink; + } + + public String getReturnSchemaObject() { + return returnSchemaObject; + } + + public void setReturnSchemaObject(String returnSchemaObject) { + this.returnSchemaObject = returnSchemaObject; + } + + public static class Response { + + private String responseCode; + + private String description; + + public String getResponseCode() { + return responseCode; + } + + public void setResponseCode(String responseCode) { + this.responseCode = responseCode; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + @Override + public String toString() { + return "Response{" + + "responseCode='" + responseCode + '\'' + + ", description='" + description + '\'' + + '}'; + } + } + + } + +} diff --git a/aai-core/src/main/java/org/openecomp/aai/util/swagger/Definition.java b/aai-core/src/main/java/org/openecomp/aai/util/swagger/Definition.java new file mode 100644 index 00000000..31c38c56 --- /dev/null +++ b/aai-core/src/main/java/org/openecomp/aai/util/swagger/Definition.java @@ -0,0 +1,199 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * 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.aai.util.swagger; + +import java.util.List; + +public class Definition { + + private String definitionName; + + private String definitionDescription; + + private List<Property> propertyList; + + private List<Property> schemaPropertyList; + + private List<Property> regularPropertyList; + + private boolean hasDescription; + + public String getDefinitionName() { + return definitionName; + } + + public void setDefinitionName(String definitionName) { + this.definitionName = definitionName; + } + + public List<Property> getPropertyList() { + return propertyList; + } + + public void setPropertyList(List<Property> propertyList) { + this.propertyList = propertyList; + } + + public String getDefinitionDescription() { + return definitionDescription; + } + + public void setDefinitionDescription(String definitionDescription) { + this.definitionDescription = definitionDescription; + } + + @Override + public String toString() { + return "Definition{" + + "definitionName='" + definitionName + '\'' + + ", definitionDescription='" + definitionDescription + '\'' + + ", propertyList=" + propertyList + + '}'; + } + + public boolean isHasDescription() { + return hasDescription; + } + + public void setHasDescription(boolean hasDescription) { + this.hasDescription = hasDescription; + } + + public List<Property> getSchemaPropertyList() { + return schemaPropertyList; + } + + public void setSchemaPropertyList(List<Property> schemaPropertyList) { + this.schemaPropertyList = schemaPropertyList; + } + + public List<Property> getRegularPropertyList() { + return regularPropertyList; + } + + public void setRegularPropertyList(List<Property> regularPropertyList) { + this.regularPropertyList = regularPropertyList; + } + + public static class Property { + + private String propertyName; + + private String propertyDescription; + + private boolean hasPropertyDescription; + + private String propertyType; + + private boolean hasType; + + private String propertyReference; + + private String propertyReferenceObjectName; + + private boolean isRequired; + + private boolean hasPropertyReference; + + public Property(){} + + public String getPropertyName() { + return propertyName; + } + + public void setPropertyName(String propertyName) { + this.propertyName = propertyName; + } + + public String getPropertyType() { + return propertyType; + } + + public void setPropertyType(String propertyType) { + this.propertyType = propertyType; + } + + public String getPropertyReference() { + return propertyReference; + } + + public void setPropertyReference(String propertyReference) { + this.propertyReference = propertyReference; + } + + @Override + public String toString() { + return "Property{" + + "propertyName='" + propertyName + '\'' + + ", propertyType='" + propertyType + '\'' + + ", propertyReference='" + propertyReference + '\'' + + '}'; + } + + public boolean isHasType() { + return hasType; + } + + public void setHasType(boolean hasType) { + this.hasType = hasType; + } + + public boolean isRequired() { + return isRequired; + } + + public void setRequired(boolean required) { + isRequired = required; + } + + public boolean isHasPropertyReference() { + return hasPropertyReference; + } + + public void setHasPropertyReference(boolean hasPropertyReference) { + this.hasPropertyReference = hasPropertyReference; + } + + public String getPropertyReferenceObjectName() { + return propertyReferenceObjectName; + } + + public void setPropertyReferenceObjectName(String propertyReferenceObjectName) { + this.propertyReferenceObjectName = propertyReferenceObjectName; + } + + public String getPropertyDescription() { + return propertyDescription; + } + + public void setPropertyDescription(String propertyDescription) { + this.propertyDescription = propertyDescription; + } + + public boolean isHasPropertyDescription() { + return hasPropertyDescription; + } + + public void setHasPropertyDescription(boolean hasPropertyDescription) { + this.hasPropertyDescription = hasPropertyDescription; + } + } +} diff --git a/aai-core/src/main/java/org/openecomp/aai/util/swagger/GenerateSwagger.java b/aai-core/src/main/java/org/openecomp/aai/util/swagger/GenerateSwagger.java new file mode 100644 index 00000000..66e91318 --- /dev/null +++ b/aai-core/src/main/java/org/openecomp/aai/util/swagger/GenerateSwagger.java @@ -0,0 +1,428 @@ +/*- + * ============LICENSE_START======================================================= + * org.openecomp.aai + * ================================================================================ + * 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.aai.util.swagger; + +import com.fasterxml.jackson.dataformat.yaml.snakeyaml.Yaml; +import com.fasterxml.jackson.dataformat.yaml.snakeyaml.constructor.SafeConstructor; +import freemarker.template.Configuration; +import freemarker.template.Template; +import freemarker.template.TemplateException; + +import java.io.*; +import java.util.*; +import java.util.stream.Collectors; + +public class GenerateSwagger { + + public static final String LINE_SEPARATOR = System.getProperty("line.separator"); + public static final String DEFAULT_WIKI = ""; + + public static final String DEFAULT_SCHEMA_DIR = "../aai-schema"; + public static final String CURRENT_VERSION = "v10"; + + public static void main(String[] args) throws IOException, TemplateException { + + String schemaDir = System.getProperty("aai.schema.dir"); + String versionToGenerate = System.getProperty("aai.generate.version"); + String wikiLink = System.getProperty("aai.wiki.link"); + + if(schemaDir == null){ + System.out.println("Warning: Schema directory is not set so using default schema dir: " + DEFAULT_SCHEMA_DIR); + schemaDir = DEFAULT_SCHEMA_DIR; + } + + if(versionToGenerate == null){ + System.out.println("Warning: Version to generate is not set so using default version: " + CURRENT_VERSION); + versionToGenerate = CURRENT_VERSION; + } + + if(wikiLink == null){ + System.out.println("Warning: aai.wiki.link property is not set so using default"); + wikiLink = DEFAULT_WIKI; + } + + String yamlFile = schemaDir + "/src/main/resources/aai_swagger_yaml/aai_swagger_" + versionToGenerate + ".yaml"; + File swaggerYamlFile = new File(yamlFile); + + if(!swaggerYamlFile.exists()){ + System.err.println("Unable to find the swagger yaml file: " + swaggerYamlFile); + System.exit(1); + } + + Yaml yaml = new Yaml(new SafeConstructor()); + Map<String, Object> swaggerMap = null; + + try (BufferedReader reader = new BufferedReader(new FileReader(swaggerYamlFile))){ + swaggerMap = (Map<String, Object>) yaml.load(reader); + } catch(IOException ex){ + ex.printStackTrace(); + } + + Map<String, Object> map = (Map<String, Object>) swaggerMap.get("paths"); + Map<String, Object> schemaDefinitionmap = (Map<String, Object>) swaggerMap.get("definitions"); + Map<String, Object> infoMap = (Map<String, Object>) swaggerMap.get("info"); + Map<String, List<Api>> tagMap = new LinkedHashMap<>(); + + List<Api> apis = convertToApi(map); + apis.forEach((api) -> { + if(!tagMap.containsKey(api.getTag())){ + List<Api> newApis = new ArrayList<>(); + newApis.add(api); + tagMap.put(api.getTag(), newApis); + } else { + tagMap.get(api.getTag()).add(api); + } + }); + + Map<String, List<Api>> sortedTagMap = new TreeMap<>(tagMap); + sortedTagMap.forEach((key, value) -> { + value.sort(Comparator.comparing(Api::getPath)); + }); + + Map<String, Object> resultMap = new HashMap<>(); + + List<Definition> definitionList = convertToDefinition(schemaDefinitionmap); + + definitionList = definitionList + .stream().sorted(Comparator.comparing(Definition::getDefinitionName)).collect(Collectors.toList()); + + resultMap.put("aaiApis", tagMap); + resultMap.put("sortedAaiApis", sortedTagMap); + resultMap.put("wikiLink", wikiLink); + resultMap.put("definitions", definitionList); + if (infoMap.containsKey("description")) { + String infoDescription = infoMap.get("description").toString(); + + infoDescription = Arrays.stream(infoDescription.split("\n")) + .map(line -> { + line = line.trim(); + String hyperLink = ""; + if(line.trim().contains("https://")){ + int startIndex = line.indexOf("https://"); + int endIndex = line.lastIndexOf("/"); + hyperLink = line.substring(startIndex, endIndex); + return String.format("<a href=\"%s\">%s</a><br/>", hyperLink, line); + } + return String.format("%s<br/>", line); + }) + + .collect(Collectors.joining(LINE_SEPARATOR)); + + resultMap.put("description", infoDescription); + } + + Configuration configuration = new Configuration(); + configuration.setClassForTemplateLoading(Api.class, "/"); + configuration.setDirectoryForTemplateLoading(new File("src/main/resources/")); + + Template template = configuration.getTemplate("swagger.html.ftl"); + + String outputDirStr = schemaDir + "/src/main/resources/aai_swagger_html"; + + File outputDir = new File(outputDirStr); + + if(!outputDir.exists()){ + boolean resp = outputDir.mkdir(); + if(!resp){ + System.err.println("Unable to create the directory: " + outputDirStr); + System.exit(1); + } + } else if(outputDir.isFile()){ + System.err.println("Unable to create the directory: " + outputDirStr + " since a filename with that string exists"); + System.exit(1); + } + + Writer file = new FileWriter(new File(outputDirStr + "/aai_swagger_" + versionToGenerate + ".html")); + template.process(resultMap, file); + } + + public static List<Api> convertToApi(Map<String, Object> pathMap){ + + if(pathMap == null) + throw new IllegalArgumentException(); + + List<Api> apis = new ArrayList<>(); + + pathMap.forEach( (pathKey, pathValue) -> { + + Api api = new Api(); + Map<String, Object> httpVerbMap = (Map<String, Object>) pathValue; + List<Api.HttpVerb> httpVerbs = new ArrayList<>(); + + api.setPath(pathKey); + + httpVerbMap.forEach((httpVerbKey, httpVerbValue) -> { + + Api.HttpVerb httpVerb = new Api.HttpVerb(); + + Map<String, Object> httpVerbValueMap = (Map<String,Object>)httpVerbValue; + + httpVerb.setType(httpVerbKey); + + if(httpVerbValueMap.containsKey("tags")){ + httpVerb.setTags((List<String>)httpVerbValueMap.get("tags")); + } + + if(httpVerbValueMap.containsKey("summary")){ + httpVerb.setSummary((String)httpVerbValueMap.get("summary")); + } + + if(httpVerbValueMap.containsKey("operationId")){ + httpVerb.setOperationId((String)httpVerbValueMap.get("operationId")); + } + + if(httpVerbValueMap.containsKey("consumes")){ + httpVerb.setConsumes((List<String>)httpVerbValueMap.get("consumes")); + if(httpVerb.getConsumes() != null){ + httpVerb.setConsumerEnabled(true); + } + } + + if(httpVerbValueMap.containsKey("produces")){ + httpVerb.setProduces((List<String>)httpVerbValueMap.get("produces")); + } + + if(httpVerbValueMap.containsKey("parameters")){ + List<Map<String, Object>> parameters = (List<Map<String, Object>>) httpVerbValueMap.get("parameters"); + List<Map<String, Object>> requestParameters = parameters + .stream() + .filter((parameter) -> !parameter.get("name").equals("body")) + .collect(Collectors.toList()); + httpVerb.setParameters(requestParameters); + if(httpVerb.getParameters() != null){ + httpVerb.setParametersEnabled(true); + } + + List<Map<String, Object>> requestBodyList = parameters + .stream() + .filter((parameter) -> parameter.get("name").equals("body")) + .collect(Collectors.toList()); + + Map<String, Object> requestBody = null; + + if(requestBodyList != null && requestBodyList.size() == 1){ + requestBody = requestBodyList.get(0); + httpVerb.setBodyParametersEnabled(true); + httpVerb.setBodyParameters(requestBody); + + if(requestBody != null && requestBody.containsKey("schema")){ + Map<String, Object> schemaMap = (Map<String, Object>)requestBody.get("schema"); + if(schemaMap != null && schemaMap.containsKey("$ref")){ + String schemaLink = schemaMap.get("$ref").toString(); + httpVerb.setSchemaLink(schemaLink); + int retCode = schemaLink.lastIndexOf('/'); + if(retCode != -1 && retCode != schemaLink.length()){ + httpVerb.setSchemaType(schemaLink.substring(retCode)); + } + } + } + } + } + + if(httpVerbValueMap.containsKey("responses")){ + + List<Api.HttpVerb.Response> responses = new ArrayList<Api.HttpVerb.Response>(); + + Map<String, Object> responsesMap = (Map<String, Object>) httpVerbValueMap.get("responses"); + + responsesMap + .entrySet() + .stream() + .filter((res) -> !"default".equalsIgnoreCase(res.getKey())) + .forEach((responseMap) -> { + + Map<String, Object> responseValueMap = (Map<String, Object>)responseMap.getValue(); + + Api.HttpVerb.Response response = new Api.HttpVerb.Response(); + + response.setResponseCode(responseMap.getKey()); + response.setDescription((String) responseValueMap.get("description")); + + if(responseValueMap != null && responseValueMap.containsKey("schema")){ + Map<String, Object> schemaMap = (Map<String, Object>)responseValueMap.get("schema"); + if(schemaMap != null && schemaMap.containsKey("$ref")){ + String schemaLink = schemaMap.get("$ref").toString(); + httpVerb.setHasReturnSchema(true); + httpVerb.setReturnSchemaLink(schemaLink); + int retCode = schemaLink.lastIndexOf('/'); + if(retCode != -1 && retCode != schemaLink.length()){ + httpVerb.setReturnSchemaObject(schemaLink.substring(retCode)); + } + } + } + + responses.add(response); + } + ); + + httpVerb.setResponses(responses); + } + + httpVerbs.add(httpVerb); + }); + + api.setHttpMethods(httpVerbs); + apis.add(api); + }); + + return apis; + } + + public static List<Definition> convertToDefinition(Map<String, Object> definitionMap) { + + if(definitionMap == null) + throw new IllegalArgumentException(); + + List<Definition> defintionsList = new ArrayList<>(); + + definitionMap + .entrySet() + .forEach((entry) -> { + + Definition definition = new Definition(); + String key = entry.getKey(); + Map<String, Object> valueMap = (Map<String, Object>) entry.getValue(); + + definition.setDefinitionName(key); + + if(valueMap.containsKey("description")){ + String description = valueMap.get("description").toString(); + description = formatDescription(description); + definition.setDefinitionDescription(description); + definition.setHasDescription(true); + } + + List<Definition.Property> definitionProperties = new ArrayList<>(); + + List<String> requiredProperties = (valueMap.get("required") == null) ? new ArrayList<>() : (List<String>) valueMap.get("required"); + + Set<String> requiredPropsSet = requiredProperties.stream().collect(Collectors.toSet()); + + valueMap + .entrySet() + .stream() + .filter( (e) -> "properties".equals(e.getKey())) + .forEach((propertyEntries) -> { + Map<String, Object> propertyRealEntries = (Map<String, Object>) propertyEntries.getValue(); + propertyRealEntries + .entrySet() + .forEach((propertyEntry) -> { + Definition.Property definitionProperty = new Definition.Property(); + String propertyKey = propertyEntry.getKey(); + if(requiredPropsSet.contains(propertyKey)){ + definitionProperty.setRequired(true); + } + definitionProperty.setPropertyName(propertyKey); + Map<String, Object> definitionPropertyMap = (Map<String, Object>) propertyEntry.getValue(); + + if(definitionPropertyMap.containsKey("description")){ + definitionProperty.setPropertyDescription(definitionPropertyMap.get("description").toString()); + definitionProperty.setHasPropertyDescription(true); + } + if(definitionPropertyMap.containsKey("type")){ + String type = definitionPropertyMap.get("type").toString(); + definitionProperty.setPropertyType(type); + definitionProperty.setHasType(true); + if ("array".equals(type)) { + definitionProperty.setPropertyType("object[]"); + if(!definitionPropertyMap.containsKey("items")){ + throw new RuntimeException("Unable to find the property items even though the type is array for " + propertyEntry.getKey()); + } else { + Map<String, Object> itemMap = (Map<String, Object>) definitionPropertyMap.get("items"); + if(itemMap.containsKey("$ref")){ + definitionProperty.setHasPropertyReference(true); + String refItem = itemMap.get("$ref").toString(); + int retCode = refItem.lastIndexOf('/'); + if(retCode != -1 && retCode != refItem.length()){ + definitionProperty.setPropertyReferenceObjectName(refItem.substring(retCode + 1)); + } + definitionProperty.setPropertyReference(refItem); + } + } + } else { + if(definitionPropertyMap.containsKey("$ref")){ + definitionProperty.setHasPropertyReference(true); + String refItem = definitionPropertyMap.get("$ref").toString(); + int retCode = refItem.lastIndexOf('/'); + if(retCode != -1 && retCode != refItem.length()){ + definitionProperty.setPropertyReferenceObjectName(refItem.substring(retCode + 1)); + } + definitionProperty.setPropertyReference(refItem); + } + } + } + definitionProperties.add(definitionProperty); + }); + }); + + definition.setPropertyList(definitionProperties); + + List<Definition.Property> schemaProperties = definitionProperties. + stream() + .filter((o) -> o.isHasPropertyReference()) + .collect(Collectors.toList()); + + List<Definition.Property> regularProperties = definitionProperties. + stream() + .filter((o) -> !o.isHasPropertyReference()) + .collect(Collectors.toList()); + + definition.setRegularPropertyList(regularProperties); + definition.setSchemaPropertyList(schemaProperties); + + defintionsList.add(definition); + }); + return defintionsList; + } + + public static String formatDescription(String description){ + + description = Arrays.stream(description.split("\n")) + .map((line) -> { + line = line.trim(); + if(line.contains("######")){ + line = line.replaceAll("#", ""); + line = line.trim(); + String headerId = line.toLowerCase().replaceAll("\\s", "-"); + + if(line.contains("Related Nodes")){ + return String.format("<h6 id=\"%s\">%s</h6>%s<ul>", headerId, line, LINE_SEPARATOR); + } else { + return String.format("<h6 id=\"%s\">%s</h6>", headerId, line); + } + } else if(line.startsWith("-")){ + line = line.replaceFirst("-", ""); + line = line.trim(); + return String.format("<li>%s</li>", line); + } else { + return String.format("<p>%s</p>", line); + } + }) + .collect(Collectors.joining(LINE_SEPARATOR)); + + if(description.contains("<ul>")){ + description = description + "</ul>"; + } + + return description; + } + +} |