From a489456f6780b491d31f6bb853e469bcb6b65c61 Mon Sep 17 00:00:00 2001 From: "Singal, Kapil (ks220y)" Date: Tue, 4 Sep 2018 13:28:39 -0400 Subject: SDN Controller Blueprints Assignment Creating SDN Controller Blueprints Resource Assignment Service Change-Id: I1d997226a515c36b2ca07d23991c0e98df2ab78c Issue-ID: CCSDK-507 Signed-off-by: Singal, Kapil (ks220y) --- .../assignment/ConfigAssignmentConstants.java | 34 +++ .../assignment/data/ResourceAssignmentData.java | 160 +++++++++++ .../assignment/processor/ProcessorFactory.java | 49 ++++ .../processor/ResourceAssignmentProcessor.java | 117 ++++++++ .../service/ConfigAssignmentPersistService.java | 120 ++++++++ .../service/ConfigAssignmentProcessService.java | 235 ++++++++++++++++ .../service/ConfigAssignmentService.java | 32 +++ .../service/ConfigAssignmentServiceImpl.java | 76 ++++++ .../assignment/service/ConfigAssignmentUtils.java | 302 +++++++++++++++++++++ .../assignment/service/ConfigPreviewService.java | 116 ++++++++ .../service/ResourceDictionaryService.java | 109 ++++++++ .../assignment/service/ResourceModelService.java | 94 +++++++ .../org/opendaylight/blueprint/impl-blueprint.xml | 67 +++++ 13 files changed, 1511 insertions(+) create mode 100644 blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/ConfigAssignmentConstants.java create mode 100644 blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/data/ResourceAssignmentData.java create mode 100644 blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/processor/ProcessorFactory.java create mode 100644 blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/processor/ResourceAssignmentProcessor.java create mode 100644 blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentPersistService.java create mode 100644 blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentProcessService.java create mode 100644 blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentService.java create mode 100644 blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentServiceImpl.java create mode 100644 blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentUtils.java create mode 100644 blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigPreviewService.java create mode 100644 blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ResourceDictionaryService.java create mode 100644 blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ResourceModelService.java create mode 100644 blueprints-processor/plugin/assignment-provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml (limited to 'blueprints-processor/plugin/assignment-provider') diff --git a/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/ConfigAssignmentConstants.java b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/ConfigAssignmentConstants.java new file mode 100644 index 000000000..9ab5ae6a6 --- /dev/null +++ b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/ConfigAssignmentConstants.java @@ -0,0 +1,34 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.onap.ccsdk.config.assignment; + +@SuppressWarnings("squid:S1118") +public class ConfigAssignmentConstants { + + public static final String OUTPUT_PARAM_STATUS = "status"; + public static final String OUTPUT_PARAM_RESOURCE_ASSIGNMENT_PARAMS = "resource-assignment-params"; + public static final String OUTPUT_PARAM_MASHED_DATA = "mashed-data"; + public static final String OUTPUT_PARAM_ERROR_MESSAGE = "error-message"; + public static final String OUTPUT_STATUS_SUCCESS = "success"; + public static final String OUTPUT_STATUS_FAILURE = "failure"; + + public static final String INPUT_PARAM_REQUEST_ID = "request-id"; + public static final String INPUT_PARAM_RESOURCE_ID = "resource-id"; + public static final String INPUT_PARAM_RESOURCE_TYPE = "resource-type"; + public static final String INPUT_PARAM_ACTION_NAME = "action-name"; + public static final String INPUT_PARAM_TEMPLATE_NAMES = "template-names"; + public static final String INPUT_PARAM_INPUT_DATA = "input-data"; + +} diff --git a/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/data/ResourceAssignmentData.java b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/data/ResourceAssignmentData.java new file mode 100644 index 000000000..d8a2745ea --- /dev/null +++ b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/data/ResourceAssignmentData.java @@ -0,0 +1,160 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.onap.ccsdk.config.assignment.data; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.onap.ccsdk.config.model.data.ResourceAssignment; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; + +public class ResourceAssignmentData { + + private String requestId; + private boolean reloadModel; + private String resourceType; + private String resourceId; + private String serviceTemplateName; + private String serviceTemplateVersion; + private String actionName; + private String inputData; + private SvcLogicContext svcLogicContext; + private List templateNames; + private Map> templatesResourceAssignments = new HashMap<>(); + private Map templatesContents = new HashMap<>(); + private Map templatesMashedContents = new HashMap<>(); + private Map templatesData = new HashMap<>(); + private Map context = new HashMap<>(); + + public String getRequestId() { + return requestId; + } + + public void setRequestId(String requestId) { + this.requestId = requestId; + } + + public String getResourceType() { + return resourceType; + } + + public void setResourceType(String resourceType) { + this.resourceType = resourceType; + } + + public String getResourceId() { + return resourceId; + } + + public void setResourceId(String resourceId) { + this.resourceId = resourceId; + } + + public String getServiceTemplateName() { + return serviceTemplateName; + } + + public void setServiceTemplateName(String serviceTemplateName) { + this.serviceTemplateName = serviceTemplateName; + } + + public String getServiceTemplateVersion() { + return serviceTemplateVersion; + } + + public void setServiceTemplateVersion(String serviceTemplateVersion) { + this.serviceTemplateVersion = serviceTemplateVersion; + } + + public String getActionName() { + return actionName; + } + + public void setActionName(String actionName) { + this.actionName = actionName; + } + + public String getInputData() { + return inputData; + } + + public void setInputData(String inputData) { + this.inputData = inputData; + } + + public List getTemplateNames() { + return templateNames; + } + + public void setTemplateNames(List templateNames) { + this.templateNames = templateNames; + } + + public Map> getTemplatesResourceAssignments() { + return templatesResourceAssignments; + } + + public void setTemplatesResourceAssignments(Map> templatesResourceAssignments) { + this.templatesResourceAssignments = templatesResourceAssignments; + } + + public Map getTemplatesContents() { + return templatesContents; + } + + public void setTemplatesContents(Map templatesContents) { + this.templatesContents = templatesContents; + } + + public Map getTemplatesMashedContents() { + return templatesMashedContents; + } + + public void setTemplatesMashedContents(Map templatesMashedContents) { + this.templatesMashedContents = templatesMashedContents; + } + + public Map getTemplatesData() { + return templatesData; + } + + public void setTemplatesData(Map templatesData) { + this.templatesData = templatesData; + } + + public Map getContext() { + return context; + } + + public void setContext(Map context) { + this.context = context; + } + + public SvcLogicContext getSvcLogicContext() { + return svcLogicContext; + } + + public void setSvcLogicContext(SvcLogicContext svcLogicContext) { + this.svcLogicContext = svcLogicContext; + } + + public boolean isReloadModel() { + return reloadModel; + } + + public void setReloadModel(boolean reloadModel) { + this.reloadModel = reloadModel; + } +} diff --git a/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/processor/ProcessorFactory.java b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/processor/ProcessorFactory.java new file mode 100644 index 000000000..d80fec8a9 --- /dev/null +++ b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/processor/ProcessorFactory.java @@ -0,0 +1,49 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.onap.ccsdk.config.assignment.processor; + +import org.onap.ccsdk.config.data.adaptor.service.ConfigResourceService; +import org.onap.ccsdk.config.model.ConfigModelConstant; +import org.onap.ccsdk.config.model.service.ComponentNode; +import org.onap.ccsdk.config.model.service.ComponentNodeService; +import org.onap.ccsdk.config.rest.adaptor.service.ConfigRestAdaptorService; + +public class ProcessorFactory { + + private ConfigResourceService configResourceService; + private ConfigRestAdaptorService configRestAdaptorService; + private ComponentNodeService componentNodeService; + + public ProcessorFactory(ConfigResourceService configResourceService, + ConfigRestAdaptorService configRestAdaptorService, ComponentNodeService componentNodeService) { + this.componentNodeService = componentNodeService; + this.configResourceService = configResourceService; + this.configRestAdaptorService = configRestAdaptorService; + } + + public ComponentNode getInstance(String source) { + + if (ConfigModelConstant.SOURCE_DEFAULT.equalsIgnoreCase(source)) { + return new DefaultResourceProcessor(configResourceService); + } else if (ConfigModelConstant.SOURCE_DB.equalsIgnoreCase(source)) { + return new DBResourceProcessor(configResourceService); + } else if (ConfigModelConstant.SOURCE_MDSAL.equalsIgnoreCase(source)) { + return new MdsalResourceProcessor(configRestAdaptorService); + } + // Default + return new InputResourceProcessor(configResourceService); + } + +} diff --git a/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/processor/ResourceAssignmentProcessor.java b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/processor/ResourceAssignmentProcessor.java new file mode 100644 index 000000000..c1c9b93c0 --- /dev/null +++ b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/processor/ResourceAssignmentProcessor.java @@ -0,0 +1,117 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.onap.ccsdk.config.assignment.processor; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.collections.CollectionUtils; +import org.onap.ccsdk.config.model.data.ResourceAssignment; +import org.onap.ccsdk.config.model.utils.TopologicalSortingUtils; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +public class ResourceAssignmentProcessor { + private static EELFLogger logger = EELFManager.getInstance().getLogger(ResourceAssignmentProcessor.class); + + private List assignments; + private Map resourceAssignmentMap; + + @SuppressWarnings("squid:S1172") + public ResourceAssignmentProcessor(List assignments, SvcLogicContext ctx) { + this.assignments = assignments; + this.resourceAssignmentMap = new HashMap<>(); + } + + @SuppressWarnings("squid:S3776") + public List> process() { + List> sequenceBatchResourceAssignment = new ArrayList<>(); + if (this.assignments != null) { + logger.info("Assignments ({})", this.assignments); + this.assignments.forEach(resourceMapping -> { + if (resourceMapping != null) { + logger.trace("Processing Key ({})", resourceMapping.getName()); + resourceAssignmentMap.put(resourceMapping.getName(), resourceMapping); + } + }); + + TopologicalSortingUtils topologySorting = new TopologicalSortingUtils<>(); + this.resourceAssignmentMap.forEach((mappingKey, mapping) -> { + if (mapping != null) { + if (mapping.getDependencies() != null && !mapping.getDependencies().isEmpty()) { + for (String dependency : mapping.getDependencies()) { + topologySorting.add(resourceAssignmentMap.get(dependency), mapping); + } + } else { + topologySorting.add(null, mapping); + } + } + }); + + List sequencedResourceAssignments = topologySorting.topSort(); + logger.info("Sorted Sequenced Assignments ({})", sequencedResourceAssignments); + + List batchResourceAssignment = null; + List batchAssignmentName = null; + for (int i = 0; i < sequencedResourceAssignments.size(); i++) { + ResourceAssignment resourceAssignment = sequencedResourceAssignments.get(i); + ResourceAssignment previousResourceAssignment = null; + + if (i > 0) { + previousResourceAssignment = sequencedResourceAssignments.get(i - 1); + } + if (resourceAssignment != null) { + + boolean dependencyPresence = false; + if (batchAssignmentName != null && resourceAssignment.getDependencies() != null) { + dependencyPresence = + CollectionUtils.containsAny(batchAssignmentName, resourceAssignment.getDependencies()); + } + + logger.trace("({}) -> Checking ({}), with ({}), result ({})", resourceAssignment.getName(), + batchAssignmentName, resourceAssignment.getDependencies(), dependencyPresence); + + if (previousResourceAssignment != null && resourceAssignment.getDictionarySource() != null + && resourceAssignment.getDictionarySource() + .equalsIgnoreCase(previousResourceAssignment.getDictionarySource()) + && !dependencyPresence) { + batchResourceAssignment.add(resourceAssignment); + batchAssignmentName.add(resourceAssignment.getName()); + } else { + if (batchResourceAssignment != null) { + sequenceBatchResourceAssignment.add(batchResourceAssignment); + logger.trace("Created old Set ({})", batchAssignmentName); + } + batchResourceAssignment = new ArrayList<>(); + batchResourceAssignment.add(resourceAssignment); + + batchAssignmentName = new ArrayList<>(); + batchAssignmentName.add(resourceAssignment.getName()); + } + } + + if (i == (sequencedResourceAssignments.size() - 1)) { + logger.trace("Created old Set ({})", batchAssignmentName); + sequenceBatchResourceAssignment.add(batchResourceAssignment); + } + } + logger.info("Batched Sequence : ({})", sequenceBatchResourceAssignment); + } + return sequenceBatchResourceAssignment; + } + +} diff --git a/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentPersistService.java b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentPersistService.java new file mode 100644 index 000000000..e27d57a79 --- /dev/null +++ b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentPersistService.java @@ -0,0 +1,120 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.onap.ccsdk.config.assignment.service; + +import java.util.List; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.onap.ccsdk.config.data.adaptor.DataAdaptorConstants; +import org.onap.ccsdk.config.data.adaptor.domain.ConfigResource; +import org.onap.ccsdk.config.data.adaptor.domain.ResourceAssignmentData; +import org.onap.ccsdk.config.data.adaptor.domain.TransactionLog; +import org.onap.ccsdk.config.data.adaptor.service.ConfigResourceService; +import org.onap.ccsdk.config.model.ConfigModelConstant; +import org.onap.ccsdk.config.model.data.ResourceAssignment; +import org.onap.ccsdk.config.model.utils.ResourceAssignmentUtils; +import org.onap.ccsdk.config.model.utils.TransformationUtils; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +public class ConfigAssignmentPersistService { + + private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigAssignmentPersistService.class); + + private ConfigResourceService configResourceService; + + public ConfigAssignmentPersistService(ConfigResourceService configResourceService) { + this.configResourceService = configResourceService; + } + + public void saveResourceMapping(org.onap.ccsdk.config.assignment.data.ResourceAssignmentData resourceAssignmentData, + String templateName, List resourceAssignments) throws SvcLogicException { + try { + + if (resourceAssignmentData == null) { + throw new SvcLogicException("Resource assignment data is missing"); + } + + if (StringUtils.isBlank(resourceAssignmentData.getRequestId())) { + logger.warn("Request Id ({}) is missing, may be getting request for resource update.", + resourceAssignmentData.getRequestId()); + } + + if (StringUtils.isBlank(resourceAssignmentData.getResourceId())) { + throw new SvcLogicException("Resource Id is missing"); + } + + if (StringUtils.isBlank(resourceAssignmentData.getResourceType())) { + throw new SvcLogicException("Resource type is missing"); + } + + if (StringUtils.isBlank(resourceAssignmentData.getActionName())) { + throw new SvcLogicException("Action name is missing"); + } + + if (StringUtils.isBlank(templateName)) { + throw new SvcLogicException("template name is missing"); + } + + StringBuilder builder = new StringBuilder(); + builder.append("Resource Assignment for Template Name :"); + builder.append(templateName); + builder.append("\n"); + builder.append(TransformationUtils.getJson(resourceAssignments, true)); + + configResourceService.save(new TransactionLog(resourceAssignmentData.getRequestId(), + DataAdaptorConstants.LOG_MESSAGE_TYPE_LOG, builder.toString())); + + // Resource Data should be Regenerated based on the new Updates + String resourceData = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments); + + List resourceAssignmentDataList = + ConfigAssignmentUtils.convertResoureAssignmentList(resourceAssignments); + + ConfigResource configResource = new ConfigResource(); + configResource.setRequestId(resourceAssignmentData.getRequestId()); + configResource.setServiceTemplateName(resourceAssignmentData.getServiceTemplateName()); + configResource.setServiceTemplateVersion(resourceAssignmentData.getServiceTemplateVersion()); + configResource.setRecipeName(resourceAssignmentData.getActionName()); + configResource.setResourceId(resourceAssignmentData.getResourceId()); + configResource.setResourceType(resourceAssignmentData.getResourceType()); + configResource.setResourceData(resourceData); + configResource.setTemplateName(templateName); + configResource.setStatus(ConfigModelConstant.STATUS_SUCCESS); + configResource.setUpdatedBy(ConfigModelConstant.USER_SYSTEM); + + if (CollectionUtils.isNotEmpty(resourceAssignmentDataList)) { + configResource.setResourceAssignments(resourceAssignmentDataList); + } + configResource = configResourceService.saveConfigResource(configResource); + logger.info("Resource data saved successfully for the template ({}) with resource id ({})", templateName, + configResource.getResourceId()); + + builder = new StringBuilder(); + builder.append("Resource Data Template Name :"); + builder.append(templateName); + builder.append("\n"); + builder.append(resourceData); + configResourceService.save(new TransactionLog(resourceAssignmentData.getRequestId(), + DataAdaptorConstants.LOG_MESSAGE_TYPE_LOG, builder.toString())); + + } catch (Exception e) { + throw new SvcLogicException("ConfigAssignmentPersistService : " + e.getMessage(), e); + } + + } + +} diff --git a/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentProcessService.java b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentProcessService.java new file mode 100644 index 000000000..81e76897e --- /dev/null +++ b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentProcessService.java @@ -0,0 +1,235 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.onap.ccsdk.config.assignment.service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.onap.ccsdk.config.assignment.ConfigAssignmentConstants; +import org.onap.ccsdk.config.assignment.data.ResourceAssignmentData; +import org.onap.ccsdk.config.assignment.processor.ProcessorFactory; +import org.onap.ccsdk.config.assignment.processor.ResourceAssignmentProcessor; +import org.onap.ccsdk.config.data.adaptor.service.ConfigResourceService; +import org.onap.ccsdk.config.generator.service.ConfigGeneratorService; +import org.onap.ccsdk.config.model.ConfigModelConstant; +import org.onap.ccsdk.config.model.data.ResourceAssignment; +import org.onap.ccsdk.config.model.data.dict.ResourceDefinition; +import org.onap.ccsdk.config.model.service.ComponentNode; +import org.onap.ccsdk.config.model.service.ComponentNodeService; +import org.onap.ccsdk.config.model.service.ConfigModelService; +import org.onap.ccsdk.config.model.utils.ResourceAssignmentUtils; +import org.onap.ccsdk.config.model.utils.TransformationUtils; +import org.onap.ccsdk.config.rest.adaptor.service.ConfigRestAdaptorService; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +public class ConfigAssignmentProcessService { + + private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigAssignmentProcessService.class); + + private ComponentNodeService componentNodeService; + private ConfigResourceService configResourceService; + private ConfigModelService configModelService; + private ConfigRestAdaptorService configRestAdaptorService; + private ConfigGeneratorService configGeneratorService; + + public ConfigAssignmentProcessService(ConfigResourceService configResourceService, + ConfigRestAdaptorService configRestAdaptorService, ConfigModelService configModelService, + ComponentNodeService componentNodeService, ConfigGeneratorService configGeneratorService) { + this.componentNodeService = componentNodeService; + this.configResourceService = configResourceService; + this.configModelService = configModelService; + this.configRestAdaptorService = configRestAdaptorService; + this.configGeneratorService = configGeneratorService; + } + + @SuppressWarnings("squid:S1141") + public void resolveResources(ResourceAssignmentData resourceAssignmentData) throws SvcLogicException { + try { + validateInputParams(resourceAssignmentData); + + String serviceTemplateName = resourceAssignmentData.getServiceTemplateName(); + String serviceTemplateVersion = resourceAssignmentData.getServiceTemplateVersion(); + String actionName = resourceAssignmentData.getActionName(); + String inputData = resourceAssignmentData.getInputData(); + SvcLogicContext svcLogicContext = resourceAssignmentData.getSvcLogicContext(); + List templateNames = resourceAssignmentData.getTemplateNames(); + + if (resourceAssignmentData.isReloadModel()) { + Map context = new HashMap<>(); + context.put(ConfigModelConstant.PROPERTY_ACTION_NAME, actionName); + context = configModelService.prepareContext(context, inputData, serviceTemplateName, + serviceTemplateVersion); + context.forEach((key, value) -> svcLogicContext.setAttribute(key, value)); + logger.info("List of Resources provided in input: {}", svcLogicContext.toProperties()); + } + + Map componentContext = resourceAssignmentData.getContext(); + + if (CollectionUtils.isNotEmpty(templateNames)) { + // Get the Resource Assignments for templates and Validate the mappings + ResourceModelService resourceModelService = new ResourceModelService(configModelService); + + // Get the Resource Assignment + Map> templatesResourceAssignments = + resourceModelService.getTemplatesResourceAssignments(svcLogicContext, templateNames); + + // Get the Template Contents + Map templatesContents = + resourceModelService.getTemplatesContents(svcLogicContext, templateNames); + + // Process each template + for (String templateName : templateNames) { + List resourceAssignments = templatesResourceAssignments.get(templateName); + String templateContent = templatesContents.get(templateName); + if (resourceAssignments != null) { + String templateData = null; + try { + // Populate the Dictionary + ResourceDictionaryService resourceDictionaryService = + new ResourceDictionaryService(configRestAdaptorService); + Map dictionaries = + resourceDictionaryService.getDataDictionaryDefinitions(resourceAssignments); + + processResourceAssignments(resourceAssignmentData, svcLogicContext, componentContext, + templateName, resourceAssignments, dictionaries); + + logger.info("decrypting config data for templateName {}", templateName); + templateData = + ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments); + } finally { + saveResourceMapping(resourceAssignmentData, templateName, resourceAssignments); + } + + logger.info("generating config preview for templateName {}", templateName); + ConfigPreviewService configPreviewService = new ConfigPreviewService(configResourceService, + configModelService, configGeneratorService); + String mashedData = configPreviewService.generatePreview(templateContent, templateData); + resourceAssignmentData.getTemplatesMashedContents().put(templateName, mashedData); + resourceAssignmentData.getTemplatesData().put(templateName, templateData); + + } else { + // Do nothing for Mapping not found + logger.warn("No resource Assignment mappings to resolve for templateName {}", templateName); + } + } + } + + } catch (Exception e) { + throw new SvcLogicException(e.getMessage(), e); + } + } + + private void processResourceAssignments(ResourceAssignmentData resourceAssignmentData, SvcLogicContext ctx, + Map componentContext, String templateName, List resourceAssignments, + Map dictionaries) throws SvcLogicException { + + String recipeName = resourceAssignmentData.getActionName(); + + ResourceAssignmentProcessor resourceAssignmentProcessor = + new ResourceAssignmentProcessor(resourceAssignments, ctx); + List> sequenceBatchResourceAssignment = resourceAssignmentProcessor.process(); + + logger.debug("Resource dictionary Info ({})", dictionaries); + + if (sequenceBatchResourceAssignment != null) { + componentContext.put(ConfigModelConstant.PROPERTY_ACTION_NAME, recipeName); + componentContext.put(ConfigModelConstant.PROPERTY_TEMPLATE_NAME, templateName); + componentContext.put(ConfigModelConstant.PROPERTY_DICTIONARIES, dictionaries); + for (List batchResourceAssignment : sequenceBatchResourceAssignment) { + + processBatchResourceAssignments(resourceAssignmentData, ctx, componentContext, batchResourceAssignment); + + logger.debug("Batch Resource data status ({})", TransformationUtils.getJson(batchResourceAssignment)); + } + } + } + + private void processBatchResourceAssignments(ResourceAssignmentData resourceAssignmentData, SvcLogicContext ctx, + Map componentContext, List batchResourceAssignment) + throws SvcLogicException { + + if (CollectionUtils.isNotEmpty(batchResourceAssignment)) { + + ResourceAssignment batchFirstResourceAssignment = batchResourceAssignment.get(0); + if (batchFirstResourceAssignment != null + && StringUtils.isNotBlank(batchFirstResourceAssignment.getDictionarySource())) { + String source = batchFirstResourceAssignment.getDictionarySource(); + // Processing their Source + logger.info("Processing source ({}) with batch ({}) ", source, batchResourceAssignment); + componentContext.put(ConfigModelConstant.PROPERTY_RESOURCE_ASSIGNMENTS, batchResourceAssignment); + + ProcessorFactory factory = + new ProcessorFactory(configResourceService, configRestAdaptorService, componentNodeService); + + ComponentNode processor = factory.getInstance(source); + + Map inParams = new HashMap<>(); + inParams.put(ConfigAssignmentConstants.INPUT_PARAM_REQUEST_ID, resourceAssignmentData.getRequestId()); + inParams.put(ConfigAssignmentConstants.INPUT_PARAM_RESOURCE_ID, resourceAssignmentData.getResourceId()); + inParams.put(ConfigAssignmentConstants.INPUT_PARAM_RESOURCE_TYPE, + resourceAssignmentData.getResourceType()); + inParams.put(ConfigAssignmentConstants.INPUT_PARAM_ACTION_NAME, resourceAssignmentData.getActionName()); + inParams.put(ConfigAssignmentConstants.INPUT_PARAM_TEMPLATE_NAMES, + resourceAssignmentData.getTemplateNames().toString()); + processor.process(inParams, ctx, componentContext); + } + } + } + + private void saveResourceMapping(ResourceAssignmentData resourceAssignmentData, String templateName, + List resourceAssignments) throws SvcLogicException { + if (resourceAssignmentData != null && StringUtils.isNotBlank(templateName)) { + + ConfigAssignmentPersistService configAssignmentPersistService = + new ConfigAssignmentPersistService(configResourceService); + configAssignmentPersistService.saveResourceMapping(resourceAssignmentData, templateName, + resourceAssignments); + } + } + + private void validateInputParams(ResourceAssignmentData resourceAssignmentData) throws SvcLogicException { + if (resourceAssignmentData == null) { + throw new SvcLogicException("Input parameters missing"); + } + + String requestId = resourceAssignmentData.getRequestId(); + if (StringUtils.isBlank(requestId)) { + throw new SvcLogicException("Request id parameters missing"); + } + String resourceId = resourceAssignmentData.getResourceId(); + if (StringUtils.isBlank(resourceId)) { + throw new SvcLogicException("Resource id parameter is missing"); + } + String resourceType = resourceAssignmentData.getResourceType(); + if (StringUtils.isBlank(resourceType)) { + throw new SvcLogicException("Resource type parameter is missing"); + } + String actionName = resourceAssignmentData.getActionName(); + if (StringUtils.isBlank(actionName)) { + throw new SvcLogicException("Action name is parameter is missing"); + } + + List templatesNames = resourceAssignmentData.getTemplateNames(); + if (CollectionUtils.isEmpty(templatesNames)) { + throw new SvcLogicException("Template names parameter missing"); + } + } + +} diff --git a/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentService.java b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentService.java new file mode 100644 index 000000000..e0fcc9175 --- /dev/null +++ b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentService.java @@ -0,0 +1,32 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.onap.ccsdk.config.assignment.service; + +import java.util.List; +import org.onap.ccsdk.config.assignment.data.ResourceAssignmentData; +import org.onap.ccsdk.config.model.data.ResourceAssignment; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; + +public interface ConfigAssignmentService { + + public void resolveResources(ResourceAssignmentData resourceAssignmentData) throws SvcLogicException; + + public void saveResourceMapping(ResourceAssignmentData resourceAssignmentData, String templateName, + List resourceAssignments) throws SvcLogicException; + + public ResourceAssignmentData generateTemplateResourceMash(ResourceAssignmentData resourceAssignmentData) + throws SvcLogicException; + +} diff --git a/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentServiceImpl.java b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentServiceImpl.java new file mode 100644 index 000000000..48e15b15a --- /dev/null +++ b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentServiceImpl.java @@ -0,0 +1,76 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.onap.ccsdk.config.assignment.service; + +import java.util.List; +import org.onap.ccsdk.config.assignment.data.ResourceAssignmentData; +import org.onap.ccsdk.config.data.adaptor.service.ConfigResourceService; +import org.onap.ccsdk.config.generator.service.ConfigGeneratorService; +import org.onap.ccsdk.config.model.data.ResourceAssignment; +import org.onap.ccsdk.config.model.service.ComponentNodeService; +import org.onap.ccsdk.config.model.service.ConfigModelService; +import org.onap.ccsdk.config.rest.adaptor.service.ConfigRestAdaptorService; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +public class ConfigAssignmentServiceImpl implements ConfigAssignmentService { + + private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigAssignmentServiceImpl.class); + + private ComponentNodeService componentNodeService; + private ConfigResourceService configResourceService; + private ConfigModelService configModelService; + private ConfigRestAdaptorService configRestAdaptorService; + private ConfigGeneratorService configGeneratorService; + + private static final String CLASS_NAME = "ConfigAssignmentServiceImpl"; + + public ConfigAssignmentServiceImpl(ConfigResourceService configResourceService, + ConfigRestAdaptorService configRestAdaptorService, ConfigModelService configModelService, + ComponentNodeService componentNodeService, ConfigGeneratorService configGeneratorService) { + logger.info("{} Constuctor Initated...", CLASS_NAME); + this.componentNodeService = componentNodeService; + this.configResourceService = configResourceService; + this.configModelService = configModelService; + this.configRestAdaptorService = configRestAdaptorService; + this.configGeneratorService = configGeneratorService; + } + + @Override + public void resolveResources(ResourceAssignmentData resourceAssignmentData) throws SvcLogicException { + ConfigAssignmentProcessService configAssignmentProcessService = + new ConfigAssignmentProcessService(configResourceService, configRestAdaptorService, configModelService, + componentNodeService, configGeneratorService); + configAssignmentProcessService.resolveResources(resourceAssignmentData); + } + + @Override + public void saveResourceMapping(ResourceAssignmentData resourceAssignmentData, String templateName, + List resourceAssignments) throws SvcLogicException { + ConfigAssignmentPersistService configAssignmentPersistService = + new ConfigAssignmentPersistService(configResourceService); + configAssignmentPersistService.saveResourceMapping(resourceAssignmentData, templateName, resourceAssignments); + } + + @Override + public ResourceAssignmentData generateTemplateResourceMash(ResourceAssignmentData resourceAssignmentData) + throws SvcLogicException { + ConfigPreviewService configPreviewService = + new ConfigPreviewService(configResourceService, configModelService, configGeneratorService); + return configPreviewService.generateTemplateResourceMash(resourceAssignmentData); + } + +} diff --git a/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentUtils.java b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentUtils.java new file mode 100644 index 000000000..1f74b71ad --- /dev/null +++ b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigAssignmentUtils.java @@ -0,0 +1,302 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.onap.ccsdk.config.assignment.service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.IteratorUtils; +import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.onap.ccsdk.config.data.adaptor.domain.ResourceAssignmentData; +import org.onap.ccsdk.config.model.ConfigModelConstant; +import org.onap.ccsdk.config.model.ConfigModelException; +import org.onap.ccsdk.config.model.ValidTypes; +import org.onap.ccsdk.config.model.data.DataType; +import org.onap.ccsdk.config.model.data.EntrySchema; +import org.onap.ccsdk.config.model.data.PropertyDefinition; +import org.onap.ccsdk.config.model.data.ResourceAssignment; +import org.onap.ccsdk.config.model.data.dict.ResourceDefinition; +import org.onap.ccsdk.config.model.domain.ResourceDictionary; +import org.onap.ccsdk.config.model.utils.JsonUtils; +import org.onap.ccsdk.config.model.utils.ResourceAssignmentUtils; +import org.onap.ccsdk.config.model.utils.TransformationUtils; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import com.fasterxml.jackson.databind.node.ObjectNode; + +public class ConfigAssignmentUtils { + + private ConfigAssignmentUtils() { + + } + + private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigAssignmentUtils.class); + + public static synchronized Object getContextKeyValue(SvcLogicContext context, String key) { + Object value = null; + if (context != null && key != null) { + if (context.getAttributeKeySet().contains(key)) { + String strValue = context.getAttribute(key); + if (StringUtils.isNotBlank(strValue)) { + value = strValue; + } + } else { + // Do Nothing + } + } + return value; + } + + /* + * Populate the Field property type for the Data type + */ + public static synchronized String getPropertyType(SvcLogicContext ctx, String dataTypeName, String propertyName) + throws SvcLogicException { + String type = ValidTypes.DATA_TYPE_STRING; + try { + if (ctx != null && StringUtils.isNotBlank(dataTypeName) && StringUtils.isNotBlank(propertyName)) { + String dataTypeContent = ctx.getAttribute(ConfigModelConstant.PROPERTY_DATA_TYPES_DOT + dataTypeName); + if (StringUtils.isNotBlank(dataTypeContent)) { + DataType dataType = TransformationUtils.readValue(dataTypeContent, DataType.class); + if (dataType != null && dataType.getProperties() != null + && dataType.getProperties().containsKey(propertyName)) { + PropertyDefinition propertyDefinition = dataType.getProperties().get(propertyName); + if (StringUtils.isNotBlank(propertyDefinition.getType())) { + type = propertyDefinition.getType(); + logger.trace("Data type({})'s property ({}) is ({})", dataTypeName, propertyName, type); + } else { + throw new SvcLogicException(String.format("Couldn't get data type (%s) ", dataTypeName)); + } + } + } else { + throw new SvcLogicException(String.format("Couldn't get data type (%s) content", dataTypeName)); + } + } + } catch (Exception e) { + logger.error("couldn't get data type({})'s property ({}), type ({}), error message ({}).", dataTypeName, + propertyName, type, e.getMessage()); + throw new SvcLogicException(e.getMessage()); + } + return type; + } + + /* + * Populate the Field property type for the Data type + */ + public static synchronized PropertyDefinition getPropertyDefinition(SvcLogicContext ctx, String dataTypeName, + String propertyName) throws SvcLogicException { + PropertyDefinition propertyDefinition = null; + try { + if (ctx != null && StringUtils.isNotBlank(dataTypeName) && StringUtils.isNotBlank(propertyName)) { + String dataTypeContent = ctx.getAttribute(ConfigModelConstant.PROPERTY_DATA_TYPES_DOT + dataTypeName); + if (StringUtils.isNotBlank(dataTypeContent)) { + DataType dataType = TransformationUtils.readValue(dataTypeContent, DataType.class); + if (dataType != null && dataType.getProperties() != null + && dataType.getProperties().containsKey(propertyName)) { + propertyDefinition = dataType.getProperties().get(propertyName); + if (propertyDefinition == null) { + throw new SvcLogicException(String.format("couldn't get data type (%s) ", dataTypeName)); + } + } + } else { + throw new SvcLogicException(String.format("couldn't get data type (%s) content.", dataTypeName)); + } + } + } catch (Exception e) { + throw new SvcLogicException(e.getMessage()); + } + return propertyDefinition; + } + + public static synchronized ResourceDefinition getDictionaryDefinition(Map dictionaries, + String dictionaryName) { + ResourceDefinition resourceDefinition = null; + if (dictionaries != null && StringUtils.isNotBlank(dictionaryName)) { + ResourceDictionary resourceDictionary = dictionaries.get(dictionaryName); + if (resourceDictionary != null && StringUtils.isNotBlank(resourceDictionary.getDefinition())) { + resourceDefinition = + TransformationUtils.readValue(resourceDictionary.getDefinition(), ResourceDefinition.class); + } + } + return resourceDefinition; + } + + @SuppressWarnings("squid:S3776") + public static synchronized void populateValueForOutputMapping(SvcLogicContext ctx, + Map componentContext, ResourceAssignment resourceAssignment, + Map outputKeyMapping, JsonNode responseNode) + throws ConfigModelException, SvcLogicException { + if (resourceAssignment == null) { + throw new SvcLogicException("resourceAssignment is null."); + } + + if (ctx == null) { + throw new SvcLogicException("service logic context is null."); + } + + if (componentContext == null) { + throw new SvcLogicException("component context is null."); + } + + logger.info("populating value for output mapping ({}), from json ({})", outputKeyMapping, responseNode); + String dictionaryName = resourceAssignment.getDictionaryName(); + String type = resourceAssignment.getProperty().getType(); + + String entrySchema = null; + if (ValidTypes.getPrimitivePropertType().contains(type)) { + ResourceAssignmentUtils.setResourceDataValue(componentContext, resourceAssignment, responseNode); + } else if (ValidTypes.getListPropertType().contains(type)) { + // Array Types + if (resourceAssignment.getProperty().getEntrySchema() != null) { + entrySchema = resourceAssignment.getProperty().getEntrySchema().getType(); + } + + if (StringUtils.isNotBlank(entrySchema)) { + ArrayNode arrayNode = JsonNodeFactory.instance.arrayNode(); + if (ValidTypes.getPrimitivePropertType().contains(entrySchema)) { + arrayNode = (ArrayNode) responseNode; + } else if (MapUtils.isNotEmpty(outputKeyMapping)) { + List responseArrayNode = IteratorUtils.toList(responseNode.elements()); + for (JsonNode responseSingleJsonNode : responseArrayNode) { + if (responseSingleJsonNode != null) { + ObjectNode arrayChildNode = JsonNodeFactory.instance.objectNode(); + for (Map.Entry mapping : outputKeyMapping.entrySet()) { + JsonNode responseKeyValue = responseSingleJsonNode.get(mapping.getKey()); + + String propertyTypeForDataType = + ConfigAssignmentUtils.getPropertyType(ctx, entrySchema, mapping.getKey()); + logger.info("For List Type Resource: key ({}), value ({}), type ({})", + mapping.getKey(), responseKeyValue, propertyTypeForDataType); + JsonUtils.populateJsonNodeValues(mapping.getValue(), responseKeyValue, + propertyTypeForDataType, arrayChildNode); + } + arrayNode.add(arrayChildNode); + } + } + } else { + arrayNode = (ArrayNode) responseNode; + } + // Set the List of Complex Values + ResourceAssignmentUtils.setResourceDataValue(componentContext, resourceAssignment, arrayNode); + } else { + throw new SvcLogicException( + String.format("Entry schema is not defined for dictionary (%s) info", dictionaryName)); + } + } else { + // Complex Types + ObjectNode objectNode = null; + if (MapUtils.isNotEmpty(outputKeyMapping)) { + objectNode = JsonNodeFactory.instance.objectNode(); + for (Map.Entry mapping : outputKeyMapping.entrySet()) { + JsonNode responseKeyValue = responseNode.get(mapping.getKey()); + String propertyTypeForDataType = + ConfigAssignmentUtils.getPropertyType(ctx, entrySchema, mapping.getKey()); + logger.info("For Complex Type Resource: key ({}), value ({}), type ({})", mapping.getKey(), + responseKeyValue, propertyTypeForDataType); + JsonUtils.populateJsonNodeValues(mapping.getValue(), responseKeyValue, propertyTypeForDataType, + objectNode); + } + } else { + objectNode = (ObjectNode) responseNode; + } + ResourceAssignmentUtils.setResourceDataValue(componentContext, resourceAssignment, objectNode); + } + } + + @SuppressWarnings("squid:S3776") + public static synchronized List convertResoureAssignmentDataList( + List resourceAssignmentDataList) { + List assignments = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(resourceAssignmentDataList)) { + for (ResourceAssignmentData resourceAssignmentData : resourceAssignmentDataList) { + if (resourceAssignmentData != null) { + ResourceAssignment resourceAssignment = new ResourceAssignment(); + resourceAssignment.setName(resourceAssignmentData.getTemplateKeyName()); + resourceAssignment.setVersion(resourceAssignmentData.getVersion()); + resourceAssignment.setUpdatedBy(resourceAssignmentData.getUpdatedBy()); + resourceAssignment.setUpdatedDate(resourceAssignmentData.getUpdatedDate()); + resourceAssignment.setDictionaryName(resourceAssignmentData.getResourceName()); + resourceAssignment.setDictionarySource(resourceAssignmentData.getSource()); + resourceAssignment.setStatus(resourceAssignmentData.getStatus()); + resourceAssignment.setMessage(resourceAssignmentData.getMessage()); + PropertyDefinition property = new PropertyDefinition(); + property.setType(resourceAssignmentData.getDataType()); + + if (StringUtils.isNotBlank(resourceAssignmentData.getResourceValue())) { + if (ValidTypes.getPrimitivePropertType().contains(resourceAssignmentData.getDataType())) { + property.setValue(resourceAssignmentData.getResourceValue()); + } else { + JsonNode valueNode = + TransformationUtils.getJsonNodeForString(resourceAssignmentData.getResourceValue()); + property.setValue(valueNode); + } + } + if (StringUtils.isNotBlank(resourceAssignmentData.getEntrySchema())) { + EntrySchema entrySchema = new EntrySchema(); + entrySchema.setType(resourceAssignmentData.getEntrySchema()); + property.setEntrySchema(entrySchema); + } else { + property.setEntrySchema(null); + } + resourceAssignment.setProperty(property); + assignments.add(resourceAssignment); + } + } + + } + return assignments; + } + + @SuppressWarnings("squid:S3776") + public static synchronized List convertResoureAssignmentList( + List assignments) { + List resourceAssignmentDataList = new ArrayList<>(); + if (CollectionUtils.isNotEmpty(assignments)) { + for (ResourceAssignment assignment : assignments) { + if (assignment != null) { + ResourceAssignmentData resourceAssignmentData = new ResourceAssignmentData(); + resourceAssignmentData.setTemplateKeyName(assignment.getName()); + resourceAssignmentData.setVersion(assignment.getVersion()); + resourceAssignmentData.setUpdatedBy(assignment.getUpdatedBy()); + resourceAssignmentData.setUpdatedDate(assignment.getUpdatedDate()); + if (assignment.getProperty() != null) { + resourceAssignmentData.setDataType(assignment.getProperty().getType()); + if (assignment.getProperty().getEntrySchema() != null) { + resourceAssignmentData.setEntrySchema(assignment.getProperty().getEntrySchema().getType()); + } + if (assignment.getProperty().getValue() != null) { + String valueContent = TransformationUtils.getJson(assignment.getProperty().getValue()); + resourceAssignmentData.setResourceValue(valueContent); + } + } + resourceAssignmentData.setResourceName(assignment.getDictionaryName()); + resourceAssignmentData.setSource(assignment.getDictionarySource()); + resourceAssignmentData.setStatus(assignment.getStatus()); + resourceAssignmentData.setMessage(assignment.getMessage()); + resourceAssignmentDataList.add(resourceAssignmentData); + } + } + } + return resourceAssignmentDataList; + } + +} diff --git a/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigPreviewService.java b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigPreviewService.java new file mode 100644 index 000000000..7aa2d438a --- /dev/null +++ b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ConfigPreviewService.java @@ -0,0 +1,116 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.onap.ccsdk.config.assignment.service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.onap.ccsdk.config.assignment.data.ResourceAssignmentData; +import org.onap.ccsdk.config.data.adaptor.domain.ConfigResource; +import org.onap.ccsdk.config.data.adaptor.service.ConfigResourceService; +import org.onap.ccsdk.config.generator.data.ConfigGeneratorInfo; +import org.onap.ccsdk.config.generator.service.ConfigGeneratorService; +import org.onap.ccsdk.config.model.ConfigModelConstant; +import org.onap.ccsdk.config.model.service.ConfigModelService; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +public class ConfigPreviewService { + private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigAssignmentPersistService.class); + private ConfigResourceService configResourceService; + private ConfigModelService configModelService; + private ConfigGeneratorService configGeneratorService; + + public ConfigPreviewService(ConfigResourceService configResourceService, ConfigModelService configModelService, + ConfigGeneratorService configGeneratorService) { + this.configResourceService = configResourceService; + this.configModelService = configModelService; + this.configGeneratorService = configGeneratorService; + } + + public String generatePreview(String templateContent, String templateData) throws SvcLogicException { + String mashedData = ""; + ConfigGeneratorInfo configGeneratorInfo = + configGeneratorService.generateConfiguration(templateContent, templateData); + if (configGeneratorInfo != null) { + mashedData = configGeneratorInfo.getMashedData(); + } + return mashedData; + } + + public ResourceAssignmentData generateTemplateResourceMash(ResourceAssignmentData resourceAssignmentData) + throws SvcLogicException { + if (resourceAssignmentData == null) { + throw new SvcLogicException("Resource assignment data is missing"); + } + if (StringUtils.isBlank(resourceAssignmentData.getServiceTemplateName())) { + throw new SvcLogicException("Service template name is missing"); + } + if (StringUtils.isBlank(resourceAssignmentData.getServiceTemplateVersion())) { + throw new SvcLogicException("Service template version is missing"); + } + if (StringUtils.isBlank(resourceAssignmentData.getResourceType())) { + throw new SvcLogicException("Resource type is missing"); + } + if (StringUtils.isBlank(resourceAssignmentData.getResourceId())) { + throw new SvcLogicException("Resource Id is missing"); + } + if (StringUtils.isBlank(resourceAssignmentData.getActionName())) { + throw new SvcLogicException("Action name is missing"); + } + + String serviceTemplateName = resourceAssignmentData.getServiceTemplateName(); + String serviceTemplateVersion = resourceAssignmentData.getServiceTemplateVersion(); + String actionName = resourceAssignmentData.getActionName(); + String resourceId = resourceAssignmentData.getResourceId(); + String resourceType = resourceAssignmentData.getResourceType(); + String inputData = "{}"; + + Map context = new HashMap<>(); + context.put(ConfigModelConstant.PROPERTY_ACTION_NAME, actionName); + context = configModelService.prepareContext(context, inputData, serviceTemplateName, serviceTemplateVersion); + + ConfigResource configResourceQuery = new ConfigResource(); + configResourceQuery.setServiceTemplateVersion(serviceTemplateName); + configResourceQuery.setServiceTemplateVersion(serviceTemplateVersion); + configResourceQuery.setRecipeName(actionName); + configResourceQuery.setResourceId(resourceId); + configResourceQuery.setResourceType(resourceType); + + List configResources = configResourceService.getConfigResource(configResourceQuery); + if (CollectionUtils.isNotEmpty(configResources)) { + for (ConfigResource cr : configResources) { + String templateContent = context + .get(ConfigModelConstant.PROPERTY_NODE_TEMPLATES_DOT + cr.getTemplateName() + ".content"); + String templateData = cr.getResourceData(); + String previewContent = generatePreview(templateContent, templateData); + resourceAssignmentData.getTemplatesMashedContents().put(cr.getTemplateName(), previewContent); + logger.info("Preview generated for template name ({}) ", cr.getTemplateName()); + logger.trace("Preview generated for preview ({}) ", previewContent); + } + } else { + logger.info( + "Couldn't get config resource for service template name ({}) service template version ({})" + + " action ({}) resource id ({}) resource type ({})", + serviceTemplateName, serviceTemplateVersion, actionName, resourceId, resourceType); + } + return resourceAssignmentData; + + } + +} diff --git a/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ResourceDictionaryService.java b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ResourceDictionaryService.java new file mode 100644 index 000000000..97a4dd45e --- /dev/null +++ b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ResourceDictionaryService.java @@ -0,0 +1,109 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.onap.ccsdk.config.assignment.service; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.lang3.StringUtils; +import org.onap.ccsdk.config.model.data.ResourceAssignment; +import org.onap.ccsdk.config.model.data.dict.ResourceDefinition; +import org.onap.ccsdk.config.model.domain.ResourceDictionary; +import org.onap.ccsdk.config.model.utils.TransformationUtils; +import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorConstants; +import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorException; +import org.onap.ccsdk.config.rest.adaptor.service.ConfigRestAdaptorService; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +public class ResourceDictionaryService { + + private static EELFLogger logger = EELFManager.getInstance().getLogger(ResourceDictionaryService.class); + private ConfigRestAdaptorService configRestAdaptorService; + + public ResourceDictionaryService(ConfigRestAdaptorService configRestAdaptorService) { + this.configRestAdaptorService = configRestAdaptorService; + } + + @SuppressWarnings("squid:S3776") + public Map getDataDictionaryDefinitions(List resourceAssignments) + throws SvcLogicException { + try { + Map dictionaries = new HashMap<>(); + if (resourceAssignments != null) { + List names = new ArrayList<>(); + for (ResourceAssignment resourceAssignment : resourceAssignments) { + if (resourceAssignment != null && StringUtils.isNotBlank(resourceAssignment.getDictionaryName())) { + + if (!names.contains(resourceAssignment.getDictionaryName())) { + names.add(resourceAssignment.getDictionaryName()); + } + + if (resourceAssignment.getDependencies() != null + && !resourceAssignment.getDependencies().isEmpty()) { + List dependencieNames = resourceAssignment.getDependencies(); + for (String dependencieName : dependencieNames) { + if (StringUtils.isNotBlank(dependencieName) && !names.contains(dependencieName)) { + names.add(dependencieName); + } + } + } + } + } + queryResourceDictionaryDefinitions(dictionaries, names); + } + return dictionaries; + } catch (Exception e) { + throw new SvcLogicException("Failed in getting resource data dictionary : " + e.getMessage()); + } + + } + + @SuppressWarnings("squid:S3776") + private void queryResourceDictionaryDefinitions(Map dictionaries, List names) + throws SvcLogicException, ConfigRestAdaptorException { + logger.info("Getting resource dictionary definition for the names ({})", names); + if (!names.isEmpty()) { + + String dictionaryContents = configRestAdaptorService.postResource( + ConfigRestAdaptorConstants.SELECTOR_MODEL_SERVICE, "dictionarybynames", names, String.class); + + if (StringUtils.isNotBlank(dictionaryContents)) { + List dataDictionaries = + TransformationUtils.getListfromJson(dictionaryContents, ResourceDictionary.class); + if (dataDictionaries != null) { + for (ResourceDictionary dataDictionary : dataDictionaries) { + if (dataDictionary != null && StringUtils.isNotBlank(dataDictionary.getName()) + && StringUtils.isNotBlank(dataDictionary.getDefinition())) { + ResourceDefinition resourceDefinition = TransformationUtils + .readValue(dataDictionary.getDefinition(), ResourceDefinition.class); + if (resourceDefinition != null && StringUtils.isNotBlank(resourceDefinition.getName())) { + dictionaries.put(resourceDefinition.getName(), resourceDefinition); + } else { + throw new SvcLogicException( + "Failed in getting resource data dictionary definition for : " + + dataDictionary.getName()); + } + } + } + } + } else { + logger.warn("No resource dictionary definition found for the names ({})", names); + } + } + } +} diff --git a/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ResourceModelService.java b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ResourceModelService.java new file mode 100644 index 000000000..7eb21967d --- /dev/null +++ b/blueprints-processor/plugin/assignment-provider/src/main/java/org/onap/ccsdk/config/assignment/service/ResourceModelService.java @@ -0,0 +1,94 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package org.onap.ccsdk.config.assignment.service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; +import org.onap.ccsdk.config.model.data.ResourceAssignment; +import org.onap.ccsdk.config.model.service.ConfigModelService; +import org.onap.ccsdk.config.model.utils.TransformationUtils; +import org.onap.ccsdk.config.model.validator.ResourceAssignmentValidator; +import org.onap.ccsdk.sli.core.sli.SvcLogicContext; +import org.onap.ccsdk.sli.core.sli.SvcLogicException; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; + +public class ResourceModelService { + private static EELFLogger logger = EELFManager.getInstance().getLogger(ResourceModelService.class); + + private ConfigModelService configModelService; + + public ResourceModelService(ConfigModelService configModelService) { + this.configModelService = configModelService; + } + + public Map getTemplatesContents(SvcLogicContext ctx, List templateNames) + throws SvcLogicException { + Map templatesContents = new HashMap<>(); + try { + if (CollectionUtils.isNotEmpty(templateNames)) { + for (String templateName : templateNames) { + String templateContent = this.configModelService.getNodeTemplateContent(ctx, templateName); + logger.trace("Processing template ({}) with content : {}", templateName, templateContent); + templatesContents.put(templateName, templateContent); + } + } + } catch (Exception e) { + throw new SvcLogicException(e.getMessage()); + } + return templatesContents; + } + + public Map> getTemplatesResourceAssignments(SvcLogicContext ctx, + List templateNames) throws SvcLogicException { + Map> templatesResourceAssignments = new HashMap<>(); + try { + if (CollectionUtils.isNotEmpty(templateNames)) { + for (String templateName : templateNames) { + String resourceMappingContent = this.configModelService.getNodeTemplateMapping(ctx, templateName); + logger.info("Processing template ({}) with resource assignment content : {}", templateName, + resourceMappingContent); + + if (StringUtils.isNotBlank(resourceMappingContent)) { + + List resourceAssignments = + TransformationUtils.getListfromJson(resourceMappingContent, ResourceAssignment.class); + + if (resourceAssignments != null) { + ResourceAssignmentValidator resourceAssignmentValidator = + new ResourceAssignmentValidator(resourceAssignments); + resourceAssignmentValidator.validateResourceAssignment(); + logger.info("Resource assignment validated successfully for the template ({})", + templateName); + templatesResourceAssignments.put(templateName, resourceAssignments); + } else { + throw new SvcLogicException(String.format( + "Failed to convert assignment content (%s) to object", resourceMappingContent)); + } + } else { + // Do nothing, because som e templates may not have mappings + } + } + } + } catch (Exception e) { + throw new SvcLogicException(e.getMessage()); + } + + return templatesResourceAssignments; + } +} diff --git a/blueprints-processor/plugin/assignment-provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml b/blueprints-processor/plugin/assignment-provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml new file mode 100644 index 000000000..ebefd645c --- /dev/null +++ b/blueprints-processor/plugin/assignment-provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml @@ -0,0 +1,67 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -- cgit 1.2.3-korg