Controller Blueprints Nitrogen to Oxygen Migration
[ccsdk/features.git] / blueprints-processor / plugin / assignment-provider / src / main / java / org / onap / ccsdk / features / assignment / service / ResourceModelService.java
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  * \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
8  * \r
9  * http://www.apache.org/licenses/LICENSE-2.0\r
10  * \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
16  */\r
17 \r
18 package org.onap.ccsdk.features.assignment.service;\r
19 \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
33 \r
34 public class ResourceModelService {\r
35     private static EELFLogger logger = EELFManager.getInstance().getLogger(ResourceModelService.class);\r
36 \r
37     private ConfigModelService configModelService;\r
38 \r
39     public ResourceModelService(ConfigModelService configModelService) {\r
40         this.configModelService = configModelService;\r
41     }\r
42 \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
46         try {\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
52                 }\r
53             }\r
54         } catch (Exception e) {\r
55             throw new SvcLogicException(e.getMessage());\r
56         }\r
57         return templatesContents;\r
58     }\r
59 \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
63         try {\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
69 \r
70                     if (StringUtils.isNotBlank(resourceMappingContent)) {\r
71 \r
72                         List<ResourceAssignment> resourceAssignments =\r
73                                 TransformationUtils.getListfromJson(resourceMappingContent, ResourceAssignment.class);\r
74 \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
80                                     templateName);\r
81                             templatesResourceAssignments.put(templateName, resourceAssignments);\r
82                         } else {\r
83                             throw new SvcLogicException(String.format(\r
84                                     "Failed to convert assignment content (%s) to object", resourceMappingContent));\r
85                         }\r
86                     } else {\r
87                         // Do nothing, because som e templates may not have mappings\r
88                     }\r
89                 }\r
90             }\r
91         } catch (Exception e) {\r
92             throw new SvcLogicException(e.getMessage());\r
93         }\r
94 \r
95         return templatesResourceAssignments;\r
96     }\r
97 }\r