Catalog alignment
[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 org.apache.tinkerpop.gremlin.structure.Vertex;
24 import org.janusgraph.core.JanusGraph;
25 import org.janusgraph.core.JanusGraphFactory;
26 import org.janusgraph.core.JanusGraphVertex;
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 janusGraphFile, String beHost, String bePort, String adminUser) {
43                 log.debug("retrieving all products from graph");
44                 RestUtils restUtils = null;
45         List<String> productList = getAllProducts(janusGraphFile);
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 janusGraphFile) {
60                 JanusGraph graph = null;
61                 try {
62                         graph = openGraph(janusGraphFile);
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<JanusGraphVertex> 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             e.printStackTrace();
79             log.info("get All Products failed - {}" , e);
80                         if(graph != null) {
81                             graph.tx().rollback();
82                         }
83                         return null;
84
85                 } finally {
86                         if (graph != null) {
87                                 graph.close();
88                         }
89                 }
90         }
91
92         private JanusGraph openGraph(String janusGraphFileLocation) {
93
94                 return JanusGraphFactory.open(janusGraphFileLocation);
95                 
96         }
97
98 }