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