5ea528081c4a2853a5434b7ed5090a2a7f1b0104
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / synchronizer / GizmoEntitySummarizer.java
1 package org.onap.aai.sparky.synchronizer;
2
3 import static java.util.concurrent.CompletableFuture.supplyAsync;
4
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.Map;
9 import java.util.Map.Entry;
10 import java.util.TreeMap;
11 import java.util.concurrent.CountDownLatch;
12 import java.util.concurrent.ExecutorService;
13 import java.util.function.Supplier;
14
15 import org.onap.aai.cl.api.Logger;
16 import org.onap.aai.cl.eelf.LoggerFactory;
17 import org.onap.aai.restclient.client.OperationResult;
18 import org.onap.aai.restclient.enums.RestAuthenticationMode;
19 import org.onap.aai.sparky.config.oxm.OxmModelLoader;
20 import org.onap.aai.sparky.dal.GizmoAdapter;
21 import org.onap.aai.sparky.dal.exception.ElasticSearchOperationException;
22 import org.onap.aai.sparky.dal.rest.RestClientConstructionException;
23 import org.onap.aai.sparky.dal.rest.config.RestEndpointConfig;
24 import org.onap.aai.sparky.logging.AaiUiMsgs;
25 import org.onap.aai.sparky.util.NodeUtils;
26 import org.onap.aai.sparky.util.OxmModelAndProcessorHelper;
27
28 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.databind.node.ArrayNode;
31
32 public class GizmoEntitySummarizer {
33
34         protected ObjectMapper mapper;
35         protected OxmModelLoader oxmModelLoader;
36         private static final Logger logger = LoggerFactory.getInstance().getLogger(GizmoEntitySummarizer.class);
37         protected ExecutorService gizmoExecutor;
38         protected GizmoAdapter gizmoAdapter;
39         protected OxmModelAndProcessorHelper oxmHelper;
40
41         /*
42          * We need to add another concept to the OxmModelLoader which is to generate
43          * a list of entity containers from the OXM JaxbContext
44          */
45
46         public GizmoEntitySummarizer()
47                         throws ElasticSearchOperationException, IOException, RestClientConstructionException {
48
49                 OxmModelAndProcessorHelper.API_VERSION_OVERRIDE = 11;
50
51                 this.gizmoExecutor = NodeUtils.createNamedExecutor("GIZMO-WORKER", 5, logger);
52
53                 oxmHelper = OxmModelAndProcessorHelper.getInstance();
54                 this.oxmModelLoader = oxmHelper.getModelLoader();
55
56                 this.mapper = new ObjectMapper();
57
58                 RestEndpointConfig gizmoConfig = new RestEndpointConfig();
59
60                 gizmoConfig.setEndpointIpAddress("10.147.138.153");
61                 gizmoConfig.setEndpointServerPort("9520");
62                 gizmoConfig.setNumRequestRetries(5);
63                 gizmoConfig.setRestAuthenticationMode(RestAuthenticationMode.SSL_CERT);
64                 gizmoConfig.setConnectTimeoutInMs(60000);
65                 gizmoConfig.setReadTimeoutInMs(30000);
66                 gizmoConfig.setCertFileName("client-cert-onap.p12");
67                 gizmoConfig.setCertPassword("OBF:1y0q1uvc1uum1uvg1pil1pjl1uuq1uvk1uuu1y10");
68                 gizmoConfig.setTruststoreFileName("synchronizer.jks");
69                 gizmoConfig.setValidateServerCertChain(false);
70                 gizmoConfig.setValidateServerHostname(false);
71
72                 gizmoAdapter = new GizmoAdapter(oxmModelLoader, gizmoConfig);
73
74                 gizmoAdapter.setInventoryBasePath("/services/inventory/v12/");
75                 gizmoAdapter.setRelationshipsBasePath("/services/inventory/relationships/v12/");
76
77         }
78
79         private Map<String, Integer> getNumEntitiesPerType() {
80
81                 Collection<String> containerTypes = oxmHelper.getOxmEntityContainerLookup().getEntityContainers();
82                 Collection<String> links = new ArrayList<String>();
83                 Map<String, Integer> entityTypeCounts = new TreeMap<String, Integer>();
84
85                 final CountDownLatch latch = new CountDownLatch(containerTypes.size());
86
87                 for (String entityType : containerTypes) {
88
89                         supplyAsync(new Supplier<Void>() {
90
91                                 @Override
92                                 public Void get() {
93
94                                         OperationResult typeLinksResult = null;
95                                         try {
96                                                 typeLinksResult = gizmoAdapter.queryGizmoWithRetries(
97                                                                 gizmoAdapter.getFullInventoryUrl(entityType), "application/json", 1);
98
99                                                 if (typeLinksResult != null) {
100
101                                                         if (typeLinksResult.wasSuccessful() && typeLinksResult.getResult() != null) {
102
103                                                                 JsonNode rootNode = mapper.readValue(typeLinksResult.getResult(), JsonNode.class);
104
105                                                                 if (rootNode.isArray()) {
106                                                                         ArrayNode arrayNode = (ArrayNode) rootNode;
107                                                                         entityTypeCounts.put(entityType, new Integer(arrayNode.size()));
108                                                                 } else {
109                                                                         entityTypeCounts.put(entityType, new Integer(-1));
110                                                                 }
111
112                                                         } else {
113                                                                 // -1
114                                                                 entityTypeCounts.put(entityType, new Integer(-1));
115                                                         }
116
117                                                 }
118
119                                         } catch (Exception exc) {
120                                                 entityTypeCounts.put(entityType, new Integer(-1));
121                                         }
122
123                                         return null;
124                                 }
125
126                         }, gizmoExecutor).whenComplete((result, error) -> {
127
128                                 latch.countDown();
129
130                                 if (error != null) {
131                                         logger.error(AaiUiMsgs.ERROR_GENERIC,
132                                                         "An error occurred getting data from AAI. Error = " + error.getMessage());
133                                 }
134
135                         });
136
137                 }
138
139                 // System.out.println("self links size = " + selflinks.size());
140
141                 try {
142                         latch.await();
143                 } catch (InterruptedException e) {
144
145                 }
146
147                 return entityTypeCounts;
148         }
149
150         private Map<String, Integer> getNumRelationshipsPerType() {
151
152                 Map<String, Integer> entityTypeCounts = new TreeMap<String, Integer>();
153
154                 final CountDownLatch latch = new CountDownLatch(1);
155
156                 supplyAsync(new Supplier<Void>() {
157
158                         @Override
159                         public Void get() {
160
161                                 OperationResult typeLinksResult = null;
162                                 try {
163                                         typeLinksResult = gizmoAdapter.queryGizmoWithRetries(gizmoAdapter.getFullRelationshipUrl("has"),
164                                                         "application/json", 1);
165
166                                         if (typeLinksResult != null) {
167
168                                                 if (typeLinksResult.wasSuccessful() && typeLinksResult.getResult() != null) {
169
170                                                         JsonNode rootNode = mapper.readValue(typeLinksResult.getResult(), JsonNode.class);
171
172                                                         if (rootNode.isArray()) {
173                                                                 ArrayNode arrayNode = (ArrayNode) rootNode;
174                                                                 entityTypeCounts.put("has", new Integer(arrayNode.size()));
175                                                         } else {
176                                                                 entityTypeCounts.put("has", new Integer(-1));
177                                                         }
178
179                                                 } else {
180                                                         // -1
181                                                         entityTypeCounts.put("has", new Integer(-1));
182                                                 }
183
184                                         } else {
185                                                 entityTypeCounts.put("has", new Integer(-1));
186                                         }
187
188                                 } catch (Exception exc) {
189                                         entityTypeCounts.put("has", new Integer(-1));
190                                 }
191
192                                 return null;
193                         }
194
195                 }, gizmoExecutor).whenComplete((result, error) -> {
196
197                         latch.countDown();
198
199                         if (error != null) {
200                                 logger.error(AaiUiMsgs.ERROR_GENERIC,
201                                                 "An error occurred getting data from AAI. Error = " + error.getMessage());
202                         }
203
204                 });
205
206                 // System.out.println("self links size = " + selflinks.size());
207
208                 try {
209                         latch.await();
210                 } catch (InterruptedException e) {
211
212                 }
213
214                 return entityTypeCounts;
215         }
216
217         public void shutdown() {
218                 this.gizmoExecutor.shutdown();
219         }
220
221         public static void main(String[] args)
222                         throws ElasticSearchOperationException, IOException, RestClientConstructionException {
223
224                 System.setProperty("CONFIG_HOME", "X:\\2018_dev\\OSEAAI\\gizmo_integration\\onap_sparky-be\\appconfig-local\\");
225                 GizmoEntitySummarizer gizmoSummarizer = new GizmoEntitySummarizer();
226
227                 Map<String, Integer> entityCounts = gizmoSummarizer.getNumEntitiesPerType();
228                 Map<String, Integer> relationshipCounts = gizmoSummarizer.getNumRelationshipsPerType();
229                 gizmoSummarizer.shutdown();
230
231                 System.out.println("Gizmo Entities:");
232
233                 for (Entry<String, Integer> entry : entityCounts.entrySet()) {
234                         String key = entry.getKey();
235                         Integer value = entry.getValue();
236
237                         System.out.printf("\t%s : %d\n", key, value);
238                 }
239
240                 System.out.println("\nGizmo Relationships:");
241
242                 for (Entry<String, Integer> entry : relationshipCounts.entrySet()) {
243                         String key = entry.getKey();
244                         Integer value = entry.getValue();
245
246                         System.out.printf("\t%s : %d\n", key, value);
247                 }
248
249         }
250
251 }