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