1 package org.openecomp.sdc.be.model.jsontitan.operations;
3 import com.thinkaurelius.titan.core.TitanGraph;
4 import com.thinkaurelius.titan.core.TitanVertex;
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;
36 import javax.annotation.Resource;
37 import java.io.BufferedOutputStream;
39 import java.io.FileOutputStream;
40 import java.io.OutputStream;
43 import static org.junit.Assert.assertEquals;
44 import static org.junit.Assert.assertTrue;
46 @RunWith(SpringJUnit4ClassRunner.class)
47 @ContextConfiguration("classpath:application-context-test.xml")
48 public class InterfacesOperationTest extends ModelTestBase{
50 protected TitanDao titanDao;
52 private InterfaceOperation interfaceOperation;
55 protected TitanGraphClient titanGraphClient;
58 protected NodeTypeOperation nodeTypeOperation;
60 protected ToscaOperationFacade toscaOperationFacade;
63 protected TopologyTemplateOperation topologyTemplateOperation;
66 protected IElementOperation elementDao;
69 private ToscaElementLifecycleOperation lifecycleOperation;
71 protected static final String USER_ID = "jh0003";
72 protected static final String VF_NAME = "VF_NAME";
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";
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";
87 String categoryName = "category";
88 String subcategory = "mycategory";
89 String outputDirectory = "C:\\Output";
92 public static void initInterfacesOperation() {
96 private GraphVertex ownerVertex;
97 private GraphVertex modifierVertex;
98 private GraphVertex vfVertex;
99 private GraphVertex serviceVertex;
100 private GraphVertex rootVertex;
103 public void setupBefore() {
106 createResourceCategory();
107 createServiceCategory();
108 GraphTestUtils.createRootCatalogVertex(titanDao);
109 rootVertex = createRootNodeType();
110 createNodeType("firstVf");
111 serviceVertex = createTopologyTemplate("firstService");
115 public void cleanAfter() {
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());
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());
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());
155 private InterfaceDefinition buildInterfaceDefinition() {
156 InterfaceDefinition interfaceDefinition = new InterfaceDefinition();
157 interfaceDefinition.setType("tosca.interfaces.standard");
158 interfaceDefinition.setCreationDate(new Long(101232));
161 return interfaceDefinition;
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());
175 private InterfaceDefinition createInterface(String uniqueID, String description, String type, String toscaResourceName,
176 Map<String, Operation> op) {
177 InterfaceDefinition id = new InterfaceDefinition();
179 id.setDescription(description);
180 id.setUniqueId(uniqueID);
181 id.setToscaResourceName(toscaResourceName);
182 id.setOperationsMap(op);
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));
192 return interfaceDefinitionMap;
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);
207 private void createResourceCategory() {
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();
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();
231 Either<GraphVertex, TitanOperationStatus> catRes = titanDao.createVertex(cat);
233 Either<GraphVertex, TitanOperationStatus> subCatRes = titanDao.createVertex(subCat);
235 TitanOperationStatus status = titanDao.createEdge(catRes.left().value().getVertex(), subCatRes.left().value().getVertex(), EdgeLabelEnum.SUB_CATEGORY, new HashMap<>());
236 assertEquals(TitanOperationStatus.OK, status);
239 private void createServiceCategory() {
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();
252 Either<GraphVertex, TitanOperationStatus> catRes = titanDao.createVertex(cat);
254 assertTrue(catRes.isLeft());
257 private GraphVertex createTopologyTemplate(String name) {
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();
271 cat.setName(categoryName);
272 service.setCategories(categories);
274 service.setComponentType(ComponentTypeEnum.SERVICE);
275 Either<TopologyTemplate, StorageOperationStatus> createRes = topologyTemplateOperation.createTopologyTemplate(service);
276 assertTrue(createRes.isLeft());
278 Either<GraphVertex, TitanOperationStatus> getNodeTyeRes = titanDao.getVertexById(createRes.left().value().getUniqueId());
279 assertTrue(getNodeTyeRes.isLeft());
281 // serviceVertex = getNodeTyeRes.left().value();
283 return getNodeTyeRes.left().value();
286 private <T extends ToscaDataDefinition> NodeType createNodeType(String nodeTypeName) {
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();
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);
308 List<String> derivedFrom = new ArrayList<>();
309 derivedFrom.add("root");
310 vf.setDerivedFrom(derivedFrom);
311 vf.setComponentType(ComponentTypeEnum.RESOURCE);
313 Either<NodeType, StorageOperationStatus> createVFRes = nodeTypeOperation.createNodeType(vf);
314 assertTrue(createVFRes.isLeft());
316 Either<GraphVertex, TitanOperationStatus> getNodeTyeRes = titanDao.getVertexById(createVFRes.left().value().getUniqueId());
317 assertTrue(getNodeTyeRes.isLeft());
319 vfVertex = getNodeTyeRes.left().value();
321 List<PropertyDataDefinition> addProperties = new ArrayList<>();
322 PropertyDataDefinition prop11 = new PropertyDataDefinition();
323 prop11.setName("prop11");
324 prop11.setDefaultValue("def11");
326 addProperties.add(prop11);
328 PropertyDataDefinition prop22 = new PropertyDataDefinition();
329 prop22.setName("prop22");
330 prop22.setDefaultValue("def22");
331 addProperties.add(prop22);
333 StorageOperationStatus status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, addProperties, JsonPresentationFields.NAME);
334 assertTrue(status == StorageOperationStatus.OK);
336 PropertyDataDefinition prop33 = new PropertyDataDefinition();
337 prop33.setName("prop33");
338 prop33.setDefaultValue("def33");
340 status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex, EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop33, JsonPresentationFields.NAME);
341 assertTrue(status == StorageOperationStatus.OK);
343 PropertyDataDefinition prop44 = new PropertyDataDefinition();
344 prop44.setName("prop44");
345 prop44.setDefaultValue("def44");
347 status = nodeTypeOperation.addToscaDataToToscaElement(vfVertex.getUniqueId(), EdgeLabelEnum.PROPERTIES, VertexTypeEnum.PROPERTIES, prop44, JsonPresentationFields.NAME);
348 assertTrue(status == StorageOperationStatus.OK);
350 PropertyDataDefinition capProp = new PropertyDataDefinition();
351 capProp.setName("capProp");
352 capProp.setDefaultValue("capPropDef");
354 MapDataDefinition dataToCreate = new MapPropertiesDataDefinition();
355 dataToCreate.put("capProp", capProp);
357 Map<String, MapDataDefinition> capProps = new HashMap();
358 capProps.put("capName", dataToCreate);
360 Either<GraphVertex, StorageOperationStatus> res = nodeTypeOperation.associateElementToData(vfVertex, VertexTypeEnum.CAPABILITIES_PROPERTIES, EdgeLabelEnum.CAPABILITIES_PROPERTIES, capProps);
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);
369 private GraphVertex createRootNodeType() {
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);
384 List<CategoryDefinition> categories = new ArrayList<>();
385 CategoryDefinition cat = new CategoryDefinition();
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);
395 List<String> derivedFrom = new ArrayList<>();
396 vf.setDerivedFrom(derivedFrom);
398 Map<String, PropertyDataDefinition> properties = new HashMap<>();
399 PropertyDataDefinition prop1 = new PropertyDataDefinition();
400 prop1.setName("derived1");
401 prop1.setDefaultValue("deriveddef1");
403 properties.put("derived1", prop1);
405 PropertyDataDefinition prop2 = new PropertyDataDefinition();
406 prop2.setUniqueId("derived2");
407 prop2.setName("deriveddef2");
408 properties.put("derived2", prop2);
410 PropertyDataDefinition prop3 = new PropertyDataDefinition();
411 prop3.setName("derived3");
412 prop3.setDefaultValue("deriveddef3");
413 properties.put("derived3", prop3);
415 vf.setProperties(properties);
416 vf.setComponentType(ComponentTypeEnum.RESOURCE);
417 Either<NodeType, StorageOperationStatus> createVFRes = nodeTypeOperation.createNodeType(vf);
418 assertTrue(createVFRes.isLeft());
420 Either<GraphVertex, TitanOperationStatus> getNodeTyeRes = titanDao.getVertexById(createVFRes.left().value().getUniqueId());
421 assertTrue(getNodeTyeRes.isLeft());
422 return getNodeTyeRes.left().value();
425 private void createUsers() {
427 GraphVertex ownerV = new GraphVertex(VertexTypeEnum.USER);
428 ownerV.setUniqueId("user1");
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());
440 ownerVertex = createUserRes.left().value();
442 GraphVertex modifierV = new GraphVertex(VertexTypeEnum.USER);
443 modifierV.setUniqueId("user2");
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());
455 modifierVertex = createUserRes.left().value();
457 Either<GraphVertex, TitanOperationStatus> getOwnerRes = lifecycleOperation.findUser(ownerVertex.getUniqueId());
458 assertTrue(getOwnerRes.isLeft());
463 public void teardown() {
467 private void clearGraph() {
468 Either<TitanGraph, TitanOperationStatus> graphResult = titanDao.getGraph();
469 TitanGraph graph = graphResult.left().value();
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();