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.config.model.service;
\r
20 import java.util.List;
\r
21 import java.util.Map;
\r
22 import org.apache.commons.lang3.StringUtils;
\r
23 import org.onap.ccsdk.config.model.ConfigModelConstant;
\r
24 import org.onap.ccsdk.config.model.domain.ConfigModel;
\r
25 import org.onap.ccsdk.config.model.domain.ConfigModelContent;
\r
26 import org.onap.ccsdk.config.model.utils.PrepareContextUtils;
\r
27 import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorConstants;
\r
28 import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorException;
\r
29 import org.onap.ccsdk.config.rest.adaptor.service.ConfigRestAdaptorService;
\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 ConfigBlueprintService {
\r
36 private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigBlueprintService.class);
\r
38 private final ConfigRestAdaptorService configRestAdaptorService;
\r
40 public ConfigBlueprintService(ConfigRestAdaptorService configRestAdaptorService) {
\r
41 this.configRestAdaptorService = configRestAdaptorService;
\r
44 public Map<String, String> prepareContext(Map<String, String> context, String input, String serviceTemplateName,
\r
45 String serviceTemplateVersion) throws SvcLogicException {
\r
47 PrepareContextUtils prepareContextUtils = new PrepareContextUtils();
\r
48 String serviceTemplateContent = getServiceModel(context, serviceTemplateName, serviceTemplateVersion);
\r
50 if (StringUtils.isBlank(serviceTemplateContent)) {
\r
51 throw new SvcLogicException(String.format("Failed to get the Service Template (%s), version (%s)",
\r
52 serviceTemplateName, serviceTemplateVersion));
\r
55 return prepareContextUtils.prepareContext(context, input, serviceTemplateContent);
\r
56 } catch (Exception e) {
\r
57 throw new SvcLogicException(e.getMessage(), e);
\r
61 @SuppressWarnings("squid:S3776")
\r
62 private String getServiceModel(Map<String, String> context, String serviceTemplateName,
\r
63 String serviceTemplateVersion) throws SvcLogicException, ConfigRestAdaptorException {
\r
64 String content = null;
\r
66 logger.info("Getting service template ({}) of version ({}) ", serviceTemplateName, serviceTemplateVersion);
\r
68 String path = "configmodelbyname/" + serviceTemplateName + "/version/" + serviceTemplateVersion;
\r
70 ConfigModel configModel = configRestAdaptorService
\r
71 .getResource(ConfigRestAdaptorConstants.SELECTOR_MODEL_SERVICE, path, ConfigModel.class);
\r
73 if (configModel == null || configModel.getConfigModelContents() == null
\r
74 || configModel.getConfigModelContents().isEmpty()) {
\r
75 throw new SvcLogicException("Service template model is missing for service template name ("
\r
76 + serviceTemplateName + "), service template version (" + serviceTemplateVersion + ") ");
\r
78 if (configModel.getPublished() == null || !configModel.getPublished().equalsIgnoreCase("Y")) {
\r
79 throw new SvcLogicException(String.format(
\r
80 "Service template model is not published for service template (%s) ", serviceTemplateName));
\r
83 List<ConfigModelContent> configModelContents = configModel.getConfigModelContents();
\r
84 for (ConfigModelContent configModelContent : configModelContents) {
\r
85 if (configModelContent != null) {
\r
86 if (ConfigModelConstant.MODEL_CONTENT_TYPE_TOSCA_JSON.equals(configModelContent.getContentType())) {
\r
87 content = configModelContent.getContent();
\r
88 } else if (ConfigModelConstant.MODEL_CONTENT_TYPE_TEMPLATE
\r
89 .equals(configModelContent.getContentType())) {
\r
90 context.put(ConfigModelConstant.PROPERTY_NODE_TEMPLATES_DOT + configModelContent.getName()
\r
91 + ".content", configModelContent.getContent());
\r
95 logger.trace("Service model data : {} ", content);
\r