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.HashMap;
\r
21 import java.util.List;
\r
22 import java.util.Map;
\r
23 import org.apache.commons.collections.CollectionUtils;
\r
24 import org.apache.commons.lang3.StringUtils;
\r
25 import org.onap.ccsdk.features.model.data.ResourceAssignment;
\r
26 import org.onap.ccsdk.features.model.service.ConfigModelService;
\r
27 import org.onap.ccsdk.features.model.utils.TransformationUtils;
\r
28 import org.onap.ccsdk.features.model.validator.ResourceAssignmentValidator;
\r
29 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
\r
30 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
\r
31 import com.att.eelf.configuration.EELFLogger;
\r
32 import com.att.eelf.configuration.EELFManager;
\r
34 public class ResourceModelService {
\r
35 private static EELFLogger logger = EELFManager.getInstance().getLogger(ResourceModelService.class);
\r
37 private ConfigModelService configModelService;
\r
39 public ResourceModelService(ConfigModelService configModelService) {
\r
40 this.configModelService = configModelService;
\r
43 public Map<String, String> getTemplatesContents(SvcLogicContext ctx, List<String> templateNames)
\r
44 throws SvcLogicException {
\r
45 Map<String, String> templatesContents = new HashMap<>();
\r
47 if (CollectionUtils.isNotEmpty(templateNames)) {
\r
48 for (String templateName : templateNames) {
\r
49 String templateContent = this.configModelService.getNodeTemplateContent(ctx, templateName);
\r
50 logger.trace("Processing template ({}) with content : {}", templateName, templateContent);
\r
51 templatesContents.put(templateName, templateContent);
\r
54 } catch (Exception e) {
\r
55 throw new SvcLogicException(e.getMessage());
\r
57 return templatesContents;
\r
60 public Map<String, List<ResourceAssignment>> getTemplatesResourceAssignments(SvcLogicContext ctx,
\r
61 List<String> templateNames) throws SvcLogicException {
\r
62 Map<String, List<ResourceAssignment>> templatesResourceAssignments = new HashMap<>();
\r
64 if (CollectionUtils.isNotEmpty(templateNames)) {
\r
65 for (String templateName : templateNames) {
\r
66 String resourceMappingContent = this.configModelService.getNodeTemplateMapping(ctx, templateName);
\r
67 logger.info("Processing template ({}) with resource assignment content : {}", templateName,
\r
68 resourceMappingContent);
\r
70 if (StringUtils.isNotBlank(resourceMappingContent)) {
\r
72 List<ResourceAssignment> resourceAssignments =
\r
73 TransformationUtils.getListfromJson(resourceMappingContent, ResourceAssignment.class);
\r
75 if (resourceAssignments != null) {
\r
76 ResourceAssignmentValidator resourceAssignmentValidator =
\r
77 new ResourceAssignmentValidator(resourceAssignments);
\r
78 resourceAssignmentValidator.validateResourceAssignment();
\r
79 logger.info("Resource assignment validated successfully for the template ({})",
\r
81 templatesResourceAssignments.put(templateName, resourceAssignments);
\r
83 throw new SvcLogicException(String.format(
\r
84 "Failed to convert assignment content (%s) to object", resourceMappingContent));
\r
87 // Do nothing, because som e templates may not have mappings
\r
91 } catch (Exception e) {
\r
92 throw new SvcLogicException(e.getMessage());
\r
95 return templatesResourceAssignments;
\r