b07c7280d6b8e4dbd7fa224428a9ad65f25fb51c
[aai/resources.git] /
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.aai.migration.v12;
21
22 import static org.junit.Assert.assertArrayEquals;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
27
28 import java.util.Optional;
29
30 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
31 import org.apache.tinkerpop.gremlin.process.traversal.strategy.verification.ReadOnlyStrategy;
32 import org.apache.tinkerpop.gremlin.structure.Vertex;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.aai.AAISetup;
37 import org.onap.aai.dbmap.DBConnectionType;
38 import org.onap.aai.introspection.Loader;
39 import org.onap.aai.introspection.LoaderFactory;
40 import org.onap.aai.introspection.ModelType;
41 import org.onap.aai.introspection.Version;
42 import org.onap.aai.serialization.db.EdgeRules;
43 import org.onap.aai.serialization.engines.QueryStyle;
44 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
45 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
46
47 import org.janusgraph.core.JanusGraphFactory;
48 import org.janusgraph.core.JanusGraph;
49 import org.janusgraph.core.JanusGraphTransaction;
50
51 public class MigrateServiceInstanceToConfigurationTest extends AAISetup {
52
53         private final static Version version = Version.v12;
54         private final static ModelType introspectorFactoryType = ModelType.MOXY;
55         private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL;
56         private final static DBConnectionType type = DBConnectionType.REALTIME;
57
58         private static Loader loader;
59         private static TransactionalGraphEngine dbEngine;
60         private static JanusGraph graph;
61         private static MigrateServiceInstanceToConfiguration migration;
62         private static JanusGraphTransaction tx;
63         private static GraphTraversalSource g;
64         private static EdgeRules rules;
65
66         @BeforeClass
67         public static void setUp() throws Exception {
68                 graph = JanusGraphFactory.build().set("storage.backend","inmemory").open();
69                 tx = graph.newTransaction();
70                 g = tx.traversal();
71                 loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
72                 dbEngine = new JanusGraphDBEngine(
73                                 queryStyle,
74                                 type,
75                                 loader);
76                 rules = EdgeRules.getInstance();
77
78                 Vertex customer1 = g.addV()
79                                 .property("aai-node-type", "customer")
80                                 .property("global-customer-id", "customer-id-1")
81                                 .property("subscriber-type", "CUST")
82                                 .next();
83                 
84                 Vertex customer2 = g.addV()
85                                 .property("aai-node-type", "customer")
86                                 .property("global-customer-id", "customer-id-2")
87                                 .property("subscriber-type", "CUST")
88                                 .next();
89                 
90                 Vertex customer3 = g.addV()
91                                 .property("aai-node-type", "customer")
92                                 .property("global-customer-id", "customer-id-3")
93                                 .property("subscriber-type", "CUST")
94                                 .next();
95                 
96                 Vertex customer4 = g.addV()
97                                 .property("aai-node-type", "customer")
98                                 .property("global-customer-id", "customer-id-4")
99                                 .property("subscriber-type", "CUST")
100                                 .next();
101
102                 Vertex servSub1 = g.addV()
103                                 .property("aai-node-type", "service-subscription")
104                                 .property("service-type", "DHV")
105                                 .next();
106                 
107                 Vertex servSub2 = g.addV()
108                                 .property("aai-node-type", "service-subscription")
109                                 .property("service-type", "OTHER")
110                                 .next();
111                 
112                 Vertex servSub3 = g.addV()
113                                 .property("aai-node-type", "service-subscription")
114                                 .property("service-type", "DHV")
115                                 .next();
116                 
117                 Vertex servSub4 = g.addV()
118                                 .property("aai-node-type", "service-subscription")
119                                 .property("service-type", "DHV")
120                                 .next();
121                 
122                 Vertex servSub5 = g.addV()
123                                 .property("aai-node-type", "service-subscription")
124                                 .property("service-type", "DHV")
125                                 .next();
126                 
127                 Vertex servInstance1 = g.addV()
128                                 .property("aai-node-type", "service-instance")
129                                 .property("service-instance-id", "service-inst-1")
130                                 .property("operational-status", "activated")
131                                 .property("bandwidth-total", "5")
132                                 .next();
133                 
134                 Vertex servInstance2 = g.addV()
135                                 .property("aai-node-type", "service-instance")
136                                 .property("service-instance-id", "service-inst-2")
137                                 .property("operational-status", "activated")
138                                 .property("bandwidth-total", "8")
139                                 .next();
140                 
141                 Vertex servInstance3 = g.addV()
142                                 .property("aai-node-type", "service-instance")
143                                 .property("service-instance-id", "service-inst-3")
144                                 .property("operational-status", "activated")
145                                 .property("bandwidth-total", "10")
146                                 .next();
147                 
148                 Vertex servInstance4 = g.addV()
149                                 .property("aai-node-type", "service-instance")
150                                 .property("service-instance-id", "service-inst-4")
151                                 .property("operational-status", "activated")
152                                 .property("bandwidth-total", "15")
153                                 .next();
154                 
155                 Vertex config1 = g.addV()
156                                 .property("aai-node-type", "configuration")
157                                 .property("configuration-id", "configuration-1")
158                                 .property("configuration-type", "DHV")
159                                 .property("tunnel-bandwidth", "7")
160                                 .next();
161                 
162                 Vertex config2 = g.addV()
163                                 .property("aai-node-type", "configuration")
164                                 .property("configuration-id", "configuration-2")
165                                 .property("configuration-type", "OTHER")
166                                 .property("tunnel-bandwidth", "3")
167                                 .next();
168                 
169                 Vertex config3 = g.addV()
170                                 .property("aai-node-type", "configuration")
171                                 .property("configuration-id", "configuration-3")
172                                 .property("configuration-type", "OTHER")
173                                 .property("tunnel-bandwidth", "2")
174                                 .next();
175                 
176                 Vertex config4 = g.addV()
177                                 .property("aai-node-type", "configuration")
178                                 .property("configuration-id", "configuration-4")
179                                 .property("configuration-type", "OTHER")
180                                 .property("tunnel-bandwidth", "4")
181                                 .next();
182
183                 // graph 1
184                 rules.addTreeEdge(g, customer1, servSub1);
185                 rules.addTreeEdge(g, customer1, servSub2);
186                 rules.addTreeEdge(g, servSub1, servInstance1);
187                 rules.addTreeEdge(g, servSub2, servInstance2);
188                 
189                 // graph 2
190                 rules.addTreeEdge(g, customer2, servSub3);
191                 
192                 // graph 3
193                 rules.addTreeEdge(g, customer3, servSub4);
194                 rules.addTreeEdge(g, servSub4, servInstance3);
195                 rules.addEdge(g, servInstance3, config1);
196                 rules.addEdge(g, servInstance3, config2);
197                 
198                 // graph 4
199                 rules.addTreeEdge(g, customer4, servSub5);
200                 rules.addTreeEdge(g, servSub5, servInstance4);
201                 rules.addEdge(g, servInstance4, config3);
202                 rules.addEdge(g, servInstance4, config4);
203
204                 TransactionalGraphEngine spy = spy(dbEngine);
205                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
206
207                 GraphTraversalSource traversal = g;
208                 GraphTraversalSource readOnly = tx.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()));
209                 when (spy.tx()).thenReturn(tx);
210                 when(spy.asAdmin()).thenReturn(adminSpy);
211                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
212                 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(readOnly);
213                 
214                 migration = new MigrateServiceInstanceToConfiguration(spy);
215                 migration.run();
216         }
217         
218         @AfterClass
219         public static void cleanUp() {
220                 tx.tx().rollback();
221                 graph.close();
222         }
223         
224         @Test
225         public void testRun_createConfigNode() throws Exception {
226                 // check if graph nodes exist
227                 assertEquals("customer node exists", true, 
228                                 g.V().has("global-customer-id", "customer-id-1")
229                                 .hasNext());
230                 
231                 assertEquals("service subscription node, service-type=DHV", true, 
232                                 g.V().has("global-customer-id", "customer-id-1")
233                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
234                                 .hasNext());
235                 
236                 assertEquals("service instance node, bandwidth-total=5", true, 
237                                 g.V().has("global-customer-id", "customer-id-1")
238                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
239                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-1").has("bandwidth-total", "5")
240                                 .hasNext());
241                 
242                 // check if configuration node gets created
243                 assertEquals("configuration node exists", true, 
244                                 g.V().has("global-customer-id", "customer-id-1")
245                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
246                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-1")
247                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
248                                 .hasNext());
249                 
250                 // check configuration type
251                 assertEquals("configuration node, configuration-type=DHV", true, 
252                                 g.V().has("global-customer-id", "customer-id-1")
253                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
254                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-1")
255                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("configuration-type", "DHV")
256                                 .hasNext());
257                 
258                 // check configuration tunnel-bandwidth
259                 assertEquals("configuration node, tunnel-bandwidth=5", true, 
260                                 g.V().has("global-customer-id", "customer-id-1")
261                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
262                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-1")
263                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "5")
264                                 .hasNext());
265         }
266
267         @Test
268         public void testRun_configNodeNotCreated() throws Exception {
269                 // check if graph nodes exist
270                 assertEquals("customer node exists", true, 
271                                 g.V().has("global-customer-id", "customer-id-1")
272                                 .hasNext());
273                 
274                 assertEquals("service subscription node, service-type=OTHER", true, 
275                                 g.V().has("global-customer-id", "customer-id-1")
276                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "OTHER")
277                                 .hasNext());
278                 
279                 assertEquals("service instance node, bandwidth-total=8", true, 
280                                 g.V().has("global-customer-id", "customer-id-1")
281                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "OTHER")
282                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-2").has("bandwidth-total", "8")
283                                 .hasNext());
284                 
285                 // configuration node should not be created
286                 assertEquals("configuration node does not exist", false, 
287                                 g.V().has("global-customer-id", "customer-id-1")
288                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "OTHER")
289                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-2")
290                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
291                                 .hasNext());
292                 
293                 // edge between service instance and configuration should not be created
294                 assertEquals("configuration node does not exist", false, 
295                                 g.V().has("global-customer-id", "customer-id-1")
296                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "OTHER")
297                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-2")
298                                 .out("org.onap.relationships.inventory.Uses").hasNext());
299         }
300         
301         @Test
302         public void testRun_noServiceInstance() throws Exception {
303                 // check if graph nodes exist
304                 assertEquals("customer node exists", true, 
305                                 g.V().has("global-customer-id", "customer-id-2")
306                                 .hasNext());
307                 
308                 assertEquals("service subscription node, service-type=DHV", true, 
309                                 g.V().has("global-customer-id", "customer-id-2")
310                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
311                                 .hasNext());
312                 
313                 // no service instance nodes
314                 assertEquals("no service instance nodes", false, 
315                                 g.V().has("global-customer-id", "customer-id-2")
316                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
317                                 .in("org.onap.relationships.inventory.BelongsTo").has("aai-node-type", "service-instance")
318                                 .hasNext());
319         }
320         
321         @Test
322         public void testRun_existingConfig() throws Exception {
323                 // check if graph nodes exist
324                 assertEquals("customer node exists", true, 
325                                 g.V().has("global-customer-id", "customer-id-3")
326                                 .hasNext());
327                 
328                 assertEquals("service subscription node, service-type=DHV", true, 
329                                 g.V().has("global-customer-id", "customer-id-3")
330                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
331                                 .hasNext());
332                 
333                 assertEquals("service instance node, bandwidth-total=10", true, 
334                                 g.V().has("global-customer-id", "customer-id-3")
335                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
336                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-3").has("bandwidth-total", "10")
337                                 .hasNext());
338                 
339                 assertEquals("configuration node with type DHV, tunnel-bandwidth changed to 10", true, 
340                                 g.V().has("global-customer-id", "customer-id-3")
341                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
342                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-3")
343                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "10")
344                                 .hasNext());
345                 
346                 assertEquals("configuration node with type OTHER, tunnel-bandwidth remains same", true, 
347                                 g.V().has("global-customer-id", "customer-id-3")
348                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
349                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-3")
350                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "3")
351                                 .hasNext());
352         }
353         
354         @Test
355         public void testRun_existingConfigNotDHV() throws Exception {
356                 // check if graph nodes exist
357                 assertEquals("customer node exists", true, 
358                                 g.V().has("global-customer-id", "customer-id-4")
359                                 .hasNext());
360                 
361                 assertEquals("service subscription node, service-type=DHV", true, 
362                                 g.V().has("global-customer-id", "customer-id-4")
363                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
364                                 .hasNext());
365                 
366                 assertEquals("service instance node, bandwidth-total=15", true, 
367                                 g.V().has("global-customer-id", "customer-id-4")
368                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
369                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-4").has("bandwidth-total", "15")
370                                 .hasNext());
371                 
372                 assertEquals("first configuration node with type OTHER, tunnel-bandwidth remains same", true, 
373                                 g.V().has("global-customer-id", "customer-id-4")
374                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
375                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-4")
376                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "2")
377                                 .hasNext());
378                 
379                 assertEquals("second configuration node with type OTHER, tunnel-bandwidth remains same", true, 
380                                 g.V().has("global-customer-id", "customer-id-4")
381                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
382                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-4")
383                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "4")
384                                 .hasNext());
385                 
386                 assertEquals("new configuration node created with type DHV, tunnel-bandwidth=15", true, 
387                                 g.V().has("global-customer-id", "customer-id-4")
388                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
389                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "service-inst-4")
390                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration").has("tunnel-bandwidth", "15")
391                                 .hasNext());
392         }
393         
394         @Test
395         public void testGetAffectedNodeTypes() {
396                 Optional<String[]> types = migration.getAffectedNodeTypes();
397                 Optional<String[]> expected = Optional.of(new String[]{"service-instance"});
398                 
399                 assertNotNull(types);
400                 assertArrayEquals(expected.get(), types.get());
401         }
402
403         @Test
404         public void testGetMigrationName() {
405                 String migrationName = migration.getMigrationName();
406
407                 assertNotNull(migrationName);
408                 assertEquals("service-instance-to-configuration", migrationName);
409         }
410 }