2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * Modifications copyright (c) 2018 Nokia
21 * ===================================================================
25 package org.onap.clamp.clds.client.req.policy;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.google.common.base.Strings;
30 import com.google.common.collect.ImmutableMap;
31 import org.onap.clamp.clds.config.ClampProperties;
32 import org.onap.clamp.clds.model.properties.ModelProperties;
33 import org.onap.clamp.clds.model.properties.PolicyChain;
34 import org.onap.clamp.clds.model.properties.PolicyItem;
35 import org.onap.policy.api.AttributeType;
36 import org.onap.policy.controlloop.policy.builder.BuilderException;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Component;
40 import java.io.UnsupportedEncodingException;
41 import java.util.HashMap;
45 public class OperationalPolicyAttributesConstructor {
47 private static final EELFLogger logger = EELFManager.getInstance()
48 .getLogger(OperationalPolicyAttributesConstructor.class);
49 static final String TEMPLATE_NAME = "templateName";
50 static final String CLOSED_LOOP_CONTROL_NAME = "closedLoopControlName";
51 static final String NOTIFICATION_TOPIC = "notificationTopic";
52 static final String OPERATION_TOPIC = "operationTopic";
53 static final String CONTROL_LOOP_YAML = "controlLoopYaml";
54 static final String CONTROLLER = "controller";
55 static final String RECIPE = "Recipe";
56 static final String MAX_RETRIES = "MaxRetries";
57 static final String RETRY_TIME_LIMIT = "RetryTimeLimit";
58 static final String RESOURCE_ID = "ResourceId";
59 static final String RECIPE_TOPIC = "RecipeTopic";
60 private OperationalPolicyYamlFormatter policyYamlFormatter;
63 protected OperationalPolicyAttributesConstructor(OperationalPolicyYamlFormatter policyYamlFormatter) {
64 this.policyYamlFormatter = policyYamlFormatter;
67 public Map<AttributeType, Map<String, String>> formatAttributes(ClampProperties refProp,
68 ModelProperties modelProperties,
69 String modelElementId, PolicyChain policyChain)
70 throws BuilderException, UnsupportedEncodingException {
71 modelProperties.setCurrentModelElementId(modelElementId);
72 modelProperties.setPolicyUniqueId(policyChain.getPolicyId());
74 String globalService = modelProperties.getGlobal().getService();
76 Map<String, String> ruleAttributes = prepareRuleAttributes(refProp, modelProperties, modelElementId,
77 policyChain, globalService);
78 Map<String, String> matchingAttributes = prepareMatchingAttributes(refProp, globalService);
80 return createAttributesMap(matchingAttributes, ruleAttributes);
83 private Map<String, String> prepareRuleAttributes(ClampProperties clampProperties, ModelProperties modelProperties,
84 String modelElementId, PolicyChain policyChain, String globalService)
85 throws BuilderException, UnsupportedEncodingException {
86 logger.info("Preparing rule attributes...");
87 String templateName = clampProperties.getStringValue("op.templateName", globalService);
88 String operationTopic = clampProperties.getStringValue("op.operationTopic", globalService);
89 String notificationTopic = clampProperties.getStringValue("op.notificationTopic", globalService);
91 Map<String, String> ruleAttributes = new HashMap<>();
92 ruleAttributes.put(TEMPLATE_NAME, templateName);
93 ruleAttributes.put(CLOSED_LOOP_CONTROL_NAME, modelProperties.getControlNameAndPolicyUniqueId());
94 ruleAttributes.put(NOTIFICATION_TOPIC, notificationTopic);
96 ImmutableMap<String, String> attributes = createRuleAttributesFromPolicy(clampProperties, modelProperties,
97 modelElementId, policyChain, globalService, operationTopic);
98 ruleAttributes.putAll(attributes);
99 logger.info("Prepared: " + ruleAttributes);
100 return ruleAttributes;
103 private Map<String, String> prepareMatchingAttributes(ClampProperties refProp, String globalService) {
104 logger.info("Preparing matching attributes...");
105 String controller = refProp.getStringValue("op.controller", globalService);
106 Map<String, String> matchingAttributes = new HashMap<>();
107 matchingAttributes.put(CONTROLLER, controller);
108 logger.info("Prepared: " + matchingAttributes);
109 return matchingAttributes;
112 private Map<AttributeType, Map<String, String>> createAttributesMap(Map<String, String> matchingAttributes,
113 Map<String, String> ruleAttributes) {
114 Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
115 attributes.put(AttributeType.RULE, ruleAttributes);
116 attributes.put(AttributeType.MATCHING, matchingAttributes);
120 private ImmutableMap<String, String> createRuleAttributesFromPolicy(ClampProperties refProp, ModelProperties modelProperties,
121 String modelElementId, PolicyChain policyChain,
122 String globalService, String operationTopic)
123 throws BuilderException, UnsupportedEncodingException {
124 if (Strings.isNullOrEmpty(operationTopic)) {
125 // if no operationTopic, then don't format yaml - use first policy
126 String recipeTopic = refProp.getStringValue("op.recipeTopic", globalService);
127 return createRuleAttributesFromPolicyItem(
128 policyChain.getPolicyItems().get(0), recipeTopic);
130 return createRuleAttributesFromPolicyChain(policyChain, modelProperties,
131 modelElementId, operationTopic);
135 private ImmutableMap<String, String> createRuleAttributesFromPolicyItem(PolicyItem policyItem, String recipeTopic) {
136 logger.info("recipeTopic=" + recipeTopic);
137 return ImmutableMap.<String, String>builder()
138 .put(RECIPE_TOPIC, recipeTopic)
139 .put(RECIPE, policyItem.getRecipe())
140 .put(MAX_RETRIES, String.valueOf(policyItem.getMaxRetries()))
141 .put(RETRY_TIME_LIMIT, String.valueOf(policyItem.getRetryTimeLimit()))
142 .put(RESOURCE_ID, String.valueOf(policyItem.getTargetResourceId()))
146 private ImmutableMap<String, String> createRuleAttributesFromPolicyChain(PolicyChain policyChain,
147 ModelProperties modelProperties,
148 String modelElementId,
149 String operationTopic)
150 throws BuilderException, UnsupportedEncodingException {
151 logger.info("operationTopic=" + operationTopic);
152 String yaml = policyYamlFormatter.formatYaml(modelProperties, modelElementId, policyChain);
153 return ImmutableMap.<String, String>builder()
154 .put(OPERATION_TOPIC, operationTopic)
155 .put(CONTROL_LOOP_YAML, yaml)