Fix Policy Yaml
[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     private static final EELFLogger logger = EELFManager.getInstance()
42         .getLogger(GuardPolicyAttributesConstructor.class);
43
44     private GuardPolicyAttributesConstructor() {
45     }
46
47     public static Map<AttributeType, Map<String, String>> formatAttributes(ModelProperties modelProperties, PolicyItem policyItem) {
48         Map<String, String> matchingAttributes = prepareMatchingAttributes(policyItem, modelProperties);
49         return createAttributesMap(matchingAttributes);
50     }
51
52     public static List<PolicyItem> getAllPolicyGuardsFromPolicyChain(PolicyChain policyChain) {
53         List<PolicyItem> listItem = new ArrayList<>();
54         for (PolicyItem policyItem : policyChain.getPolicyItems()) {
55             if ("on".equals(policyItem.getEnableGuardPolicy())) {
56                 listItem.add(policyItem);
57             }
58         }
59         return listItem;
60     }
61
62     private static Map<String, String> prepareMatchingAttributes(PolicyItem policyItem, ModelProperties modelProp) {
63         logger.info("Preparing matching attributes for guard...");
64         Map<String, String> matchingAttributes = new HashMap<>();
65         matchingAttributes.put("actor",policyItem.getActor());
66         matchingAttributes.put("recipe",policyItem.getRecipe());
67         matchingAttributes.put("targets",policyItem.getGuardTargets());
68         matchingAttributes.put("clname",modelProp.getControlNameAndPolicyUniqueId());
69         if (RuleProvider.GUARD_MIN_MAX.equals(RuleProvider.valueOf(policyItem.getGuardPolicyType()))) {
70             matchingAttributes.put("min",policyItem.getMinGuard());
71             matchingAttributes.put("max",policyItem.getMaxGuard());
72         } else if (RuleProvider.GUARD_YAML.equals(RuleProvider.valueOf(policyItem.getGuardPolicyType()))) {
73             matchingAttributes.put("limit",policyItem.getLimitGuard());
74             matchingAttributes.put("timeWindow",policyItem.getTimeWindowGuard());
75             matchingAttributes.put("timeUnits",policyItem.getTimeUnitsGuard());
76         }
77         matchingAttributes.put("guardActiveStart",policyItem.getGuardActiveStart());
78         matchingAttributes.put("guardActiveEnd",policyItem.getGuardActiveEnd());
79
80         logger.info("Prepared: " + matchingAttributes);
81         return matchingAttributes;
82     }
83
84     private static Map<AttributeType, Map<String, String>> createAttributesMap(Map<String, String> matchingAttributes) {
85         Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
86         attributes.put(AttributeType.MATCHING, matchingAttributes);
87         return attributes;
88     }
89 }