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