7eb21967dddb7435fb7ee143c4fd21afb8d48999
[ccsdk/features.git] /
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
5  * in compliance with the License. You may obtain a copy of the License at\r
6  * \r
7  * http://www.apache.org/licenses/LICENSE-2.0\r
8  * \r
9  * Unless required by applicable law or agreed to in writing, software distributed under the License\r
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
11  * or implied. See the License for the specific language governing permissions and limitations under\r
12  * the License.\r
13  */\r
14 \r
15 package org.onap.ccsdk.config.assignment.service;\r
16 \r
17 import java.util.HashMap;\r
18 import java.util.List;\r
19 import java.util.Map;\r
20 import org.apache.commons.collections.CollectionUtils;\r
21 import org.apache.commons.lang3.StringUtils;\r
22 import org.onap.ccsdk.config.model.data.ResourceAssignment;\r
23 import org.onap.ccsdk.config.model.service.ConfigModelService;\r
24 import org.onap.ccsdk.config.model.utils.TransformationUtils;\r
25 import org.onap.ccsdk.config.model.validator.ResourceAssignmentValidator;\r
26 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;\r
27 import org.onap.ccsdk.sli.core.sli.SvcLogicException;\r
28 import com.att.eelf.configuration.EELFLogger;\r
29 import com.att.eelf.configuration.EELFManager;\r
30 \r
31 public class ResourceModelService {\r
32     private static EELFLogger logger = EELFManager.getInstance().getLogger(ResourceModelService.class);\r
33     \r
34     private ConfigModelService configModelService;\r
35     \r
36     public ResourceModelService(ConfigModelService configModelService) {\r
37         this.configModelService = configModelService;\r
38     }\r
39     \r
40     public Map<String, String> getTemplatesContents(SvcLogicContext ctx, List<String> templateNames)\r
41             throws SvcLogicException {\r
42         Map<String, String> templatesContents = new HashMap<>();\r
43         try {\r
44             if (CollectionUtils.isNotEmpty(templateNames)) {\r
45                 for (String templateName : templateNames) {\r
46                     String templateContent = this.configModelService.getNodeTemplateContent(ctx, templateName);\r
47                     logger.trace("Processing template ({}) with  content : {}", templateName, templateContent);\r
48                     templatesContents.put(templateName, templateContent);\r
49                 }\r
50             }\r
51         } catch (Exception e) {\r
52             throw new SvcLogicException(e.getMessage());\r
53         }\r
54         return templatesContents;\r
55     }\r
56     \r
57     public Map<String, List<ResourceAssignment>> getTemplatesResourceAssignments(SvcLogicContext ctx,\r
58             List<String> templateNames) throws SvcLogicException {\r
59         Map<String, List<ResourceAssignment>> templatesResourceAssignments = new HashMap<>();\r
60         try {\r
61             if (CollectionUtils.isNotEmpty(templateNames)) {\r
62                 for (String templateName : templateNames) {\r
63                     String resourceMappingContent = this.configModelService.getNodeTemplateMapping(ctx, templateName);\r
64                     logger.info("Processing template ({}) with resource assignment content : {}", templateName,\r
65                             resourceMappingContent);\r
66                     \r
67                     if (StringUtils.isNotBlank(resourceMappingContent)) {\r
68                         \r
69                         List<ResourceAssignment> resourceAssignments =\r
70                                 TransformationUtils.getListfromJson(resourceMappingContent, ResourceAssignment.class);\r
71                         \r
72                         if (resourceAssignments != null) {\r
73                             ResourceAssignmentValidator resourceAssignmentValidator =\r
74                                     new ResourceAssignmentValidator(resourceAssignments);\r
75                             resourceAssignmentValidator.validateResourceAssignment();\r
76                             logger.info("Resource assignment validated successfully for the template ({})",\r
77                                     templateName);\r
78                             templatesResourceAssignments.put(templateName, resourceAssignments);\r
79                         } else {\r
80                             throw new SvcLogicException(String.format(\r
81                                     "Failed to convert assignment content (%s) to object", resourceMappingContent));\r
82                         }\r
83                     } else {\r
84                         // Do nothing, because som e templates may not have mappings\r
85                     }\r
86                 }\r
87             }\r
88         } catch (Exception e) {\r
89             throw new SvcLogicException(e.getMessage());\r
90         }\r
91         \r
92         return templatesResourceAssignments;\r
93     }\r
94 }\r