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