2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\r
5 * Licensed under the Apache License, Version 2.0 (the "License");
\r
6 * you may not use this file except in compliance with the License.
\r
7 * You may obtain a copy of the License at
\r
9 * http://www.apache.org/licenses/LICENSE-2.0
\r
11 * Unless required by applicable law or agreed to in writing, software
\r
12 * distributed under the License is distributed on an "AS IS" BASIS,
\r
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 * See the License for the specific language governing permissions and
\r
15 * limitations under the License.
\r
18 package org.onap.ccsdk.features.assignment.service;
\r
20 import java.util.List;
\r
21 import org.apache.commons.collections.CollectionUtils;
\r
22 import org.apache.commons.lang3.StringUtils;
\r
23 import org.onap.ccsdk.features.data.adaptor.DataAdaptorConstants;
\r
24 import org.onap.ccsdk.features.data.adaptor.domain.ConfigResource;
\r
25 import org.onap.ccsdk.features.data.adaptor.domain.ResourceAssignmentData;
\r
26 import org.onap.ccsdk.features.data.adaptor.domain.TransactionLog;
\r
27 import org.onap.ccsdk.features.data.adaptor.service.ConfigResourceService;
\r
28 import org.onap.ccsdk.features.model.ConfigModelConstant;
\r
29 import org.onap.ccsdk.features.model.data.ResourceAssignment;
\r
30 import org.onap.ccsdk.features.model.utils.ResourceAssignmentUtils;
\r
31 import org.onap.ccsdk.features.model.utils.TransformationUtils;
\r
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
\r
33 import com.att.eelf.configuration.EELFLogger;
\r
34 import com.att.eelf.configuration.EELFManager;
\r
36 public class ConfigAssignmentPersistService {
\r
38 private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigAssignmentPersistService.class);
\r
40 private ConfigResourceService configResourceService;
\r
42 public ConfigAssignmentPersistService(ConfigResourceService configResourceService) {
\r
43 this.configResourceService = configResourceService;
\r
46 public void saveResourceMapping(
\r
47 org.onap.ccsdk.features.assignment.data.ResourceAssignmentData resourceAssignmentData, String templateName,
\r
48 List<ResourceAssignment> resourceAssignments) throws SvcLogicException {
\r
51 if (resourceAssignmentData == null) {
\r
52 throw new SvcLogicException("Resource assignment data is missing");
\r
55 if (StringUtils.isBlank(resourceAssignmentData.getRequestId())) {
\r
56 logger.warn("Request Id ({}) is missing, may be getting request for resource update.",
\r
57 resourceAssignmentData.getRequestId());
\r
60 if (StringUtils.isBlank(resourceAssignmentData.getResourceId())) {
\r
61 throw new SvcLogicException("Resource Id is missing");
\r
64 if (StringUtils.isBlank(resourceAssignmentData.getResourceType())) {
\r
65 throw new SvcLogicException("Resource type is missing");
\r
68 if (StringUtils.isBlank(resourceAssignmentData.getActionName())) {
\r
69 throw new SvcLogicException("Action name is missing");
\r
72 if (StringUtils.isBlank(templateName)) {
\r
73 throw new SvcLogicException("template name is missing");
\r
76 StringBuilder builder = new StringBuilder();
\r
77 builder.append("Resource Assignment for Template Name :");
\r
78 builder.append(templateName);
\r
79 builder.append("\n");
\r
80 builder.append(TransformationUtils.getJson(resourceAssignments, true));
\r
82 configResourceService.save(new TransactionLog(resourceAssignmentData.getRequestId(),
\r
83 DataAdaptorConstants.LOG_MESSAGE_TYPE_LOG, builder.toString()));
\r
85 // Resource Data should be Regenerated based on the new Updates
\r
86 String resourceData = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments);
\r
88 List<ResourceAssignmentData> resourceAssignmentDataList =
\r
89 ConfigAssignmentUtils.convertResoureAssignmentList(resourceAssignments);
\r
91 ConfigResource configResource = new ConfigResource();
\r
92 configResource.setRequestId(resourceAssignmentData.getRequestId());
\r
93 configResource.setServiceTemplateName(resourceAssignmentData.getServiceTemplateName());
\r
94 configResource.setServiceTemplateVersion(resourceAssignmentData.getServiceTemplateVersion());
\r
95 configResource.setRecipeName(resourceAssignmentData.getActionName());
\r
96 configResource.setResourceId(resourceAssignmentData.getResourceId());
\r
97 configResource.setResourceType(resourceAssignmentData.getResourceType());
\r
98 configResource.setResourceData(resourceData);
\r
99 configResource.setTemplateName(templateName);
\r
100 configResource.setStatus(ConfigModelConstant.STATUS_SUCCESS);
\r
101 configResource.setUpdatedBy(ConfigModelConstant.USER_SYSTEM);
\r
103 if (CollectionUtils.isNotEmpty(resourceAssignmentDataList)) {
\r
104 configResource.setResourceAssignments(resourceAssignmentDataList);
\r
106 configResource = configResourceService.saveConfigResource(configResource);
\r
107 logger.info("Resource data saved successfully for the template ({}) with resource id ({})", templateName,
\r
108 configResource.getResourceId());
\r
110 builder = new StringBuilder();
\r
111 builder.append("Resource Data Template Name :");
\r
112 builder.append(templateName);
\r
113 builder.append("\n");
\r
114 builder.append(resourceData);
\r
115 configResourceService.save(new TransactionLog(resourceAssignmentData.getRequestId(),
\r
116 DataAdaptorConstants.LOG_MESSAGE_TYPE_LOG, builder.toString()));
\r
118 } catch (Exception e) {
\r
119 throw new SvcLogicException("ConfigAssignmentPersistService : " + e.getMessage(), e);
\r