Restore version to SNAPSHOT
[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 java.io.IOException;
27 import java.util.Iterator;
28 import java.util.Map.Entry;
29
30 import org.onap.clamp.clds.model.prop.Global;
31 import org.onap.clamp.clds.model.prop.ModelProperties;
32 import org.onap.clamp.clds.model.prop.ResourceGroup;
33 import org.onap.clamp.clds.model.prop.ServiceConfiguration;
34 import org.onap.clamp.clds.model.prop.StringMatch;
35 import org.onap.clamp.clds.model.refprop.RefProp;
36
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39 import com.fasterxml.jackson.databind.node.ArrayNode;
40 import com.fasterxml.jackson.databind.node.ObjectNode;
41
42 /**
43  * Construct a Policy for String Match Micro Service request given CLDS objects.
44  */
45 public class StringMatchPolicyReq {
46     protected static final EELFLogger logger        = EELFManager.getInstance().getLogger(StringMatchPolicyReq.class);
47     protected static final EELFLogger metricsLogger = EELFManager.getInstance().getMetricsLogger();
48
49     /**
50      * Format Policy String Match request.
51      *
52      * @param refProp
53      * @param prop
54      * @return
55      * @throws IOException
56      */
57     public static String format(RefProp refProp, ModelProperties prop) throws IOException {
58         Global global = prop.getGlobal();
59         String service = global.getService();
60
61         StringMatch sm = prop.getType(StringMatch.class);
62         prop.setCurrentModelElementId(sm.getId());
63         ObjectNode rootNode = (ObjectNode) refProp.getJsonTemplate("sm.template", service);
64         rootNode.put("policyName", prop.getCurrentPolicyScopeAndPolicyName());
65         ObjectNode content = rootNode.with("content");
66         appendServiceConfigurations(refProp, service, content, sm, prop);
67
68         String stringMatchPolicyReq = rootNode.toString();
69         logger.info("stringMatchPolicyReq=" + stringMatchPolicyReq);
70         return stringMatchPolicyReq;
71     }
72
73     /**
74      * Add serviceConfigurations to json
75      *
76      * @param appendToNode
77      * @param sm
78      * @throws IOException
79      */
80     public static void appendServiceConfigurations(RefProp refProp, String service, ObjectNode appendToNode,
81             StringMatch sm, ModelProperties prop) throws IOException {
82         // "serviceConfigurations":{
83         ObjectNode scNodes = appendToNode.with("serviceConfigurations");
84
85         int index = 0;
86         if (sm != null && sm.getResourceGroups() != null) {
87             for (ResourceGroup resourceGroup : sm.getResourceGroups()) {
88                 Iterator<ServiceConfiguration> scItr = resourceGroup.getServiceConfigurations().iterator();
89
90                 while (scItr.hasNext()) {
91                     ServiceConfiguration sc = scItr.next();
92
93                     // "ItemX":{
94                     index++;
95                     String keyValue = "Item" + index;
96                     ObjectNode scNode = (ObjectNode) refProp.getJsonTemplate("sm.sc.template", service);
97                     scNodes.set(keyValue, scNode);
98
99                     // "rulegroup":"abc",
100                     String rulegroupInd = refProp.getStringValue("sm.rulegroup", service);
101                     String groupNumber = resourceGroup.getGroupNumber();
102                     if (rulegroupInd != null && rulegroupInd.equalsIgnoreCase("true") && groupNumber != null
103                             && groupNumber.length() > 0) {
104                         scNode.put("rulegroup", groupNumber);
105                     }
106
107                     // "closedLoopControlName":
108                     prop.setPolicyUniqueId(resourceGroup.getPolicyId());
109                     scNode.put("closedLoopControlName", prop.getControlNameAndPolicyUniqueId());
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
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     }
139 }