2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.aai.serialization.db;
22 import org.janusgraph.core.JanusGraphFactory;
23 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
24 import org.apache.tinkerpop.gremlin.structure.*;
25 import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
27 import org.junit.rules.ExpectedException;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.Parameterized;
30 import org.onap.aai.AAISetup;
31 import org.onap.aai.db.props.AAIProperties;
32 import org.onap.aai.dbmap.DBConnectionType;
33 import org.onap.aai.edges.EdgeIngestor;
34 import org.onap.aai.edges.enums.EdgeType;
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.introspection.*;
37 import org.onap.aai.parsers.query.QueryParser;
38 import org.onap.aai.serialization.engines.QueryStyle;
39 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
40 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
41 import org.onap.aai.setup.SchemaVersion;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.test.annotation.DirtiesContext;
45 import java.io.UnsupportedEncodingException;
47 import java.net.URISyntaxException;
50 import static org.junit.Assert.*;
51 import static org.mockito.Mockito.spy;
52 import static org.mockito.Mockito.when;
54 @RunWith(value = Parameterized.class)
55 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
56 public class DbSerializerTest extends AAISetup {
58 //to use, set thrown.expect to whatever your test needs
59 //this line establishes default of expecting no exception to be thrown
61 public ExpectedException thrown = ExpectedException.none();
63 protected static Graph graph;
66 protected EdgeSerializer edgeSer;
68 protected EdgeIngestor ei;
70 private SchemaVersion version;
71 private final ModelType introspectorFactoryType = ModelType.MOXY;
72 private final DBConnectionType type = DBConnectionType.REALTIME;
73 private Loader loader;
74 private TransactionalGraphEngine dbEngine;
75 private TransactionalGraphEngine engine; //for tests that aren't mocking the engine
76 private DBSerializer dbser;
77 TransactionalGraphEngine spy;
78 TransactionalGraphEngine.Admin adminSpy;
80 @Parameterized.Parameter(value = 0)
81 public QueryStyle queryStyle;
83 @Parameterized.Parameters(name = "QueryStyle.{0}")
84 public static Collection<Object[]> data() {
85 return Arrays.asList(new Object[][]{
86 {QueryStyle.TRAVERSAL},
87 {QueryStyle.TRAVERSAL_URI}
92 public static void init() throws Exception {
93 graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
98 public void setup() throws Exception {
100 version = schemaVersions.getDefaultVersion();
101 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
102 dbEngine = new JanusGraphDBEngine(queryStyle, type, loader);
104 adminSpy = spy(dbEngine.asAdmin());
107 engine = new JanusGraphDBEngine(queryStyle, type, loader);
108 dbser = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST");
112 public void testFindDeletableDoesNotReturnDuplicates() throws AAIException {
114 Vertex genericVnf1 = graph.addVertex("aai-node-type", "generic-vnf", "vnf-id", "vnf1", "vnf-name", "vnfName1");
116 Vertex lInterface1 = graph.addVertex("aai-node-type", "l-interface", "interface-name", "lInterface1");
117 Vertex lInterface2 = graph.addVertex("aai-node-type", "l-interface", "interface-name", "lInterface2");
119 Vertex logicalLink1 = graph.addVertex("aai-node-type", "logical-link", "link-name", "logicalLink1");
120 Vertex logicalLink2 = graph.addVertex("aai-node-type", "logical-link", "link-name", "logicalLink2");
122 GraphTraversalSource g = graph.traversal();
124 edgeSer.addTreeEdge(g, genericVnf1, lInterface1);
125 edgeSer.addTreeEdge(g, genericVnf1, lInterface2);
126 edgeSer.addEdge(g, lInterface1, logicalLink1);
127 edgeSer.addEdge(g, lInterface1, logicalLink2);
128 // This line will cause the logical link2 to be found twice under linterface 1
129 // and also under the linterface 2 and since in the past deletable returned
130 // duplicates this test checks that it shouldn't return duplicates
131 edgeSer.addEdge(g, lInterface2, logicalLink2);
133 when(spy.asAdmin()).thenReturn(adminSpy);
134 when(adminSpy.getTraversalSource()).thenReturn(g);
135 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(g);
137 List<Vertex> deletableVertexes = spy.getQueryEngine().findDeletable(genericVnf1);
138 Set<Vertex> vertexSet = new HashSet<>();
140 for (Vertex deletableVertex : deletableVertexes) {
141 if(!vertexSet.contains(deletableVertex)){
142 vertexSet.add(deletableVertex);
144 fail("Find deletable is returning a list of duplicate vertexes");
150 public void tearDown() throws Exception {
155 public static void destroy() throws Exception {
160 public void subnetSetup() throws AAIException {
162 * This setus up the test graph, For future junits , add more vertices
166 Vertex l3interipv4addresslist_1 = graph.traversal().addV("aai-node-type", "l3-interface-ipv4-address-list",
167 "l3-interface-ipv4-address", "l3-interface-ipv4-address-1").next();
168 Vertex subnet_2 = graph.traversal().addV("aai-node-type", "subnet", "subnet-id", "subnet-id-2").next();
169 Vertex l3interipv6addresslist_3 = graph.traversal().addV("aai-node-type", "l3-interface-ipv6-address-list",
170 "l3-interface-ipv6-address", "l3-interface-ipv6-address-3").next();
171 Vertex subnet_4 = graph.traversal().addV("aai-node-type", "subnet", "subnet-id", "subnet-id-4").next();
172 Vertex subnet_5 = graph.traversal().addV("aai-node-type", "subnet", "subnet-id", "subnet-id-5").next();
173 Vertex l3network_6 = graph.traversal()
174 .addV("aai-node-type", "l3-network", "network-id", "network-id-6", "network-name", "network-name-6")
177 GraphTraversalSource g = graph.traversal();
178 edgeSer.addEdge(g, l3interipv4addresslist_1, subnet_2);
179 edgeSer.addEdge(g, l3interipv6addresslist_3, subnet_4);
180 edgeSer.addTreeEdge(g, subnet_5, l3network_6);
184 public void l3NetworkSetup() throws AAIException {
186 * This setus up the test graph, For future junits , add more vertices
190 Vertex l3network1 = graph.addVertex("aai-node-type", "l3-network", "network-id", "network-id-v1", "network-name", "network-name-v1");
191 Vertex l3network2 = graph.addVertex("aai-node-type", "l3-network", "network-id", "network-id-v2", "network-name", "network-name-v2");
192 Vertex subnet1 = graph.addVertex("aai-node-type", "subnet", "subnet-id", "subnet-id-v1");
193 Vertex subnet2 = graph.addVertex("aai-node-type", "subnet", "subnet-id", "subnet-id-v2");
195 Vertex l3interipv4addresslist_1 = graph.addVertex("aai-node-type", "l3-interface-ipv4-address-list",
196 "l3-interface-ipv4-address", "l3-intr-v1");
197 Vertex l3interipv6addresslist_1 = graph.addVertex("aai-node-type", "l3-interface-ipv6-address-list",
198 "l3-interface-ipv6-address", "l3-interface-ipv6-v1");
204 GraphTraversalSource g = graph.traversal();
205 edgeSer.addTreeEdge(g, subnet1, l3network1);
206 edgeSer.addEdge(g, l3interipv4addresslist_1, subnet1);
207 edgeSer.addEdge(g, l3interipv6addresslist_1, subnet1);
209 edgeSer.addTreeEdge(g, subnet2, l3network2);
215 public void vserverSetup() throws AAIException {
217 * This setus up the test graph, For future junits , add more vertices
221 Vertex vserver1 = graph.addVertex("aai-node-type", "vserver", "vserver-id", "vss1",
222 AAIProperties.AAI_URI.toString(),
223 "/cloud-infrastructure/cloud-regions/cloud-region/me/123/tenants/tenant/453/vservers/vserver/vss1");
225 Vertex lInterface1 = graph.addVertex("aai-node-type", "l-interface", "interface-name", "lIntr1");
226 Vertex lInterface2 = graph.addVertex("aai-node-type", "l-interface", "interface-name", "lIntr2");
228 Vertex logicalLink1 = graph.addVertex("aai-node-type", "logical-link", "link-name", "logLink1");
229 Vertex logicalLink2 = graph.addVertex("aai-node-type", "logical-link", "link-name", "logLink2");
231 Vertex l3interipv4addresslist_1 = graph.addVertex("aai-node-type", "l3-interface-ipv4-address-list",
232 "l3-interface-ipv4-address", "l3-intr-ipv4-address-1");
233 Vertex l3interipv6addresslist_2 = graph.addVertex("aai-node-type", "l3-interface-ipv6-address-list",
234 "l3-interface-ipv4-address", "l3-intr-ipv6-address-1");
236 GraphTraversalSource g = graph.traversal();
238 edgeSer.addTreeEdge(g, lInterface1, vserver1);
239 edgeSer.addTreeEdge(g, lInterface2, vserver1);
240 edgeSer.addTreeEdge(g, l3interipv4addresslist_1, lInterface1);
241 edgeSer.addTreeEdge(g, l3interipv6addresslist_2, lInterface2);
243 edgeSer.addEdge(g, lInterface1, logicalLink1);
244 edgeSer.addEdge(g, lInterface2, logicalLink2);
248 public void subnetDelWithInEdgesIpv4Test() throws AAIException {
250 String expected_message = "Object is being reference by additional objects preventing it from being deleted. Please clean up references from the following types [l3-interface-ipv4-address-list]";
253 * This subnet has in-edges with l3-ipv4 and NOT ok to delete
255 Vertex subnet = graph.traversal().V().has("aai-node-type", "subnet").has("subnet-id", "subnet-id-2").next();
257 String exceptionMessage = testCascadeDelete(subnet);
258 assertEquals(expected_message, exceptionMessage);
263 public void subnetDelWithInEdgesIpv6Test() throws AAIException {
265 String expected_message = "Object is being reference by additional objects preventing it from being deleted. Please clean up references from the following types [l3-interface-ipv6-address-list]";
268 * This subnet has in-edges with l3-ipv6 and NOT ok to delete
270 Vertex subnet = graph.traversal().V().has("aai-node-type", "subnet").has("subnet-id", "subnet-id-4").next();
271 String exceptionMessage = testCascadeDelete(subnet);
272 assertEquals(expected_message, exceptionMessage);
277 public void subnetDelWithInEdgesL3network() throws AAIException {
279 String expected_message = "";
282 * This subnet has in-edges with l3-network and ok to delete
284 Vertex subnet = graph.traversal().V().has("aai-node-type", "subnet").has("subnet-id", "subnet-id-5").next();
286 String exceptionMessage = testCascadeDelete(subnet);
287 assertEquals(expected_message, exceptionMessage);
291 public String testCascadeDelete(Vertex v) throws AAIException {
293 GraphTraversalSource traversal = graph.traversal();
294 when(spy.asAdmin()).thenReturn(adminSpy);
295 when(adminSpy.getTraversalSource()).thenReturn(traversal);
296 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(traversal);
298 String exceptionMessage = "";
299 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
300 List<Vertex> deletableVertices = spy.getQueryEngine().findDeletable(v);
303 serializer.delete(v, deletableVertices, "resourceVersion", false);
304 } catch (AAIException exception) {
305 exceptionMessage = exception.getMessage();
307 return exceptionMessage;
311 public String testDelete(Vertex v) throws AAIException {
313 GraphTraversalSource traversal = graph.traversal();
314 when(spy.asAdmin()).thenReturn(adminSpy);
315 when(adminSpy.getTraversalSource()).thenReturn(traversal);
316 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(traversal);
318 String exceptionMessage = "";
319 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
322 serializer.delete(v, "resourceVersion", false);
323 } catch (AAIException exception) {
324 exceptionMessage = exception.getMessage();
326 return exceptionMessage;
331 public void createNewVertexTest() throws AAIException {
332 engine.startTransaction();
334 Introspector testObj = loader.introspectorFromName("generic-vnf");
336 Vertex testVertex = dbser.createNewVertex(testObj);
337 Vertex fromGraph = engine.tx().traversal().V().has("aai-node-type","generic-vnf").toList().get(0);
338 assertEquals(testVertex.id(), fromGraph.id());
339 assertEquals("AAI-TEST", fromGraph.property(AAIProperties.SOURCE_OF_TRUTH.toString()).value());
344 public void touchStandardVertexPropertiesTest() throws AAIException, InterruptedException {
345 engine.startTransaction();
346 DBSerializer dbser2 = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST-2");
347 Vertex vert = graph.addVertex("aai-node-type", "generic-vnf");
349 dbser.touchStandardVertexProperties(vert, true);
350 String resverStart = (String)vert.property(AAIProperties.RESOURCE_VERSION).value();
351 String lastModTimeStart = (String)vert.property(AAIProperties.LAST_MOD_TS).value();
353 Thread.sleep(10); //bc the resource version is set based on current time in milliseconds,
354 //if this test runs through too fast the value may not change
355 //causing the test to fail. sleeping ensures a different value
357 dbser2.touchStandardVertexProperties(vert, false);
358 assertFalse(resverStart.equals(vert.property(AAIProperties.RESOURCE_VERSION).value()));
359 assertFalse(lastModTimeStart.equals(vert.property(AAIProperties.LAST_MOD_TS).value()));
360 assertEquals("AAI-TEST-2", vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value());
365 public void touchStandardVertexPropertiesAAIUUIDTest() throws AAIException, InterruptedException {
366 engine.startTransaction();
368 Graph graph = TinkerGraph.open();
369 Vertex v = graph.addVertex("aai-node-type", "generic-vnf");
371 dbser.touchStandardVertexProperties(v, true);
373 assertTrue(v.property(AAIProperties.AAI_UUID).isPresent());
375 UUID.fromString((String)v.property(AAIProperties.AAI_UUID).value());
376 } catch (IllegalArgumentException e) {
377 fail("Vertex uuid is not valid uuid");
382 public void verifyResourceVersion_SunnyDayTest() throws AAIException {
383 engine.startTransaction();
385 assertTrue(dbser.verifyResourceVersion("delete", "vnfc", "abc", "abc", "vnfcs/vnfc/vnfcId"));
390 public void verifyResourceVersion_CreateWithRVTest() throws AAIException {
391 engine.startTransaction();
393 thrown.expect(AAIException.class);
394 thrown.expectMessage("resource-version passed for create of generic-vnfs/generic-vnf/myid");
395 dbser.verifyResourceVersion("create", "generic-vnf", null, "old-res-ver", "generic-vnfs/generic-vnf/myid");
400 public void verifyResourceVersion_MissingRVTest() throws AAIException {
401 engine.startTransaction();
403 thrown.expect(AAIException.class);
404 thrown.expectMessage("resource-version not passed for update of generic-vnfs/generic-vnf/myid");
405 dbser.verifyResourceVersion("update", "generic-vnf", "current-res-ver", null, "generic-vnfs/generic-vnf/myid");
410 public void verifyResourceVersion_MismatchRVTest() throws AAIException {
411 engine.startTransaction();
413 thrown.expect(AAIException.class);
414 thrown.expectMessage("resource-version MISMATCH for update of generic-vnfs/generic-vnf/myid");
415 dbser.verifyResourceVersion("update", "generic-vnf", "current-res-ver", "old-res-ver", "generic-vnfs/generic-vnf/myid");
420 public void trimClassNameTest() throws AAIException {
421 assertEquals("GenericVnf", dbser.trimClassName("GenericVnf"));
422 assertEquals("GenericVnf", dbser.trimClassName("org.onap.aai.GenericVnf"));
426 public void getURIForVertexTest() throws AAIException, URISyntaxException, UnsupportedEncodingException {
427 engine.startTransaction();
429 Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123");
430 Vertex ten = engine.tx().addVertex("aai-node-type", "tenant", "tenant-id", "453");
432 edgeSer.addTreeEdge(engine.tx().traversal(), cr, ten);
434 URI compare = new URI("/cloud-infrastructure/cloud-regions/cloud-region/me/123/tenants/tenant/453");
435 assertEquals(compare, dbser.getURIForVertex(ten));
437 cr.property("aai-node-type").remove();
438 URI compareFailure = new URI("/unknown-uri");
439 assertEquals(compareFailure, dbser.getURIForVertex(ten));
444 public void getVertexPropertiesTest() throws AAIException, UnsupportedEncodingException {
445 engine.startTransaction();
447 Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123");
449 Introspector crIntro = dbser.getVertexProperties(cr);
450 assertEquals("cloud-region", crIntro.getDbName());
451 assertEquals("me", crIntro.getValue("cloud-owner"));
452 assertEquals("123", crIntro.getValue("cloud-region-id"));
457 public void getEdgeBetweenTest() throws AAIException {
458 engine.startTransaction();
460 Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123");
461 Vertex ten = engine.tx().addVertex("aai-node-type", "tenant", "tenant-id", "453");
463 edgeSer.addTreeEdge(engine.tx().traversal(), cr, ten);
465 Edge e = dbser.getEdgeBetween(EdgeType.TREE, ten, cr, null);
466 assertEquals("org.onap.relationships.inventory.BelongsTo", e.label());
471 public void deleteEdgeTest() throws AAIException, UnsupportedEncodingException {
472 engine.startTransaction();
474 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
475 Vertex vnfc = engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
477 edgeSer.addEdge(engine.tx().traversal(), gvnf, vnfc);
479 Introspector relData = loader.introspectorFromName("relationship-data");
480 relData.setValue("relationship-key", "vnfc.vnfc-name");
481 relData.setValue("relationship-value", "a-name");
482 Introspector relationship = loader.introspectorFromName("relationship");
483 relationship.setValue("related-to", "vnfc");
484 relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
485 relationship.setValue("relationship-data",relData);
487 assertTrue(dbser.deleteEdge(relationship, gvnf));
489 assertFalse(engine.tx().traversal().V(gvnf).both("uses").hasNext());
490 assertFalse(engine.tx().traversal().V(vnfc).both("uses").hasNext());
495 public void createEdgeTest() throws AAIException, UnsupportedEncodingException {
496 engine.startTransaction();
498 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
499 Vertex vnfc = engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
502 Introspector relData = loader.introspectorFromName("relationship-data");
503 relData.setValue("relationship-key", "vnfc.vnfc-name");
504 relData.setValue("relationship-value", "a-name");
505 Introspector relationship = loader.introspectorFromName("relationship");
506 relationship.setValue("related-to", "vnfc");
507 relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
508 relationship.setValue("relationship-data",relData);
510 assertTrue(dbser.createEdge(relationship, gvnf));
511 assertTrue(engine.tx().traversal().V(gvnf).both("org.onap.relationships.inventory.BelongsTo").hasNext());
512 assertTrue(engine.tx().traversal().V(vnfc).both("org.onap.relationships.inventory.BelongsTo").hasNext());
517 public void createCousinEdgeThatShouldBeTreeTest() throws AAIException, UnsupportedEncodingException, URISyntaxException {
518 engine.startTransaction();
520 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
521 Vertex vf = engine.tx().addVertex("aai-node-type","vf-module","vf-module-id","vf-id", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf/vf-modules/vf-module/vf-id");
523 edgeSer.addTreeEdge(engine.tx().traversal(), gvnf, vf);
525 Introspector relationship = loader.introspectorFromName("relationship");
526 relationship.setValue("related-to", "vf-module");
527 relationship.setValue("related-link", dbser.getURIForVertex(vf).toString());
528 Introspector relationshipList = loader.introspectorFromName("relationship-list");
529 relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
531 Introspector gvnfObj = loader.introspectorFromName("generic-vnf");
532 Vertex gvnf2 = dbser.createNewVertex(gvnfObj);
533 gvnfObj.setValue("relationship-list", relationshipList.getUnderlyingObject());
534 gvnfObj.setValue("vnf-id", "myvnf-1");
536 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(new URI("/network/generic-vnfs/generic-vnf/myvnf-1"));
539 dbser.serializeToDb(gvnfObj, gvnf2, uriQuery, null, "test");
540 } catch (AAIException e) {
541 assertEquals("AAI_6145", e.getCode());
546 public void createEdgeNodeDoesNotExistExceptionTest() throws AAIException, UnsupportedEncodingException {
547 engine.startTransaction();
549 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
551 //rainy day case, edge to non-existent object
552 Introspector relData = loader.introspectorFromName("relationship-data");
553 relData.setValue("relationship-key", "vnfc.vnfc-name");
554 relData.setValue("relationship-value", "b-name");
555 Introspector relationship = loader.introspectorFromName("relationship");
556 relationship.setValue("related-to", "vnfc");
557 relationship.setValue("related-link", "/network/vnfcs/vnfc/b-name");
558 relationship.setValue("relationship-data",relData);
560 thrown.expect(AAIException.class);
561 thrown.expectMessage("Node of type vnfc. Could not find object at: /network/vnfcs/vnfc/b-name");
562 dbser.createEdge(relationship, gvnf);
567 public void serializeSingleVertexTopLevelTest() throws AAIException, UnsupportedEncodingException {
568 engine.startTransaction();
570 Introspector gvnf = loader.introspectorFromName("generic-vnf");
571 Vertex gvnfVert = dbser.createNewVertex(gvnf);
573 gvnf.setValue("vnf-id", "myvnf");
574 dbser.serializeSingleVertex(gvnfVert, gvnf, "test");
575 assertTrue(engine.tx().traversal().V().has("aai-node-type","generic-vnf").has("vnf-id","myvnf").hasNext());
580 public void serializeSingleVertexChildTest() throws AAIException, UnsupportedEncodingException {
581 engine.startTransaction();
583 Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123");
584 Introspector tenIn = loader.introspectorFromName("tenant");
585 Vertex ten = dbser.createNewVertex(tenIn);
587 edgeSer.addTreeEdge(engine.tx().traversal(), cr, ten);
589 tenIn.setValue("tenant-id", "453");
590 tenIn.setValue("tenant-name", "mytenant");
592 dbser.serializeSingleVertex(ten, tenIn, "test");
594 assertTrue(engine.tx().traversal().V().has("aai-node-type","tenant").has("tenant-id","453").has("tenant-name","mytenant").hasNext());
600 public void getVertexPropertiesRelationshipHasLabelTest() throws AAIException, UnsupportedEncodingException {
601 engine.startTransaction();
603 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","vnf-123");
604 Vertex vnfc = engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","vnfc-123");
606 edgeSer.addEdge(engine.tx().traversal(), gvnf, vnfc);
608 Introspector obj = loader.introspectorFromName("generic-vnf");
609 obj = this.dbser.dbToObject(Arrays.asList(gvnf), obj, AAIProperties.MAXIMUM_DEPTH, false, "false");
612 MarshallerProperties properties = new MarshallerProperties
613 .Builder(org.onap.aai.restcore.MediaType.getEnum("application/json")).formatted(true).build();
614 System.out.println(obj.marshal(properties));
616 assertEquals("edge label between generic-vnf and vnfs is uses",
617 "org.onap.relationships.inventory.BelongsTo",
618 obj.getWrappedValue("relationship-list")
619 .getWrappedListValue("relationship")
621 .getValue("relationship-label")
628 public void getVertexPropertiesRelationshipOldVersionNoEdgeLabelTest() throws AAIException, UnsupportedEncodingException {
630 SchemaVersion version = schemaVersions.getAppRootVersion();
631 DBSerializer dbser = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST");
632 Loader loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
634 engine.startTransaction();
636 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","vnf-123");
637 Vertex vnfc = engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","vnfc-123");
639 edgeSer.addEdge(engine.tx().traversal(), gvnf, vnfc);
641 Introspector obj = loader.introspectorFromName("generic-vnf");
642 obj = dbser.dbToObject(Arrays.asList(gvnf), obj, AAIProperties.MAXIMUM_DEPTH, false, "false");
644 assertEquals("Relationship does not contain edge-property", false, obj.getWrappedValue("relationship-list").getWrappedListValue("relationship").get(0).hasProperty("relationship-label"));
650 public void createEdgeWithInvalidLabelTest() throws AAIException, UnsupportedEncodingException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
652 engine.startTransaction();
654 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
655 engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
657 Introspector relData = loader.introspectorFromName("relationship-data");
658 relData.setValue("relationship-key", "vnfc.vnfc-name");
659 relData.setValue("relationship-value", "a-name");
660 Introspector relationship = loader.introspectorFromName("relationship");
661 relationship.setValue("related-to", "vnfc");
662 relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
663 relationship.setValue("relationship-data",relData);
664 relationship.setValue("relationship-label", "NA");
666 thrown.expect(AAIException.class);
667 thrown.expectMessage("No rule found");
668 thrown.expectMessage("node type: generic-vnf, node type: vnfc, label: NA, type: COUSIN");
669 dbser.createEdge(relationship, gvnf);
674 public void addRelatedToPropertyTest() throws AAIException {
675 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getAppRootVersion());
676 Introspector gv = loader.introspectorFromName("generic-vnf");
677 gv.setValue("vnf-name", "myname");
678 Introspector rel = loader.introspectorFromName("relationship");
679 DBSerializer dbser = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine,
680 ModelType.MOXY, "AAI-TEST");
681 dbser.addRelatedToProperty(rel, gv);
682 List<Introspector> relToProps = rel.getWrappedListValue("related-to-property");
683 assertTrue(relToProps.size() == 1);
684 Introspector relToProp = relToProps.get(0);
685 assertTrue("generic-vnf.vnf-name".equals(relToProp.getValue("property-key")));
686 assertTrue("myname".equals(relToProp.getValue("property-value")));
690 public void dbToObjectContainerMismatchTest() throws AAIException, UnsupportedEncodingException {
691 DBSerializer dbser = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine,
692 ModelType.MOXY, "AAI-TEST");
693 Graph vertexMaker = TinkerGraph.open();
694 Vertex a = vertexMaker.addVertex(T.id, "0");
695 Vertex b = vertexMaker.addVertex(T.id, "1");
696 List<Vertex> vertices = Arrays.asList(a,b);
698 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getAppRootVersion());
699 Introspector intro = loader.introspectorFromName("image"); //just need any non-container object
701 thrown.expect(AAIException.class);
702 thrown.expectMessage("query object mismatch: this object cannot hold multiple items.");
704 dbser.dbToObject(vertices, intro, Integer.MAX_VALUE, true, "doesn't matter");
708 public void dbToObjectTest() throws AAIException, UnsupportedEncodingException {
709 engine.startTransaction();
711 DBSerializer dbser = new DBSerializer(version, engine,
712 ModelType.MOXY, "AAI-TEST");
713 Vertex gv1 = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1");
714 Vertex gv2 = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id2");
715 List<Vertex> vertices = Arrays.asList(gv1, gv2);
717 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, version);
718 Introspector gvContainer = loader.introspectorFromName("generic-vnfs");
720 Introspector res = dbser.dbToObject(vertices, gvContainer, 0, true, "true");
721 List<Introspector> gvs = res.getWrappedListValue("generic-vnf");
722 assertTrue(gvs.size() == 2);
723 for (Introspector i : gvs) {
724 String vnfId = i.getValue("vnf-id");
725 assertTrue("id1".equals(vnfId) || "id2".equals(vnfId));
732 public void getEdgeBetweenNoLabelTest() throws AAIException {
733 DBSerializer dbser = new DBSerializer(version, engine,
734 ModelType.MOXY, "AAI-TEST");
735 engine.startTransaction();
736 Vertex gv = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1");
737 Vertex lint = engine.tx().addVertex("aai-node-type", "l-interface", "interface-name", "name1");
738 edgeSer.addTreeEdge(engine.tx().traversal(), gv, lint);
740 Edge res = dbser.getEdgeBetween(EdgeType.TREE, gv, lint);
741 assertEquals("org.onap.relationships.inventory.BelongsTo", res.label());
746 public void deleteItemsWithTraversal() throws AAIException {
747 DBSerializer dbser = new DBSerializer(version, engine,
748 ModelType.MOXY, "AAI-TEST");
749 engine.startTransaction();
750 Vertex gv = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1");
751 Vertex lint = engine.tx().addVertex("aai-node-type", "l-interface", "interface-name", "name1");
753 assertTrue(engine.tx().traversal().V().has("vnf-id", "id1").hasNext());
754 assertTrue(engine.tx().traversal().V().has("interface-name", "name1").hasNext());
756 dbser.deleteItemsWithTraversal(Arrays.asList(gv, lint));
758 assertTrue(!engine.tx().traversal().V().has("vnf-id", "id1").hasNext());
759 assertTrue(!engine.tx().traversal().V().has("interface-name", "name1").hasNext());
765 public void serializeToDbWithParentTest() throws AAIException, UnsupportedEncodingException, URISyntaxException {
766 DBSerializer dbser = new DBSerializer(version, engine,
767 ModelType.MOXY, "AAI-TEST");
768 engine.startTransaction();
769 Vertex gv = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1", "aai-uri", "/network/generic-vnfs/generic-vnf/id1");
770 Vertex lint = engine.tx().addVertex("aai-node-type", "l-interface");
771 edgeSer.addTreeEdge(engine.tx().traversal(), gv, lint);
773 Introspector lintIntro = loader.introspectorFromName("l-interface");
774 lintIntro.setValue("interface-name", "name1");
775 lintIntro.setValue("interface-role", "actor");
776 URI lintURI = new URI("/network/generic-vnfs/generic-vnf/id1/l-interfaces/l-interface/name1");
777 QueryParser uriQuery = engine.getQueryBuilder(gv).createQueryFromURI(lintURI);
778 dbser.serializeToDb(lintIntro, lint, uriQuery, "test-identifier", "AAI-TEST");
780 assertTrue(engine.tx().traversal().V(lint).has("interface-role", "actor").hasNext());
786 public void getLatestVersionViewTest() throws AAIException, UnsupportedEncodingException {
787 DBSerializer dbser = new DBSerializer(version, engine,
788 ModelType.MOXY, "AAI-TEST");
789 engine.startTransaction();
790 Vertex phys = engine.tx().addVertex("aai-node-type", "physical-link", "link-name", "zaldo",
791 "speed-value", "very-fast", "service-provider-bandwidth-up-units", "things");
793 Introspector res = dbser.getLatestVersionView(phys);
794 assertTrue("zaldo".equals(res.getValue("link-name")));
795 assertTrue("very-fast".equals(res.getValue("speed-value")));
796 assertTrue("things".equals(res.getValue("service-provider-bandwidth-up-units")));
800 public void cascadeVserverDeleteTest() throws AAIException {
802 String expected_message = "";
804 /* vserver-->l-interface -->logical-link
807 Vertex vserver = graph.traversal().V().has("aai-node-type", "vserver").has("vserver-id", "vss1").next();
809 String exceptionMessage = testCascadeDelete(vserver);
810 assertEquals(expected_message, exceptionMessage);
815 public void cascadeL3NetworkPreventDeleteTest() throws AAIException {
817 ArrayList expected_messages = new ArrayList<String>();
818 expected_messages.add("Object is being reference by additional objects preventing it from being deleted. Please clean up references from the following types [l3-interface-ipv4-address-list, l3-interface-ipv6-address-list]");
819 expected_messages.add("Object is being reference by additional objects preventing it from being deleted. Please clean up references from the following types [l3-interface-ipv6-address-list, l3-interface-ipv4-address-list]");
821 /* vserver-->l-interface -->logical-link
824 Vertex l3network = graph.traversal().V().has("aai-node-type", "l3-network").has("network-id", "network-id-v1").next();
826 String exceptionMessage = testCascadeDelete(l3network);
827 assertTrue(expected_messages.contains(exceptionMessage));
832 public void cascadeL3NetworkDeleteTest() throws AAIException {
834 String expected_message = "";
836 /* vserver-->l-interface -->logical-link
839 Vertex l3network = graph.traversal().V().has("aai-node-type", "l3-network").has("network-id", "network-id-v2").next();
841 String exceptionMessage = testCascadeDelete(l3network);
842 assertEquals(expected_message, exceptionMessage);