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.hamcrest.CoreMatchers.is;
51 import static org.junit.Assert.*;
52 import static org.mockito.Mockito.spy;
53 import static org.mockito.Mockito.when;
55 @RunWith(value = Parameterized.class)
56 @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
57 public class DbSerializerTest extends AAISetup {
59 //to use, set thrown.expect to whatever your test needs
60 //this line establishes default of expecting no exception to be thrown
62 public ExpectedException thrown = ExpectedException.none();
64 protected static Graph graph;
67 protected EdgeSerializer edgeSer;
69 protected EdgeIngestor ei;
71 private SchemaVersion version;
72 private final ModelType introspectorFactoryType = ModelType.MOXY;
73 private final DBConnectionType type = DBConnectionType.REALTIME;
74 private Loader loader;
75 private TransactionalGraphEngine dbEngine;
76 private TransactionalGraphEngine engine; //for tests that aren't mocking the engine
77 private DBSerializer dbser;
78 TransactionalGraphEngine spy;
79 TransactionalGraphEngine.Admin adminSpy;
81 @Parameterized.Parameter(value = 0)
82 public QueryStyle queryStyle;
84 @Parameterized.Parameters(name = "QueryStyle.{0}")
85 public static Collection<Object[]> data() {
86 return Arrays.asList(new Object[][]{
87 {QueryStyle.TRAVERSAL},
88 {QueryStyle.TRAVERSAL_URI}
93 public static void init() throws Exception {
94 graph = JanusGraphFactory.build().set("storage.backend", "inmemory").open();
99 public void setup() throws Exception {
101 version = schemaVersions.getDefaultVersion();
102 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
103 dbEngine = new JanusGraphDBEngine(queryStyle, type, loader);
105 adminSpy = spy(dbEngine.asAdmin());
108 engine = new JanusGraphDBEngine(queryStyle, type, loader);
109 dbser = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST");
113 public void testFindDeletableDoesNotReturnDuplicates() throws AAIException {
115 Vertex genericVnf1 = graph.addVertex("aai-node-type", "generic-vnf", "vnf-id", "vnf1", "vnf-name", "vnfName1");
117 Vertex lInterface1 = graph.addVertex("aai-node-type", "l-interface", "interface-name", "lInterface1");
118 Vertex lInterface2 = graph.addVertex("aai-node-type", "l-interface", "interface-name", "lInterface2");
120 Vertex logicalLink1 = graph.addVertex("aai-node-type", "logical-link", "link-name", "logicalLink1");
121 Vertex logicalLink2 = graph.addVertex("aai-node-type", "logical-link", "link-name", "logicalLink2");
123 GraphTraversalSource g = graph.traversal();
125 edgeSer.addTreeEdge(g, genericVnf1, lInterface1);
126 edgeSer.addTreeEdge(g, genericVnf1, lInterface2);
127 edgeSer.addEdge(g, lInterface1, logicalLink1);
128 edgeSer.addEdge(g, lInterface1, logicalLink2);
129 // This line will cause the logical link2 to be found twice under linterface 1
130 // and also under the linterface 2 and since in the past deletable returned
131 // duplicates this test checks that it shouldn't return duplicates
132 edgeSer.addEdge(g, lInterface2, logicalLink2);
134 when(spy.asAdmin()).thenReturn(adminSpy);
135 when(adminSpy.getTraversalSource()).thenReturn(g);
136 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(g);
138 List<Vertex> deletableVertexes = spy.getQueryEngine().findDeletable(genericVnf1);
139 Set<Vertex> vertexSet = new HashSet<>();
141 for (Vertex deletableVertex : deletableVertexes) {
142 if(!vertexSet.contains(deletableVertex)){
143 vertexSet.add(deletableVertex);
145 fail("Find deletable is returning a list of duplicate vertexes");
151 public void tearDown() throws Exception {
156 public static void destroy() throws Exception {
161 public void subnetSetup() throws AAIException {
163 * This setus up the test graph, For future junits , add more vertices
167 Vertex l3interipv4addresslist_1 = graph.traversal().addV("aai-node-type", "l3-interface-ipv4-address-list",
168 "l3-interface-ipv4-address", "l3-interface-ipv4-address-1").next();
169 Vertex subnet_2 = graph.traversal().addV("aai-node-type", "subnet", "subnet-id", "subnet-id-2").next();
170 Vertex l3interipv6addresslist_3 = graph.traversal().addV("aai-node-type", "l3-interface-ipv6-address-list",
171 "l3-interface-ipv6-address", "l3-interface-ipv6-address-3").next();
172 Vertex subnet_4 = graph.traversal().addV("aai-node-type", "subnet", "subnet-id", "subnet-id-4").next();
173 Vertex subnet_5 = graph.traversal().addV("aai-node-type", "subnet", "subnet-id", "subnet-id-5").next();
174 Vertex l3network_6 = graph.traversal()
175 .addV("aai-node-type", "l3-network", "network-id", "network-id-6", "network-name", "network-name-6")
178 GraphTraversalSource g = graph.traversal();
179 edgeSer.addEdge(g, l3interipv4addresslist_1, subnet_2);
180 edgeSer.addEdge(g, l3interipv6addresslist_3, subnet_4);
181 edgeSer.addTreeEdge(g, subnet_5, l3network_6);
185 public void l3NetworkSetup() throws AAIException {
187 * This setus up the test graph, For future junits , add more vertices
191 Vertex l3network1 = graph.addVertex("aai-node-type", "l3-network", "network-id", "network-id-v1", "network-name", "network-name-v1");
192 Vertex l3network2 = graph.addVertex("aai-node-type", "l3-network", "network-id", "network-id-v2", "network-name", "network-name-v2");
193 Vertex subnet1 = graph.addVertex("aai-node-type", "subnet", "subnet-id", "subnet-id-v1");
194 Vertex subnet2 = graph.addVertex("aai-node-type", "subnet", "subnet-id", "subnet-id-v2");
196 Vertex l3interipv4addresslist_1 = graph.addVertex("aai-node-type", "l3-interface-ipv4-address-list",
197 "l3-interface-ipv4-address", "l3-intr-v1");
198 Vertex l3interipv6addresslist_1 = graph.addVertex("aai-node-type", "l3-interface-ipv6-address-list",
199 "l3-interface-ipv6-address", "l3-interface-ipv6-v1");
205 GraphTraversalSource g = graph.traversal();
206 edgeSer.addTreeEdge(g, subnet1, l3network1);
207 edgeSer.addEdge(g, l3interipv4addresslist_1, subnet1);
208 edgeSer.addEdge(g, l3interipv6addresslist_1, subnet1);
210 edgeSer.addTreeEdge(g, subnet2, l3network2);
216 public void vserverSetup() throws AAIException {
218 * This setus up the test graph, For future junits , add more vertices
222 Vertex vserver1 = graph.addVertex("aai-node-type", "vserver", "vserver-id", "vss1",
223 AAIProperties.AAI_URI.toString(),
224 "/cloud-infrastructure/cloud-regions/cloud-region/me/123/tenants/tenant/453/vservers/vserver/vss1");
226 Vertex lInterface1 = graph.addVertex("aai-node-type", "l-interface", "interface-name", "lIntr1");
227 Vertex lInterface2 = graph.addVertex("aai-node-type", "l-interface", "interface-name", "lIntr2");
229 Vertex logicalLink1 = graph.addVertex("aai-node-type", "logical-link", "link-name", "logLink1");
230 Vertex logicalLink2 = graph.addVertex("aai-node-type", "logical-link", "link-name", "logLink2");
232 Vertex l3interipv4addresslist_1 = graph.addVertex("aai-node-type", "l3-interface-ipv4-address-list",
233 "l3-interface-ipv4-address", "l3-intr-ipv4-address-1");
234 Vertex l3interipv6addresslist_2 = graph.addVertex("aai-node-type", "l3-interface-ipv6-address-list",
235 "l3-interface-ipv4-address", "l3-intr-ipv6-address-1");
237 GraphTraversalSource g = graph.traversal();
239 edgeSer.addTreeEdge(g, lInterface1, vserver1);
240 edgeSer.addTreeEdge(g, lInterface2, vserver1);
241 edgeSer.addTreeEdge(g, l3interipv4addresslist_1, lInterface1);
242 edgeSer.addTreeEdge(g, l3interipv6addresslist_2, lInterface2);
244 edgeSer.addEdge(g, lInterface1, logicalLink1);
245 edgeSer.addEdge(g, lInterface2, logicalLink2);
249 public void subnetDelWithInEdgesIpv4Test() throws AAIException {
251 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]";
254 * This subnet has in-edges with l3-ipv4 and NOT ok to delete
256 Vertex subnet = graph.traversal().V().has("aai-node-type", "subnet").has("subnet-id", "subnet-id-2").next();
258 String exceptionMessage = testCascadeDelete(subnet);
259 assertEquals(expected_message, exceptionMessage);
264 public void subnetDelWithInEdgesIpv6Test() throws AAIException {
266 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]";
269 * This subnet has in-edges with l3-ipv6 and NOT ok to delete
271 Vertex subnet = graph.traversal().V().has("aai-node-type", "subnet").has("subnet-id", "subnet-id-4").next();
272 String exceptionMessage = testCascadeDelete(subnet);
273 assertEquals(expected_message, exceptionMessage);
278 public void subnetDelWithInEdgesL3network() throws AAIException {
280 String expected_message = "";
283 * This subnet has in-edges with l3-network and ok to delete
285 Vertex subnet = graph.traversal().V().has("aai-node-type", "subnet").has("subnet-id", "subnet-id-5").next();
287 String exceptionMessage = testCascadeDelete(subnet);
288 assertEquals(expected_message, exceptionMessage);
292 public String testCascadeDelete(Vertex v) throws AAIException {
294 GraphTraversalSource traversal = graph.traversal();
295 when(spy.asAdmin()).thenReturn(adminSpy);
296 when(adminSpy.getTraversalSource()).thenReturn(traversal);
297 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(traversal);
299 String exceptionMessage = "";
300 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
301 List<Vertex> deletableVertices = spy.getQueryEngine().findDeletable(v);
304 serializer.delete(v, deletableVertices, "resourceVersion", false);
305 } catch (AAIException exception) {
306 exceptionMessage = exception.getMessage();
308 return exceptionMessage;
312 public String testDelete(Vertex v) throws AAIException {
314 GraphTraversalSource traversal = graph.traversal();
315 when(spy.asAdmin()).thenReturn(adminSpy);
316 when(adminSpy.getTraversalSource()).thenReturn(traversal);
317 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(traversal);
319 String exceptionMessage = "";
320 DBSerializer serializer = new DBSerializer(version, spy, introspectorFactoryType, "AAI_TEST");
323 serializer.delete(v, "resourceVersion", false);
324 } catch (AAIException exception) {
325 exceptionMessage = exception.getMessage();
327 return exceptionMessage;
332 public void createNewVertexTest() throws AAIException {
333 engine.startTransaction();
335 Introspector testObj = loader.introspectorFromName("generic-vnf");
337 Vertex testVertex = dbser.createNewVertex(testObj);
338 Vertex fromGraph = engine.tx().traversal().V().has("aai-node-type","generic-vnf").toList().get(0);
339 assertEquals(testVertex.id(), fromGraph.id());
340 assertEquals("AAI-TEST", fromGraph.property(AAIProperties.SOURCE_OF_TRUTH.toString()).value());
345 public void touchStandardVertexPropertiesTest() throws AAIException, InterruptedException {
346 engine.startTransaction();
348 //if this test runs through too fast the value may not change, causing the test to fail. sleeping ensures a different value
350 DBSerializer dbser2 = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST-2");
351 Vertex vert = graph.addVertex("aai-node-type", "generic-vnf");
353 // Upon first creation of the Vertex and the DBSerializer
354 // the source of truth and created-ts should be the same as their modified counterparts
355 dbser2.touchStandardVertexProperties(vert, true);
356 String createTS = (String)vert.property(AAIProperties.CREATED_TS).value();
357 String modTS = (String)vert.property(AAIProperties.LAST_MOD_TS).value();
358 String sot = (String)vert.property(AAIProperties.SOURCE_OF_TRUTH).value();
359 String lastModSOT = (String)vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value();
360 assertTrue(createTS.equals(modTS));
361 assertTrue(sot.equals(lastModSOT));
363 //if this test runs through too fast the value may not change, causing the test to fail. sleeping ensures a different value
366 // Not new vertex && new DBSerializer (A new serializer since a new one will be created per transaction)
367 // Here the vertex will be modified by a different source of truth
368 DBSerializer dbser3 = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST-3");
369 dbser3.touchStandardVertexProperties(vert, false);
370 createTS = (String)vert.property(AAIProperties.CREATED_TS).value();
371 modTS = (String)vert.property(AAIProperties.LAST_MOD_TS).value();
372 sot = (String)vert.property(AAIProperties.SOURCE_OF_TRUTH).value();
373 lastModSOT = (String)vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value();
374 assertFalse(createTS.equals(modTS));
375 assertFalse(sot.equals(lastModSOT));
377 //if this test runs through too fast the value may not change, causing the test to fail. sleeping ensures a different value
380 // The currentTimeMillis used for the created-ts and modified-ts is created at DBSerializer instantiation
381 // Every REST transaction should create a new DBSerializer - thus a new currentTimeMillis is used at the time of transaction.
382 // Using an existing vertex, but treating it as new && using an older DBSerializer
383 dbser.touchStandardVertexProperties(vert, true);
384 String resverStart = (String)vert.property(AAIProperties.RESOURCE_VERSION).value();
385 String lastModTimeStart = (String)vert.property(AAIProperties.LAST_MOD_TS).value();
386 createTS = (String)vert.property(AAIProperties.CREATED_TS).value();
387 modTS = (String)vert.property(AAIProperties.LAST_MOD_TS).value();
388 assertTrue(createTS.equals(modTS));
389 assertEquals("AAI-TEST", vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value());
391 //if this test runs through too fast the value may not change, causing the test to fail. sleeping ensures a different value
394 dbser2.touchStandardVertexProperties(vert, false);
395 String resourceVer = (String)vert.property(AAIProperties.RESOURCE_VERSION).value();
396 String lastModTs = (String)vert.property(AAIProperties.LAST_MOD_TS).value();
397 String lastModSoT = (String)vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value();
399 assertFalse(resverStart.equals(resourceVer));
400 assertFalse(lastModTimeStart.equals(lastModTs));
401 assertEquals("AAI-TEST-2", lastModSoT);
405 public void touchStandardVertexPropertiesAAIUUIDTest() throws AAIException, InterruptedException {
406 engine.startTransaction();
408 Graph graph = TinkerGraph.open();
409 Vertex v = graph.addVertex("aai-node-type", "generic-vnf");
411 dbser.touchStandardVertexProperties(v, true);
413 assertTrue(v.property(AAIProperties.AAI_UUID).isPresent());
415 UUID.fromString((String)v.property(AAIProperties.AAI_UUID).value());
416 } catch (IllegalArgumentException e) {
417 fail("Vertex uuid is not valid uuid");
422 public void verifyResourceVersion_SunnyDayTest() throws AAIException {
423 engine.startTransaction();
425 assertTrue(dbser.verifyResourceVersion("delete", "vnfc", "abc", "abc", "vnfcs/vnfc/vnfcId"));
430 public void verifyResourceVersion_CreateWithRVTest() throws AAIException {
431 engine.startTransaction();
433 thrown.expect(AAIException.class);
434 thrown.expectMessage("resource-version passed for create of generic-vnfs/generic-vnf/myid");
435 dbser.verifyResourceVersion("create", "generic-vnf", null, "old-res-ver", "generic-vnfs/generic-vnf/myid");
440 public void verifyResourceVersion_MissingRVTest() throws AAIException {
441 engine.startTransaction();
443 thrown.expect(AAIException.class);
444 thrown.expectMessage("resource-version not passed for update of generic-vnfs/generic-vnf/myid");
445 dbser.verifyResourceVersion("update", "generic-vnf", "current-res-ver", null, "generic-vnfs/generic-vnf/myid");
450 public void verifyResourceVersion_MismatchRVTest() throws AAIException {
451 engine.startTransaction();
453 thrown.expect(AAIException.class);
454 thrown.expectMessage("resource-version MISMATCH for update of generic-vnfs/generic-vnf/myid");
455 dbser.verifyResourceVersion("update", "generic-vnf", "current-res-ver", "old-res-ver", "generic-vnfs/generic-vnf/myid");
460 public void trimClassNameTest() throws AAIException {
461 assertEquals("GenericVnf", dbser.trimClassName("GenericVnf"));
462 assertEquals("GenericVnf", dbser.trimClassName("org.onap.aai.GenericVnf"));
466 public void getURIForVertexTest() throws AAIException, URISyntaxException, UnsupportedEncodingException {
467 engine.startTransaction();
469 Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123", "aai-uri", "/cloud-infrastructure/cloud-regions/cloud-region/me/123");
470 Vertex ten = engine.tx().addVertex("aai-node-type", "tenant", "tenant-id", "453");
472 edgeSer.addTreeEdge(engine.tx().traversal(), cr, ten);
474 ten.property("aai-uri", "/cloud-infrastructure/cloud-regions/cloud-region/me/123/tenants/tenant/453");
476 URI compare = new URI("/cloud-infrastructure/cloud-regions/cloud-region/me/123/tenants/tenant/453");
477 assertEquals(compare, dbser.getURIForVertex(ten));
479 URI compareFailure = new URI("/unknown-uri");
480 ten.property("aai-uri").remove();
481 assertEquals(compareFailure, dbser.getURIForVertex(ten));
486 public void getVertexPropertiesTest() throws AAIException, UnsupportedEncodingException {
487 engine.startTransaction();
489 Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123");
491 Introspector crIntro = dbser.getVertexProperties(cr);
492 assertEquals("cloud-region", crIntro.getDbName());
493 assertEquals("me", crIntro.getValue("cloud-owner"));
494 assertEquals("123", crIntro.getValue("cloud-region-id"));
499 public void getEdgeBetweenTest() throws AAIException {
500 engine.startTransaction();
502 Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123");
503 Vertex ten = engine.tx().addVertex("aai-node-type", "tenant", "tenant-id", "453");
505 edgeSer.addTreeEdge(engine.tx().traversal(), cr, ten);
507 Edge e = dbser.getEdgeBetween(EdgeType.TREE, ten, cr, null);
508 assertEquals("org.onap.relationships.inventory.BelongsTo", e.label());
513 public void deleteEdgeTest() throws AAIException, UnsupportedEncodingException {
514 engine.startTransaction();
516 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
517 Vertex vnfc = engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
519 edgeSer.addEdge(engine.tx().traversal(), gvnf, vnfc);
521 Introspector relData = loader.introspectorFromName("relationship-data");
522 relData.setValue("relationship-key", "vnfc.vnfc-name");
523 relData.setValue("relationship-value", "a-name");
524 Introspector relationship = loader.introspectorFromName("relationship");
525 relationship.setValue("related-to", "vnfc");
526 relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
527 relationship.setValue("relationship-data",relData);
529 assertTrue(dbser.deleteEdge(relationship, gvnf));
531 assertFalse(engine.tx().traversal().V(gvnf).both("uses").hasNext());
532 assertFalse(engine.tx().traversal().V(vnfc).both("uses").hasNext());
537 public void createEdgeTest() throws AAIException, UnsupportedEncodingException {
538 engine.startTransaction();
540 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
541 Vertex vnfc = engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
544 Introspector relData = loader.introspectorFromName("relationship-data");
545 relData.setValue("relationship-key", "vnfc.vnfc-name");
546 relData.setValue("relationship-value", "a-name");
547 Introspector relationship = loader.introspectorFromName("relationship");
548 relationship.setValue("related-to", "vnfc");
549 relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
550 relationship.setValue("relationship-data",relData);
552 assertTrue(dbser.createEdge(relationship, gvnf));
553 assertTrue(engine.tx().traversal().V(gvnf).both("org.onap.relationships.inventory.BelongsTo").hasNext());
554 assertTrue(engine.tx().traversal().V(vnfc).both("org.onap.relationships.inventory.BelongsTo").hasNext());
559 public void createCousinEdgeThatShouldBeTreeTest() throws AAIException, UnsupportedEncodingException, URISyntaxException {
560 engine.startTransaction();
562 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
563 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");
565 edgeSer.addTreeEdge(engine.tx().traversal(), gvnf, vf);
567 Introspector relationship = loader.introspectorFromName("relationship");
568 relationship.setValue("related-to", "vf-module");
569 relationship.setValue("related-link", dbser.getURIForVertex(vf).toString());
570 Introspector relationshipList = loader.introspectorFromName("relationship-list");
571 relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
573 Introspector gvnfObj = loader.introspectorFromName("generic-vnf");
574 Vertex gvnf2 = dbser.createNewVertex(gvnfObj);
575 gvnfObj.setValue("relationship-list", relationshipList.getUnderlyingObject());
576 gvnfObj.setValue("vnf-id", "myvnf-1");
578 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(new URI("/network/generic-vnfs/generic-vnf/myvnf-1"));
581 dbser.serializeToDb(gvnfObj, gvnf2, uriQuery, null, "test");
582 } catch (AAIException e) {
583 assertEquals("AAI_6145", e.getCode());
588 public void createEdgeNodeDoesNotExistExceptionTest() throws AAIException, UnsupportedEncodingException {
589 engine.startTransaction();
591 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
593 //rainy day case, edge to non-existent object
594 Introspector relData = loader.introspectorFromName("relationship-data");
595 relData.setValue("relationship-key", "vnfc.vnfc-name");
596 relData.setValue("relationship-value", "b-name");
597 Introspector relationship = loader.introspectorFromName("relationship");
598 relationship.setValue("related-to", "vnfc");
599 relationship.setValue("related-link", "/network/vnfcs/vnfc/b-name");
600 relationship.setValue("relationship-data",relData);
602 thrown.expect(AAIException.class);
603 thrown.expectMessage("Node of type vnfc. Could not find object at: /network/vnfcs/vnfc/b-name");
604 dbser.createEdge(relationship, gvnf);
609 public void serializeSingleVertexTopLevelTest() throws AAIException, UnsupportedEncodingException {
610 engine.startTransaction();
612 Introspector gvnf = loader.introspectorFromName("generic-vnf");
613 Vertex gvnfVert = dbser.createNewVertex(gvnf);
615 gvnf.setValue("vnf-id", "myvnf");
616 dbser.serializeSingleVertex(gvnfVert, gvnf, "test");
617 assertTrue(engine.tx().traversal().V().has("aai-node-type","generic-vnf").has("vnf-id","myvnf").hasNext());
622 public void serializeSingleVertexChildTest() throws AAIException, UnsupportedEncodingException {
623 engine.startTransaction();
625 Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123", "aai-uri", "/cloud-infrastructure/cloud-regions/cloud-region/me/123");
626 Introspector tenIn = loader.introspectorFromName("tenant");
627 Vertex ten = dbser.createNewVertex(tenIn);
628 ten.property("aai-uri", cr.property("aai-uri").value().toString() + "/tenants/tenant/453");
630 edgeSer.addTreeEdge(engine.tx().traversal(), cr, ten);
632 tenIn.setValue("tenant-id", "453");
633 tenIn.setValue("tenant-name", "mytenant");
635 dbser.serializeSingleVertex(ten, tenIn, "test");
637 assertTrue(engine.tx().traversal().V().has("aai-node-type","tenant").has("tenant-id","453").has("tenant-name","mytenant").hasNext());
643 public void getVertexPropertiesRelationshipHasLabelTest() throws AAIException, UnsupportedEncodingException {
644 engine.startTransaction();
646 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","vnf-123","aai-uri", "/network/generic-vnfs/generic-vnf/vnf-123");
647 Vertex vnfc = engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","vnfc-123","aai-uri", "/network/vnfcs/vnfc/vnfc-123");
649 edgeSer.addEdge(engine.tx().traversal(), gvnf, vnfc);
651 Introspector obj = loader.introspectorFromName("generic-vnf");
652 obj = this.dbser.dbToObject(Arrays.asList(gvnf), obj, AAIProperties.MAXIMUM_DEPTH, false, "false");
655 MarshallerProperties properties = new MarshallerProperties
656 .Builder(org.onap.aai.restcore.MediaType.getEnum("application/json")).formatted(true).build();
657 System.out.println(obj.marshal(properties));
659 assertEquals("edge label between generic-vnf and vnfs is uses",
660 "org.onap.relationships.inventory.BelongsTo",
661 obj.getWrappedValue("relationship-list")
662 .getWrappedListValue("relationship")
664 .getValue("relationship-label")
671 public void getVertexPropertiesRelationshipOldVersionNoEdgeLabelTest() throws AAIException, UnsupportedEncodingException {
673 SchemaVersion version = schemaVersions.getAppRootVersion();
674 DBSerializer dbser = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST");
675 Loader loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
677 engine.startTransaction();
679 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","vnf-123", "aai-uri", "/network/generic-vnfs/generic-vnf/vnf-123");
680 Vertex vnfc = engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","vnfc-123", "aai-uri", "/network/vnfcs/vnfc/vnfc-123");
682 edgeSer.addEdge(engine.tx().traversal(), gvnf, vnfc);
684 Introspector obj = loader.introspectorFromName("generic-vnf");
685 obj = dbser.dbToObject(Arrays.asList(gvnf), obj, AAIProperties.MAXIMUM_DEPTH, false, "false");
687 assertEquals("Relationship does not contain edge-property", false, obj.getWrappedValue("relationship-list").getWrappedListValue("relationship").get(0).hasProperty("relationship-label"));
693 public void createEdgeWithInvalidLabelTest() throws AAIException, UnsupportedEncodingException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
695 engine.startTransaction();
697 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
698 engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
700 Introspector relData = loader.introspectorFromName("relationship-data");
701 relData.setValue("relationship-key", "vnfc.vnfc-name");
702 relData.setValue("relationship-value", "a-name");
703 Introspector relationship = loader.introspectorFromName("relationship");
704 relationship.setValue("related-to", "vnfc");
705 relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
706 relationship.setValue("relationship-data",relData);
707 relationship.setValue("relationship-label", "NA");
709 thrown.expect(AAIException.class);
710 thrown.expectMessage("No rule found");
711 thrown.expectMessage("node type: generic-vnf, node type: vnfc, label: NA, type: COUSIN");
712 dbser.createEdge(relationship, gvnf);
717 public void addRelatedToPropertyTest() throws AAIException {
718 engine.startTransaction();
720 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf",
723 "aai-uri", "/network/generic-vnfs/generic-vnf/myname"
725 engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
726 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getAppRootVersion());
727 Introspector gv = loader.introspectorFromName("generic-vnf");
728 gv.setValue("vnf-name", "myname");
730 Introspector rel = loader.introspectorFromName("relationship");
731 DBSerializer dbser = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine,
732 ModelType.MOXY, "AAI-TEST");
733 dbser.addRelatedToProperty(rel, gvnf, "generic-vnf");
734 List<Introspector> relToProps = rel.getWrappedListValue("related-to-property");
735 assertThat(relToProps.size(), is(1));
736 Introspector relToProp = relToProps.get(0);
737 assertThat(relToProp.getValue("property-key"), is("generic-vnf.vnf-name"));
738 assertThat(relToProp.getValue("property-value"), is("myname"));
742 public void dbToObjectContainerMismatchTest() throws AAIException, UnsupportedEncodingException {
743 DBSerializer dbser = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine,
744 ModelType.MOXY, "AAI-TEST");
745 Graph vertexMaker = TinkerGraph.open();
746 Vertex a = vertexMaker.addVertex(T.id, "0");
747 Vertex b = vertexMaker.addVertex(T.id, "1");
748 List<Vertex> vertices = Arrays.asList(a,b);
750 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getAppRootVersion());
751 Introspector intro = loader.introspectorFromName("image"); //just need any non-container object
753 thrown.expect(AAIException.class);
754 thrown.expectMessage("query object mismatch: this object cannot hold multiple items.");
756 dbser.dbToObject(vertices, intro, Integer.MAX_VALUE, true, "doesn't matter");
760 public void dbToObjectTest() throws AAIException, UnsupportedEncodingException {
761 engine.startTransaction();
763 DBSerializer dbser = new DBSerializer(version, engine,
764 ModelType.MOXY, "AAI-TEST");
765 Vertex gv1 = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1");
766 Vertex gv2 = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id2");
767 List<Vertex> vertices = Arrays.asList(gv1, gv2);
769 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, version);
770 Introspector gvContainer = loader.introspectorFromName("generic-vnfs");
772 Introspector res = dbser.dbToObject(vertices, gvContainer, 0, true, "true");
773 List<Introspector> gvs = res.getWrappedListValue("generic-vnf");
774 assertTrue(gvs.size() == 2);
775 for (Introspector i : gvs) {
776 String vnfId = i.getValue("vnf-id");
777 assertTrue("id1".equals(vnfId) || "id2".equals(vnfId));
784 public void getEdgeBetweenNoLabelTest() throws AAIException {
785 DBSerializer dbser = new DBSerializer(version, engine,
786 ModelType.MOXY, "AAI-TEST");
787 engine.startTransaction();
788 Vertex gv = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1");
789 Vertex lint = engine.tx().addVertex("aai-node-type", "l-interface", "interface-name", "name1");
790 edgeSer.addTreeEdge(engine.tx().traversal(), gv, lint);
792 Edge res = dbser.getEdgeBetween(EdgeType.TREE, gv, lint);
793 assertEquals("org.onap.relationships.inventory.BelongsTo", res.label());
798 public void deleteItemsWithTraversal() throws AAIException {
799 DBSerializer dbser = new DBSerializer(version, engine,
800 ModelType.MOXY, "AAI-TEST");
801 engine.startTransaction();
802 Vertex gv = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1");
803 Vertex lint = engine.tx().addVertex("aai-node-type", "l-interface", "interface-name", "name1");
805 assertTrue(engine.tx().traversal().V().has("vnf-id", "id1").hasNext());
806 assertTrue(engine.tx().traversal().V().has("interface-name", "name1").hasNext());
808 dbser.deleteItemsWithTraversal(Arrays.asList(gv, lint));
810 assertTrue(!engine.tx().traversal().V().has("vnf-id", "id1").hasNext());
811 assertTrue(!engine.tx().traversal().V().has("interface-name", "name1").hasNext());
817 public void serializeToDbWithParentTest() throws AAIException, UnsupportedEncodingException, URISyntaxException {
818 DBSerializer dbser = new DBSerializer(version, engine,
819 ModelType.MOXY, "AAI-TEST");
820 engine.startTransaction();
821 Vertex gv = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1", "aai-uri", "/network/generic-vnfs/generic-vnf/id1");
822 Vertex lint = engine.tx().addVertex("aai-node-type", "l-interface");
823 edgeSer.addTreeEdge(engine.tx().traversal(), gv, lint);
825 Introspector lintIntro = loader.introspectorFromName("l-interface");
826 lintIntro.setValue("interface-name", "name1");
827 lintIntro.setValue("interface-role", "actor");
828 URI lintURI = new URI("/network/generic-vnfs/generic-vnf/id1/l-interfaces/l-interface/name1");
829 QueryParser uriQuery = engine.getQueryBuilder(gv).createQueryFromURI(lintURI);
830 dbser.serializeToDb(lintIntro, lint, uriQuery, "test-identifier", "AAI-TEST");
832 assertTrue(engine.tx().traversal().V(lint).has("interface-role", "actor").hasNext());
838 public void getLatestVersionViewTest() throws AAIException, UnsupportedEncodingException {
839 DBSerializer dbser = new DBSerializer(version, engine,
840 ModelType.MOXY, "AAI-TEST");
841 engine.startTransaction();
842 Vertex phys = engine.tx().addVertex("aai-node-type", "physical-link", "link-name", "zaldo",
843 "speed-value", "very-fast", "service-provider-bandwidth-up-units", "things");
845 Introspector res = dbser.getLatestVersionView(phys);
846 assertTrue("zaldo".equals(res.getValue("link-name")));
847 assertTrue("very-fast".equals(res.getValue("speed-value")));
848 assertTrue("things".equals(res.getValue("service-provider-bandwidth-up-units")));
852 public void cascadeVserverDeleteTest() throws AAIException {
854 String expected_message = "";
856 /* vserver-->l-interface -->logical-link
859 Vertex vserver = graph.traversal().V().has("aai-node-type", "vserver").has("vserver-id", "vss1").next();
861 String exceptionMessage = testCascadeDelete(vserver);
862 assertEquals(expected_message, exceptionMessage);
867 public void cascadeL3NetworkPreventDeleteTest() throws AAIException {
869 ArrayList expected_messages = new ArrayList<String>();
870 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]");
871 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]");
873 /* vserver-->l-interface -->logical-link
876 Vertex l3network = graph.traversal().V().has("aai-node-type", "l3-network").has("network-id", "network-id-v1").next();
878 String exceptionMessage = testCascadeDelete(l3network);
879 assertTrue(expected_messages.contains(exceptionMessage));
884 public void cascadeL3NetworkDeleteTest() throws AAIException {
886 String expected_message = "";
888 /* vserver-->l-interface -->logical-link
891 Vertex l3network = graph.traversal().V().has("aai-node-type", "l3-network").has("network-id", "network-id-v2").next();
893 String exceptionMessage = testCascadeDelete(l3network);
894 assertEquals(expected_message, exceptionMessage);