2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=========================================================
22 package org.onap.appc.encryptiontool.fqdn;
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.function.Consumer;
30 import org.apache.commons.lang.StringUtils;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
36 import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39 import com.fasterxml.jackson.core.JsonProcessingException;
40 import com.fasterxml.jackson.databind.DeserializationFeature;
41 import com.fasterxml.jackson.databind.ObjectMapper;
43 public class ParseAdminArtifcat implements SvcLogicJavaPlugin {
45 private static final EELFLogger log = EELFManager.getInstance().getLogger(ParseAdminArtifcat.class);
46 private static ParseAdminArtifcat parseArtifact = null;
47 private static String fqdn = null;
48 private SvcLogicResource serviceLogic;
50 public static ParseAdminArtifcat initialise() {
51 parseArtifact = new ParseAdminArtifcat();
55 public ParseAdminArtifcat() {
56 serviceLogic = new SqlResource();
59 protected ParseAdminArtifcat(SqlResource svcLogic) {
60 serviceLogic = svcLogic;
63 protected static Map<String, String> parseAdminArtifact(ArtifactMapper js) {
64 List<FqdnList> fqdnList = js.getFqdnList();
65 Map<String, String> mp = new HashMap<String, String>();
66 for (FqdnList listFqdn : fqdnList) {
68 for (CloudOwnerList clList : listFqdn.getCloudOwnerList()) {
70 for (RegionIdList rgList : clList.getRegionIdList()) {
72 for (String tenantId : rgList.getTenantIdList()) {
73 mp.put(clList.getCloudOwner().trim() + ";" + rgList.getRegionId().trim() + ";" + tenantId,
74 listFqdn.getVnfManagementServerFqdn());
75 log.info("list of mapped details from admin artifact" + clList.getCloudOwner().trim() + ";"
76 + rgList.getRegionId().trim() + ";" + tenantId + "-->"
77 + listFqdn.getVnfManagementServerFqdn());
90 public String retrieveFqdn(SvcLogicContext ctx)
91 throws SvcLogicException, RuntimeException, JsonProcessingException, IOException {
93 String cloudOwner = "";
94 String cloudRegionId = "";
95 String jsonContent = getAdminArtifact(ctx);
96 if (jsonContent == null) {
97 throw new RuntimeException("Artifact content missing");
100 if (StringUtils.isNotBlank(ctx.getAttribute("payloadTenant"))) {
101 tenantId = ctx.getAttribute("payloadTenant");
103 tenantId = ctx.getAttribute("tenantAai");
105 if (StringUtils.isNotBlank(ctx.getAttribute("payloadCloudOwner"))) {
106 cloudOwner = ctx.getAttribute("payloadCloudOwner");
108 cloudOwner = ctx.getAttribute("cloudOwneraai");
110 if (StringUtils.isNotBlank(ctx.getAttribute("payloadCloudRegion"))) {
111 cloudRegionId = ctx.getAttribute("payloadCloudRegion");
113 cloudRegionId = ctx.getAttribute("cloudRegionAai");
117 String key = cloudOwner.trim() + ";" + cloudRegionId.trim() + ";" + tenantId.trim();
118 log.info("cloudowner--cloudregion--tenantid retrieved constructed :" + key);
119 log.info("tenantid--cloudowner--cloudregion retrieved from payload :" + ctx.getAttribute("payloadTenant") + ":"
120 + ctx.getAttribute("payloadCloudOwner") + ":" + ctx.getAttribute("payloadCloudRegion"));
121 log.info("tenantid--cloudowner--cloudregion retrieved from a&ai : " + ctx.getAttribute("tenantAai") + ":"
122 + ctx.getAttribute("cloudOwneraai") + ":" + ctx.getAttribute("cloudRegionAai"));
124 ObjectMapper objectMapper = new ObjectMapper();
125 objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
126 ArtifactMapper js = objectMapper.readValue(jsonContent, ArtifactMapper.class);
127 Map<String, String> mp = parseAdminArtifact(js);
128 for (String t : mp.keySet()) {
129 if (t.contains(key)) {
130 log.info("Matching fqdn from admin artifact found for a&ai data :" + key);
134 log.info(fqdn + "--> url and port retrieved ");
135 } catch (IOException e) {
136 log.error(e.getMessage());
141 public String getAdminArtifact(SvcLogicContext ctx) {
143 QueryStatus status = null;
144 String fn = "ParseAdminArtifcat:getAdminArtifact";
145 String jsonContent = null;
146 String artifcatName = "ansible_admin_FQDN_Artifact_0.0.1V.json";
148 String query = "SELECT ARTIFACT_CONTENT as adminjson" + " FROM ASDC_ARTIFACTS " + "WHERE ARTIFACT_NAME = '"
149 + artifcatName + "' " + "ORDER BY INTERNAL_VERSION DESC LIMIT 1 ";
150 log.info("Getting artifact details :" + query);
151 status = serviceLogic.query("SQL", false, null, query, null, null, ctx);
152 jsonContent = ctx.getAttribute("adminjson");
153 log.info("adminjsonblock:" + jsonContent);
154 if (status == QueryStatus.FAILURE) {
155 log.info(fn + ": Error retrieving artifact details");
156 throw new SvcLogicException("Error retrieving artifact details");
158 } catch (Exception e) {
159 log.debug("Error while accessing database" + e.getMessage());
160 log.info("Error connecting to database" + e.getMessage());
161 log.error("Error accessing database", e);
162 throw new RuntimeException(e);