ddadb55cc32304e96a01b6d0ccb0f5ddd7f55d56
[clamp.git] / src / main / java / org / onap / clamp / clds / client / OperationalPolicyDelegate.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  * Modifications copyright (c) 2018 Nokia
21  * ===================================================================
22  * 
23  */
24
25 package org.onap.clamp.clds.client;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29
30 import java.io.UnsupportedEncodingException;
31 import java.util.Map;
32
33 import org.apache.camel.Exchange;
34 import org.apache.camel.Handler;
35 import org.onap.clamp.clds.client.req.policy.OperationalPolicyAttributesConstructor;
36 import org.onap.clamp.clds.client.req.policy.PolicyClient;
37 import org.onap.clamp.clds.config.ClampProperties;
38 import org.onap.clamp.clds.model.properties.ModelProperties;
39 import org.onap.clamp.clds.model.properties.Policy;
40 import org.onap.clamp.clds.model.properties.PolicyChain;
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 Operational 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 OperationalPolicyDelegate {
53
54     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(OperationalPolicyDelegate.class);
55     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
56     private final PolicyClient policyClient;
57     private final ClampProperties refProp;
58     private final OperationalPolicyAttributesConstructor attributesConstructor;
59
60     @Autowired
61     public OperationalPolicyDelegate(PolicyClient policyClient, ClampProperties refProp,
62                                      OperationalPolicyAttributesConstructor attributesConstructor) {
63         this.policyClient = policyClient;
64         this.refProp = refProp;
65         this.attributesConstructor = attributesConstructor;
66     }
67
68     /**
69      * Perform activity. Send Operational Policy info to policy api.
70      *
71      * @param camelExchange
72      *            The Camel Exchange object containing the properties
73      * @throws BuilderException
74      *             In case of issues with OperationalPolicyRequestAttributesConstructor
75      * @throws UnsupportedEncodingException
76      *             In case of issues with the Charset encoding
77      */
78     @Handler
79     public void execute(Exchange camelExchange) throws BuilderException, UnsupportedEncodingException {
80         String responseMessage = null;
81         ModelProperties prop = ModelProperties.create(camelExchange);
82         Policy policy = prop.getType(Policy.class);
83         if (policy.isFound()) {
84             for (PolicyChain policyChain : prop.getType(Policy.class).getPolicyChains()) {
85                 Map<AttributeType, Map<String, String>> attributes = attributesConstructor.formatAttributes(refProp,
86                         prop, prop.getType(Policy.class).getId(), policyChain);
87                 responseMessage = policyClient.sendBrmsPolicy(attributes, prop, LoggingUtils.getRequestId());
88             }
89             if (responseMessage != null) {
90                 camelExchange.setProperty("operationalPolicyResponseMessage", responseMessage.getBytes());
91             }
92         }
93     }
94 }