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