2  * Copyright © 2017-2018 AT&T Intellectual Property.
\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 
   7  * http://www.apache.org/licenses/LICENSE-2.0
\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 
  15 package org.onap.ccsdk.config.assignment.service;
\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 
  31 public class ResourceModelService {
\r 
  32     private static EELFLogger logger = EELFManager.getInstance().getLogger(ResourceModelService.class);
\r 
  34     private ConfigModelService configModelService;
\r 
  36     public ResourceModelService(ConfigModelService configModelService) {
\r 
  37         this.configModelService = configModelService;
\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 
  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 
  51         } catch (Exception e) {
\r 
  52             throw new SvcLogicException(e.getMessage());
\r 
  54         return templatesContents;
\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 
  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 
  67                     if (StringUtils.isNotBlank(resourceMappingContent)) {
\r 
  69                         List<ResourceAssignment> resourceAssignments =
\r 
  70                                 TransformationUtils.getListfromJson(resourceMappingContent, ResourceAssignment.class);
\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 
  78                             templatesResourceAssignments.put(templateName, resourceAssignments);
\r 
  80                             throw new SvcLogicException(String.format(
\r 
  81                                     "Failed to convert assignment content (%s) to object", resourceMappingContent));
\r 
  84                         // Do nothing, because som e templates may not have mappings
\r 
  88         } catch (Exception e) {
\r 
  89             throw new SvcLogicException(e.getMessage());
\r 
  92         return templatesResourceAssignments;
\r