re base code
[sdc.git] / asdctool / src / main / java / org / openecomp / sdc / asdctool / Utils.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;
22
23 import com.thinkaurelius.titan.core.TitanFactory;
24 import com.thinkaurelius.titan.core.TitanGraph;
25 import org.apache.commons.configuration.Configuration;
26 import org.apache.tinkerpop.gremlin.structure.Element;
27 import org.apache.tinkerpop.gremlin.structure.Property;
28 import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
29 import org.openecomp.sdc.common.log.wrappers.Logger;
30
31 import javax.ws.rs.core.Response;
32 import javax.ws.rs.core.Response.ResponseBuilder;
33 import java.util.HashMap;
34 import java.util.Map;
35 import java.util.Map.Entry;
36
37 public class Utils {
38
39         private static Logger log = Logger.getLogger(Utils.class.getName());
40
41         public final static String NEW_LINE = System.getProperty("line.separator");
42
43         public static Response buildOkResponse(
44                         /*
45                          * ResponseFormat errorResponseWrapper,
46                          */int status, Object entity, Map<String, String> additionalHeaders) {
47                 // int status = errorResponseWrapper.getStatus();
48                 ResponseBuilder responseBuilder = Response.status(status);
49                 if (entity != null) {
50                         log.trace("returned entity is {}", entity.toString());
51                         responseBuilder = responseBuilder.entity(entity);
52                 }
53                 if (additionalHeaders != null) {
54                         for (Entry<String, String> additionalHeader : additionalHeaders.entrySet()) {
55                                 String headerName = additionalHeader.getKey();
56                                 String headerValue = additionalHeader.getValue();
57                                 log.trace("Adding header {} with value {} to the response", headerName, headerValue);
58                                 responseBuilder.header(headerName, headerValue);
59                         }
60                 }
61                 return responseBuilder.build();
62         }
63
64         public static TitanGraph openGraph(Configuration conf) {
65
66                 TitanGraph graph = null;
67                 try {
68
69                         graph = TitanFactory.open(conf);
70
71                 } catch (Exception e) {
72                         log.error("Failed to start open graph", e);
73                 }
74
75                 return graph;
76
77         }
78
79         public static boolean vertexLeftContainsRightProps(Map<String, Object> leftProps, Map<String, Object> rightProps) {
80
81                 if (rightProps != null) {
82
83                         for (Entry<String, Object> entry : rightProps.entrySet()) {
84                                 String key = entry.getKey();
85                                 Object leftValue = leftProps.get(key);
86                                 Object rightValue = entry.getValue();
87                                 if (leftValue == null) {
88                                         if (rightValue == null) {
89                                                 continue;
90                                         } else {
91                                                 log.debug("The key {} cannot be found in the properties {}",key,leftProps);
92                                                 return false;
93                                         }
94                                 }
95
96                                 // if (false == leftValue instanceof Map && false == leftValue
97                                 // instanceof List) {
98                                 if (false == leftValue.equals(rightValue)) {
99                                         log.trace("The value of key {} is differnet between properties. {} vs {}",key,leftValue,rightValue);
100                                         return false;
101                                 }
102                                 // }
103                         }
104
105                 }
106
107                 return true;
108         }
109
110         public static void setProperties(Element element, Map<String, Object> properties) {
111
112                 if (properties != null && false == properties.isEmpty()) {
113
114                         Object[] propertyKeyValues = new Object[properties.size() * 2];
115                         int i = 0;
116                         for (Entry<String, Object> entry : properties.entrySet()) {
117                                 propertyKeyValues[i++] = entry.getKey();
118                                 propertyKeyValues[i++] = entry.getValue();
119                         }
120
121                         ElementHelper.attachProperties(element, propertyKeyValues);
122
123                 }
124
125         }
126
127         public static Map<String, Object> getProperties(Element element) {
128
129                 Map<String, Object> result = new HashMap<String, Object>();
130                 ;
131
132                 if (element.keys() != null && element.keys().size() > 0) {
133                         Map<String, Property> propertyMap = ElementHelper.propertyMap(element,
134                                         element.keys().toArray(new String[element.keys().size()]));
135
136                         for (Entry<String, Property> entry : propertyMap.entrySet()) {
137                                 String key = entry.getKey();
138                                 Object value = entry.getValue().value();
139
140                                 result.put(key, value);
141                         }
142                 }
143                 return result;
144         }
145 }