2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END============================================
22 * ===================================================================
26 package org.onap.clamp.clds.client.req.policy;
28 import com.att.eelf.configuration.EELFLogger;
29 import com.att.eelf.configuration.EELFManager;
31 import java.util.ArrayList;
32 import java.util.HashMap;
33 import java.util.List;
35 import java.util.EnumMap;
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;
42 public class GuardPolicyAttributesConstructor {
43 public static final String ACTOR = "actor";
44 public static final String RECIPE = "recipe";
45 public static final String TARGETS = "targets";
46 public static final String CLNAME = "clname";
47 public static final String MIN = "min";
48 public static final String MAX = "max";
49 public static final String LIMIT = "limit";
50 public static final String TIME_WINDOW = "timeWindow";
51 public static final String TIME_UNITS = "timeUnits";
52 public static final String GUARD_ACTIVE_START = "guardActiveStart";
53 public static final String GUARD_ACTIVE_END = "guardActiveEnd";
55 private static final EELFLogger logger = EELFManager.getInstance()
56 .getLogger(GuardPolicyAttributesConstructor.class);
58 private GuardPolicyAttributesConstructor() {
61 public static Map<AttributeType, Map<String, String>> formatAttributes(ModelProperties modelProperties,
62 PolicyItem policyItem) {
63 Map<String, String> matchingAttributes = prepareMatchingAttributes(policyItem, modelProperties);
64 return createAttributesMap(matchingAttributes);
68 * Get all the Guard policies from the policy chain.
69 * @param policyChain The policy chain
70 * @return The list of guard policies
72 public static List<PolicyItem> getAllPolicyGuardsFromPolicyChain(PolicyChain policyChain) {
73 List<PolicyItem> listItem = new ArrayList<>();
74 for (PolicyItem policyItem : policyChain.getPolicyItems()) {
75 if ("on".equals(policyItem.getEnableGuardPolicy())) {
76 listItem.add(policyItem);
82 private static Map<String, String> prepareMatchingAttributes(PolicyItem policyItem, ModelProperties modelProp) {
83 logger.info("Preparing matching attributes for guard...");
84 Map<String, String> matchingAttributes = new HashMap<>();
85 matchingAttributes.put(ACTOR, policyItem.getActor());
86 matchingAttributes.put(RECIPE, policyItem.getRecipe());
87 matchingAttributes.put(TARGETS, policyItem.getGuardTargets());
88 matchingAttributes.put(CLNAME, modelProp.getControlNameAndPolicyUniqueId());
89 if ("GUARD_MIN_MAX".equals(policyItem.getGuardPolicyType())) {
90 matchingAttributes.put(MIN, policyItem.getMinGuard());
91 matchingAttributes.put(MAX, policyItem.getMaxGuard());
92 } else if ("GUARD_YAML".equals(policyItem.getGuardPolicyType())) {
93 matchingAttributes.put(LIMIT, policyItem.getLimitGuard());
94 matchingAttributes.put(TIME_WINDOW, policyItem.getTimeWindowGuard());
95 matchingAttributes.put(TIME_UNITS, policyItem.getTimeUnitsGuard());
97 matchingAttributes.put(GUARD_ACTIVE_START, policyItem.getGuardActiveStart());
98 matchingAttributes.put(GUARD_ACTIVE_END, policyItem.getGuardActiveEnd());
100 logger.info("Prepared: " + matchingAttributes);
101 return matchingAttributes;
104 private static Map<AttributeType, Map<String, String>> createAttributesMap(Map<String, String> matchingAttributes) {
105 Map<AttributeType, Map<String, String>> attributes = new EnumMap<>(AttributeType.class);
106 attributes.put(AttributeType.MATCHING, matchingAttributes);