67646678f3287c37ddfab7fe3d05bccd7379f062
[aai/graphadmin.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.*;
34 import org.onap.aai.AAISetup;
35 import org.onap.aai.dbmap.DBConnectionType;
36 import org.onap.aai.introspection.Loader;
37 import org.onap.aai.introspection.LoaderFactory;
38 import org.onap.aai.introspection.ModelType;
39 import org.onap.aai.setup.SchemaVersions;
40 import org.onap.aai.setup.SchemaVersion;
41 import org.onap.aai.serialization.engines.QueryStyle;
42 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
43 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
44
45 import org.janusgraph.core.JanusGraphFactory;
46 import org.janusgraph.core.JanusGraph;
47 import org.janusgraph.core.JanusGraphTransaction;
48
49 public class MigrateServiceInstanceToConfigurationTestPreMigrationMock extends AAISetup {
50
51         private final static ModelType introspectorFactoryType = ModelType.MOXY;
52         private final static QueryStyle queryStyle = QueryStyle.TRAVERSAL;
53
54         private Loader loader;
55         private TransactionalGraphEngine dbEngine;
56         private JanusGraph graph;
57         private MigrateServiceInstanceToConfiguration migration;
58         private JanusGraphTransaction tx;
59         private GraphTraversalSource g;
60
61         @Before
62         public void setUp() throws Exception {
63                 graph = JanusGraphFactory.build().set("storage.backend","inmemory").open();
64                 tx = graph.newTransaction();
65                 g = tx.traversal();
66                 loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, schemaVersions.getDefaultVersion());
67                 dbEngine = new JanusGraphDBEngine(
68                                 queryStyle,
69                                 loader);
70
71                 Vertex customer = g.addV()
72                                 .property("aai-node-type", "customer")
73                                 .property("global-customer-id", "customer-9972-BandwidthMigration")
74                                 .property("subscriber-type", "CUST")
75                                 .next();
76                 
77                 Vertex servSubSDNEI = g.addV()
78                                 .property("aai-node-type", "service-subscription")
79                                 .property("service-type", "SDN-ETHERNET-INTERNET")
80                                 .next();
81                 
82                 Vertex servInstance22 = g.addV()
83                                 .property("aai-node-type", "service-instance")
84                                 .property("service-instance-id", "servInstance-9972-22-BandwidthMigration")
85                                 .property("operational-status", "activated")
86                                 .property("bandwidth-total", "bandwidth-total-22-BandwidthMigration")
87                                 .next();
88                 
89                 Vertex servInstance11 = g.addV()
90                                 .property("aai-node-type", "service-instance")
91                                 .property("service-instance-id", "servInstance-9972-11-BandwidthMigration")
92                                 .property("operational-status", "activated")
93                                 .property("bandwidth-total", "bandwidth-total-11-BandwidthMigration")
94                                 .next();
95                 
96                 Vertex servSubDHV = g.addV()
97                                 .property("aai-node-type", "service-subscription")
98                                 .property("service-type", "DHV")
99                                 .next();
100                 
101                 Vertex servInstance4 = g.addV()
102                                 .property("aai-node-type", "service-instance")
103                                 .property("service-instance-id", "servInstance-9972-4-BandwidthMigration")
104                                 .property("operational-status", "activated")
105                                 .property("bandwidth-total", "bandwidth-total-4-BandwidthMigration")
106                                 .next();
107                 
108                 Vertex servInstance1 = g.addV()
109                                 .property("aai-node-type", "service-instance")
110                                 .property("service-instance-id", "ServInstance-9972-1-BandwidthMigration")
111                                 .property("operational-status", "activated")
112                                 .property("bandwidth-total", "2380")
113                                 .next();
114                 
115                 Vertex servInstance3 = g.addV()
116                                 .property("aai-node-type", "service-instance")
117                                 .property("service-instance-id", "servInstance-9972-3-BandwidthMigration")
118                                 .property("operational-status", "activated")
119                                 .property("bandwidth-total", "bandwidth-total-3-BandwidthMigration")
120                                 .next();
121
122                 Vertex servInstance2 = g.addV()
123                                 .property("aai-node-type", "service-instance")
124                                 .property("service-instance-id", "servInstance-9972-2-BandwidthMigration")
125                                 .property("operational-status", "activated")
126                                 .property("bandwidth-total", "bandwidth-total-2-BandwidthMigration")
127                                 .next();
128                 
129                 Vertex config1 = g.addV()
130                                 .property("aai-node-type", "configuration")
131                                 .property("configuration-id", "9972-config-LB1113")
132                                 .property("configuration-type", "DHV")
133                                 .property("tunnel-bandwidth", "12")
134                                 .next();
135                 
136                 Vertex config2 = g.addV()
137                                 .property("aai-node-type", "configuration")
138                                 .property("configuration-id", "9972-1config-LB1113")
139                                 .property("configuration-type", "configuration-type1-9972")
140                                 .next();
141                 
142                 Vertex allottedResource = g.addV()
143                                 .property("aai-node-type", "allotted-resource")
144                                 .property("id", "allResource-9972-BandwidthMigration")
145                                 .next();
146
147                 edgeSerializer.addTreeEdge(g, customer, servSubSDNEI);
148                 edgeSerializer.addTreeEdge(g, customer, servSubDHV);
149                 edgeSerializer.addTreeEdge(g, servSubSDNEI, servInstance22);
150                 edgeSerializer.addTreeEdge(g, servSubSDNEI, servInstance11);
151                 edgeSerializer.addTreeEdge(g, servSubDHV, servInstance4);
152                 edgeSerializer.addTreeEdge(g, servSubDHV, servInstance1);
153                 edgeSerializer.addTreeEdge(g, servSubDHV, servInstance3);
154                 edgeSerializer.addTreeEdge(g, servSubDHV, servInstance2);
155                 edgeSerializer.addEdge(g, servInstance1, allottedResource);
156                 edgeSerializer.addEdge(g, servInstance1, config1);
157                 edgeSerializer.addEdge(g, servInstance2, config2);
158
159                 TransactionalGraphEngine spy = spy(dbEngine);
160                 TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
161
162                 GraphTraversalSource traversal = g;
163                 GraphTraversalSource readOnly = tx.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()));
164                 when (spy.tx()).thenReturn(tx);
165                 when(spy.asAdmin()).thenReturn(adminSpy);
166                 when(adminSpy.getTraversalSource()).thenReturn(traversal);
167                 when(adminSpy.getReadOnlyTraversalSource()).thenReturn(readOnly);
168                 
169                 migration = new MigrateServiceInstanceToConfiguration(spy, loaderFactory, edgeIngestor, edgeSerializer, schemaVersions);
170                 migration.run();
171         }
172         
173         @After
174         public void cleanUp() {
175                 tx.tx().rollback();
176                 graph.close();
177         }
178         
179         @Test
180         public void testRun() throws Exception {
181                 // check if graph nodes exist
182                 assertEquals("customer node exists", true, 
183                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
184                                 .hasNext());
185
186                 assertEquals("service subscription node, service-type=SDN-ETHERNET-INTERNET", true, 
187                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
188                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "SDN-ETHERNET-INTERNET")
189                                 .hasNext());
190
191                 assertEquals("service instance node, bandwidth-total=bandwidth-total-22-BandwidthMigration", true, 
192                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
193                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "SDN-ETHERNET-INTERNET")
194                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-22-BandwidthMigration")
195                                 .has("bandwidth-total", "bandwidth-total-22-BandwidthMigration")
196                                 .hasNext());
197                 
198                 assertEquals("service instance node, bandwidth-total=bandwidth-total-11-BandwidthMigration", true, 
199                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
200                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "SDN-ETHERNET-INTERNET")
201                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-11-BandwidthMigration")
202                                 .has("bandwidth-total", "bandwidth-total-11-BandwidthMigration")
203                                 .hasNext());
204                 
205                 assertEquals("service subscription node, service-type=DHV", true, 
206                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
207                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
208                                 .hasNext());
209
210                 assertEquals("service instance node, bandwidth-total=servInstance-9972-4-BandwidthMigration", true, 
211                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
212                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
213                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-4-BandwidthMigration")
214                                 .has("bandwidth-total", "bandwidth-total-4-BandwidthMigration")
215                                 .hasNext());
216                 
217                 assertEquals("service instance node, bandwidth-total=ServInstance-9972-1-BandwidthMigration", true, 
218                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
219                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
220                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "ServInstance-9972-1-BandwidthMigration")
221                                 .has("bandwidth-total", "2380")
222                                 .hasNext());
223                 
224                 assertEquals("service instance node, bandwidth-total=servInstance-9972-3-BandwidthMigration", true, 
225                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
226                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
227                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-3-BandwidthMigration")
228                                 .has("bandwidth-total", "bandwidth-total-3-BandwidthMigration")
229                                 .hasNext());
230                 
231                 assertEquals("service instance node, bandwidth-total=servInstance-9972-2-BandwidthMigration", true, 
232                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
233                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
234                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-2-BandwidthMigration")
235                                 .has("bandwidth-total", "bandwidth-total-2-BandwidthMigration")
236                                 .hasNext());
237                 
238                 assertEquals("configuration node with type=configuration-type1-9972, tunnel-bandwidth does not exist", true, 
239                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
240                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
241                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-2-BandwidthMigration")
242                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
243                                 .has("configuration-type", "configuration-type1-9972")
244                                 .hasNext());
245                 
246                 // check if configuration node gets created for 2, 3, 4
247                 assertEquals("configuration node created with type=DHV, tunnel-bandwidth=servInstance-9972-4-BandwidthMigration", true, 
248                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
249                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
250                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-4-BandwidthMigration")
251                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
252                                 .has("configuration-type", "DHV").has("tunnel-bandwidth", "bandwidth-total-4-BandwidthMigration")
253                                 .hasNext());
254                 
255                 assertEquals("configuration node created with type=DHV, tunnel-bandwidth=servInstance-9972-3-BandwidthMigration", true, 
256                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
257                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
258                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-3-BandwidthMigration")
259                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
260                                 .has("configuration-type", "DHV").has("tunnel-bandwidth", "bandwidth-total-3-BandwidthMigration")
261                                 .hasNext());
262                 
263                 assertEquals("configuration node created with type=DHV, tunnel-bandwidth=servInstance-9972-2-BandwidthMigration", true, 
264                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
265                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
266                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "servInstance-9972-2-BandwidthMigration")
267                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
268                                 .has("configuration-type", "DHV").has("tunnel-bandwidth", "bandwidth-total-2-BandwidthMigration")
269                                 .hasNext());
270                 
271                 // configuration modified for ServInstance-9972-1-BandwidthMigration
272                 assertEquals("configuration node modified for ServInstance-9972-1-BandwidthMigration, tunnel-bandwidth=2380", true, 
273                                 g.V().has("global-customer-id", "customer-9972-BandwidthMigration")
274                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-type", "DHV")
275                                 .in("org.onap.relationships.inventory.BelongsTo").has("service-instance-id", "ServInstance-9972-1-BandwidthMigration")
276                                 .out("org.onap.relationships.inventory.Uses").has("aai-node-type", "configuration")
277                                 .has("configuration-type", "DHV").has("tunnel-bandwidth", "2380")
278                                 .hasNext());
279         }
280         
281         @Test
282         public void testGetAffectedNodeTypes() {
283                 Optional<String[]> types = migration.getAffectedNodeTypes();
284                 Optional<String[]> expected = Optional.of(new String[]{"service-instance"});
285                 
286                 assertNotNull(types);
287                 assertArrayEquals(expected.get(), types.get());
288         }
289
290         @Test
291         public void testGetMigrationName() {
292                 String migrationName = migration.getMigrationName();
293
294                 assertNotNull(migrationName);
295                 assertEquals("service-instance-to-configuration", migrationName);
296         }
297 }