Sync Integ to Master
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / impl / ProductLogic.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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
21 package org.openecomp.sdc.asdctool.impl;
22
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.apache.tinkerpop.gremlin.structure.Vertex;
28 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
29 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.thinkaurelius.titan.core.TitanFactory;
34 import com.thinkaurelius.titan.core.TitanGraph;
35 import com.thinkaurelius.titan.core.TitanVertex;
36
37 /**
38  * Created by mlando on 2/23/2016.
39  */
40 public class ProductLogic {
41
42         private static Logger log = LoggerFactory.getLogger(ProductLogic.class.getName());
43
44         public boolean deleteAllProducts(String titanFile, String beHost, String bePort, String adminUser) {
45                 log.debug("retrieving all products from graph");
46                 RestUtils restUtils = null;
47         List<String> productList = getAllProducts(titanFile);
48         restUtils = new RestUtils();
49         if (productList != null) {
50             for (String productUid : productList) {
51                 restUtils.deleteProduct(productUid, beHost, bePort, adminUser);
52             }
53             return true;
54         }
55         else {
56             log.error("failed to get products from graph");
57             return false;
58         }
59         }
60
61         private List<String> getAllProducts(String titanFile) {
62                 TitanGraph graph = null;
63                 try {
64                         graph = openGraph(titanFile);
65                         List<String> productsToDelete = new ArrayList<String>();
66                         Iterable vertices = graph.query()
67                                         .has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.Product.getName()).vertices();
68                         if (vertices != null) {
69                                 Iterator<TitanVertex> iter = vertices.iterator();
70                                 while (iter.hasNext()) {
71                                         Vertex vertex = iter.next();
72                                         String id = vertex.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
73                                         productsToDelete.add(id);
74                                 }
75                         }
76
77                         graph.tx().commit();
78                         return productsToDelete;
79                 } catch (Exception e) {
80                         e.printStackTrace();
81                         graph.tx().rollback();
82                         return null;
83
84                 } finally {
85                         if (graph != null) {
86                                 graph.close();
87                         }
88                 }
89         }
90
91         private TitanGraph openGraph(String titanFileLocation) {
92
93                 TitanGraph graph = TitanFactory.open(titanFileLocation);
94
95                 return graph;
96
97         }
98
99 }