38ab2bdc77720e084206644d44f2e7f9fde72c91
[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
42     /**
43      * Generate the CDS gRPC request payload from the action-name (aka operational policy recipe).
44      * The CDS gRPC request payload generation follows the below pattern:
45      *  {
46      *    "{@link CdsActionRequest#getActionName()}-request": {
47      *      "resolution-key": "{@link CdsActionRequest#getResolutionKey()} ()}",
48      *      "{@link CdsActionRequest#getActionName()}-properties": {
49      *        "{@link CdsActionRequest#getAaiProperties()} ()}",
50      *        "{@link CdsActionRequest#getPolicyPayload()} ()}"
51      *      }
52      *    }
53      *  }
54      * @return JSON string equivalent of the CDS request object
55      * @throws CoderException if error occurs when serializing to JSON string
56      */
57     public String generateCdsPayload() throws CoderException {
58         // 1. Build the innermost object to include AAI properties and policy payload information
59         Map<String, String> cdsActionPropsMap = new LinkedHashMap<>();
60         cdsActionPropsMap.putAll(aaiProperties);
61         cdsActionPropsMap.putAll(policyPayload);
62
63         // 2. Build the enclosing CDS action request properties object to contain (1) and the resolution-key
64         Map<String, Object> cdsActionRequestMap = new LinkedHashMap<>();
65         cdsActionRequestMap.put(CdsActorConstants.KEY_RESOLUTION_KEY, resolutionKey);
66         cdsActionRequestMap.put(generateCdsActionPropertiesKey(), cdsActionPropsMap);
67
68         // 3. Finally build the CDS action request object
69         Map<String, Object> cdsActionRequestObj = new LinkedHashMap<>();
70         cdsActionRequestObj.put(generateCdsActionRequestKey(), cdsActionRequestMap);
71
72         // 4. Serialize the CDS action request object
73         return CODER.encode(cdsActionRequestObj);
74     }
75
76     private String generateCdsActionPropertiesKey() {
77         return actionName + CdsActorConstants.CDS_REQUEST_PROPERTIES_SUFFIX;
78     }
79
80     private String generateCdsActionRequestKey() {
81         return actionName + CdsActorConstants.CDS_REQUEST_SUFFIX;
82     }
83 }