2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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.ccsdk.sli.northbound.uebclient;
24 import java.io.IOException;
25 import java.sql.SQLException;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.LinkedHashMap;
29 import java.util.List;
32 import javax.sql.rowset.CachedRowSet;
34 import org.onap.sdc.tosca.parser.api.IEntityDetails;
35 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
36 import org.onap.sdc.tosca.parser.elements.queries.EntityQuery;
37 import org.onap.sdc.tosca.parser.elements.queries.TopologyTemplateQuery;
38 import org.onap.sdc.tosca.parser.enums.SdcTypes;
39 import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
40 import org.onap.sdc.toscaparser.api.CapabilityAssignment;
41 import org.onap.sdc.toscaparser.api.CapabilityAssignments;
42 import org.onap.sdc.toscaparser.api.Property;
43 import org.onap.sdc.toscaparser.api.elements.Metadata;
44 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 public class SdncBaseModel {
50 private static final Logger LOG = LoggerFactory
51 .getLogger(SdncBaseModel.class);
53 protected String customizationUUID = null;
54 protected String invariantUUID = null;
55 protected String UUID = null;
56 protected String model_yaml = null;
57 protected String version = null;
58 protected String name = null;
60 protected String PARAM_INVARIANT_UUID_KEY = "invariant_uuid";
61 protected String PARAM_CUSTOMIZATION_UUID_KEY = "customization_uuid";
62 protected String PARAM_UUID_KEY = "uuid";
63 protected String PARAM_VERSION_KEY = "version";
64 protected String PARAM_NAME_KEY = "name";
65 protected String PARAM_DESCRIPTION_KEY = "description";
66 protected String PARAM_TYPE_KEY = "type";
67 protected String PARAM_CATEGORY_KEY = "category";
69 protected Map<String, String> params = null;
70 protected Map<String, String> attributeValueParams = null;
71 protected ISdcCsarHelper sdcCsarHelper = null;
72 protected static DBResourceManager jdbcDataSource = null;
73 protected static SdncUebConfiguration config = null;
74 protected IEntityDetails entityDetails = null;
77 public SdncBaseModel(ISdcCsarHelper sdcCsarHelper, IEntityDetails entityDetails, DBResourceManager jdbcDataSource, SdncUebConfiguration config) throws IOException {
78 this (sdcCsarHelper, entityDetails);
79 this.sdcCsarHelper = sdcCsarHelper;
80 this.entityDetails = entityDetails;
81 SdncBaseModel.jdbcDataSource = jdbcDataSource;
82 SdncBaseModel.config = config;
85 public SdncBaseModel(ISdcCsarHelper sdcCsarHelper, Metadata metadata, DBResourceManager jdbcDataSource) {
87 params = new HashMap<String, String>();
88 this.sdcCsarHelper = sdcCsarHelper;
89 SdncBaseModel.jdbcDataSource = jdbcDataSource;
91 // extract service metadata
92 invariantUUID = extractValue(metadata, SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID);
93 addParameter(PARAM_INVARIANT_UUID_KEY,invariantUUID);
94 addParameter(PARAM_VERSION_KEY,extractValue(metadata, SdcPropertyNames.PROPERTY_NAME_VERSION));
95 name = extractValue(metadata, SdcPropertyNames.PROPERTY_NAME_NAME);
96 addParameter(PARAM_NAME_KEY,name);
97 addParameter(PARAM_DESCRIPTION_KEY,extractValue(metadata, SdcPropertyNames.PROPERTY_NAME_DESCRIPTION));
98 addParameter(PARAM_TYPE_KEY,extractValue(metadata, SdcPropertyNames.PROPERTY_NAME_TYPE));
99 addParameter(PARAM_CATEGORY_KEY,extractValue(metadata, SdcPropertyNames.PROPERTY_NAME_CATEGORY));
102 public SdncBaseModel(ISdcCsarHelper sdcCsarHelper, IEntityDetails entityDetails) {
104 params = new HashMap<String, String>();
105 attributeValueParams = new HashMap<String, String>();
106 this.sdcCsarHelper = sdcCsarHelper;
107 this.entityDetails = entityDetails;
109 // extract common nodeTemplate metadata
110 Metadata metadata = entityDetails.getMetadata();
111 customizationUUID = extractValue (metadata, SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID);
112 invariantUUID = extractValue (metadata, SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID);
113 addParameter(PARAM_INVARIANT_UUID_KEY, invariantUUID);
114 UUID = extractValue (metadata, SdcPropertyNames.PROPERTY_NAME_UUID);
115 addParameter(PARAM_UUID_KEY, UUID);
116 addParameter(PARAM_VERSION_KEY, extractValue (metadata, SdcPropertyNames.PROPERTY_NAME_VERSION));
120 /* This is the generic approach Shoujit attempted for 18.06 but can't be implemented without parser API to
121 * get properties with substring match on the name
122 * protected void extractRelevantAttributeData(List<Property> propList, SdncUebConfiguration config) {
124 //List<Property> propList = nodeTemplate.getPropertiesObjects();
125 for (Property prop : propList) {
126 String propName = prop.getName();
127 Object propValue = prop.getValue();
129 if (propValue instanceof Map)
131 LOG.info("Property: propertyName: " + propName + " propertyValue: " + propValue.toString());
133 // Compare this property name with each config.relevant-attribute-name
134 List<String> attributeNames = config.getRelevantAttributeNames();
135 for (String attributeName : attributeNames) {
136 if (prop.getName().contains(attributeName))
137 addParameter(prop.getName(), prop.getValue().toString(), attributeValueParams);
144 protected void insertRelevantAttributeData() throws IOException{
146 insertRelevantAttributeData("");
149 protected void insertRelevantAttributeData(String type) throws IOException{
151 // type can be passed as "group" or taken from the nodeTemplate
152 String metadataType = "";
153 if (!type.isEmpty()) metadataType = type;
155 Metadata metadata = entityDetails.getMetadata();
156 metadataType = extractValue (metadata, PARAM_TYPE_KEY);
159 // Clean up all attributes for this resource
161 cleanUpExistingToscaData("ATTRIBUTE_VALUE_PAIR", "resource_uuid", getUUID(), "resource_type", "\"" + metadataType + "\"");
162 } catch (IOException e) {
163 LOG.error("Could not cleanup Tosca CSAR data from the ATTRIBUTE_VALUE_PAIR table");
164 throw new IOException (e);
167 for (String paramName : attributeValueParams.keySet()) {
168 String paramValue = attributeValueParams.get(paramName);
170 Map<String, String> attributeParams = new HashMap<String, String>();
171 addParameter("attribute_name", paramName, attributeParams);
172 addParameter("attribute_value", paramValue, attributeParams);
173 addParameter("resource_type", metadataType, attributeParams);
174 addParameter("resource_customization_uuid", getCustomizationUUID(), attributeParams);
176 LOG.info("Call insertToscaData for ATTRIBUTE_VALUE_PAIR where resource_uuid = " + getUUID() + " and attribute_name = \"" + paramName + "\"");
178 insertToscaData(buildSql("ATTRIBUTE_VALUE_PAIR", "resource_uuid", getUUID(), model_yaml, attributeParams), null);
179 } catch (IOException e) {
180 LOG.error("Could not insert Tosca CSAR data into the ATTRIBUTE_VALUE_PAIR table");
181 throw new IOException (e);
186 protected void insertEntityGroupData (IEntityDetails entityDetails, IEntityDetails targetNode, String groupType) throws IOException {
188 // Get the Groups on a node - Convert to use getEntity in 19.08
189 EntityQuery entityQuery = EntityQuery.newBuilder(groupType).build();
190 String customizationUuid = getCustomizationUUIDNoQuotes();
191 SdcTypes entitySdcType = SdcTypes.valueOf(extractValue(entityDetails.getMetadata(), SdcPropertyNames.PROPERTY_NAME_TYPE));
192 TopologyTemplateQuery topologyTemplateQuery = TopologyTemplateQuery.newBuilder(entitySdcType)
193 .customizationUUID(customizationUuid).build();
194 List<IEntityDetails> groupList = sdcCsarHelper.getEntity(entityQuery, topologyTemplateQuery, false);
195 if (groupList == null) {
199 for (IEntityDetails group : groupList){
201 // Insert into RESOURCE_GROUP/ATTRIBUTE_VALUE_PAIR and RESOURCE_GROUP_TO_TARGET_NODE_MAPPING
202 // RESOURCE_GROUP (group metadata): resource_uuid (CR node UUID), uuid, customization_uuid, invariant_uuid, name, version
203 // ATTRIBUTE_VALUE_PAIR (group properties): group_type, group_role, group_function
204 // RESOURCE_GROUP_TO_TARGET_NODE_MAPPING: group_uuid, parent_uuid (CR node UUID), target_node_uuid, target_type, table_name
206 SdncGroupModel groupModel = new SdncGroupModel (sdcCsarHelper, group, entityDetails, config, jdbcDataSource);
207 String resourceUuid = getUUID();
208 groupModel.insertGroupData(resourceUuid);
210 // insert RESOURCE_GROUP_TO_TARGET_NODE_MAPPING
212 Map<String, String> mappingCleanupParams = new HashMap<String, String>();
213 addParameter("group_uuid", groupModel.getUUID(), mappingCleanupParams);
214 addParameter("parent_uuid", extractValue(entityDetails.getMetadata(), SdcPropertyNames.PROPERTY_NAME_UUID), mappingCleanupParams);
215 addParameter("target_node_uuid", extractValue(targetNode.getMetadata(), SdcPropertyNames.PROPERTY_NAME_UUID), mappingCleanupParams);
216 cleanupExistingToscaData("RESOURCE_GROUP_TO_TARGET_NODE_MAPPING", mappingCleanupParams);
218 Map<String, String> mappingParams = new HashMap<String, String>();
219 addParameter("parent_uuid", extractValue(entityDetails.getMetadata(), SdcPropertyNames.PROPERTY_NAME_UUID), mappingParams);
220 addParameter("target_node_uuid", extractValue(targetNode.getMetadata(), SdcPropertyNames.PROPERTY_NAME_UUID), mappingParams);
221 String targetType = extractValue(targetNode.getMetadata(), PARAM_TYPE_KEY);
222 addParameter("target_type", targetType, mappingParams);
223 String tableName = "";
224 switch (targetType) {
226 tableName = "VFC_MODEL";
229 tableName = "NETWORK_MODEL";
232 addParameter("table_name", tableName, mappingParams);
233 LOG.info("Call insertToscaData for RESOURCE_GROUP_TO_TARGET_NODE_MAPPING where group_uuid = " + groupModel.getUUID());
234 insertToscaData(buildSql("RESOURCE_GROUP_TO_TARGET_NODE_MAPPING", "group_uuid", groupModel.getUUID(), model_yaml, mappingParams), null);
236 } catch (IOException e) {
237 LOG.error("Could not insert Tosca CSAR data into the RESOURCE_GROUP_TO_TARGET_NODE_MAPPING");
238 throw new IOException (e);
243 protected void insertEntityPolicyData (String nodeTemplateCustomizationUuid, String nodeTemplateUuid, SdcTypes queryType, String targetCustomizationUuid, String targetUuid, String targetType, String policyType) throws IOException {
245 EntityQuery policyEntityQuery = EntityQuery.newBuilder(policyType).build();
246 TopologyTemplateQuery topologyTemplateQuery = TopologyTemplateQuery.newBuilder(queryType).customizationUUID(nodeTemplateCustomizationUuid).build();
247 List<IEntityDetails> policyEntities = sdcCsarHelper.getEntity(policyEntityQuery, topologyTemplateQuery, false);
248 if (policyEntities == null || policyEntities.isEmpty()) {
249 LOG.debug("insertPolicyData: Could not find policy data for: " + nodeTemplateCustomizationUuid);
253 String resourceUuid = getUUID();
255 for (IEntityDetails policyEntity : policyEntities) {
257 // extract policy metadata
258 String policyUuid = extractValue(policyEntity.getMetadata(), SdcPropertyNames.PROPERTY_NAME_UUID);
259 String policyCustomizationUuid = extractValue(policyEntity.getMetadata(), SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID);
261 insertResourcePolicyData(policyEntity, resourceUuid);
262 insertResourcePolicyToTargetNodeMappingData(policyUuid, nodeTemplateUuid, targetUuid, targetCustomizationUuid, policyCustomizationUuid, targetType);
266 public void insertEntityPolicyData (String resourceCustomizationUuid, String resourceUuid, String parentUuid, String policyType, SdcTypes queryType) throws IOException {
268 EntityQuery policyEntityQuery = EntityQuery.newBuilder(policyType).build();
269 TopologyTemplateQuery topologyTemplateQuery;
270 if (queryType == SdcTypes.VF) {
271 topologyTemplateQuery = TopologyTemplateQuery.newBuilder(queryType).customizationUUID(resourceCustomizationUuid).build();
273 topologyTemplateQuery = TopologyTemplateQuery.newBuilder(queryType).build();
276 List<IEntityDetails> policyEntities = sdcCsarHelper.getEntity(policyEntityQuery, topologyTemplateQuery, false);
277 if (policyEntities == null || policyEntities.isEmpty()) {
278 LOG.debug("insertPolicyData: Could not find policy data for Service/VF: " + resourceUuid);
282 for (IEntityDetails policyEntity : policyEntities) {
284 // extract policy metadata
285 String policyUuid = extractValue(policyEntity.getMetadata(), SdcPropertyNames.PROPERTY_NAME_UUID);
286 String policyCustomizationUuid = extractValue(policyEntity.getMetadata(), SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID);
288 insertResourcePolicyData(policyEntity, resourceUuid);
289 List<IEntityDetails> targetEntities = policyEntity.getTargetEntities();
290 if (targetEntities == null || targetEntities.isEmpty()) {
291 LOG.debug("insertPolicyData: Could not find targetEntites for policy: " + policyUuid);
295 for (IEntityDetails targetEntity : targetEntities) {
297 String targetUuid = extractValue(targetEntity.getMetadata(), SdcPropertyNames.PROPERTY_NAME_UUID);
298 String targetCustomizationUuid = extractValue(targetEntity.getMetadata(), SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID);
299 String targetType = extractValue(targetEntity.getMetadata(), SdcPropertyNames.PROPERTY_NAME_TYPE);
300 insertResourcePolicyToTargetNodeMappingData(policyUuid, parentUuid, targetUuid, targetCustomizationUuid, policyCustomizationUuid, targetType);
305 protected void insertResourcePolicyData (IEntityDetails policyEntity, String resourceUuid) throws IOException {
307 // extract policy metadata
308 String policyUuid = extractValue(policyEntity.getMetadata(), SdcPropertyNames.PROPERTY_NAME_UUID);
309 String policyInvariantUuid = extractValue(policyEntity.getMetadata(), SdcPropertyNames.PROPERTY_NAME_INVARIANTUUID);
310 String policyCustomizationUuid = extractValue(policyEntity.getMetadata(), SdcPropertyNames.PROPERTY_NAME_CUSTOMIZATIONUUID);
312 // cleanup existing RESOURCE_POLICY data
313 Map<String, String> cleanupParams = new HashMap<String, String>();
314 addParameter("resource_uuid", resourceUuid, cleanupParams);
315 addParameter("policy_uuid", policyUuid, cleanupParams);
316 addParameter("policy_invariant_uuid", policyInvariantUuid, cleanupParams);
318 // insert into RESOURCE_POLICY
319 Map<String, String> policyParams = new HashMap<String, String>();
320 addParameter("policy_uuid", policyUuid, policyParams);
321 addParameter("policy_customization_uuid", policyCustomizationUuid, policyParams);
322 addParameter("policy_invariant_uuid", policyInvariantUuid, policyParams);
323 addParameter("policy_name", extractValue(policyEntity.getMetadata(), PARAM_NAME_KEY), policyParams);
324 addParameter(PARAM_VERSION_KEY, extractValue(policyEntity.getMetadata(), PARAM_VERSION_KEY), policyParams);
325 addParameter("policy_type", policyEntity.getToscaType(), policyParams);
327 // extract properties
328 addParameter("property_type", extractValue(policyEntity, PARAM_TYPE_KEY), policyParams);
329 addParameter("property_source", extractValue(policyEntity, "source"), policyParams);
330 addParameter("property_name", extractValue(policyEntity, PARAM_NAME_KEY), policyParams);
332 // Insert into RESOURCE_POLICY and RESOURCE_POLICY_TO_TARGET_NODE_MAPPING
333 // RESOURCE_POLICY: resource_uuid (CR node UUID), uuid, customization_uuid, invariant_uuid, name, version, policy_type,
334 // property_type, property_source, property_name
338 // insert into RESOURCE_POLICY
339 cleanupExistingToscaData("RESOURCE_POLICY", cleanupParams);
340 LOG.info("Call insertToscaData for RESOURCE_POLICY where resource_uuid = " + resourceUuid + " and policy_uuid = " + "\"" + policyUuid + "\"" );
341 insertToscaData(buildSql("RESOURCE_POLICY", "resource_uuid", resourceUuid, model_yaml, policyParams), null);
343 } catch (IOException e) {
344 LOG.error("Could not insert Tosca CSAR data into the RESOURCE_POLICY table");
345 throw new IOException (e);
350 protected void insertResourcePolicyToTargetNodeMappingData(String policyUuid, String parentUuid, String targetUuid, String targetCustomizationUuid, String policyCustomizationUuid, String targetType) throws IOException {
352 // insert RESOURCE_POLICY_TO_TARGET_NODE_MAPPING: policy_uuid, parent_uuid (CR node UUID), target_node_uuid, target_type, table_name
354 Map<String, String> mappingCleanupParams = new HashMap<String, String>();
355 addParameter("policy_uuid", policyUuid, mappingCleanupParams);
356 addParameter("parent_uuid", parentUuid, mappingCleanupParams);
357 addParameter("target_node_uuid", targetUuid, mappingCleanupParams);
358 cleanupExistingToscaData("RESOURCE_POLICY_TO_TARGET_NODE_MAPPING", mappingCleanupParams);
360 Map<String, String> mappingParams = new HashMap<String, String>();
361 addParameter("parent_uuid", parentUuid, mappingParams);
362 addParameter("target_node_uuid", targetUuid, mappingParams);
363 addParameter("target_node_customization_uuid", targetCustomizationUuid, mappingParams);
364 addParameter("policy_customization_uuid", policyCustomizationUuid, mappingParams);
365 addParameter("target_type", targetType, mappingParams);
366 LOG.info("Call insertToscaData for RESOURCE_POLICY_TO_TARGET_NODE_MAPPING where policy_uuid = " + "\"" + policyUuid + "\" and parent_uuid = " + "\"" + parentUuid + "\" and target_node_uuid = " + "\"" + targetUuid + "\"");
367 insertToscaData(buildSql("RESOURCE_POLICY_TO_TARGET_NODE_MAPPING", "policy_uuid", "\"" + policyUuid + "\"", model_yaml, mappingParams), null);
369 } catch (IOException e) {
370 LOG.error("Could not insert Tosca CSAR data into the RESOURCE_POLICY_TO_TARGET_NODE_MAPPING");
371 throw new IOException (e);
376 protected void insertNodeCapabilitiesData (CapabilityAssignments capabilities) throws IOException {
378 // Process the capabilities on the node template
380 List<CapabilityAssignment> capabilityList = capabilities.getAll();
382 for (CapabilityAssignment capability : capabilities.getAll()) {
384 // Insert into NODE_CAPABILITY:
385 // capability_id (generated)
386 // capability_provider_uuid - UUID of this node
387 // capability_provider_customization_uuid - customization UUID of this node
388 // capability_name - capability.getName()
389 // capability_type - ?
391 // Check capability name against relevant capabilities
392 boolean capabilityIsRelevant = false;
393 /*List<String> relevantCapabilities = config.getRelevantCapabilityNames();
394 for (String relevantCapabilityName : relevantCapabilities ) {
396 if (capability.getName().toLowerCase().contains(relevantCapabilityName.toLowerCase())) {
397 capabilityIsRelevant = true;
401 if (capabilityIsRelevant == false){
405 String capabilityProviderUuid = getUUID();
407 Map<String, String> cleanupParams = new HashMap<String, String>();
408 addParameter("capability_provider_uuid", capabilityProviderUuid, cleanupParams); // node customization UUID
409 addParameter("capability_provider_customization_uuid", getCustomizationUUIDNoQuotes(), cleanupParams); // node customization UUID
410 addParameter("capability_name", capability.getName(), cleanupParams);
412 Map<String, String> nodeCapabilityParams = new HashMap<String, String>();
413 addParameter("capability_provider_customization_uuid", getCustomizationUUIDNoQuotes(), nodeCapabilityParams); // node customization UUID
414 addParameter("capability_name", capability.getName(), nodeCapabilityParams);
415 addParameter("capability_type", extractValue(capability, PARAM_TYPE_KEY), nodeCapabilityParams);
417 // Insert NODE_CAPABILITY data for each capability
418 String capabilityId = "";
421 cleanupExistingToscaData("NODE_CAPABILITY", cleanupParams); // will also delete NODE_CAPABILITY_PROPERTY with same capability_id
422 LOG.info("Call insertToscaData for NODE_CAPABILITY where capability_provider_uuid = " + capabilityProviderUuid + " and capability_name = \"" + capability.getName() + "\"");
423 insertToscaData(buildSql("NODE_CAPABILITY", "capability_provider_uuid", capabilityProviderUuid, model_yaml, nodeCapabilityParams), null);
425 // Get capabilityId for capability just inserted
426 CachedRowSet rowData = getToscaData("NODE_CAPABILITY", nodeCapabilityParams);
428 int capabilityIdint = rowData.getInt("capability_id");
429 capabilityId = String.valueOf(capabilityIdint);
431 } catch (IOException | SQLException e) {
432 LOG.error("Could not insert Tosca CSAR data into the NODE_CAPABILITY table");
433 throw new IOException (e);
436 insertNodeCapabilityPropertyData (capability, capabilityId);
440 protected void insertNodeCapabilitiesEntityData (List<CapabilityAssignment> capabilities) throws IOException {
442 // Process the capabilities
443 for (CapabilityAssignment capability : capabilities) {
445 // Insert into NODE_CAPABILITY:
446 // capability_id (generated)
447 // capability_provider_uuid - UUID of this node
448 // capability_provider_customization_uuid - customization UUID of this node
449 // capability_name - capability.getName()
450 // capability_type - ?
452 // Check capability name against relevant capabilities
453 boolean capabilityIsRelevant = false;
454 /*List<String> relevantCapabilities = config.getRelevantCapabilityNames();
455 for (String relevantCapabilityName : relevantCapabilities ) {
457 if (capability.getName().toLowerCase().contains(relevantCapabilityName.toLowerCase())) {
458 capabilityIsRelevant = true;
462 if (capabilityIsRelevant == false){
466 String capabilityProviderUuid = getUUID();
468 Map<String, String> cleanupParams = new HashMap<String, String>();
469 addParameter("capability_provider_uuid", capabilityProviderUuid, cleanupParams); // node customization UUID
470 addParameter("capability_provider_customization_uuid", getCustomizationUUIDNoQuotes(), cleanupParams); // node customization UUID
471 addParameter("capability_name", capability.getName(), cleanupParams);
473 Map<String, String> nodeCapabilityParams = new HashMap<String, String>();
474 addParameter("capability_provider_customization_uuid", getCustomizationUUIDNoQuotes(), nodeCapabilityParams); // node customization UUID
475 addParameter("capability_name", capability.getName(), nodeCapabilityParams);
476 addParameter("capability_type", extractValue(capability, PARAM_TYPE_KEY), nodeCapabilityParams);
478 // Insert NODE_CAPABILITY data for each capability
479 String capabilityId = "";
482 cleanupExistingToscaData("NODE_CAPABILITY", cleanupParams); // will also delete NODE_CAPABILITY_PROPERTY with same capability_id
483 LOG.info("Call insertToscaData for NODE_CAPABILITY where capability_provider_uuid = " + capabilityProviderUuid + " and capability_name = \"" + capability.getName()+ "\"");
484 insertToscaData(buildSql("NODE_CAPABILITY", "capability_provider_uuid", capabilityProviderUuid, model_yaml, nodeCapabilityParams), null);
486 // Get capabilityId for capability just inserted
487 CachedRowSet rowData = getToscaData("NODE_CAPABILITY", nodeCapabilityParams);
489 int capabilityIdint = rowData.getInt("capability_id");
490 capabilityId = String.valueOf(capabilityIdint);
492 } catch (IOException | SQLException e) {
493 LOG.error("Could not insert Tosca CSAR data into the NODE_CAPABILITY table");
494 throw new IOException (e);
497 insertNodeCapabilityPropertyData (capability, capabilityId);
501 protected void insertNodeCapabilityPropertyData(CapabilityAssignment capability, String capabilityId) throws IOException {
503 // Insert property name / value into NODE_CAPABILITY_PROPERTY
504 LinkedHashMap<String, Property> propertiesMap = capability.getProperties();
505 Map<String, String> nodeCapabilityPropertyParams = new HashMap<String, String>();
507 for (String propertyMapKey : propertiesMap.keySet() ) {
508 //LOG.info("property map key = " + propertyMapKey);
509 Property property = propertiesMap.get(propertyMapKey);
511 addParameter ("capability_property_name", property.getName(), nodeCapabilityPropertyParams);
512 addParameter ("capability_property_type", property.getValue().toString(), nodeCapabilityPropertyParams);
515 // Data from NODE_CAPABILITY_PROPERTY is cleaned up via cascade delete on NODE_CAPABILITY
516 LOG.info("Call insertToscaData for NODE_CAPABILITY_PROPERTY where capability_id = \"" + capabilityId + "\" and capability_property_name = \"" + property.getName() + "\" and capability_property_type = \"" + property.getValue().toString() + "\"");
517 insertToscaData(buildSql("NODE_CAPABILITY_PROPERTY", "capability_id", capabilityId, model_yaml, nodeCapabilityPropertyParams), null);
518 } catch (IOException e) {
519 LOG.error("Could not insert Tosca CSAR data into the NODE_CAPABILITY_PROPERTY table");
520 throw new IOException (e);
526 protected void addParameter (String name, String value) {
527 if (value != null && !value.isEmpty()) {
528 // check if value already contain quotes
529 if (value.startsWith("\"", 0) && value.endsWith("\"")) {
530 params.put(name, value);
532 params.put(name, "\"" + value + "\"");
537 protected void addIntParameter (String name, String value) {
538 if (value != null && !value.isEmpty()) {
539 params.put(name, value);
543 public static void addParameter (String name, String value, Map<String, String> params) {
544 if (value != null && !value.isEmpty()) {
545 // remove any quotes within the string
546 String strippedValue = value.replace("\"","");
548 // check if value already contain quotes
549 if (strippedValue.startsWith("\"", 0) && value.endsWith("\"")) {
550 params.put(name, strippedValue);
552 params.put(name, "\"" + strippedValue + "\"");
557 protected String extractValue (Metadata metadata, String name) {
558 String value = metadata.getValue(name);
566 protected String extractBooleanValue (Metadata metadata, String name) {
567 String value = metadata.getValue(name);
568 if (value != null && !value.isEmpty()) {
569 return value.contains("true") ? "Y" : "N";
575 public static String extractValue (ISdcCsarHelper sdcCsarHelper, Metadata metadata, String name) {
576 String value = metadata.getValue(name);
584 protected String extractValue (IEntityDetails entityDetails, String name) {
586 if (entityDetails.getProperties().containsKey(name)) {
587 Property property = entityDetails.getProperties().get(name);
588 if (property != null && property.getValue() != null) {
589 value = property.getValue().toString();
593 if (value != null && !value.isEmpty() && !value.equalsIgnoreCase("null")) {
600 protected String extractValue (IEntityDetails entityDetails, String path, String name) {
603 if (entityDetails.getProperties().containsKey(path)) {
604 Property property = entityDetails.getProperties().get(path);
605 if (property != null && !property.getLeafPropertyValue(name).isEmpty()) {
606 value = property.getLeafPropertyValue(name).get(0);
610 if (value != null && !value.isEmpty() && !value.equalsIgnoreCase("null")) {
617 protected String extractValue (IEntityDetails entityDetails, String path1, String path2, String name) {
620 value = extractNestedValue (entityDetails, path1, path2, name);
622 if (value != null && !value.isEmpty() && !value.equalsIgnoreCase("null")) {
629 protected String extractValue (IEntityDetails entityDetails, String path1, String path2, String path3, String name) {
632 value = extractNestedValue (entityDetails, path1, path2, path3, name);
634 if (value != null && !value.isEmpty() && !value.equalsIgnoreCase("null")) {
641 protected String extractBooleanValue (IEntityDetails entityDetails, String path1, String path2, String name) {
644 value = extractNestedValue (entityDetails, path1, path2, name);
646 if (value != null && !value.isEmpty()) {
647 return value.contains("true") ? "Y" : "N";
653 protected String extractNestedValue (IEntityDetails entityDetails, String path1, String path2, String name) {
656 if (entityDetails.getProperties().containsKey(path1)) {
657 Property path1Property = entityDetails.getProperties().get(path1);
658 if (path1Property != null) {
659 Map<String, Object> path1PropertyValue = (Map<String, Object>) path1Property.getValue();
660 if (path1PropertyValue.containsKey(path2)) {
661 Map<String, Object> path2PropertyValue = (Map<String, Object>) path1PropertyValue.get(path2);
662 if (path2PropertyValue != null && path2PropertyValue.containsKey(name)) {
663 value = path2PropertyValue.get(name).toString();
672 protected String extractNestedValue (IEntityDetails entityDetails, String path1, String path2, String path3, String name) {
675 if (entityDetails.getProperties().containsKey(path1)) {
676 Property path1Property = entityDetails.getProperties().get(path1);
677 if (path1Property != null) {
678 Map<String, Object> path1PropertyValue = (Map<String, Object>) path1Property.getValue();
679 if (path1PropertyValue.containsKey(path2)) {
680 Map<String, Object> path2PropertyValue = (Map<String, Object>) path1PropertyValue.get(path2);
681 if (path2PropertyValue != null && path2PropertyValue.containsKey(path3)) {
682 Map<String, Object> path3PropertyValue = (Map<String, Object>) path2PropertyValue.get(path3);
683 if (path3PropertyValue != null && path3PropertyValue.containsKey(name)) {
684 value = path3PropertyValue.get(name).toString();
694 protected String extractIntegerValue (Property property, String name) {
697 if (!property.getLeafPropertyValue(name).isEmpty()) {
698 value = property.getLeafPropertyValue(name).get(0);
701 if (value != null && !value.isEmpty() && !value.equalsIgnoreCase("null")) {
708 protected String extractGetInputValue (IEntityDetails group, IEntityDetails entityDetails, String name) {
710 // Extract the property on entityDetails after getting the property name from the group
711 String getInputName = extractGetInputName (group, name);
712 String entityPropertyValue = "";
714 if (entityDetails.getProperties().containsKey(getInputName)) {
715 Property entityProperty = entityDetails.getProperties().get(getInputName);
716 entityPropertyValue = entityProperty.getValue().toString();
719 if (!entityPropertyValue.isEmpty()) {
720 return entityPropertyValue;
726 protected String extractGetInputName (IEntityDetails group, String name) {
728 String getInputName = name;
729 String groupPropertyValue = "";
730 if (group.getProperties().containsKey(name)) {
731 Property groupProperty = group.getProperties().get(name);
732 groupPropertyValue = groupProperty.getValue().toString();
734 if (!groupPropertyValue.isEmpty()) {
735 int getInputIndex = groupPropertyValue.indexOf("{get_input=");
736 if (getInputIndex > -1) {
737 getInputName = groupPropertyValue.substring(getInputIndex+11, groupPropertyValue.length()-1);
745 protected String extractValue (CapabilityAssignment capability, String name) {
748 if (capability.getProperties().containsKey(name)) {
749 Property property = capability.getProperties().get(name);
750 value = property.getLeafPropertyValue(name).get(0);
753 if (value != null && !value.isEmpty() && !value.equalsIgnoreCase("null")) {
760 protected String extractValue (CapabilityAssignment capability, String path, String name) {
763 if (capability.getProperties().containsKey(path)) {
764 Property property = capability.getProperties().get(path);
765 if (property != null && !property.getLeafPropertyValue(name).isEmpty()) {
766 value = property.getLeafPropertyValue(name).get(0);
770 if (value != null && !value.isEmpty() && !value.equalsIgnoreCase("null")) {
777 protected String extractBooleanValue (IEntityDetails entityDetails, String name) {
779 if (entityDetails.getProperties().containsKey(name)) {
780 Property property = entityDetails.getProperties().get(name);
781 if (property != null && property.getValue() != null) {
782 value = property.getValue().toString();
786 if (value != null && !value.isEmpty() && !value.equalsIgnoreCase("null")) {
787 return value.contains("true") ? "Y" : "N";
793 protected String extractBooleanValue (IEntityDetails entityDetails, String path, String name) {
795 if (entityDetails.getProperties().containsKey(path)) {
796 Property property = entityDetails.getProperties().get(path);
797 if (property != null) {
798 if (property != null && !property.getLeafPropertyValue(name).isEmpty()) {
799 value = property.getLeafPropertyValue(name).get(0);
804 if (value != null && !value.isEmpty() && !value.equalsIgnoreCase("null")) {
805 return value.contains("true") ? "Y" : "N";
811 protected Object extractObjectValue (IEntityDetails entityDetails, String name) {
813 if (entityDetails.getProperties().containsKey(name)) {
814 Property property = entityDetails.getProperties().get(name);
815 if (property != null && property.getValue() != null) {
816 value = property.getValue();
827 public static String extractSubstitutionMappingTypeName (ISdcCsarHelper sdcCsarHelper) {
828 String value = sdcCsarHelper.getServiceSubstitutionMappingsTypeName();
836 protected String getUUID() {
837 return ("\"" + UUID + "\"");
839 public String getInvariantUUID() {
840 return ("\"" + invariantUUID + "\"");
842 public String getCustomizationUUID() {
843 return ("\"" + customizationUUID + "\"");
845 public String getCustomizationUUIDNoQuotes() {
846 return (customizationUUID);
848 public void setCustomizationUUID(String customizationUUID) {
849 this.customizationUUID = customizationUUID;
851 public String getName() {
855 public String buildSql(String tableName, String model_yaml) {
857 StringBuilder sb = new StringBuilder();
858 sb.append("INSERT into " + tableName + " (customization_uuid, model_yaml, ");
861 for (String paramKey : params.keySet()) {
864 if (paramCount < params.size()) sb.append(", ");
867 sb.append(") values (" + getCustomizationUUID() + ", \"" + model_yaml + "\", ");
870 for (String paramKey : params.keySet()) {
872 String paramValue = params.get(paramKey);
873 sb.append(paramValue);
874 if (paramCount < params.size()) sb.append(", ");
878 return sb.toString();
881 public String buildSql(String tableName, String keyName, String keyValue, String model_yaml, Map<String, String> params) {
883 StringBuilder sb = new StringBuilder();
884 sb.append("INSERT into " + tableName + " (" + keyName + ", ");
887 for (String paramKey : params.keySet()) {
890 if (paramCount < params.size()) sb.append(", ");
893 sb.append(") values (" + keyValue + ", ");
896 for (String paramKey : params.keySet()) {
898 String paramValue = params.get(paramKey);
899 sb.append(paramValue);
900 if (paramCount < params.size()) sb.append(", ");
904 return sb.toString();
907 public static String getSql(String tableName, String keyName, String keyValue, String model_yaml, Map<String, String> params) {
909 StringBuilder sb = new StringBuilder();
910 sb.append("INSERT into " + tableName + " (" + keyName + ", ");
913 for (String paramKey : params.keySet()) {
916 if (paramCount < params.size()) sb.append(", ");
919 sb.append(") values (" + keyValue + ", ");
922 for (String paramKey : params.keySet()) {
924 String paramValue = params.get(paramKey);
925 sb.append(paramValue);
926 if (paramCount < params.size()) sb.append(", ");
930 return sb.toString();
933 protected void insertToscaData(String toscaDataString, ArrayList<String> arguments) throws IOException
935 LOG.debug("insertToscaData: " + toscaDataString);
939 jdbcDataSource.writeData(toscaDataString, arguments, null);
941 } catch (SQLException e) {
942 LOG.error("Could not insert Tosca data into the database");
943 throw new IOException (e);
948 protected void cleanUpExistingToscaData(String tableName, String keyName, String keyValue) throws IOException
953 CachedRowSet data = jdbcDataSource.getData("SELECT * from " + tableName + " where " + keyName + " = " + keyValue + ";", null, "");
959 LOG.debug("cleanUpExistingToscaData from: " + tableName + " for " + keyValue);
960 jdbcDataSource.writeData("DELETE from " + tableName + " where " + keyName + " = " + keyValue + ";", null, null);
964 } catch (SQLException e) {
965 LOG.error("Could not clean up existing " + tableName + " for " + keyValue, e);
970 protected void cleanUpExistingToscaData(String tableName, String key1Name, String key1Value, String key2Name, String key2Value) throws IOException
975 CachedRowSet data = jdbcDataSource.getData("SELECT * from " + tableName + " where " + key1Name + " = " + key1Value + " AND " + key2Name + " = " + key2Value + ";", null, "");
981 LOG.debug("cleanUpExistingToscaData from : " + tableName + " for " + key1Value + " and " + key2Value);
982 jdbcDataSource.writeData("DELETE from " + tableName + " where " + key1Name + " = " + key1Value + " AND " + key2Name + " = " + key2Value + ";", null, null);
986 } catch (SQLException e) {
987 LOG.error("Could not clean up existing " + tableName + " for " + key1Value + " and " + key2Value, e);
992 protected boolean cleanupExistingToscaData(String tableName, Map<String, String> keyParams) throws IOException
994 return SdncBaseModel.cleanupExistingToscaData(SdncBaseModel.jdbcDataSource, tableName, keyParams);
997 public static boolean cleanupExistingToscaData(DBResourceManager jdbcDataSource, String tableName, Map<String, String> keyParams) throws IOException
999 boolean dataExists = false;
1000 StringBuilder sb = new StringBuilder();
1001 sb.append("SELECT * from " + tableName + " where ");
1004 for (String paramKey : keyParams.keySet()) {
1006 String paramValue = keyParams.get(paramKey);
1007 sb.append(paramKey);
1009 sb.append(paramValue);
1010 if (paramCount < keyParams.size()) sb.append(" AND ");
1017 CachedRowSet data = jdbcDataSource.getData(sb.toString(), null, "");
1018 while(data.next()) {
1022 if (rowCount != 0) {
1023 LOG.debug("cleanupExistingToscaData in " + tableName + ": Data FOUND");
1024 String deleteStmt = sb.replace(sb.indexOf("SELECT *"), sb.indexOf("SELECT")+8, "DELETE").toString();
1025 jdbcDataSource.writeData(deleteStmt, null, null);
1029 } catch (SQLException e) {
1030 LOG.error("Could not get data in " + tableName, e);
1036 protected boolean checkForExistingToscaData(String tableName, Map<String, String> keyParams) throws IOException
1038 boolean dataExists = false;
1039 StringBuilder sb = new StringBuilder();
1040 sb.append("SELECT * from " + tableName + " where ");
1043 for (String paramKey : keyParams.keySet()) {
1045 String paramValue = keyParams.get(paramKey);
1046 sb.append(paramKey);
1048 sb.append(paramValue);
1049 if (paramCount < keyParams.size()) sb.append(" AND ");
1056 CachedRowSet data = jdbcDataSource.getData(sb.toString(), null, "");
1057 while(data.next()) {
1060 if (rowCount != 0) {
1061 LOG.debug("checkForExistingToscaData in " + tableName + ": Data FOUND");
1065 } catch (SQLException e) {
1066 LOG.error("Could not get data in " + tableName, e);
1072 protected CachedRowSet getToscaData(String tableName, Map<String, String> keyParams) throws IOException
1074 StringBuilder sb = new StringBuilder();
1075 sb.append("SELECT * from " + tableName + " where ");
1078 for (String paramKey : keyParams.keySet()) {
1080 String paramValue = keyParams.get(paramKey);
1081 sb.append(paramKey);
1083 sb.append(paramValue);
1084 if (paramCount < keyParams.size()) sb.append(" AND ");
1089 CachedRowSet data = null;
1092 data = jdbcDataSource.getData(sb.toString(), null, "");
1093 while(data.next()) {
1096 if (rowCount == 0) {
1097 LOG.info("getToscaData in " + tableName + ": Data NOT found");
1100 } catch (SQLException e) {
1101 LOG.error("Could not get data in " + tableName, e);
1107 protected void addParamsToMap (Map<String, String> fromMap, Map<String, String> toMap) {
1108 for (String key : fromMap.keySet()) {
1109 if (!toMap.containsKey(key)) {
1110 toMap.put(key, fromMap.get(key));
1115 protected String nullCheck (Object extractedObject) {
1116 String stringValue = "";
1117 if (extractedObject != null) {
1118 return extractedObject.toString();