Add unit tests
[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.ArrayList;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.onap.clamp.clds.model.properties.ModelProperties;
35 import org.onap.clamp.clds.model.properties.PolicyChain;
36 import org.onap.clamp.clds.model.properties.PolicyItem;
37 import org.onap.policy.api.AttributeType;
38 import org.onap.policy.api.RuleProvider;
39
40 public class GuardPolicyAttributesConstructor {
41     public static final String ACTOR = "actor";
42     public static final String RECIPE = "recipe";
43     public static final String TARGETS = "targets";
44     public static final String CLNAME = "clname";
45     public static final String MIN = "min";
46     public static final String MAX = "max";
47     public static final String LIMIT = "limit";
48     public static final String TIME_WINDOW = "timeWindow";
49     public static final String TIME_UNITS = "timeUnits";
50     public static final String GUARD_ACTIVE_START = "guardActiveStart";
51     public static final String GUARD_ACTIVE_END = "guardActiveEnd";
52
53     private static final EELFLogger logger = EELFManager.getInstance()
54         .getLogger(GuardPolicyAttributesConstructor.class);
55
56     private GuardPolicyAttributesConstructor() {
57     }
58
59     public static Map<AttributeType, Map<String, String>> formatAttributes(ModelProperties modelProperties,
60         PolicyItem policyItem) {
61         Map<String, String> matchingAttributes = prepareMatchingAttributes(policyItem, modelProperties);
62         return createAttributesMap(matchingAttributes);
63     }
64
65     public static List<PolicyItem> getAllPolicyGuardsFromPolicyChain(PolicyChain policyChain) {
66         List<PolicyItem> listItem = new ArrayList<>();
67         for (PolicyItem policyItem : policyChain.getPolicyItems()) {
68             if ("on".equals(policyItem.getEnableGuardPolicy())) {
69                 listItem.add(policyItem);
70             }
71         }
72         return listItem;
73     }
74
75     private static Map<String, String> prepareMatchingAttributes(PolicyItem policyItem, ModelProperties modelProp) {
76         logger.info("Preparing matching attributes for guard...");
77         Map<String, String> matchingAttributes = new HashMap<>();
78         matchingAttributes.put(ACTOR, policyItem.getActor());
79         matchingAttributes.put(RECIPE, policyItem.getRecipe());
80         matchingAttributes.put(TARGETS, policyItem.getGuardTargets());
81         matchingAttributes.put(CLNAME, modelProp.getControlNameAndPolicyUniqueId());
82         if (RuleProvider.GUARD_MIN_MAX.equals(RuleProvider.valueOf(policyItem.getGuardPolicyType()))) {
83             matchingAttributes.put(MIN, policyItem.getMinGuard());
84             matchingAttributes.put(MAX, policyItem.getMaxGuard());
85         } else if (RuleProvider.GUARD_YAML.equals(RuleProvider.valueOf(policyItem.getGuardPolicyType()))) {
86             matchingAttributes.put(LIMIT, policyItem.getLimitGuard());
87             matchingAttributes.put(TIME_WINDOW, policyItem.getTimeWindowGuard());
88             matchingAttributes.put(TIME_UNITS, policyItem.getTimeUnitsGuard());
89         }
90         matchingAttributes.put(GUARD_ACTIVE_START, policyItem.getGuardActiveStart());
91         matchingAttributes.put(GUARD_ACTIVE_END, policyItem.getGuardActiveEnd());
92
93         logger.info("Prepared: " + matchingAttributes);
94         return matchingAttributes;
95     }
96
97     private static Map<AttributeType, Map<String, String>> createAttributesMap(Map<String, String> matchingAttributes) {
98         Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
99         attributes.put(AttributeType.MATCHING, matchingAttributes);
100         return attributes;
101     }
102 }