Fix check style issues
[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
31 import org.apache.camel.Exchange;
32 import org.apache.camel.Handler;
33 import org.onap.clamp.clds.client.req.policy.GuardPolicyAttributesConstructor;
34 import org.onap.clamp.clds.client.req.policy.PolicyClient;
35 import org.onap.clamp.clds.config.ClampProperties;
36 import org.onap.clamp.clds.model.properties.ModelProperties;
37 import org.onap.clamp.clds.model.properties.Policy;
38 import org.onap.clamp.clds.model.properties.PolicyChain;
39 import org.onap.clamp.clds.model.properties.PolicyItem;
40 import org.onap.clamp.clds.util.LoggingUtils;
41 import org.onap.policy.controlloop.policy.builder.BuilderException;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Component;
44
45 /**
46  * Send Guard Policy info to policy API. It uses the policy code to define the
47  * model and communicate with it. See also the PolicyClient class.
48  */
49 @Component
50 public class GuardPolicyDelegate {
51
52     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(GuardPolicyDelegate.class);
53     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
54     private final PolicyClient policyClient;
55     private final ClampProperties refProp;
56
57     @Autowired
58     public GuardPolicyDelegate(PolicyClient policyClient, ClampProperties refProp) {
59         this.policyClient = policyClient;
60         this.refProp = refProp;
61     }
62
63     /**
64      * Perform activity. Send Guard Policies info to policy api.
65      *
66      * @param camelExchange
67      *        The Camel Exchange object containing the properties
68      * @throws BuilderException
69      *         In case of issues with OperationalPolicyRequestAttributesConstructor
70      * @throws UnsupportedEncodingException
71      *         In case of issues with the Charset encoding
72      */
73     @Handler
74     public void execute(Exchange camelExchange) throws BuilderException, UnsupportedEncodingException {
75         ModelProperties prop = ModelProperties.create(camelExchange);
76         Policy policy = prop.getType(Policy.class);
77         if (policy.isFound()) {
78             for (PolicyChain policyChain : prop.getType(Policy.class).getPolicyChains()) {
79                 for (PolicyItem policyItem:GuardPolicyAttributesConstructor
80                         .getAllPolicyGuardsFromPolicyChain(policyChain)) {
81                     prop.setCurrentModelElementId(policy.getId());
82                     prop.setPolicyUniqueId(policyChain.getPolicyId());
83                     prop.setGuardUniqueId(policyItem.getId());
84                     policyClient.sendGuardPolicy(GuardPolicyAttributesConstructor
85                         .formatAttributes(prop, policyItem), prop, LoggingUtils.getRequestId(), policyItem);
86                 }
87             }
88         }
89     }
90
91
92 }