Guard policy Backend
[clamp.git] / src / main / java / org / onap / clamp / clds / client / GuardPolicyDelegate.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;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
29 import java.io.UnsupportedEncodingException;
30 import java.util.Map;
31
32 import org.apache.camel.Exchange;
33 import org.apache.camel.Handler;
34 import org.onap.clamp.clds.client.req.policy.GuardPolicyAttributesConstructor;
35 import org.onap.clamp.clds.client.req.policy.PolicyClient;
36 import org.onap.clamp.clds.config.ClampProperties;
37 import org.onap.clamp.clds.model.properties.ModelProperties;
38 import org.onap.clamp.clds.model.properties.Policy;
39 import org.onap.clamp.clds.model.properties.PolicyChain;
40 import org.onap.clamp.clds.model.properties.PolicyItem;
41 import org.onap.clamp.clds.util.LoggingUtils;
42 import org.onap.policy.api.AttributeType;
43 import org.onap.policy.controlloop.policy.builder.BuilderException;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.stereotype.Component;
46
47 /**
48  * Send Guard Policy info to policy API. It uses the policy code to define
49  * the model and communicate with it. See also the PolicyClient class.
50  */
51 @Component
52 public class GuardPolicyDelegate {
53
54     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(GuardPolicyDelegate.class);
55     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
56     private final PolicyClient policyClient;
57     private final ClampProperties refProp;
58
59     @Autowired
60     public GuardPolicyDelegate(PolicyClient policyClient, ClampProperties refProp) {
61         this.policyClient = policyClient;
62         this.refProp = refProp;
63     }
64
65     /**
66      * Perform activity. Send Guard Policies info to policy api.
67      *
68      * @param camelExchange
69      *        The Camel Exchange object containing the properties
70      * @throws BuilderException
71      *         In case of issues with OperationalPolicyRequestAttributesConstructor
72      * @throws UnsupportedEncodingException
73      *         In case of issues with the Charset encoding
74      */
75     @Handler
76     public void execute(Exchange camelExchange) throws BuilderException, UnsupportedEncodingException {
77         String responseMessageGuard = null;
78         ModelProperties prop = ModelProperties.create(camelExchange);
79         Policy policy = prop.getType(Policy.class);
80         if (policy.isFound()) {
81             for (PolicyChain policyChain : prop.getType(Policy.class).getPolicyChains()) {
82                 for(PolicyItem policyItem:policyChain.getPolicyItems()) {
83                     if ("on".equals(policyItem.getEnableGuardPolicy()))
84                         responseMessageGuard = createGuardPolicy(prop, policyItem);
85                 }
86             }
87             if (responseMessageGuard != null) {
88                 camelExchange.setProperty("guardPolicyResponseMessage", responseMessageGuard.getBytes());
89             }
90         }
91     }
92
93     private String createGuardPolicy(ModelProperties prop, PolicyItem policyItem) {
94         Map<AttributeType, Map<String, String>> attributes = GuardPolicyAttributesConstructor
95             .formatAttributes(refProp, prop, prop.getType(Policy.class).getId(), policyItem);
96         return policyClient.sendGuardPolicy(attributes, prop, LoggingUtils.getRequestId());
97     }
98 }