f7fc4d211cc29f00079554bdd90dcbd3adfee4b2
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-management
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.drools.protocol.coders;
22
23 import java.util.List;
24
25 public class TopicCoderFilterConfiguration {
26
27     /**
28      * Custom coder, contains class and static field to access parser that the controller desires to
29      * use instead of the framework provided parser.
30      */
31     public abstract static class CustomCoder {
32         protected String className;
33         protected String staticCoderField;
34
35         /**
36          * create custom coder from raw string in the following format (typically embedded in a property
37          * file):
38          *
39          * <p>Note this is to support decoding/encoding of partial structures that are only known by the
40          * model.
41          *
42          * @param rawCustomCoder with format: &lt;class-containing-custom-coder&gt;,&lt;static-coder-field&gt.
43          */
44         public CustomCoder(String rawCustomCoder) {
45             if (rawCustomCoder != null && !rawCustomCoder.isEmpty()) {
46
47                 this.className = rawCustomCoder.substring(0, rawCustomCoder.indexOf(","));
48                 if (this.className == null || this.className.isEmpty()) {
49                     throw new IllegalArgumentException(
50                             "No classname to create CustomCoder cannot be created");
51                 }
52
53                 this.staticCoderField = rawCustomCoder.substring(rawCustomCoder.indexOf(",") + 1);
54                 if (this.staticCoderField == null || this.staticCoderField.isEmpty()) {
55                     throw new IllegalArgumentException(
56                             "No staticCoderField to create CustomCoder cannot be created for class " + className);
57                 }
58             }
59         }
60         
61         /**
62          * Constructor.
63          * 
64          * @param className class name
65          * @param staticCoderField static coder field
66          */
67         public CustomCoder(String className, String staticCoderField) {
68             if (className == null || className.isEmpty()) {
69                 throw new IllegalArgumentException("No classname to create CustomCoder cannot be created");
70             }
71
72             if (staticCoderField == null || staticCoderField.isEmpty()) {
73                 throw new IllegalArgumentException(
74                         "No staticCoderField to create CustomCoder cannot be created for class " + className);
75             }
76
77             this.className = className;
78             this.staticCoderField = staticCoderField;
79         }
80
81         /** 
82          * Get class container.
83          * 
84          * @return the className 
85          **/
86         public String getClassContainer() {
87             return className;
88         }
89
90         /** 
91          * Set class container.
92          * 
93          * @param className the className to set 
94          **/
95         public void setClassContainer(String className) {
96             this.className = className;
97         }
98
99         /** 
100          * Get static coder field.
101          * 
102          * @return the staticCoderField 
103          **/
104         public String getStaticCoderField() {
105             return staticCoderField;
106         }
107
108         /** 
109          * Set static coder field.
110          * 
111          * @param staticCoderField the staticGson to set 
112          **/
113         public void setStaticCoderField(String staticCoderField) {
114             this.staticCoderField = staticCoderField;
115         }
116
117         @Override
118         public String toString() {
119             StringBuilder builder = new StringBuilder();
120             builder
121                 .append("CustomCoder [className=")
122                 .append(className)
123                 .append(", staticCoderField=")
124                 .append(staticCoderField)
125                 .append("]");
126             return builder.toString();
127         }
128     }
129
130     public static class CustomGsonCoder extends CustomCoder {
131
132         public CustomGsonCoder(String className, String staticCoderField) {
133             super(className, staticCoderField);
134         }
135
136         public CustomGsonCoder(String customGson) {
137             super(customGson);
138         }
139
140         @Override
141         public String toString() {
142             StringBuilder builder = new StringBuilder();
143             builder.append("CustomGsonCoder [toString()=").append(super.toString()).append("]");
144             return builder.toString();
145         }
146     }
147
148     public static class CustomJacksonCoder extends CustomCoder {
149
150         public CustomJacksonCoder(String className, String staticCoderField) {
151             super(className, staticCoderField);
152         }
153
154         public CustomJacksonCoder(String customJackson) {
155             super(customJackson);
156         }
157
158         @Override
159         public String toString() {
160             StringBuilder builder = new StringBuilder();
161             builder.append("CustomJacksonCoder [toString()=").append(super.toString()).append("]");
162             return builder.toString();
163         }
164     }
165
166     /**
167      * Coder/Decoder class and Filter container. The decoder class is potential, in order to be
168      * operational needs to be fetched from an available class loader.
169      */
170     public static class PotentialCoderFilter {
171
172         /* decoder class (pending from being able to be fetched and found in some class loader) */
173         protected String codedClass;
174
175         /* filters to apply to the selection of the decodedClass; */
176         protected JsonProtocolFilter filter;
177
178         /**
179          * constructor.
180          *
181          * @param codedClass decoder class
182          * @param filter filters to apply
183          */
184         public PotentialCoderFilter(String codedClass, JsonProtocolFilter filter) {
185             this.codedClass = codedClass;
186             this.filter = filter;
187         }
188
189         /** 
190          * Get coded class.
191          * 
192          * @return the decodedClass 
193          **/
194         public String getCodedClass() {
195             return codedClass;
196         }
197
198         /** Set coded class.
199          * 
200          * @param decodedClass the decodedClass to set 
201          **/
202         public void setCodedClass(String decodedClass) {
203             this.codedClass = decodedClass;
204         }
205
206         /** 
207          * Get filter.
208          * 
209          * @return the filter 
210          **/
211         public JsonProtocolFilter getFilter() {
212             return filter;
213         }
214
215         /** 
216          * Set filter.
217          * 
218          * @param filter the filter to set 
219          **/
220         public void setFilter(JsonProtocolFilter filter) {
221             this.filter = filter;
222         }
223
224         @Override
225         public String toString() {
226             StringBuilder builder = new StringBuilder();
227             builder
228                 .append("PotentialCoderFilter [codedClass=")
229                 .append(codedClass)
230                 .append(", filter=")
231                 .append(filter)
232                 .append("]");
233             return builder.toString();
234         }
235     }
236
237     /* the source topic */
238     protected final String topic;
239
240     /* List of decoder -> filters */
241     protected final List<PotentialCoderFilter> coderFilters;
242
243     /* custom gson coder that this controller prefers to use instead of the framework ones */
244     protected CustomGsonCoder customGsonCoder;
245
246     /* custom jackson coder that this controller prefers to use instead of the framework ones */
247     protected CustomJacksonCoder customJacksonCoder;
248
249     /**
250      * Constructor.
251      *
252      * @param decoderFilters list of decoders and associated filters
253      * @param topic the topic
254      */
255     public TopicCoderFilterConfiguration(
256             String topic,
257             List<PotentialCoderFilter> decoderFilters,
258             CustomGsonCoder customGsonCoder,
259             CustomJacksonCoder customJacksonCoder) {
260         this.coderFilters = decoderFilters;
261         this.topic = topic;
262         this.customGsonCoder = customGsonCoder;
263         this.customJacksonCoder = customJacksonCoder;
264     }
265
266     /** 
267      * Get topic.
268      * @return the topic 
269      **/
270     public String getTopic() {
271         return topic;
272     }
273
274     /** Get coder filters.
275      * 
276      * @return the decoderFilters 
277      **/
278     public List<PotentialCoderFilter> getCoderFilters() {
279         return coderFilters;
280     }
281
282     /** 
283      * Get custom gson coder.
284      * 
285      * @return the customGsonCoder 
286      **/
287     public CustomGsonCoder getCustomGsonCoder() {
288         return customGsonCoder;
289     }
290
291     /** 
292      * Set custom gson coder.
293      * 
294      * @param customGsonCoder the customGsonCoder to set 
295      **/
296     public void setCustomGsonCoder(CustomGsonCoder customGsonCoder) {
297         this.customGsonCoder = customGsonCoder;
298     }
299
300     /** Get custom jackson coder.
301      * 
302      * @return the customJacksonCoder 
303      **/
304     public CustomJacksonCoder getCustomJacksonCoder() {
305         return customJacksonCoder;
306     }
307
308     /** 
309      * Set custom Jackson coder.
310      * @param customJacksonCoder the customJacksonCoder to set 
311      **/
312     public void setCustomJacksonCoder(CustomJacksonCoder customJacksonCoder) {
313         this.customJacksonCoder = customJacksonCoder;
314     }
315
316     @Override
317     public String toString() {
318         StringBuilder builder = new StringBuilder();
319         builder
320             .append("TopicCoderFilterConfiguration [topic=")
321             .append(topic)
322             .append(", coderFilters=")
323             .append(coderFilters)
324             .append(", customGsonCoder=")
325             .append(customGsonCoder)
326             .append(", customJacksonCoder=")
327             .append(customJacksonCoder)
328             .append("]");
329         return builder.toString();
330     }
331 }