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();
347 //if this test runs through too fast the value may not change, causing the test to fail. sleeping ensures a different value
349 DBSerializer dbser2 = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST-2");
350 Vertex vert = graph.addVertex("aai-node-type", "generic-vnf");
352 // Upon first creation of the Vertex and the DBSerializer
353 // the source of truth and created-ts should be the same as their modified counterparts
354 dbser2.touchStandardVertexProperties(vert, true);
355 String createTS = (String)vert.property(AAIProperties.CREATED_TS).value();
356 String modTS = (String)vert.property(AAIProperties.LAST_MOD_TS).value();
357 String sot = (String)vert.property(AAIProperties.SOURCE_OF_TRUTH).value();
358 String lastModSOT = (String)vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value();
359 assertTrue(createTS.equals(modTS));
360 assertTrue(sot.equals(lastModSOT));
362 //if this test runs through too fast the value may not change, causing the test to fail. sleeping ensures a different value
365 // Not new vertex && new DBSerializer (A new serializer since a new one will be created per transaction)
366 // Here the vertex will be modified by a different source of truth
367 DBSerializer dbser3 = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST-3");
368 dbser3.touchStandardVertexProperties(vert, false);
369 createTS = (String)vert.property(AAIProperties.CREATED_TS).value();
370 modTS = (String)vert.property(AAIProperties.LAST_MOD_TS).value();
371 sot = (String)vert.property(AAIProperties.SOURCE_OF_TRUTH).value();
372 lastModSOT = (String)vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value();
373 assertFalse(createTS.equals(modTS));
374 assertFalse(sot.equals(lastModSOT));
376 //if this test runs through too fast the value may not change, causing the test to fail. sleeping ensures a different value
379 // The currentTimeMillis used for the created-ts and modified-ts is created at DBSerializer instantiation
380 // Every REST transaction should create a new DBSerializer - thus a new currentTimeMillis is used at the time of transaction.
381 // Using an existing vertex, but treating it as new && using an older DBSerializer
382 dbser.touchStandardVertexProperties(vert, true);
383 String resverStart = (String)vert.property(AAIProperties.RESOURCE_VERSION).value();
384 String lastModTimeStart = (String)vert.property(AAIProperties.LAST_MOD_TS).value();
385 createTS = (String)vert.property(AAIProperties.CREATED_TS).value();
386 modTS = (String)vert.property(AAIProperties.LAST_MOD_TS).value();
387 assertTrue(createTS.equals(modTS));
388 assertEquals("AAI-TEST", vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value());
390 //if this test runs through too fast the value may not change, causing the test to fail. sleeping ensures a different value
393 dbser2.touchStandardVertexProperties(vert, false);
394 String resourceVer = (String)vert.property(AAIProperties.RESOURCE_VERSION).value();
395 String lastModTs = (String)vert.property(AAIProperties.LAST_MOD_TS).value();
396 String lastModSoT = (String)vert.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH).value();
398 assertFalse(resverStart.equals(resourceVer));
399 assertFalse(lastModTimeStart.equals(lastModTs));
400 assertEquals("AAI-TEST-2", lastModSoT);
404 public void touchStandardVertexPropertiesAAIUUIDTest() throws AAIException, InterruptedException {
405 engine.startTransaction();
407 Graph graph = TinkerGraph.open();
408 Vertex v = graph.addVertex("aai-node-type", "generic-vnf");
410 dbser.touchStandardVertexProperties(v, true);
412 assertTrue(v.property(AAIProperties.AAI_UUID).isPresent());
414 UUID.fromString((String)v.property(AAIProperties.AAI_UUID).value());
415 } catch (IllegalArgumentException e) {
416 fail("Vertex uuid is not valid uuid");
421 public void verifyResourceVersion_SunnyDayTest() throws AAIException {
422 engine.startTransaction();
424 assertTrue(dbser.verifyResourceVersion("delete", "vnfc", "abc", "abc", "vnfcs/vnfc/vnfcId"));
429 public void verifyResourceVersion_CreateWithRVTest() throws AAIException {
430 engine.startTransaction();
432 thrown.expect(AAIException.class);
433 thrown.expectMessage("resource-version passed for create of generic-vnfs/generic-vnf/myid");
434 dbser.verifyResourceVersion("create", "generic-vnf", null, "old-res-ver", "generic-vnfs/generic-vnf/myid");
439 public void verifyResourceVersion_MissingRVTest() throws AAIException {
440 engine.startTransaction();
442 thrown.expect(AAIException.class);
443 thrown.expectMessage("resource-version not passed for update of generic-vnfs/generic-vnf/myid");
444 dbser.verifyResourceVersion("update", "generic-vnf", "current-res-ver", null, "generic-vnfs/generic-vnf/myid");
449 public void verifyResourceVersion_MismatchRVTest() throws AAIException {
450 engine.startTransaction();
452 thrown.expect(AAIException.class);
453 thrown.expectMessage("resource-version MISMATCH for update of generic-vnfs/generic-vnf/myid");
454 dbser.verifyResourceVersion("update", "generic-vnf", "current-res-ver", "old-res-ver", "generic-vnfs/generic-vnf/myid");
459 public void trimClassNameTest() throws AAIException {
460 assertEquals("GenericVnf", dbser.trimClassName("GenericVnf"));
461 assertEquals("GenericVnf", dbser.trimClassName("org.onap.aai.GenericVnf"));
465 public void getURIForVertexTest() throws AAIException, URISyntaxException, UnsupportedEncodingException {
466 engine.startTransaction();
468 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");
469 Vertex ten = engine.tx().addVertex("aai-node-type", "tenant", "tenant-id", "453");
471 edgeSer.addTreeEdge(engine.tx().traversal(), cr, ten);
473 ten.property("aai-uri", "/cloud-infrastructure/cloud-regions/cloud-region/me/123/tenants/tenant/453");
475 URI compare = new URI("/cloud-infrastructure/cloud-regions/cloud-region/me/123/tenants/tenant/453");
476 assertEquals(compare, dbser.getURIForVertex(ten));
478 URI compareFailure = new URI("/unknown-uri");
479 ten.property("aai-uri").remove();
480 assertEquals(compareFailure, dbser.getURIForVertex(ten));
485 public void getVertexPropertiesTest() throws AAIException, UnsupportedEncodingException {
486 engine.startTransaction();
488 Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123");
490 Introspector crIntro = dbser.getVertexProperties(cr);
491 assertEquals("cloud-region", crIntro.getDbName());
492 assertEquals("me", crIntro.getValue("cloud-owner"));
493 assertEquals("123", crIntro.getValue("cloud-region-id"));
498 public void getEdgeBetweenTest() throws AAIException {
499 engine.startTransaction();
501 Vertex cr = engine.tx().addVertex("aai-node-type", "cloud-region", "cloud-owner", "me", "cloud-region-id", "123");
502 Vertex ten = engine.tx().addVertex("aai-node-type", "tenant", "tenant-id", "453");
504 edgeSer.addTreeEdge(engine.tx().traversal(), cr, ten);
506 Edge e = dbser.getEdgeBetween(EdgeType.TREE, ten, cr, null);
507 assertEquals("org.onap.relationships.inventory.BelongsTo", e.label());
512 public void deleteEdgeTest() throws AAIException, UnsupportedEncodingException {
513 engine.startTransaction();
515 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
516 Vertex vnfc = engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
518 edgeSer.addEdge(engine.tx().traversal(), gvnf, vnfc);
520 Introspector relData = loader.introspectorFromName("relationship-data");
521 relData.setValue("relationship-key", "vnfc.vnfc-name");
522 relData.setValue("relationship-value", "a-name");
523 Introspector relationship = loader.introspectorFromName("relationship");
524 relationship.setValue("related-to", "vnfc");
525 relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
526 relationship.setValue("relationship-data",relData);
528 assertTrue(dbser.deleteEdge(relationship, gvnf));
530 assertFalse(engine.tx().traversal().V(gvnf).both("uses").hasNext());
531 assertFalse(engine.tx().traversal().V(vnfc).both("uses").hasNext());
536 public void createEdgeTest() throws AAIException, UnsupportedEncodingException {
537 engine.startTransaction();
539 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
540 Vertex vnfc = engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
543 Introspector relData = loader.introspectorFromName("relationship-data");
544 relData.setValue("relationship-key", "vnfc.vnfc-name");
545 relData.setValue("relationship-value", "a-name");
546 Introspector relationship = loader.introspectorFromName("relationship");
547 relationship.setValue("related-to", "vnfc");
548 relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
549 relationship.setValue("relationship-data",relData);
551 assertTrue(dbser.createEdge(relationship, gvnf));
552 assertTrue(engine.tx().traversal().V(gvnf).both("org.onap.relationships.inventory.BelongsTo").hasNext());
553 assertTrue(engine.tx().traversal().V(vnfc).both("org.onap.relationships.inventory.BelongsTo").hasNext());
558 public void createCousinEdgeThatShouldBeTreeTest() throws AAIException, UnsupportedEncodingException, URISyntaxException {
559 engine.startTransaction();
561 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
562 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");
564 edgeSer.addTreeEdge(engine.tx().traversal(), gvnf, vf);
566 Introspector relationship = loader.introspectorFromName("relationship");
567 relationship.setValue("related-to", "vf-module");
568 relationship.setValue("related-link", dbser.getURIForVertex(vf).toString());
569 Introspector relationshipList = loader.introspectorFromName("relationship-list");
570 relationshipList.setValue("relationship", Collections.singletonList(relationship.getUnderlyingObject()));
572 Introspector gvnfObj = loader.introspectorFromName("generic-vnf");
573 Vertex gvnf2 = dbser.createNewVertex(gvnfObj);
574 gvnfObj.setValue("relationship-list", relationshipList.getUnderlyingObject());
575 gvnfObj.setValue("vnf-id", "myvnf-1");
577 QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(new URI("/network/generic-vnfs/generic-vnf/myvnf-1"));
580 dbser.serializeToDb(gvnfObj, gvnf2, uriQuery, null, "test");
581 } catch (AAIException e) {
582 assertEquals("AAI_6145", e.getCode());
587 public void createEdgeNodeDoesNotExistExceptionTest() throws AAIException, UnsupportedEncodingException {
588 engine.startTransaction();
590 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
592 //rainy day case, edge to non-existent object
593 Introspector relData = loader.introspectorFromName("relationship-data");
594 relData.setValue("relationship-key", "vnfc.vnfc-name");
595 relData.setValue("relationship-value", "b-name");
596 Introspector relationship = loader.introspectorFromName("relationship");
597 relationship.setValue("related-to", "vnfc");
598 relationship.setValue("related-link", "/network/vnfcs/vnfc/b-name");
599 relationship.setValue("relationship-data",relData);
601 thrown.expect(AAIException.class);
602 thrown.expectMessage("Node of type vnfc. Could not find object at: /network/vnfcs/vnfc/b-name");
603 dbser.createEdge(relationship, gvnf);
608 public void serializeSingleVertexTopLevelTest() throws AAIException, UnsupportedEncodingException {
609 engine.startTransaction();
611 Introspector gvnf = loader.introspectorFromName("generic-vnf");
612 Vertex gvnfVert = dbser.createNewVertex(gvnf);
614 gvnf.setValue("vnf-id", "myvnf");
615 dbser.serializeSingleVertex(gvnfVert, gvnf, "test");
616 assertTrue(engine.tx().traversal().V().has("aai-node-type","generic-vnf").has("vnf-id","myvnf").hasNext());
621 public void serializeSingleVertexChildTest() throws AAIException, UnsupportedEncodingException {
622 engine.startTransaction();
624 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");
625 Introspector tenIn = loader.introspectorFromName("tenant");
626 Vertex ten = dbser.createNewVertex(tenIn);
627 ten.property("aai-uri", cr.property("aai-uri").value().toString() + "/tenants/tenant/453");
629 edgeSer.addTreeEdge(engine.tx().traversal(), cr, ten);
631 tenIn.setValue("tenant-id", "453");
632 tenIn.setValue("tenant-name", "mytenant");
634 dbser.serializeSingleVertex(ten, tenIn, "test");
636 assertTrue(engine.tx().traversal().V().has("aai-node-type","tenant").has("tenant-id","453").has("tenant-name","mytenant").hasNext());
642 public void getVertexPropertiesRelationshipHasLabelTest() throws AAIException, UnsupportedEncodingException {
643 engine.startTransaction();
645 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","vnf-123","aai-uri", "/network/generic-vnfs/generic-vnf/vnf-123");
646 Vertex vnfc = engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","vnfc-123","aai-uri", "/network/vnfcs/vnfc/vnfc-123");
648 edgeSer.addEdge(engine.tx().traversal(), gvnf, vnfc);
650 Introspector obj = loader.introspectorFromName("generic-vnf");
651 obj = this.dbser.dbToObject(Arrays.asList(gvnf), obj, AAIProperties.MAXIMUM_DEPTH, false, "false");
654 MarshallerProperties properties = new MarshallerProperties
655 .Builder(org.onap.aai.restcore.MediaType.getEnum("application/json")).formatted(true).build();
656 System.out.println(obj.marshal(properties));
658 assertEquals("edge label between generic-vnf and vnfs is uses",
659 "org.onap.relationships.inventory.BelongsTo",
660 obj.getWrappedValue("relationship-list")
661 .getWrappedListValue("relationship")
663 .getValue("relationship-label")
670 public void getVertexPropertiesRelationshipOldVersionNoEdgeLabelTest() throws AAIException, UnsupportedEncodingException {
672 SchemaVersion version = schemaVersions.getAppRootVersion();
673 DBSerializer dbser = new DBSerializer(version, engine, introspectorFactoryType, "AAI-TEST");
674 Loader loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
676 engine.startTransaction();
678 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","vnf-123", "aai-uri", "/network/generic-vnfs/generic-vnf/vnf-123");
679 Vertex vnfc = engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","vnfc-123", "aai-uri", "/network/vnfcs/vnfc/vnfc-123");
681 edgeSer.addEdge(engine.tx().traversal(), gvnf, vnfc);
683 Introspector obj = loader.introspectorFromName("generic-vnf");
684 obj = dbser.dbToObject(Arrays.asList(gvnf), obj, AAIProperties.MAXIMUM_DEPTH, false, "false");
686 assertEquals("Relationship does not contain edge-property", false, obj.getWrappedValue("relationship-list").getWrappedListValue("relationship").get(0).hasProperty("relationship-label"));
692 public void createEdgeWithInvalidLabelTest() throws AAIException, UnsupportedEncodingException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
694 engine.startTransaction();
696 Vertex gvnf = engine.tx().addVertex("aai-node-type","generic-vnf","vnf-id","myvnf", "aai-uri", "/network/generic-vnfs/generic-vnf/myvnf");
697 engine.tx().addVertex("aai-node-type","vnfc","vnfc-name","a-name", "aai-uri", "/network/vnfcs/vnfc/a-name");
699 Introspector relData = loader.introspectorFromName("relationship-data");
700 relData.setValue("relationship-key", "vnfc.vnfc-name");
701 relData.setValue("relationship-value", "a-name");
702 Introspector relationship = loader.introspectorFromName("relationship");
703 relationship.setValue("related-to", "vnfc");
704 relationship.setValue("related-link", "/network/vnfcs/vnfc/a-name");
705 relationship.setValue("relationship-data",relData);
706 relationship.setValue("relationship-label", "NA");
708 thrown.expect(AAIException.class);
709 thrown.expectMessage("No rule found");
710 thrown.expectMessage("node type: generic-vnf, node type: vnfc, label: NA, type: COUSIN");
711 dbser.createEdge(relationship, gvnf);
716 public void addRelatedToPropertyTest() throws AAIException {
717 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getAppRootVersion());
718 Introspector gv = loader.introspectorFromName("generic-vnf");
719 gv.setValue("vnf-name", "myname");
720 Introspector rel = loader.introspectorFromName("relationship");
721 DBSerializer dbser = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine,
722 ModelType.MOXY, "AAI-TEST");
723 dbser.addRelatedToProperty(rel, gv);
724 List<Introspector> relToProps = rel.getWrappedListValue("related-to-property");
725 assertTrue(relToProps.size() == 1);
726 Introspector relToProp = relToProps.get(0);
727 assertTrue("generic-vnf.vnf-name".equals(relToProp.getValue("property-key")));
728 assertTrue("myname".equals(relToProp.getValue("property-value")));
732 public void dbToObjectContainerMismatchTest() throws AAIException, UnsupportedEncodingException {
733 DBSerializer dbser = new DBSerializer(schemaVersions.getAppRootVersion(), dbEngine,
734 ModelType.MOXY, "AAI-TEST");
735 Graph vertexMaker = TinkerGraph.open();
736 Vertex a = vertexMaker.addVertex(T.id, "0");
737 Vertex b = vertexMaker.addVertex(T.id, "1");
738 List<Vertex> vertices = Arrays.asList(a,b);
740 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getAppRootVersion());
741 Introspector intro = loader.introspectorFromName("image"); //just need any non-container object
743 thrown.expect(AAIException.class);
744 thrown.expectMessage("query object mismatch: this object cannot hold multiple items.");
746 dbser.dbToObject(vertices, intro, Integer.MAX_VALUE, true, "doesn't matter");
750 public void dbToObjectTest() throws AAIException, UnsupportedEncodingException {
751 engine.startTransaction();
753 DBSerializer dbser = new DBSerializer(version, engine,
754 ModelType.MOXY, "AAI-TEST");
755 Vertex gv1 = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1");
756 Vertex gv2 = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id2");
757 List<Vertex> vertices = Arrays.asList(gv1, gv2);
759 Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, version);
760 Introspector gvContainer = loader.introspectorFromName("generic-vnfs");
762 Introspector res = dbser.dbToObject(vertices, gvContainer, 0, true, "true");
763 List<Introspector> gvs = res.getWrappedListValue("generic-vnf");
764 assertTrue(gvs.size() == 2);
765 for (Introspector i : gvs) {
766 String vnfId = i.getValue("vnf-id");
767 assertTrue("id1".equals(vnfId) || "id2".equals(vnfId));
774 public void getEdgeBetweenNoLabelTest() throws AAIException {
775 DBSerializer dbser = new DBSerializer(version, engine,
776 ModelType.MOXY, "AAI-TEST");
777 engine.startTransaction();
778 Vertex gv = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1");
779 Vertex lint = engine.tx().addVertex("aai-node-type", "l-interface", "interface-name", "name1");
780 edgeSer.addTreeEdge(engine.tx().traversal(), gv, lint);
782 Edge res = dbser.getEdgeBetween(EdgeType.TREE, gv, lint);
783 assertEquals("org.onap.relationships.inventory.BelongsTo", res.label());
788 public void deleteItemsWithTraversal() throws AAIException {
789 DBSerializer dbser = new DBSerializer(version, engine,
790 ModelType.MOXY, "AAI-TEST");
791 engine.startTransaction();
792 Vertex gv = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1");
793 Vertex lint = engine.tx().addVertex("aai-node-type", "l-interface", "interface-name", "name1");
795 assertTrue(engine.tx().traversal().V().has("vnf-id", "id1").hasNext());
796 assertTrue(engine.tx().traversal().V().has("interface-name", "name1").hasNext());
798 dbser.deleteItemsWithTraversal(Arrays.asList(gv, lint));
800 assertTrue(!engine.tx().traversal().V().has("vnf-id", "id1").hasNext());
801 assertTrue(!engine.tx().traversal().V().has("interface-name", "name1").hasNext());
807 public void serializeToDbWithParentTest() throws AAIException, UnsupportedEncodingException, URISyntaxException {
808 DBSerializer dbser = new DBSerializer(version, engine,
809 ModelType.MOXY, "AAI-TEST");
810 engine.startTransaction();
811 Vertex gv = engine.tx().addVertex("aai-node-type", "generic-vnf", "vnf-id", "id1", "aai-uri", "/network/generic-vnfs/generic-vnf/id1");
812 Vertex lint = engine.tx().addVertex("aai-node-type", "l-interface");
813 edgeSer.addTreeEdge(engine.tx().traversal(), gv, lint);
815 Introspector lintIntro = loader.introspectorFromName("l-interface");
816 lintIntro.setValue("interface-name", "name1");
817 lintIntro.setValue("interface-role", "actor");
818 URI lintURI = new URI("/network/generic-vnfs/generic-vnf/id1/l-interfaces/l-interface/name1");
819 QueryParser uriQuery = engine.getQueryBuilder(gv).createQueryFromURI(lintURI);
820 dbser.serializeToDb(lintIntro, lint, uriQuery, "test-identifier", "AAI-TEST");
822 assertTrue(engine.tx().traversal().V(lint).has("interface-role", "actor").hasNext());
828 public void getLatestVersionViewTest() throws AAIException, UnsupportedEncodingException {
829 DBSerializer dbser = new DBSerializer(version, engine,
830 ModelType.MOXY, "AAI-TEST");
831 engine.startTransaction();
832 Vertex phys = engine.tx().addVertex("aai-node-type", "physical-link", "link-name", "zaldo",
833 "speed-value", "very-fast", "service-provider-bandwidth-up-units", "things");
835 Introspector res = dbser.getLatestVersionView(phys);
836 assertTrue("zaldo".equals(res.getValue("link-name")));
837 assertTrue("very-fast".equals(res.getValue("speed-value")));
838 assertTrue("things".equals(res.getValue("service-provider-bandwidth-up-units")));
842 public void cascadeVserverDeleteTest() throws AAIException {
844 String expected_message = "";
846 /* vserver-->l-interface -->logical-link
849 Vertex vserver = graph.traversal().V().has("aai-node-type", "vserver").has("vserver-id", "vss1").next();
851 String exceptionMessage = testCascadeDelete(vserver);
852 assertEquals(expected_message, exceptionMessage);
857 public void cascadeL3NetworkPreventDeleteTest() throws AAIException {
859 ArrayList expected_messages = new ArrayList<String>();
860 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]");
861 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]");
863 /* vserver-->l-interface -->logical-link
866 Vertex l3network = graph.traversal().V().has("aai-node-type", "l3-network").has("network-id", "network-id-v1").next();
868 String exceptionMessage = testCascadeDelete(l3network);
869 assertTrue(expected_messages.contains(exceptionMessage));
874 public void cascadeL3NetworkDeleteTest() throws AAIException {
876 String expected_message = "";
878 /* vserver-->l-interface -->logical-link
881 Vertex l3network = graph.traversal().V().has("aai-node-type", "l3-network").has("network-id", "network-id-v2").next();
883 String exceptionMessage = testCascadeDelete(l3network);
884 assertEquals(expected_message, exceptionMessage);