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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
19 package org.onap.policy.controlloop.actor.cds.request;
21 import java.io.Serializable;
22 import java.util.LinkedHashMap;
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;
32 public class CdsActionRequest implements Serializable {
34 private static final long serialVersionUID = -4172157702597791493L;
35 private static final StandardCoder CODER = new StandardCoder();
37 private String actionName;
38 private String resolutionKey;
39 private Map<String, String> aaiProperties;
40 private Map<String, String> policyPayload;
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:
46 * "{@link CdsActionRequest#getActionName()}-request": {
47 * "resolution-key": "{@link CdsActionRequest#getResolutionKey()} ()}",
48 * "{@link CdsActionRequest#getActionName()}-properties": {
49 * "{@link CdsActionRequest#getAaiProperties()} ()}",
50 * "{@link CdsActionRequest#getPolicyPayload()} ()}"
54 * @return JSON string equivalent of the CDS request object
55 * @throws CoderException if error occurs when serializing to JSON string
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);
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);
68 // 3. Finally build the CDS action request object
69 Map<String, Object> cdsActionRequestObj = new LinkedHashMap<>();
70 cdsActionRequestObj.put(generateCdsActionRequestKey(), cdsActionRequestMap);
72 // 4. Serialize the CDS action request object
73 return CODER.encode(cdsActionRequestObj);
76 private String generateCdsActionPropertiesKey() {
77 return actionName + CdsActorConstants.CDS_REQUEST_PROPERTIES_SUFFIX;
80 private String generateCdsActionRequestKey() {
81 return actionName + CdsActorConstants.CDS_REQUEST_SUFFIX;