e68a6f4446f90e0220de529415997729f754db05
[appc.git] / appc-config / appc-encryption-tool / provider / src / main / java / org / onap / appc / encryptiontool / fqdn / ParseAdminArtifcat.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.encryptiontool.fqdn;
23
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.function.Consumer;
29
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;
42
43 public class ParseAdminArtifcat implements SvcLogicJavaPlugin {
44
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;
49
50     public static ParseAdminArtifcat initialise() {
51         parseArtifact = new ParseAdminArtifcat();
52         return parseArtifact;
53     }
54
55     public ParseAdminArtifcat() {
56         serviceLogic = new SqlResource();
57     }
58
59     protected ParseAdminArtifcat(SqlResource svcLogic) {
60         serviceLogic = svcLogic;
61     }
62
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) {
67
68             for (CloudOwnerList clList : listFqdn.getCloudOwnerList()) {
69
70                 for (RegionIdList rgList : clList.getRegionIdList()) {
71
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());
78                     }
79
80                 }
81
82             }
83
84         }
85
86         return mp;
87
88     }
89
90     public String retrieveFqdn(SvcLogicContext ctx)
91             throws SvcLogicException, RuntimeException, JsonProcessingException, IOException {
92         String tenantId = "";
93         String cloudOwner = "";
94         String cloudRegionId = "";
95         String jsonContent = getAdminArtifact(ctx);
96         if (jsonContent == null) {
97             throw new RuntimeException("Artifact content missing");
98         }
99
100         if (StringUtils.isNotBlank(ctx.getAttribute("payloadTenant"))) {
101             tenantId = ctx.getAttribute("payloadTenant");
102         } else {
103             tenantId = ctx.getAttribute("tenantAai");
104         }
105         if (StringUtils.isNotBlank(ctx.getAttribute("payloadCloudOwner"))) {
106             cloudOwner = ctx.getAttribute("payloadCloudOwner");
107         } else {
108             cloudOwner = ctx.getAttribute("cloudOwneraai");
109         }
110         if (StringUtils.isNotBlank(ctx.getAttribute("payloadCloudRegion"))) {
111             cloudRegionId = ctx.getAttribute("payloadCloudRegion");
112         } else {
113             cloudRegionId = ctx.getAttribute("cloudRegionAai");
114
115         }
116
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"));
123         try {
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);
131                     fqdn = mp.get(key);
132                 }
133             }
134             log.info(fqdn + "--> url and port retrieved ");
135         } catch (IOException e) {
136             log.error(e.getMessage());
137         }
138         return fqdn;
139     }
140
141     public String getAdminArtifact(SvcLogicContext ctx) {
142
143         QueryStatus status = null;
144         String fn = "ParseAdminArtifcat:getAdminArtifact";
145         String jsonContent = null;
146         String artifcatName = "ansible_admin_FQDN_Artifact_0.0.1V.json";
147         try {
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");
157             }
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);
163         }
164         return jsonContent;
165
166     }
167
168 }