Fix check style issues
[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
39 public class GuardPolicyAttributesConstructor {
40     public static final String ACTOR = "actor";
41     public static final String RECIPE = "recipe";
42     public static final String TARGETS = "targets";
43     public static final String CLNAME = "clname";
44     public static final String MIN = "min";
45     public static final String MAX = "max";
46     public static final String LIMIT = "limit";
47     public static final String TIME_WINDOW = "timeWindow";
48     public static final String TIME_UNITS = "timeUnits";
49     public static final String GUARD_ACTIVE_START = "guardActiveStart";
50     public static final String GUARD_ACTIVE_END = "guardActiveEnd";
51
52     private static final EELFLogger logger = EELFManager.getInstance()
53         .getLogger(GuardPolicyAttributesConstructor.class);
54
55     private GuardPolicyAttributesConstructor() {
56     }
57
58     public static Map<AttributeType, Map<String, String>> formatAttributes(ModelProperties modelProperties,
59         PolicyItem policyItem) {
60         Map<String, String> matchingAttributes = prepareMatchingAttributes(policyItem, modelProperties);
61         return createAttributesMap(matchingAttributes);
62     }
63
64     /**
65      * Get all the Guard policies from the policy chain.
66      * @param policyChain The policy chain
67      * @return The list of guard policies
68      */
69     public static List<PolicyItem> getAllPolicyGuardsFromPolicyChain(PolicyChain policyChain) {
70         List<PolicyItem> listItem = new ArrayList<>();
71         for (PolicyItem policyItem : policyChain.getPolicyItems()) {
72             if ("on".equals(policyItem.getEnableGuardPolicy())) {
73                 listItem.add(policyItem);
74             }
75         }
76         return listItem;
77     }
78
79     private static Map<String, String> prepareMatchingAttributes(PolicyItem policyItem, ModelProperties modelProp) {
80         logger.info("Preparing matching attributes for guard...");
81         Map<String, String> matchingAttributes = new HashMap<>();
82         matchingAttributes.put(ACTOR, policyItem.getActor());
83         matchingAttributes.put(RECIPE, policyItem.getRecipe());
84         matchingAttributes.put(TARGETS, policyItem.getGuardTargets());
85         matchingAttributes.put(CLNAME, modelProp.getControlNameAndPolicyUniqueId());
86         if ("GUARD_MIN_MAX".equals(policyItem.getGuardPolicyType())) {
87             matchingAttributes.put(MIN, policyItem.getMinGuard());
88             matchingAttributes.put(MAX, policyItem.getMaxGuard());
89         } else if ("GUARD_YAML".equals(policyItem.getGuardPolicyType())) {
90             matchingAttributes.put(LIMIT, policyItem.getLimitGuard());
91             matchingAttributes.put(TIME_WINDOW, policyItem.getTimeWindowGuard());
92             matchingAttributes.put(TIME_UNITS, policyItem.getTimeUnitsGuard());
93         }
94         matchingAttributes.put(GUARD_ACTIVE_START, policyItem.getGuardActiveStart());
95         matchingAttributes.put(GUARD_ACTIVE_END, policyItem.getGuardActiveEnd());
96
97         logger.info("Prepared: " + matchingAttributes);
98         return matchingAttributes;
99     }
100
101     private static Map<AttributeType, Map<String, String>> createAttributesMap(Map<String, String> matchingAttributes) {
102         Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
103         attributes.put(AttributeType.MATCHING, matchingAttributes);
104         return attributes;
105     }
106 }