4193db59f6eab577123155987c736db31ccb570c
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 Bell Canada. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.policy.controlloop.actor.cds.request;
20
21 import java.io.Serializable;
22 import java.util.LinkedHashMap;
23 import java.util.Map;
24 import lombok.Getter;
25 import lombok.Setter;
26 import org.onap.policy.common.utils.coder.CoderException;
27 import org.onap.policy.common.utils.coder.StandardCoder;
28 import org.onap.policy.controlloop.actor.cds.constants.CdsActorConstants;
29
30 @Getter
31 @Setter
32 public class CdsActionRequest implements Serializable {
33
34     private static final long serialVersionUID = -4172157702597791493L;
35     private static final StandardCoder CODER = new StandardCoder();
36
37     private String actionName;
38     private String resolutionKey;
39     private Map<String, String> aaiProperties;
40     private Map<String, String> policyPayload;
41     private Map<String, String> additionalEventParams;
42
43     /**
44      * Generate the CDS gRPC request payload from the action-name (aka operational policy recipe).
45      * The CDS gRPC request payload generation follows the below pattern:
46      *  {
47      *    "{@link CdsActionRequest#getActionName()}-request": {
48      *      "resolution-key": "{@link CdsActionRequest#getResolutionKey()}",
49      *      "{@link CdsActionRequest#getActionName()}-properties": {
50      *        "{@link CdsActionRequest#getAaiProperties()}",
51      *        "{@link CdsActionRequest#getPolicyPayload()}",
52      *        "{@link CdsActionRequest#getAdditionalEventParams()}"
53      *      }
54      *    }
55      *  }
56      * @return JSON string equivalent of the CDS request object
57      * @throws CoderException if error occurs when serializing to JSON string
58      */
59     public String generateCdsPayload() throws CoderException {
60         // 1. Build the innermost object to include AAI properties and policy payload information
61         Map<String, String> cdsActionPropsMap = new LinkedHashMap<>();
62         cdsActionPropsMap.putAll(aaiProperties);
63         cdsActionPropsMap.putAll(policyPayload);
64         if (additionalEventParams != null) {
65             cdsActionPropsMap.putAll(additionalEventParams);
66         }
67
68         // 2. Build the enclosing CDS action request properties object to contain (1) and the resolution-key
69         Map<String, Object> cdsActionRequestMap = new LinkedHashMap<>();
70         cdsActionRequestMap.put(CdsActorConstants.KEY_RESOLUTION_KEY, resolutionKey);
71         cdsActionRequestMap.put(generateCdsActionPropertiesKey(), cdsActionPropsMap);
72
73         // 3. Finally build the CDS action request object
74         Map<String, Object> cdsActionRequestObj = new LinkedHashMap<>();
75         cdsActionRequestObj.put(generateCdsActionRequestKey(), cdsActionRequestMap);
76
77         // 4. Serialize the CDS action request object
78         return CODER.encode(cdsActionRequestObj);
79     }
80
81     private String generateCdsActionPropertiesKey() {
82         return actionName + CdsActorConstants.CDS_REQUEST_PROPERTIES_SUFFIX;
83     }
84
85     private String generateCdsActionRequestKey() {
86         return actionName + CdsActorConstants.CDS_REQUEST_SUFFIX;
87     }
88 }