re base code
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / jsontitan / operations / InterfacesOperationTest.java
1 package org.openecomp.sdc.be.model.jsontitan.operations;
2
3 import com.thinkaurelius.titan.core.TitanGraph;
4 import com.thinkaurelius.titan.core.TitanVertex;
5 import fj.data.Either;
6 import org.junit.*;
7 import org.junit.runner.RunWith;
8 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
9 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
10 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
11 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
12 import org.openecomp.sdc.be.dao.titan.TitanGraphClient;
13 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
14 import org.openecomp.sdc.be.datatypes.elements.MapDataDefinition;
15 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
16 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
17 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
18 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
19 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
20 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
21 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
22 import org.openecomp.sdc.be.model.*;
23 import org.openecomp.sdc.be.model.category.CategoryDefinition;
24 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
25 import org.openecomp.sdc.be.model.jsontitan.datamodel.NodeType;
26 import org.openecomp.sdc.be.model.jsontitan.datamodel.TopologyTemplate;
27 import org.openecomp.sdc.be.model.jsontitan.utils.GraphTestUtils;
28 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
29 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
30 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
31 import org.openecomp.sdc.common.util.ValidationUtils;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.test.context.ContextConfiguration;
34 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
35
36 import javax.annotation.Resource;
37 import java.io.BufferedOutputStream;
38 import java.io.File;
39 import java.io.FileOutputStream;
40 import java.io.OutputStream;
41 import java.util.*;
42
43 import static org.junit.Assert.assertEquals;
44 import static org.junit.Assert.assertTrue;
45
46 @RunWith(SpringJUnit4ClassRunner.class)
47 @ContextConfiguration("classpath:application-context-test.xml")
48 public class InterfacesOperationTest extends ModelTestBase{
49     @Resource
50     protected TitanDao titanDao;
51     @Resource
52     private InterfaceOperation interfaceOperation;
53
54     @Autowired
55     protected TitanGraphClient titanGraphClient;
56
57     @Resource
58     protected NodeTypeOperation nodeTypeOperation;
59     @Autowired
60     protected ToscaOperationFacade toscaOperationFacade;
61
62     @Resource
63     protected TopologyTemplateOperation topologyTemplateOperation;
64
65     @Autowired
66     protected IElementOperation elementDao;
67
68     @Resource
69     private ToscaElementLifecycleOperation lifecycleOperation;
70
71     protected static final String USER_ID = "jh0003";
72     protected static final String VF_NAME  = "VF_NAME";
73     protected User user;
74
75     public static final String RESOURCE_CATEGORY = "Network Layer 2-3";
76     public static final String RESOURCE_SUBCATEGORY = "Router";
77     public static final String RESOURCE_NAME = "Resource Name";
78
79     private CategoryDefinition categoryDefinition;
80     private SubCategoryDefinition subCategoryDefinition = new SubCategoryDefinition();
81     protected static final String RESOURCE_ID = "resourceID";
82     protected static final String WORKFLOW_OPERATION_ID = "workflowOperationId";
83     public static final String DERIVED_NAME = "derivedName";
84     public static final String CSAR_UUID = "bla bla";
85
86
87     String categoryName = "category";
88     String subcategory = "mycategory";
89     String outputDirectory = "C:\\Output";
90
91     @BeforeClass
92     public static void initInterfacesOperation() {
93         init();
94     }
95
96     private GraphVertex ownerVertex;
97     private GraphVertex modifierVertex;
98     private GraphVertex vfVertex;
99     private GraphVertex serviceVertex;
100     private GraphVertex rootVertex;
101
102     @Before
103     public void setupBefore() {
104         clearGraph();
105         createUsers();
106         createResourceCategory();
107         createServiceCategory();
108         GraphTestUtils.createRootCatalogVertex(titanDao);
109         rootVertex = createRootNodeType();
110         createNodeType("firstVf");
111         serviceVertex = createTopologyTemplate("firstService");
112     }
113
114     @After
115     public void cleanAfter() {
116         clearGraph();
117     }
118
119     @Test
120     public void testAddInterface() {
121         org.openecomp.sdc.be.model.Resource resource = createResource();
122         InterfaceDefinition interfaceDefinition = buildInterfaceDefinition();
123         Either<InterfaceDefinition, StorageOperationStatus> res = interfaceOperation.addInterface(resource.getUniqueId(),
124             interfaceDefinition);
125         Assert.assertTrue(res.isLeft());
126     }
127
128     @Test
129     public void testUpdateInterface() {
130         org.openecomp.sdc.be.model.Resource resource = createResource();
131         InterfaceDefinition interfaceDefinition = buildInterfaceDefinition();
132         interfaceDefinition.setOperationsMap(createMockOperationMap());
133         Either<InterfaceDefinition, StorageOperationStatus> res = interfaceOperation.addInterface(resource.getUniqueId(),
134             interfaceDefinition);
135         Assert.assertTrue(res.isLeft());
136         InterfaceDefinition value = res.left().value();
137         String new_description = "New Description";
138         value.setDescription(new_description);
139         res = interfaceOperation.updateInterface(resource.getUniqueId(),
140             interfaceDefinition);
141         assertTrue(res.isLeft());
142         assertEquals(new_description,res.left().value().getDescription());
143     }
144
145     @Test
146     public void testUpdateInterfaceShouldFailWhenNOtCreatedFirst() {
147         org.openecomp.sdc.be.model.Resource resource = createResource();
148         InterfaceDefinition interfaceDefinition = buildInterfaceDefinition();
149         interfaceDefinition.setOperationsMap(createMockOperationMap());
150         Either<InterfaceDefinition, StorageOperationStatus> res = interfaceOperation.updateInterface(resource.getUniqueId(),
151             interfaceDefinition);
152         Assert.assertTrue(res.isRight());
153     }
154
155     private InterfaceDefinition buildInterfaceDefinition() {
156         InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
157         interfaceDefinition.setType("tosca.interfaces.standard");
158         interfaceDefinition.setCreationDate(new Long(101232));
159
160
161         return interfaceDefinition;
162     }
163
164     private org.openecomp.sdc.be.model.Resource createResource() {
165         org.openecomp.sdc.be.model.Resource resource = new org.openecomp.sdc.be.model.Resource();
166         resource.setUniqueId(RESOURCE_ID);
167         resource.setName(RESOURCE_NAME);
168         resource.addCategory(RESOURCE_CATEGORY, RESOURCE_SUBCATEGORY);
169         resource.setDescription("My short description");
170         resource.setInterfaces(createMockInterfaceDefinition());
171         return resource;
172     }
173
174
175     private InterfaceDefinition createInterface(String uniqueID, String description, String type, String toscaResourceName,
176         Map<String, Operation> op) {
177         InterfaceDefinition id = new InterfaceDefinition();
178         id.setType(type);
179         id.setDescription(description);
180         id.setUniqueId(uniqueID);
181         id.setToscaResourceName(toscaResourceName);
182         id.setOperationsMap(op);
183         return id;
184     }
185
186     private  Map<String, InterfaceDefinition> createMockInterfaceDefinition() {
187         Map<String, Operation> operationMap = createMockOperationMap();
188         Map<String, InterfaceDefinition> interfaceDefinitionMap = new HashMap<>();
189         interfaceDefinitionMap.put("int1", createInterface("int1", "Interface 1",
190             "lifecycle", "tosca", operationMap));
191
192         return interfaceDefinitionMap;
193     }
194
195     private Map<String, Operation> createMockOperationMap() {
196         Operation operation = new Operation();
197         operation.setDefinition(false);
198         operation.setName("create");
199         Map<String, Operation> operationMap = new HashMap<>();
200         operationMap.put("op1", operation);
201         return operationMap;
202     }
203
204
205
206
207     private void createResourceCategory() {
208
209         GraphVertex cat = new GraphVertex(VertexTypeEnum.RESOURCE_CATEGORY);
210         Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>();
211         String catId = UniqueIdBuilder.buildComponentCategoryUid(categoryName, VertexTypeEnum.RESOURCE_CATEGORY);
212         cat.setUniqueId(catId);
213         metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, catId);
214         metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.RESOURCE_CATEGORY.getName());
215         metadataProperties.put(GraphPropertyEnum.NAME, categoryName);
216         metadataProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(categoryName));
217         cat.setMetadataProperties(metadataProperties);
218         cat.updateMetadataJsonWithCurrentMetadataProperties();
219
220         GraphVertex subCat = new GraphVertex(VertexTypeEnum.RESOURCE_SUBCATEGORY);
221         metadataProperties = new HashMap<>();
222         String subCatId = UniqueIdBuilder.buildSubCategoryUid(cat.getUniqueId(), subcategory);
223         subCat.setUniqueId(subCatId);
224         metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, subCatId);
225         metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.RESOURCE_SUBCATEGORY.getName());
226         metadataProperties.put(GraphPropertyEnum.NAME, subcategory);
227         metadataProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(subcategory));
228         subCat.setMetadataProperties(metadataProperties);
229         subCat.updateMetadataJsonWithCurrentMetadataProperties();
230
231         Either<GraphVertex, TitanOperationStatus> catRes = titanDao.createVertex(cat);
232
233         Either<GraphVertex, TitanOperationStatus> subCatRes = titanDao.createVertex(subCat);
234
235         TitanOperationStatus status = titanDao.createEdge(catRes.left().value().getVertex(), subCatRes.left().value().getVertex(), EdgeLabelEnum.SUB_CATEGORY, new HashMap<>());
236         assertEquals(TitanOperationStatus.OK, status);
237     }
238
239     private void createServiceCategory() {
240
241         GraphVertex cat = new GraphVertex(VertexTypeEnum.SERVICE_CATEGORY);
242         Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>();
243         String catId = UniqueIdBuilder.buildComponentCategoryUid(categoryName, VertexTypeEnum.SERVICE_CATEGORY);
244         cat.setUniqueId(catId);
245         metadataProperties.put(GraphPropertyEnum.UNIQUE_ID, catId);
246         metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.SERVICE_CATEGORY.getName());
247         metadataProperties.put(GraphPropertyEnum.NAME, categoryName);
248         metadataProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normalizeCategoryName4Uniqueness(categoryName));
249         cat.setMetadataProperties(metadataProperties);
250         cat.updateMetadataJsonWithCurrentMetadataProperties();
251
252         Either<GraphVertex, TitanOperationStatus> catRes = titanDao.createVertex(cat);
253
254         assertTrue(catRes.isLeft());
255     }
256
257     private GraphVertex createTopologyTemplate(String name) {
258
259         TopologyTemplate service = new TopologyTemplate();
260         String uniqueId = UniqueIdBuilder.buildResourceUniqueId();
261         service.setUniqueId(uniqueId);
262         service.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID));
263         service.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), name);
264         service.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
265         service.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "0.1");
266         service.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VF.name());
267         service.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), ComponentTypeEnum.RESOURCE);
268         List<CategoryDefinition> categories = new ArrayList<>();
269         CategoryDefinition cat = new CategoryDefinition();
270         categories.add(cat);
271         cat.setName(categoryName);
272         service.setCategories(categories);
273
274         service.setComponentType(ComponentTypeEnum.SERVICE);
275         Either<TopologyTemplate, StorageOperationStatus> createRes = topologyTemplateOperation.createTopologyTemplate(service);
276         assertTrue(createRes.isLeft());
277
278         Either<GraphVertex, TitanOperationStatus> getNodeTyeRes = titanDao.getVertexById(createRes.left().value().getUniqueId());
279         assertTrue(getNodeTyeRes.isLeft());
280
281         // serviceVertex = getNodeTyeRes.left().value();
282
283         return getNodeTyeRes.left().value();
284     }
285
286     private <T extends ToscaDataDefinition> NodeType createNodeType(String nodeTypeName) {
287
288         NodeType vf = new NodeType();
289         String uniqueId =  RESOURCE_ID; // UniqueIdBuilder.buildResourceUniqueId();
290         vf.setUniqueId(uniqueId);
291         vf.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID));
292         vf.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), nodeTypeName);
293         vf.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
294         vf.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "0.1");
295         vf.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VF.name());
296         vf.getMetadata().put(JsonPresentationFields.COMPONENT_TYPE.getPresentation(), ComponentTypeEnum.RESOURCE);
297         List<CategoryDefinition> categories = new ArrayList<>();
298         CategoryDefinition cat = new CategoryDefinition();
299         categories.add(cat);
300         cat.setName(categoryName);
301         List<SubCategoryDefinition> subCategories = new ArrayList<>();
302         SubCategoryDefinition subCat = new SubCategoryDefinition();
303         subCat.setName(subcategory);
304         subCategories.add(subCat);
305         cat.setSubcategories(subCategories);
306         vf.setCategories(categories);
307
308         List<String> derivedFrom = new ArrayList<>();
309         derivedFrom.add("root");
310         vf.setDerivedFrom(derivedFrom);
311         vf.setComponentType(ComponentTypeEnum.RESOURCE);
312
313         Either<NodeType, StorageOperationStatus> createVFRes = nodeTypeOperation.createNodeType(vf);
314         assertTrue(createVFRes.isLeft());
315
316         Either<GraphVertex, TitanOperationStatus> getNodeTyeRes = titanDao.getVertexById(createVFRes.left().value().getUniqueId());
317         assertTrue(getNodeTyeRes.isLeft());
318
319         vfVertex = getNodeTyeRes.left().value();
320
321         List<PropertyDataDefinition> addProperties = new ArrayList<>();
322         PropertyDataDefinition prop11 = new PropertyDataDefinition();
323         prop11.setName("prop11");
324         prop11.setDefaultValue("def11");
325
326         addProperties.add(prop11);
327
328         PropertyDataDefinition prop22 = new PropertyDataDefinition();
329         prop22.setName("prop22");
330         prop22.setDefaultValue("def22");
331         addProperties.add(prop22);
332
333         StorageOperationStatus status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, addProperties, JsonPresentationFields.NAME);
334         assertTrue(status == StorageOperationStatus.OK);
335
336         PropertyDataDefinition prop33 = new PropertyDataDefinition();
337         prop33.setName("prop33");
338         prop33.setDefaultValue("def33");
339
340         status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop33, JsonPresentationFields.NAME);
341         assertTrue(status == StorageOperationStatus.OK);
342
343         PropertyDataDefinition prop44 = new PropertyDataDefinition();
344         prop44.setName("prop44");
345         prop44.setDefaultValue("def44");
346
347         status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop44, JsonPresentationFields.NAME);
348         assertTrue(status == StorageOperationStatus.OK);
349
350         PropertyDataDefinition capProp = new PropertyDataDefinition();
351         capProp.setName("capProp");
352         capProp.setDefaultValue("capPropDef");
353
354         MapDataDefinition dataToCreate = new MapPropertiesDataDefinition();
355         dataToCreate.put("capProp", capProp);
356
357         Map<String, MapDataDefinition> capProps = new HashMap();
358         capProps.put("capName", dataToCreate);
359
360         Either<GraphVertex, StorageOperationStatus> res = nodeTypeOperation.associateElementToData(vfVertex, VertexTypeEnum.CAPABILITIES_PROPERTIES, EdgeLabelEnum.CAPABILITIES_PROPERTIES, capProps);
361
362         List<String> pathKeys = new ArrayList<>();
363         pathKeys.add("capName");
364         capProp.setDefaultValue("BBBB");
365         status = nodeTypeOperation.updateToscaDataDeepElementOfToscaElement(vfVertex, EdgeLabelEnum.CAPABILITIES_PROPERTIES, VertexTypeEnum.CAPABILITIES_PROPERTIES, capProp, pathKeys, JsonPresentationFields.NAME);
366         return vf;
367     }
368
369     private GraphVertex createRootNodeType() {
370
371         NodeType vf = new NodeType();
372         String uniqueId = UniqueIdBuilder.buildResourceUniqueId();
373         vf.setUniqueId(uniqueId);
374         vf.setComponentType(ComponentTypeEnum.RESOURCE);
375         vf.setCreatorUserId((String) ownerVertex.getMetadataProperty(GraphPropertyEnum.USERID));
376         vf.getMetadata().put(JsonPresentationFields.NAME.getPresentation(), "root");
377         vf.getMetadata().put(JsonPresentationFields.UNIQUE_ID.getPresentation(), uniqueId);
378         vf.getMetadata().put(JsonPresentationFields.VERSION.getPresentation(), "1.0");
379         vf.getMetadata().put(JsonPresentationFields.TYPE.getPresentation(), ResourceTypeEnum.VFC.name());
380         vf.getMetadata().put(JsonPresentationFields.LIFECYCLE_STATE.getPresentation(), LifecycleStateEnum.CERTIFIED.name());
381         vf.getMetadata().put(JsonPresentationFields.TOSCA_RESOURCE_NAME.getPresentation(), "root");
382         vf.getMetadata().put(JsonPresentationFields.HIGHEST_VERSION.getPresentation(), true);
383
384         List<CategoryDefinition> categories = new ArrayList<>();
385         CategoryDefinition cat = new CategoryDefinition();
386         categories.add(cat);
387         cat.setName(categoryName);
388         List<SubCategoryDefinition> subCategories = new ArrayList<>();
389         SubCategoryDefinition subCat = new SubCategoryDefinition();
390         subCat.setName(subcategory);
391         subCategories.add(subCat);
392         cat.setSubcategories(subCategories);
393         vf.setCategories(categories);
394
395         List<String> derivedFrom = new ArrayList<>();
396         vf.setDerivedFrom(derivedFrom);
397
398         Map<String, PropertyDataDefinition> properties = new HashMap<>();
399         PropertyDataDefinition prop1 = new PropertyDataDefinition();
400         prop1.setName("derived1");
401         prop1.setDefaultValue("deriveddef1");
402
403         properties.put("derived1", prop1);
404
405         PropertyDataDefinition prop2 = new PropertyDataDefinition();
406         prop2.setUniqueId("derived2");
407         prop2.setName("deriveddef2");
408         properties.put("derived2", prop2);
409
410         PropertyDataDefinition prop3 = new PropertyDataDefinition();
411         prop3.setName("derived3");
412         prop3.setDefaultValue("deriveddef3");
413         properties.put("derived3", prop3);
414
415         vf.setProperties(properties);
416         vf.setComponentType(ComponentTypeEnum.RESOURCE);
417         Either<NodeType, StorageOperationStatus> createVFRes = nodeTypeOperation.createNodeType(vf);
418         assertTrue(createVFRes.isLeft());
419
420         Either<GraphVertex, TitanOperationStatus> getNodeTyeRes = titanDao.getVertexById(createVFRes.left().value().getUniqueId());
421         assertTrue(getNodeTyeRes.isLeft());
422         return getNodeTyeRes.left().value();
423     }
424
425     private void createUsers() {
426
427         GraphVertex ownerV = new GraphVertex(VertexTypeEnum.USER);
428         ownerV.setUniqueId("user1");
429
430         Map<GraphPropertyEnum, Object> metadataProperties = new HashMap<>();
431         metadataProperties.put(GraphPropertyEnum.USERID, ownerV.getUniqueId());
432         metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.USER.getName());
433         metadataProperties.put(GraphPropertyEnum.NAME, "user1");
434         ownerV.setMetadataProperties(metadataProperties);
435         ownerV.updateMetadataJsonWithCurrentMetadataProperties();
436         ownerV.setJson(new HashMap<>());
437         Either<GraphVertex, TitanOperationStatus> createUserRes = titanDao.createVertex(ownerV);
438         assertTrue(createUserRes.isLeft());
439
440         ownerVertex = createUserRes.left().value();
441
442         GraphVertex modifierV = new GraphVertex(VertexTypeEnum.USER);
443         modifierV.setUniqueId("user2");
444
445         metadataProperties = new HashMap<>();
446         metadataProperties.put(GraphPropertyEnum.USERID, modifierV.getUniqueId());
447         metadataProperties.put(GraphPropertyEnum.LABEL, VertexTypeEnum.USER.getName());
448         metadataProperties.put(GraphPropertyEnum.NAME, "user2");
449         modifierV.setMetadataProperties(metadataProperties);
450         modifierV.updateMetadataJsonWithCurrentMetadataProperties();
451         modifierV.setJson(new HashMap<>());
452         createUserRes = titanDao.createVertex(modifierV);
453         assertTrue(createUserRes.isLeft());
454
455         modifierVertex = createUserRes.left().value();
456
457         Either<GraphVertex, TitanOperationStatus> getOwnerRes = lifecycleOperation.findUser(ownerVertex.getUniqueId());
458         assertTrue(getOwnerRes.isLeft());
459
460     }
461
462     @After
463     public void teardown() {
464         clearGraph();
465     }
466
467     private void clearGraph() {
468         Either<TitanGraph, TitanOperationStatus> graphResult = titanDao.getGraph();
469         TitanGraph graph = graphResult.left().value();
470
471         Iterable<TitanVertex> vertices = graph.query().vertices();
472         if (vertices != null) {
473             Iterator<TitanVertex> iterator = vertices.iterator();
474             while (iterator.hasNext()) {
475                 TitanVertex vertex = iterator.next();
476                 vertex.remove();
477             }
478         }
479         titanDao.commit();
480     }
481
482 }