Merge "logstash input"
[clamp.git] / src / main / java / org / onap / clamp / clds / client / req / policy / OperationalPolicyAttributesConstructor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * ===================================================================
22  *
23  */
24
25 package org.onap.clamp.clds.client.req.policy;
26
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
32 import java.io.UnsupportedEncodingException;
33 import java.util.HashMap;
34 import java.util.Map;
35
36 import org.onap.clamp.clds.config.ClampProperties;
37 import org.onap.clamp.clds.model.properties.ModelProperties;
38 import org.onap.clamp.clds.model.properties.PolicyChain;
39 import org.onap.clamp.clds.model.properties.PolicyItem;
40 import org.onap.policy.api.AttributeType;
41 import org.onap.policy.controlloop.policy.builder.BuilderException;
42
43 public class OperationalPolicyAttributesConstructor {
44
45     private static final EELFLogger logger = EELFManager.getInstance()
46         .getLogger(OperationalPolicyAttributesConstructor.class);
47     public static final String TEMPLATE_NAME = "templateName";
48     public static final String CLOSED_LOOP_CONTROL_NAME = "closedLoopControlName";
49     public static final String NOTIFICATION_TOPIC = "notificationTopic";
50     public static final String OPERATION_TOPIC = "operationTopic";
51     public static final String CONTROL_LOOP_YAML = "controlLoopYaml";
52     public static final String CONTROLLER = "controller";
53     public static final String RECIPE = "Recipe";
54     public static final String MAX_RETRIES = "MaxRetries";
55     public static final String RETRY_TIME_LIMIT = "RetryTimeLimit";
56     public static final String RESOURCE_ID = "ResourceId";
57     public static final String RECIPE_TOPIC = "RecipeTopic";
58
59     private OperationalPolicyAttributesConstructor() {
60     }
61
62     public static Map<AttributeType, Map<String, String>> formatAttributes(ClampProperties refProp,
63         ModelProperties modelProperties,
64         String modelElementId, PolicyChain policyChain)
65             throws BuilderException, UnsupportedEncodingException {
66         modelProperties.setCurrentModelElementId(modelElementId);
67         modelProperties.setPolicyUniqueId(policyChain.getPolicyId());
68
69         String globalService = modelProperties.getGlobal().getService();
70
71         Map<String, String> ruleAttributes = prepareRuleAttributes(refProp, modelProperties, modelElementId,
72             policyChain, globalService);
73         Map<String, String> matchingAttributes = prepareMatchingAttributes(refProp, globalService);
74
75         return createAttributesMap(matchingAttributes, ruleAttributes);
76     }
77
78     private static Map<String, String> prepareRuleAttributes(ClampProperties clampProperties, ModelProperties modelProperties,
79         String modelElementId, PolicyChain policyChain, String globalService)
80             throws BuilderException, UnsupportedEncodingException {
81         logger.info("Preparing rule attributes...");
82         String templateName = clampProperties.getStringValue("op.templateName", globalService);
83         String operationTopic = clampProperties.getStringValue("op.operationTopic", globalService);
84         String notificationTopic = clampProperties.getStringValue("op.notificationTopic", globalService);
85
86         Map<String, String> ruleAttributes = new HashMap<>();
87         ruleAttributes.put(TEMPLATE_NAME, templateName);
88         ruleAttributes.put(CLOSED_LOOP_CONTROL_NAME, modelProperties.getControlNameAndPolicyUniqueId());
89         ruleAttributes.put(NOTIFICATION_TOPIC, notificationTopic);
90
91         ImmutableMap<String, String> attributes = createRuleAttributesFromPolicy(clampProperties, modelProperties,
92             modelElementId, policyChain, globalService, operationTopic);
93         ruleAttributes.putAll(attributes);
94         logger.info("Prepared: " + ruleAttributes);
95         return ruleAttributes;
96     }
97
98     private static Map<String, String> prepareMatchingAttributes(ClampProperties refProp, String globalService) {
99         logger.info("Preparing matching attributes...");
100         String controller = refProp.getStringValue("op.controller", globalService);
101         Map<String, String> matchingAttributes = new HashMap<>();
102         matchingAttributes.put(CONTROLLER, controller);
103         logger.info("Prepared: " + matchingAttributes);
104         return matchingAttributes;
105     }
106
107     private static Map<AttributeType, Map<String, String>> createAttributesMap(Map<String, String> matchingAttributes,
108         Map<String, String> ruleAttributes) {
109         Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
110         attributes.put(AttributeType.RULE, ruleAttributes);
111         attributes.put(AttributeType.MATCHING, matchingAttributes);
112         return attributes;
113     }
114
115     private static ImmutableMap<String, String> createRuleAttributesFromPolicy(ClampProperties refProp, ModelProperties modelProperties,
116         String modelElementId, PolicyChain policyChain,
117         String globalService, String operationTopic)
118             throws BuilderException, UnsupportedEncodingException {
119         if (Strings.isNullOrEmpty(operationTopic)) {
120             // if no operationTopic, then don't format yaml - use first policy
121             String recipeTopic = refProp.getStringValue("op.recipeTopic", globalService);
122             return createRuleAttributesFromPolicyItem(
123                 policyChain.getPolicyItems().get(0), recipeTopic);
124         } else {
125             return createRuleAttributesFromPolicyChain(policyChain, modelProperties,
126                 modelElementId, operationTopic);
127         }
128     }
129
130     private static ImmutableMap<String, String> createRuleAttributesFromPolicyItem(PolicyItem policyItem, String recipeTopic) {
131         logger.info("recipeTopic=" + recipeTopic);
132         return ImmutableMap.<String, String>builder()
133             .put(RECIPE_TOPIC, recipeTopic)
134             .put(RECIPE, policyItem.getRecipe())
135             .put(MAX_RETRIES, String.valueOf(policyItem.getMaxRetries()))
136             .put(RETRY_TIME_LIMIT, String.valueOf(policyItem.getRetryTimeLimit()))
137             .put(RESOURCE_ID, String.valueOf(policyItem.getTargetResourceId()))
138             .build();
139     }
140
141     private static ImmutableMap<String, String> createRuleAttributesFromPolicyChain(PolicyChain policyChain,
142         ModelProperties modelProperties,
143         String modelElementId,
144         String operationTopic)
145             throws BuilderException, UnsupportedEncodingException {
146         logger.info("operationTopic=" + operationTopic);
147         String yaml = OperationalPolicyYamlFormatter.formatYaml(modelProperties, modelElementId, policyChain);
148         return ImmutableMap.<String, String>builder()
149             .put(OPERATION_TOPIC, operationTopic)
150             .put(CONTROL_LOOP_YAML, yaml)
151             .build();
152     }
153 }