[CLAMP-1] Initial ONAP CLAMP seed code commit
[clamp.git] / src / main / java / org / onap / clamp / clds / client / req / TcaMPolicyReq.java
1 package org.onap.clamp.clds.client.req;
2
3 import java.io.IOException;
4 import java.util.Iterator;
5 import java.util.logging.Logger;
6
7 import org.onap.clamp.clds.model.prop.Global;
8 import org.onap.clamp.clds.model.prop.ModelProperties;
9 import org.onap.clamp.clds.model.prop.Tca;
10 import org.onap.clamp.clds.model.prop.TcaItem;
11 import org.onap.clamp.clds.model.prop.TcaThreshhold;
12 import org.onap.clamp.clds.model.refprop.RefProp;
13 import com.fasterxml.jackson.core.JsonParseException;
14 import com.fasterxml.jackson.databind.JsonMappingException;
15 import com.fasterxml.jackson.databind.node.ArrayNode;
16 import com.fasterxml.jackson.databind.node.ObjectNode;
17
18 /**
19  * Construct a Policy for Tca/MTca Service request given CLDS objects. 
20  * 
21  *
22  */
23 public class TcaMPolicyReq {
24         private static final Logger logger = Logger.getLogger(StringMatchPolicyReq.class.getName());
25         
26         /**
27          * Format Tca Policy request
28          * 
29          * @param refProp
30          * @param prop
31          * @return
32          * @throws JsonParseException
33          * @throws JsonMappingException
34          * @throws IOException
35          */
36         public static String formatTca(RefProp refProp, ModelProperties prop) throws JsonParseException, JsonMappingException, IOException {
37                 Global global = prop.getGlobal();
38                 String service = global.getService();
39                 
40                 Tca tca = prop.getTca();
41                 prop.setCurrentModelElementId(tca.getId());
42                 ObjectNode rootNode = (ObjectNode)refProp.getJsonTemplate("tca.template", service);
43                 rootNode.put("policyName", prop.getCurrentPolicyScopeAndPolicyName());
44                 ObjectNode content = rootNode.with("content");
45                 appendSignatures(refProp, service, content, tca, prop);
46                 
47                 String tcaPolicyReq = rootNode.toString();
48                 logger.info("tcaPolicyReq=" + tcaPolicyReq);
49                 return tcaPolicyReq;
50         }
51         
52         /**
53          * Add appendSignatures to json
54          * 
55          * @param refProp
56          * @param service
57          * @param appendToNode
58          * @param tca
59          * @param prop
60          * @throws JsonParseException
61          * @throws JsonMappingException
62          * @throws IOException
63          */
64         public static void appendSignatures(RefProp refProp, String service, ObjectNode appendToNode, Tca tca, ModelProperties prop) throws JsonParseException, JsonMappingException, IOException {
65                 //      "signatures":{
66                 ArrayNode tcaNodes = appendToNode.withArray("signatures");
67                 for(TcaItem tcaItem : tca.getTcaItems()){
68                         ObjectNode tcaNode = (ObjectNode)refProp.getJsonTemplate("tca.signature.template", service);
69                         tcaNode.put("useCaseName", tcaItem.getTcaName());
70                         tcaNode.put("signatureName", tcaItem.getTcaName()+ "_" + tcaItem.getTcaUuId());
71                         tcaNode.put("signatureUuid", tcaItem.getTcaUuId());
72                         prop.setPolicyUniqueId(tcaItem.getPolicyId());
73                         tcaNode.put("closedLoopControlName", prop.getControlNameAndPolicyUniqueId());
74                         tcaNode.put("severity", tcaItem.getSeverity());
75                         tcaNode.put("maxInterval", tcaItem.getInterval());
76                         tcaNode.put("minMessageViolations", tcaItem.getViolations());
77                         
78                         tcaNodes.add(tcaNode);
79                         Iterator<TcaThreshhold> scItr = tcaItem.getTcaThreshholds().iterator();
80                         while(scItr.hasNext()) {
81                                 TcaThreshhold tcaThreshhold = scItr.next();
82                                 // "thresholds": [
83                                 ArrayNode thNodes = tcaNode.withArray("thresholds");
84                                 ObjectNode thNode = thNodes.addObject();
85                                 thNode.put("fieldPath", tcaThreshhold.getFieldPath());
86                                 thNode.put("thresholdName", tcaThreshhold.getMetric());
87                                 thNode.put("thresholdValue", tcaThreshhold.getThreshhold());
88                                 thNode.put("direction", tcaThreshhold.getOperator());
89                         }               
90                 }
91         }
92
93 }