Create submit route
[clamp.git] / src / main / java / org / onap / clamp / policy / operational / OperationalPolicy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 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  *
22  */
23
24 package org.onap.clamp.policy.operational;
25
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonArray;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonObject;
31 import com.google.gson.annotations.Expose;
32
33 import java.io.Serializable;
34 import java.util.HashMap;
35 import java.util.Map;
36 import java.util.Map.Entry;
37
38 import javax.persistence.Column;
39 import javax.persistence.Entity;
40 import javax.persistence.FetchType;
41 import javax.persistence.Id;
42 import javax.persistence.JoinColumn;
43 import javax.persistence.ManyToOne;
44 import javax.persistence.Table;
45
46 import org.hibernate.annotations.Type;
47 import org.hibernate.annotations.TypeDef;
48 import org.hibernate.annotations.TypeDefs;
49 import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
50 import org.onap.clamp.loop.Loop;
51 import org.onap.clamp.policy.Policy;
52 import org.yaml.snakeyaml.Yaml;
53
54 @Entity
55 @Table(name = "operational_policies")
56 @TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
57 public class OperationalPolicy implements Serializable, Policy {
58     /**
59      * The serial version ID.
60      */
61     private static final long serialVersionUID = 6117076450841538255L;
62
63     @Id
64     @Expose
65     @Column(nullable = false, name = "name", unique = true)
66     private String name;
67
68     @Expose
69     @Type(type = "json")
70     @Column(columnDefinition = "json", name = "configurations_json")
71     private JsonObject configurationsJson;
72
73     @ManyToOne(fetch = FetchType.LAZY)
74     @JoinColumn(name = "loop_id", nullable = false)
75     private Loop loop;
76
77     public OperationalPolicy() {
78         // Serialization
79     }
80
81     /**
82      * The constructor.
83      *
84      * @param name
85      *        The name of the operational policy
86      * @param loop
87      *        The loop that uses this operational policy
88      * @param configurationsJson
89      *        The operational policy property in the format of json
90      */
91     public OperationalPolicy(String name, Loop loop, JsonObject configurationsJson) {
92         this.name = name;
93         this.loop = loop;
94         this.configurationsJson = configurationsJson;
95     }
96
97     @Override
98     public String getName() {
99         return name;
100     }
101
102     public void setLoop(Loop loopName) {
103         this.loop = loopName;
104     }
105
106     public Loop getLoop() {
107         return loop;
108     }
109
110     @Override
111     public JsonObject getJsonRepresentation() {
112         return configurationsJson;
113     }
114
115     public JsonObject getConfigurationsJson() {
116         return configurationsJson;
117     }
118
119     public void setConfigurationsJson(JsonObject configurationsJson) {
120         this.configurationsJson = configurationsJson;
121     }
122
123     @Override
124     public int hashCode() {
125         final int prime = 31;
126         int result = 1;
127         result = prime * result + ((name == null) ? 0 : name.hashCode());
128         return result;
129     }
130
131     @Override
132     public boolean equals(Object obj) {
133         if (this == obj) {
134             return true;
135         }
136         if (obj == null) {
137             return false;
138         }
139         if (getClass() != obj.getClass()) {
140             return false;
141         }
142         OperationalPolicy other = (OperationalPolicy) obj;
143         if (name == null) {
144             if (other.name != null) {
145                 return false;
146             }
147         } else if (!name.equals(other.name)) {
148             return false;
149         }
150         return true;
151     }
152
153     @Override
154     public String createPolicyPayload() {
155         JsonObject policyPayloadResult = new JsonObject();
156
157         policyPayloadResult.addProperty("tosca_definitions_version", "tosca_simple_yaml_1_0_0");
158
159         JsonObject topologyTemplateNode = new JsonObject();
160         policyPayloadResult.add("topology_template", topologyTemplateNode);
161
162         JsonArray policiesArray = new JsonArray();
163         topologyTemplateNode.add("policies", policiesArray);
164
165         JsonObject operationalPolicy = new JsonObject();
166         policiesArray.add(operationalPolicy);
167
168         JsonObject operationalPolicyDetails = new JsonObject();
169         operationalPolicy.add(this.name, operationalPolicyDetails);
170         operationalPolicyDetails.addProperty("type", "onap.policies.controlloop.Operational");
171         operationalPolicyDetails.addProperty("version", "1.0.0");
172
173         JsonObject metadata = new JsonObject();
174         operationalPolicyDetails.add("metadata", metadata);
175         metadata.addProperty("policy-id", this.name);
176
177         operationalPolicyDetails.add("properties", this.configurationsJson.get("operational_policy"));
178
179         Gson gson = new GsonBuilder().create();
180         Map<?, ?> jsonMap = gson.fromJson(gson.toJson(policyPayloadResult), Map.class);
181         return (new Yaml()).dump(jsonMap);
182     }
183
184     /**
185      * Return a map containing all Guard policies indexed by Guard policy Name.
186      * 
187      * @return The Guards map
188      */
189     public Map<String, String> createGuardPolicyPayloads() {
190         Map<String, String> result = new HashMap<>();
191
192         JsonObject guard = new JsonObject();
193         JsonElement guardsList = this.getConfigurationsJson().get("guard_policies");
194         for (Entry<String, JsonElement> guardElem : guardsList.getAsJsonObject().entrySet()) {
195             guard.addProperty("policy-id", guardElem.getKey());
196             guard.add("contents", guardElem.getValue());
197             result.put(guardElem.getKey(), new GsonBuilder().create().toJson(guard));
198         }
199         return result;
200     }
201
202 }