new method for policy client
[clamp.git] / src / main / java / org / onap / clamp / clds / client / req / TcaMPolicyReq.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 java.io.IOException;
27 import java.util.Iterator;
28
29 import org.onap.clamp.clds.model.prop.Global;
30 import org.onap.clamp.clds.model.prop.ModelProperties;
31 import org.onap.clamp.clds.model.prop.Tca;
32 import org.onap.clamp.clds.model.prop.TcaItem;
33 import org.onap.clamp.clds.model.prop.TcaThreshhold;
34 import org.onap.clamp.clds.model.refprop.RefProp;
35
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38 import com.fasterxml.jackson.core.JsonParseException;
39 import com.fasterxml.jackson.databind.JsonMappingException;
40 import com.fasterxml.jackson.databind.node.ArrayNode;
41 import com.fasterxml.jackson.databind.node.ObjectNode;
42
43 /**
44  * Construct a Policy for Tca/MTca Service request given CLDS objects.
45  *
46  *
47  */
48 public class TcaMPolicyReq {
49     protected static final EELFLogger logger        = EELFManager.getInstance().getLogger(TcaMPolicyReq.class);
50     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
51
52     /**
53      * Format Tca Policy request
54      *
55      * @param refProp
56      * @param prop
57      * @return
58      * @throws JsonParseException
59      * @throws JsonMappingException
60      * @throws IOException
61      */
62     public static String formatTca(RefProp refProp, ModelProperties prop)
63             throws JsonParseException, JsonMappingException, IOException {
64         Global global = prop.getGlobal();
65         String service = global.getService();
66
67         Tca tca = prop.getType(Tca.class);
68         prop.setCurrentModelElementId(tca.getId());
69         ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("tca.template", service);
70         rootNode.put("policyName", prop.getCurrentPolicyScopeAndPolicyName());
71         ObjectNode content = rootNode.with("content");
72         appendSignatures(refProp, service, content, tca, prop);
73
74         String tcaPolicyReq = rootNode.toString();
75         logger.info("tcaPolicyReq=" + tcaPolicyReq);
76         return tcaPolicyReq;
77     }
78
79     /**
80      * Add appendSignatures to json
81      *
82      * @param refProp
83      * @param service
84      * @param appendToNode
85      * @param tca
86      * @param prop
87      * @throws JsonParseException
88      * @throws JsonMappingException
89      * @throws IOException
90      */
91     public static void appendSignatures(RefProp refProp, String service, ObjectNode appendToNode, Tca tca,
92             ModelProperties prop) throws JsonParseException, JsonMappingException, IOException {
93         // "signatures":{
94         ArrayNode tcaNodes = appendToNode.withArray("signatures");
95         for (TcaItem tcaItem : tca.getTcaItems()) {
96             ObjectNode tcaNode = (ObjectNode) refProp.getJsonTemplate("tca.signature.template", service);
97             tcaNode.put("useCaseName", tcaItem.getTcaName());
98             tcaNode.put("signatureName", tcaItem.getTcaName() + "_" + tcaItem.getTcaUuId());
99             tcaNode.put("signatureUuid", tcaItem.getTcaUuId());
100             prop.setPolicyUniqueId(tcaItem.getPolicyId());
101             tcaNode.put("closedLoopControlName", prop.getControlNameAndPolicyUniqueId());
102             tcaNode.put("severity", tcaItem.getSeverity());
103             tcaNode.put("maxInterval", tcaItem.getInterval());
104             tcaNode.put("minMessageViolations", tcaItem.getViolations());
105
106             tcaNodes.add(tcaNode);
107             Iterator<TcaThreshhold> scItr = tcaItem.getTcaThreshholds().iterator();
108             while (scItr.hasNext()) {
109                 TcaThreshhold tcaThreshhold = scItr.next();
110                 // "thresholds": [
111                 ArrayNode thNodes = tcaNode.withArray("thresholds");
112                 ObjectNode thNode = thNodes.addObject();
113                 thNode.put("fieldPath", tcaThreshhold.getFieldPath());
114                 thNode.put("thresholdName", tcaThreshhold.getMetric());
115                 thNode.put("thresholdValue", tcaThreshhold.getThreshhold());
116                 thNode.put("direction", tcaThreshhold.getOperator());
117             }
118         }
119     }
120
121 }