Fix check style issues
[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     /**
63      * Format the attributes.
64      * @param refProp The Clamp properties
65      * @param modelProperties The model properties
66      * @param modelElementId The model element ID
67      * @param policyChain The policy chain
68      * @return The attributes map
69      * @throws BuilderException Exception while building up the attributes map
70      * @throws UnsupportedEncodingException Unsupported encoding exception
71      */
72     public static Map<AttributeType, Map<String, String>> formatAttributes(ClampProperties refProp,
73         ModelProperties modelProperties,
74         String modelElementId, PolicyChain policyChain)
75             throws BuilderException, UnsupportedEncodingException {
76         modelProperties.setCurrentModelElementId(modelElementId);
77         modelProperties.setPolicyUniqueId(policyChain.getPolicyId());
78
79         String globalService = modelProperties.getGlobal().getService();
80
81         Map<String, String> ruleAttributes = prepareRuleAttributes(refProp, modelProperties, modelElementId,
82             policyChain, globalService);
83         Map<String, String> matchingAttributes = prepareMatchingAttributes(refProp, globalService);
84
85         return createAttributesMap(matchingAttributes, ruleAttributes);
86     }
87
88     private static Map<String, String> prepareRuleAttributes(ClampProperties clampProperties, 
89         ModelProperties modelProperties,
90         String modelElementId, PolicyChain policyChain, String globalService)
91             throws BuilderException, UnsupportedEncodingException {
92         logger.info("Preparing rule attributes...");
93         String templateName = clampProperties.getStringValue("op.templateName", globalService);
94         String operationTopic = clampProperties.getStringValue("op.operationTopic", globalService);
95         String notificationTopic = clampProperties.getStringValue("op.notificationTopic", globalService);
96
97         Map<String, String> ruleAttributes = new HashMap<>();
98         ruleAttributes.put(TEMPLATE_NAME, templateName);
99         ruleAttributes.put(CLOSED_LOOP_CONTROL_NAME, modelProperties.getControlNameAndPolicyUniqueId());
100         ruleAttributes.put(NOTIFICATION_TOPIC, notificationTopic);
101
102         ImmutableMap<String, String> attributes = createRuleAttributesFromPolicy(clampProperties, modelProperties,
103             modelElementId, policyChain, globalService, operationTopic);
104         ruleAttributes.putAll(attributes);
105         logger.info("Prepared: " + ruleAttributes);
106         return ruleAttributes;
107     }
108
109     private static Map<String, String> prepareMatchingAttributes(ClampProperties refProp, String globalService) {
110         logger.info("Preparing matching attributes...");
111         String controller = refProp.getStringValue("op.controller", globalService);
112         Map<String, String> matchingAttributes = new HashMap<>();
113         matchingAttributes.put(CONTROLLER, controller);
114         logger.info("Prepared: " + matchingAttributes);
115         return matchingAttributes;
116     }
117
118     private static Map<AttributeType, Map<String, String>> createAttributesMap(Map<String, String> matchingAttributes,
119         Map<String, String> ruleAttributes) {
120         Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
121         attributes.put(AttributeType.RULE, ruleAttributes);
122         attributes.put(AttributeType.MATCHING, matchingAttributes);
123         return attributes;
124     }
125
126     private static ImmutableMap<String, String> createRuleAttributesFromPolicy(ClampProperties refProp, 
127         ModelProperties modelProperties,
128         String modelElementId, PolicyChain policyChain,
129         String globalService, String operationTopic)
130             throws BuilderException, UnsupportedEncodingException {
131         if (Strings.isNullOrEmpty(operationTopic)) {
132             // if no operationTopic, then don't format yaml - use first policy
133             String recipeTopic = refProp.getStringValue("op.recipeTopic", globalService);
134             return createRuleAttributesFromPolicyItem(
135                 policyChain.getPolicyItems().get(0), recipeTopic);
136         } else {
137             return createRuleAttributesFromPolicyChain(policyChain, modelProperties,
138                 modelElementId, operationTopic);
139         }
140     }
141
142     private static ImmutableMap<String, String> createRuleAttributesFromPolicyItem(PolicyItem policyItem, 
143         String recipeTopic) {
144         logger.info("recipeTopic=" + recipeTopic);
145         return ImmutableMap.<String, String>builder()
146             .put(RECIPE_TOPIC, recipeTopic)
147             .put(RECIPE, policyItem.getRecipe())
148             .put(MAX_RETRIES, String.valueOf(policyItem.getMaxRetries()))
149             .put(RETRY_TIME_LIMIT, String.valueOf(policyItem.getRetryTimeLimit()))
150             .put(RESOURCE_ID, String.valueOf(policyItem.getTargetResourceId()))
151             .build();
152     }
153
154     private static ImmutableMap<String, String> createRuleAttributesFromPolicyChain(PolicyChain policyChain,
155         ModelProperties modelProperties,
156         String modelElementId,
157         String operationTopic)
158             throws BuilderException, UnsupportedEncodingException {
159         logger.info("operationTopic=" + operationTopic);
160         String yaml = OperationalPolicyYamlFormatter.formatYaml(modelProperties, modelElementId, policyChain);
161         return ImmutableMap.<String, String>builder()
162             .put(OPERATION_TOPIC, operationTopic)
163             .put(CONTROL_LOOP_YAML, yaml)
164             .build();
165     }
166 }