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