aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/clamp/clds/client/PolicyClient.java
blob: ed29afe7e96deb1cbaa07bf1b51a77551fa193de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 *                             reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ===================================================================
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */

package org.onap.clamp.clds.client;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.ws.rs.BadRequestException;

import org.onap.clamp.clds.exception.policy.PolicyClientException;
import org.onap.clamp.clds.model.prop.ModelProperties;
import org.onap.clamp.clds.model.refprop.RefProp;
import org.onap.clamp.clds.util.LoggingUtils;
import org.onap.policy.api.AttributeType;
import org.onap.policy.api.ConfigRequestParameters;
import org.onap.policy.api.DeletePolicyCondition;
import org.onap.policy.api.DeletePolicyParameters;
import org.onap.policy.api.PolicyChangeResponse;
import org.onap.policy.api.PolicyConfig;
import org.onap.policy.api.PolicyConfigException;
import org.onap.policy.api.PolicyConfigType;
import org.onap.policy.api.PolicyEngine;
import org.onap.policy.api.PolicyEngineException;
import org.onap.policy.api.PolicyParameters;
import org.onap.policy.api.PolicyType;
import org.onap.policy.api.PushPolicyParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;

/**
 * Policy utility methods - specifically, send the policy.
 */
public class PolicyClient {
    protected static final String     POLICY_PREFIX_BASE         = "Config_";
    protected static final String     POLICY_PREFIX_BRMS_PARAM   = "Config_BRMS_Param_";
    protected static final String     POLICY_PREFIX_MICROSERVICE = "Config_MS_";

    protected static final String     LOG_POLICY_PREFIX          = "Response is ";

    protected static final EELFLogger logger                     = EELFManager.getInstance()
            .getLogger(PolicyClient.class);
    protected static final EELFLogger metricsLogger              = EELFManager.getInstance().getMetricsLogger();

    @Value("${org.onap.clamp.config.files.cldsPolicyConfig:'classpath:/clds/clds-policy-config.properties'}")
    protected String                  cldsPolicyConfigFile;

    @Autowired
    protected ApplicationContext      appContext;

    @Autowired
    protected RefProp                 refProp;

    /**
     * Perform BRMS policy type.
     *
     * @param attributes
     *            A map of attributes
     * @param prop
     *            The ModelProperties
     * @param policyRequestUuid
     *            PolicyRequest UUID
     * @return The response message of policy
     * 
     */
    public String sendBrmsPolicy(Map<AttributeType, Map<String, String>> attributes, ModelProperties prop,
            String policyRequestUuid) {

        PolicyParameters policyParameters = new PolicyParameters();

        // Set Policy Type(Mandatory)
        policyParameters.setPolicyConfigType(PolicyConfigType.BRMS_PARAM);

        // Set Policy Name(Mandatory)
        policyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId());

        // documentation says this is options, but when tested, got the
        // following failure: java.lang.Exception: Policy send failed: PE300 -
        // Data Issue: No policyDescription given.
        policyParameters.setPolicyDescription(refProp.getStringValue("op.policyDescription"));
        policyParameters.setAttributes(attributes);
        // Set a random UUID(Mandatory)
        policyParameters.setRequestID(UUID.fromString(policyRequestUuid));
        String policyNamePrefix = refProp.getStringValue("policy.op.policyNamePrefix");
        String rtnMsg = send(policyParameters, prop, policyNamePrefix);

        String policyType = refProp.getStringValue("policy.op.type");
        push(policyType, prop);

        return rtnMsg;
    }

    /**
     * Perform send of microservice policy in JSON.
     *
     * @param policyJson
     *            The policy JSON
     * @param prop
     *            The ModelProperties
     * @param policyRequestUuid
     *            The policy Request UUID
     * @return The response message of policy
     */
    public String sendMicroServiceInJson(String policyJson, ModelProperties prop, String policyRequestUuid) {

        PolicyParameters policyParameters = new PolicyParameters();

        // Set Policy Type
        policyParameters.setPolicyConfigType(PolicyConfigType.MicroService);
        policyParameters.setEcompName(refProp.getStringValue("policy.onap.name"));
        policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());

        policyParameters.setConfigBody(policyJson);
        policyParameters.setConfigBodyType(PolicyType.JSON);

        policyParameters.setRequestID(UUID.fromString(policyRequestUuid));
        String policyNamePrefix = refProp.getStringValue("policy.ms.policyNamePrefix");

        // Adding this line to clear the policy id from policy name while
        // pushing to policy engine
        prop.setPolicyUniqueId("");

        String rtnMsg = send(policyParameters, prop, policyNamePrefix);
        String policyType = refProp.getStringValue("policy.ms.type");
        push(policyType, prop);

        return rtnMsg;
    }

    /**
     * Perform send of base policy in OTHER type.
     *
     * @param configBody
     *            The config policy string body
     * @param configPolicyName
     *            The config policy name of the component that has been
     *            pre-deployed in DCAE
     * @param prop
     *            The ModelProperties
     * @param policyRequestUuid
     *            The policy request UUID
     * @return The answer from policy call
     */
    public String sendBasePolicyInOther(String configBody, String configPolicyName, ModelProperties prop,
            String policyRequestUuid) {
        PolicyParameters policyParameters = new PolicyParameters();

        // Set Policy Type
        policyParameters.setPolicyConfigType(PolicyConfigType.Base);
        policyParameters.setEcompName(refProp.getStringValue("policy.onap.name"));
        policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());

        policyParameters.setConfigBody(configBody);
        policyParameters.setConfigBodyType(PolicyType.OTHER);
        policyParameters.setConfigName("HolmesPolicy");
        policyParameters.setPolicyName(configPolicyName);

        policyParameters.setRequestID(UUID.fromString(policyRequestUuid));

        // Adding this line to clear the policy id from policy name while
        // pushing to policy engine
        prop.setPolicyUniqueId("");

        String rtnMsg = send(policyParameters, prop, POLICY_PREFIX_BASE);
        push(PolicyConfigType.Base.toString(), prop);

        return rtnMsg;
    }

    /**
     * Perform send of Microservice policy in OTHER type.
     * 
     * @param configBody
     *            The config policy string body
     * @param prop
     *            The ModelProperties
     * @param policyRequestUuid
     *            The policy request UUID
     * @return The answer from policy call
     */
    public String sendMicroServiceInOther(String configBody, ModelProperties prop, String policyRequestUuid) {

        PolicyParameters policyParameters = new PolicyParameters();
        // Set Policy Type 
        policyParameters.setPolicyConfigType(PolicyConfigType.MicroService);
        policyParameters.setOnapName("DCAE");
        policyParameters.setEcompName(refProp.getStringValue("policy.onap.name"));
        policyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
        policyParameters.setConfigBody(configBody);
        String policyNamePrefix = refProp.getStringValue("policy.ms.policyNamePrefix");

        // Adding this line to clear the policy id from policy name while
        // pushing to policy engine
        prop.setPolicyUniqueId("");

        String rtnMsg = send(policyParameters, prop, policyNamePrefix);
        String policyType = refProp.getStringValue("policy.ms.type");
        push(policyType, prop);

        return rtnMsg;
    }

    /**
     * Perform send of policy.
     *
     * @param policyParameters
     *            The PolicyParameters
     * @param prop
     *            The ModelProperties
     * @return The response message of Policy
     */
    protected String send(PolicyParameters policyParameters, ModelProperties prop, String policyNamePrefix) {
        // Verify whether it is triggered by Validation Test button from UI
        if (prop.isTest()) {
            return "send not executed for test action";
        }

        // API method to create or update Policy.
        PolicyChangeResponse response = null;
        String responseMessage = "";
        Date startTime = new Date();
        try {
            List<Integer> versions = getVersions(policyNamePrefix, prop);
            if (versions.isEmpty()) {
                LoggingUtils.setTargetContext("Policy", "createPolicy");
                logger.info("Attempting to create policy for action=" + prop.getActionCd());
                response = getPolicyEngine().createPolicy(policyParameters);
                responseMessage = response.getResponseMessage();
            } else {
                LoggingUtils.setTargetContext("Policy", "updatePolicy");
                logger.info("Attempting to update policy for action=" + prop.getActionCd());
                response = getPolicyEngine().updatePolicy(policyParameters);
                responseMessage = response.getResponseMessage();
            }
        } catch (Exception e) {
            logger.error("Exception occurred during policy communication", e);
            throw new PolicyClientException("Exception while communicating with Policy", e);
        }
        logger.info(LOG_POLICY_PREFIX + responseMessage);

        LoggingUtils.setTimeContext(startTime, new Date());

        if (response.getResponseCode() == 200) {
            logger.info("Policy send successful");
            metricsLogger.info("Policy send success");
        } else {
            logger.warn("Policy send failed: " + responseMessage);
            metricsLogger.info("Policy send failure");
            throw new BadRequestException("Policy send failed: " + responseMessage);
        }

        return responseMessage;
    }

    /**
     * Format and send push of policy.
     *
     * @param policyType
     *            The policy Type
     * @param prop
     *            The ModelProperties
     * @return The response message of policy
     */
    protected String push(String policyType, ModelProperties prop) {
        // Verify whether it is triggered by Validation Test button from UI
        if (prop.isTest()) {
            return "push not executed for test action";
        }

        PushPolicyParameters pushPolicyParameters = new PushPolicyParameters();

        // Parameter arguments
        if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) {
            pushPolicyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId());
        } else {
            pushPolicyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
        }
        logger.info("Policy Name in Push policy method - " + pushPolicyParameters.getPolicyName());

        pushPolicyParameters.setPolicyType(policyType);
        pushPolicyParameters.setPdpGroup(refProp.getStringValue("policy.pdp.group"));
        pushPolicyParameters.setRequestID(null);

        // API method to create or update Policy.
        PolicyChangeResponse response = null;
        String responseMessage = "";
        try {
            logger.info("Attempting to push policy...");
            response = getPolicyEngine().pushPolicy(pushPolicyParameters);
            responseMessage = response.getResponseMessage();
        } catch (Exception e) {
            logger.error("Exception occurred during policy communication", e);
            throw new PolicyClientException("Exception while communicating with Policy", e);
        }
        logger.info(LOG_POLICY_PREFIX + responseMessage);

        if (response != null && (response.getResponseCode() == 200 || response.getResponseCode() == 204)) {
            logger.info("Policy push successful");
        } else {
            logger.warn("Policy push failed: " + responseMessage);
            throw new BadRequestException("Policy push failed: " + responseMessage);
        }

        return responseMessage;
    }

    /**
     * Use Get Config Policy API to retrieve the versions for a policy. Return
     * versions in sorted order. Return empty list if none found.
     *
     * @param policyNamePrefix
     *            The Policy Name Prefix
     * @param prop
     *            The ModelProperties
     * @return The response message from policy
     * @throws PolicyConfigException
     *             In case of issues with policy engine
     */
    protected List<Integer> getVersions(String policyNamePrefix, ModelProperties prop) throws PolicyConfigException {

        ArrayList<Integer> versions = new ArrayList<>();
        ConfigRequestParameters configRequestParameters = new ConfigRequestParameters();
        String policyName = "";

        if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) {
            policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix) + "_" + prop.getPolicyUniqueId();
        } else {
            policyName = prop.getCurrentPolicyScopeAndFullPolicyName(policyNamePrefix);
        }

        logger.info("policyName=" + policyName);
        configRequestParameters.setPolicyName(policyName);
        try {
            Collection<PolicyConfig> response = getPolicyEngine().getConfig(configRequestParameters);
            for (PolicyConfig policyConfig : response) {
                Integer version = Integer.valueOf(policyConfig.getPolicyVersion());
                versions.add(version);
            }
            Collections.sort(versions);
            logger.info("Policy versions.size()=" + versions.size());
        } catch (PolicyConfigException e) {
            // just print warning - if no policy version found
            logger.warn("warning: policy not found...policy name - " + policyName, e.getMessage());
        }
        return versions;

    }

    /**
     * This method create a new policy engine.
     * 
     * @return A new policy engine
     */
    private PolicyEngine getPolicyEngine() {
        PolicyEngine policyEngine;
        try {
            policyEngine = new PolicyEngine(appContext.getResource(cldsPolicyConfigFile).getFile().getAbsolutePath());
        } catch (IOException e1) {
            throw new PolicyClientException("Exception when opening policy config file", e1);
        } catch (PolicyEngineException e) {
            throw new PolicyClientException("Exception when creating a new policy engine", e);
        }
        return policyEngine;
    }

    /**
     * Format and send delete Micro Service requests to Policy.
     *
     * @param prop
     *            The ModelProperties
     * @return The response message from Policy
     */
    public String deleteMicrosService(ModelProperties prop) {
        String policyType = refProp.getStringValue("policy.ms.type");
        return deletePolicy(prop, policyType);
    }

    /**
     * This method delete the Base policy.
     *
     * @param prop
     *            The model Properties
     * @return A string with the answer from policy
     */
    public String deleteBasePolicy(ModelProperties prop) {
        return deletePolicy(prop, PolicyConfigType.Base.toString());
    }

    /**
     * Format and send delete BRMS requests to Policy.
     *
     * @param prop
     *            The ModelProperties
     * @return The response message from policy
     */
    public String deleteBrms(ModelProperties prop) {
        String policyType = refProp.getStringValue("policy.op.type");
        return deletePolicy(prop, policyType);
    }

    /**
     * Format and send delete PAP and PDP requests to Policy.
     *
     * @param prop
     *            The ModelProperties
     *
     * @return The response message from policy
     */
    protected String deletePolicy(ModelProperties prop, String policyType) {
        DeletePolicyParameters deletePolicyParameters = new DeletePolicyParameters();

        if (prop.getPolicyUniqueId() != null && !prop.getPolicyUniqueId().isEmpty()) {
            deletePolicyParameters.setPolicyName(prop.getPolicyScopeAndNameWithUniqueId());
        } else {
            deletePolicyParameters.setPolicyName(prop.getCurrentPolicyScopeAndPolicyName());
        }
        logger.info("Policy Name in delete policy method - " + deletePolicyParameters.getPolicyName());
        deletePolicyParameters.setPolicyComponent("PDP");
        deletePolicyParameters.setDeleteCondition(DeletePolicyCondition.ALL);
        deletePolicyParameters.setPdpGroup(refProp.getStringValue("policy.pdp.group"));
        deletePolicyParameters.setPolicyType(policyType);
        // send delete request
        StringBuilder responseMessage = new StringBuilder(sendDeletePolicy(deletePolicyParameters, prop));

        logger.info("Deleting policy from PAP...");
        deletePolicyParameters.setPolicyComponent("PAP");
        deletePolicyParameters.setDeleteCondition(DeletePolicyCondition.ALL);

        // send delete request
        responseMessage.append(sendDeletePolicy(deletePolicyParameters, prop));

        return responseMessage.toString();
    }

    /**
     * Send delete request to Policy.
     *
     * @param deletePolicyParameters
     *            The DeletePolicyParameters
     * @param prop
     *            The ModelProperties
     * @return The response message from policy
     */
    protected String sendDeletePolicy(DeletePolicyParameters deletePolicyParameters, ModelProperties prop) {
        // Verify whether it is triggered by Validation Test button from UI
        if (prop.isTest()) {
            return "delete not executed for test action";
        }

        // API method to create or update Policy.
        PolicyChangeResponse response = null;
        String responseMessage = "";
        try {
            logger.info("Attempting to delete policy...");
            response = getPolicyEngine().deletePolicy(deletePolicyParameters);
            responseMessage = response.getResponseMessage();
        } catch (Exception e) {
            logger.error("Exception occurred during policy communnication", e);
        }
        logger.info(LOG_POLICY_PREFIX + responseMessage);

        if (response != null && response.getResponseCode() == 200) {
            logger.info("Policy delete successful");
        } else {
            logger.warn("Policy delete failed: " + responseMessage);
            throw new BadRequestException("Policy delete failed: " + responseMessage);
        }

        return responseMessage;
    }
}