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