[CLAMP-1] Initial ONAP CLAMP seed code commit
[clamp.git] / src / main / java / org / onap / clamp / clds / client / req / StringMatchPolicyReq.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 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  * ===================================================================
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.client.req;
25
26 import com.fasterxml.jackson.databind.node.ArrayNode;
27 import com.fasterxml.jackson.databind.node.ObjectNode;
28 import org.onap.clamp.clds.model.prop.Global;
29 import org.onap.clamp.clds.model.prop.ModelProperties;
30 import org.onap.clamp.clds.model.prop.ServiceConfiguration;
31 import org.onap.clamp.clds.model.prop.StringMatch;
32 import org.onap.clamp.clds.model.refprop.RefProp;
33
34 import java.io.IOException;
35 import java.util.Iterator;
36 import java.util.Map.Entry;
37 import java.util.logging.Logger;
38
39
40 /**
41  * Construct a Policy for String Match Micro Service request given CLDS objects.
42  */
43 public class StringMatchPolicyReq {
44     // currently uses the java.util.logging.Logger like the Camunda engine
45     private static final Logger logger = Logger.getLogger(StringMatchPolicyReq.class.getName());
46
47     /**
48      * Format Policy String Match request.
49      *
50      * @param refProp
51      * @param prop
52      * @return
53      * @throws IOException
54      */
55     public static String format(RefProp refProp, ModelProperties prop) throws IOException {
56         Global global = prop.getGlobal();
57         String service = global.getService();
58
59         StringMatch sm = prop.getStringMatch();
60         prop.setCurrentModelElementId(sm.getId());
61         ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("sm.template", service);
62
63         // "policyName":
64         rootNode.put("policyName", prop.getCurrentPolicyScopeAndPolicyName());
65
66         // "content":{
67         ObjectNode content = rootNode.with("content");
68
69         // "closedLoopControlName":
70         content.put("closedLoopControlName", prop.getControlName());
71
72         //              "serviceConfigurations":[
73         appendServiceConfigurations(refProp, service, content, sm);
74
75         String stringMatchPolicyReq = rootNode.toString();
76         logger.info("stringMatchPolicyReq=" + stringMatchPolicyReq);
77         return stringMatchPolicyReq;
78     }
79
80     /**
81      * Add serviceConfigurations to json
82      *
83      * @param appendToNode
84      * @param sm
85      * @throws IOException
86      */
87     public static void appendServiceConfigurations(RefProp refProp, String service, ObjectNode appendToNode, StringMatch sm) throws IOException {
88         //      "serviceConfigurations":{
89         ObjectNode scNodes = appendToNode.with("serviceConfigurations");
90
91         Iterator<ServiceConfiguration> scItr = sm.getServiceConfigurations().iterator();
92         int index = 0;
93         while (scItr.hasNext()) {
94             ServiceConfiguration sc = scItr.next();
95
96             //"ItemX":{
97             index++;
98             String keyValue = "Item" + index;
99             ObjectNode scNode = (ObjectNode) refProp.getJsonTemplate("sm.sc.template", service);
100             scNodes.set(keyValue, scNode);
101
102             // "rulegroup":"abc",
103             String rulegroupInd = refProp.getStringValue("sm.rulegroup", service);
104             String groupNumber = sc.getGroupNumber();
105             if (rulegroupInd != null && rulegroupInd.equalsIgnoreCase("true") && groupNumber != null && groupNumber.length() > 0) {
106
107                 //String rulegroup = (sc.getResourceVf() == null ? "" : String.join(" ", sc.getResourceVf())) + " - " + (sc.getResourceVfc() == null ? "" : String.join(" ", sc.getResourceVfc()));
108                 scNode.put("rulegroup", groupNumber);
109             }
110
111             // "aaiMatchingFields" : ["VM_NAME"],
112             JsonUtil.addArrayField(scNode, "aaiMatchingFields", sc.getaaiMatchingFields());
113             // "aaiSendFields" : ["VMID", "TenantID"],
114             JsonUtil.addArrayField(scNode, "aaiSendFields", sc.getaaiSendFields());
115
116             // "stringSet": [
117             ArrayNode ssNode = scNode.putArray("stringSet");
118             //  ObjectNode ssNode = scNode.with("stringSet");
119             for (Entry<String, String> entry : sc.getStringSet().entrySet()) {
120                 // exclude eventSourceType
121                 if (!entry.getKey().equals("eventSourceType")) {
122                     ssNode.add(entry.getKey());
123                     ssNode.add(entry.getValue());
124                 }
125             }
126
127             // timeWindow": "0",
128             scNode.put("timeWindow", sc.getTimeWindow());
129             // "ageLimit": "3600",
130             scNode.put("ageLimit", sc.getAgeLimit());
131             // "createClosedLoopEventId" : "Initial",
132             scNode.put("createClosedLoopEventId", sc.getCreateClosedLoopEventId());
133             // "outputEventName": "OnSet"
134             scNode.put("outputEventName", sc.getOutputEventName());
135         }
136     }
137
138 }