2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.aai.sparky.synchronizer;
24 import static java.util.concurrent.CompletableFuture.supplyAsync;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.Collection;
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;
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.setup.SchemaVersion;
41 import org.onap.aai.sparky.config.oxm.OxmModelLoader;
42 import org.onap.aai.sparky.dal.GizmoAdapter;
43 import org.onap.aai.sparky.dal.exception.ElasticSearchOperationException;
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;
50 import com.fasterxml.jackson.databind.JsonNode;
51 import com.fasterxml.jackson.databind.ObjectMapper;
52 import com.fasterxml.jackson.databind.node.ArrayNode;
54 public class GizmoEntitySummarizer {
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;
64 * We need to add another concept to the OxmModelLoader which is to generate
65 * a list of entity containers from the OXM JaxbContext
68 public GizmoEntitySummarizer()
69 throws ElasticSearchOperationException, IOException, RestClientConstructionException {
71 OxmModelAndProcessorHelper.API_VERSION_OVERRIDE = "v11";
73 this.gizmoExecutor = NodeUtils.createNamedExecutor("GIZMO-WORKER", 5, logger);
75 oxmHelper = OxmModelAndProcessorHelper.getInstance();
76 this.oxmModelLoader = oxmHelper.getModelLoader();
78 this.mapper = new ObjectMapper();
80 RestEndpointConfig gizmoConfig = new RestEndpointConfig();
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);
94 gizmoAdapter = new GizmoAdapter(oxmModelLoader, gizmoConfig);
95 gizmoAdapter.setInventoryBasePath("/services/inventory/v12/");
96 gizmoAdapter.setRelationshipsBasePath("/services/inventory/relationships/v12/");
100 private Map<String, Integer> getNumEntitiesPerType() {
102 Collection<String> containerTypes = oxmHelper.getOxmEntityContainerLookup().getEntityContainers();
103 Collection<String> links = new ArrayList<String>();
104 Map<String, Integer> entityTypeCounts = new TreeMap<String, Integer>();
106 final CountDownLatch latch = new CountDownLatch(containerTypes.size());
108 for (String entityType : containerTypes) {
110 supplyAsync(new Supplier<Void>() {
115 OperationResult typeLinksResult = null;
117 typeLinksResult = gizmoAdapter.queryGizmoWithRetries(
118 gizmoAdapter.getFullInventoryUrl(entityType), "application/json", 1);
120 if (typeLinksResult != null) {
122 if (typeLinksResult.wasSuccessful() && typeLinksResult.getResult() != null) {
124 JsonNode rootNode = mapper.readValue(typeLinksResult.getResult(), JsonNode.class);
126 if (rootNode.isArray()) {
127 ArrayNode arrayNode = (ArrayNode) rootNode;
128 entityTypeCounts.put(entityType, new Integer(arrayNode.size()));
130 entityTypeCounts.put(entityType, new Integer(-1));
135 entityTypeCounts.put(entityType, new Integer(-1));
140 } catch (Exception exc) {
141 entityTypeCounts.put(entityType, new Integer(-1));
147 }, gizmoExecutor).whenComplete((result, error) -> {
152 logger.error(AaiUiMsgs.ERROR_GENERIC,
153 "An error occurred getting data from AAI. Error = " + error.getMessage());
160 // System.out.println("self links size = " + selflinks.size());
164 } catch (InterruptedException e) {
168 return entityTypeCounts;
171 private Map<String, Integer> getNumRelationshipsPerType() {
173 Map<String, Integer> entityTypeCounts = new TreeMap<String, Integer>();
175 final CountDownLatch latch = new CountDownLatch(1);
177 supplyAsync(new Supplier<Void>() {
182 OperationResult typeLinksResult = null;
184 typeLinksResult = gizmoAdapter.queryGizmoWithRetries(gizmoAdapter.getFullRelationshipUrl("has"),
185 "application/json", 1);
187 if (typeLinksResult != null) {
189 if (typeLinksResult.wasSuccessful() && typeLinksResult.getResult() != null) {
191 JsonNode rootNode = mapper.readValue(typeLinksResult.getResult(), JsonNode.class);
193 if (rootNode.isArray()) {
194 ArrayNode arrayNode = (ArrayNode) rootNode;
195 entityTypeCounts.put("has", new Integer(arrayNode.size()));
197 entityTypeCounts.put("has", new Integer(-1));
202 entityTypeCounts.put("has", new Integer(-1));
206 entityTypeCounts.put("has", new Integer(-1));
209 } catch (Exception exc) {
210 entityTypeCounts.put("has", new Integer(-1));
216 }, gizmoExecutor).whenComplete((result, error) -> {
221 logger.error(AaiUiMsgs.ERROR_GENERIC,
222 "An error occurred getting data from AAI. Error = " + error.getMessage());
227 // System.out.println("self links size = " + selflinks.size());
231 } catch (InterruptedException e) {
235 return entityTypeCounts;
238 public void shutdown() {
239 this.gizmoExecutor.shutdown();
242 public static void main(String[] args)
243 throws ElasticSearchOperationException, IOException, RestClientConstructionException {
245 System.setProperty("CONFIG_HOME", "X:\\2018_dev\\OSEAAI\\gizmo_integration\\onap_sparky-be\\appconfig-local\\");
246 GizmoEntitySummarizer gizmoSummarizer = new GizmoEntitySummarizer();
248 Map<String, Integer> entityCounts = gizmoSummarizer.getNumEntitiesPerType();
249 Map<String, Integer> relationshipCounts = gizmoSummarizer.getNumRelationshipsPerType();
250 gizmoSummarizer.shutdown();
252 System.out.println("Gizmo Entities:");
254 for (Entry<String, Integer> entry : entityCounts.entrySet()) {
255 String key = entry.getKey();
256 Integer value = entry.getValue();
258 System.out.printf("\t%s : %d\n", key, value);
261 System.out.println("\nGizmo Relationships:");
263 for (Entry<String, Integer> entry : relationshipCounts.entrySet()) {
264 String key = entry.getKey();
265 Integer value = entry.getValue();
267 System.out.printf("\t%s : %d\n", key, value);