Merge "Add plugin in logstash image"
[clamp.git] / src / main / java / org / onap / clamp / clds / client / req / policy / GuardPolicyAttributesConstructor.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  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.client.req.policy;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
29 import java.util.HashMap;
30 import java.util.Map;
31
32 import org.onap.clamp.clds.config.ClampProperties;
33 import org.onap.clamp.clds.model.properties.ModelProperties;
34 import org.onap.clamp.clds.model.properties.PolicyItem;
35 import org.onap.policy.api.AttributeType;
36
37 public class GuardPolicyAttributesConstructor {
38     private static final EELFLogger logger = EELFManager.getInstance()
39         .getLogger(GuardPolicyAttributesConstructor.class);
40
41     private GuardPolicyAttributesConstructor() {
42     }
43
44     public static Map<AttributeType, Map<String, String>> formatAttributes(ClampProperties refProp,
45         ModelProperties modelProperties, String modelElementId, PolicyItem policyItem) {
46         Map<String, String> matchingAttributes = prepareMatchingAttributes(refProp, policyItem, modelProperties);
47         return createAttributesMap(matchingAttributes);
48     }
49
50     private static Map<String, String> prepareMatchingAttributes(ClampProperties refProp,
51         PolicyItem policyItem, ModelProperties modelProp) {
52         logger.info("Preparing matching attributes for guard...");
53         Map<String, String> matchingAttributes = new HashMap<>();
54         matchingAttributes.put("actor",policyItem.getActor());
55         matchingAttributes.put("recipe",policyItem.getRecipe());
56         matchingAttributes.put("targets",policyItem.getGuardTargets());
57         matchingAttributes.put("clname",modelProp.getControlNameAndPolicyUniqueId());
58         if ("MinMax".equals(policyItem.getGuardPolicyType())) {
59             matchingAttributes.put("min",policyItem.getMinGuard());
60             matchingAttributes.put("max",policyItem.getMaxGuard());
61         } else if ("FrequencyLimiter".equals(policyItem.getGuardPolicyType())) {
62             matchingAttributes.put("limit",policyItem.getLimitGuard());
63             matchingAttributes.put("timeWindow",policyItem.getTimeWindowGuard());
64             matchingAttributes.put("timeUnits",policyItem.getTimeUnitsGuard());
65         }
66         matchingAttributes.put("guardActiveStart",policyItem.getGuardActiveStart());
67         matchingAttributes.put("guardActiveEnd",policyItem.getGuardActiveEnd());
68
69         logger.info("Prepared: " + matchingAttributes);
70         return matchingAttributes;
71     }
72
73     private static Map<AttributeType, Map<String, String>> createAttributesMap(Map<String, String> matchingAttributes) {
74         Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
75         attributes.put(AttributeType.MATCHING, matchingAttributes);
76         return attributes;
77     }
78 }