7c1b128fe30a08c95f4d7b60e41be7c2b7514f8d
[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
29          * desires to 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
37                  * (typically embedded in a property file):
38                  * 
39                  * Note this is to support decoding/encoding of partial structures that are
40                  * only known by the model.
41                  * 
42                  * @param rawCustomCoder with format: <class-containing-custom-coder>,<static-coder-field>
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("No classname to create CustomCoder cannot be created");
50                                 }
51                                 
52                                 this.staticCoderField = rawCustomCoder.substring(rawCustomCoder.indexOf(",")+1);
53                                 if (this.staticCoderField == null || this.staticCoderField.isEmpty()) {
54                                         throw new IllegalArgumentException
55                                                 ("No staticCoderField to create CustomCoder cannot be created for class " +
56                                                  className);
57                                 }
58
59                         }
60                 }
61                 /**
62                  * @param classContainer
63                  * @param staticCoderField
64                  */
65                 public CustomCoder(String className, String staticCoderField) {
66                         if (className == null || className.isEmpty()) {
67                                 throw new IllegalArgumentException("No classname to create CustomCoder cannot be created");
68                         }
69                         
70                         if (staticCoderField == null || staticCoderField.isEmpty()) {
71                                 throw new IllegalArgumentException
72                                         ("No staticCoderField to create CustomCoder cannot be created for class " +
73                                          className);
74                         }
75                         
76                         this.className = className;
77                         this.staticCoderField = staticCoderField;
78                 }
79
80                 /**
81                  * @return the className
82                  */
83                 public String getClassContainer() {
84                         return className;
85                 }
86
87                 /**
88                  * @param className the className to set
89                  */
90                 public void setClassContainer(String className) {
91                         this.className = className;
92                 }
93
94                 /**
95                  * @return the staticCoderField
96                  */
97                 public String getStaticCoderField() {
98                         return staticCoderField;
99                 }
100
101                 /**
102                  * @param staticCoderField the staticGson to set
103                  */
104                 public void setStaticCoderField(String staticCoderField) {
105                         this.staticCoderField = staticCoderField;
106                 }
107
108                 @Override
109                 public String toString() {
110                         StringBuilder builder = new StringBuilder();
111                         builder.append("CustomCoder [className=").append(className).append(", staticCoderField=")
112                                         .append(staticCoderField).append("]");
113                         return builder.toString();
114                 }
115         }
116         
117         public static class CustomGsonCoder extends CustomCoder {
118         
119                 public CustomGsonCoder(String className, String staticCoderField) {
120                                 super(className, staticCoderField);
121                 }
122
123                 public CustomGsonCoder(String customGson) {
124                         super(customGson);
125                 }
126
127                 @Override
128                 public String toString() {
129                         StringBuilder builder = new StringBuilder();
130                         builder.append("CustomGsonCoder [toString()=").append(super.toString()).append("]");
131                         return builder.toString();
132                 }
133
134         }
135         
136         public static class CustomJacksonCoder extends CustomCoder {
137                 
138                 public CustomJacksonCoder(String className, String staticCoderField) {
139                                 super(className, staticCoderField);
140                 }
141                 
142                 public CustomJacksonCoder(String customJackson) {
143                         super(customJackson);
144                 }
145                 
146                 @Override
147                 public String toString() {
148                         StringBuilder builder = new StringBuilder();
149                         builder.append("CustomJacksonCoder [toString()=").append(super.toString()).append("]");
150                         return builder.toString();
151                 }
152
153         }
154
155         /**
156          * Coder/Decoder class and Filter container.   The decoder class is potential,
157          * in order to be operational needs to be fetched from an available
158          * class loader.
159          *
160          */
161         public static class PotentialCoderFilter {
162
163                 /**
164                  * decoder class (pending from being able to be fetched and found 
165                  * in some class loader)
166                  */
167                 protected String codedClass;
168                 
169                 /**
170                  * filters to apply to the selection of the decodedClass;
171                  */
172                 protected JsonProtocolFilter filter;
173                 
174                 /**
175                  * constructor
176                  * 
177                  * @param codedClass decoder class
178                  * @param filter filters to apply
179                  */
180                 public PotentialCoderFilter(String codedClass, JsonProtocolFilter filter) {
181                         this.codedClass = codedClass;
182                         this.filter = filter;
183                 }
184
185                 /**
186                  * @return the decodedClass
187                  */
188                 public String getCodedClass() {
189                         return codedClass;
190                 }
191
192                 /**
193                  * @param decodedClass the decodedClass to set
194                  */
195                 public void setCodedClass(String decodedClass) {
196                         this.codedClass = decodedClass;
197                 }
198
199                 /**
200                  * @return the filter
201                  */
202                 public JsonProtocolFilter getFilter() {
203                         return filter;
204                 }
205
206                 /**
207                  * @param filter the filter to set
208                  */
209                 public void setFilter(JsonProtocolFilter filter) {
210                         this.filter = filter;
211                 }
212
213                 @Override
214                 public String toString() {
215                         StringBuilder builder = new StringBuilder();
216                         builder.append("PotentialCoderFilter [codedClass=").append(codedClass).append(", filter=").append(filter)
217                                         .append("]");
218                         return builder.toString();
219                 }
220         }
221         
222         /**
223          * the source topic
224          */
225         protected final String topic;
226         
227         /**
228          * List of decoder -> filters
229          */
230         protected final List<PotentialCoderFilter> coderFilters;
231         
232         /**
233          * custom gson coder that this controller prefers to use instead of the framework ones
234          */
235         protected CustomGsonCoder customGsonCoder;
236         
237         /**
238          * custom jackson coder that this controller prefers to use instead of the framework ones
239          */
240         protected CustomJacksonCoder customJacksonCoder;
241
242         /**
243          * Constructor 
244          * 
245          * @param decoderFilters list of decoders and associated filters
246          * @param topic the topic
247          */
248         public TopicCoderFilterConfiguration(String topic, List<PotentialCoderFilter> decoderFilters,
249                                          CustomGsonCoder customGsonCoder, 
250                                          CustomJacksonCoder customJacksonCoder) {
251                 this.coderFilters = decoderFilters;
252                 this.topic = topic;
253                 this.customGsonCoder = customGsonCoder;
254                 this.customJacksonCoder = customJacksonCoder;
255         }
256
257         /**
258          * @return the topic
259          */
260         public String getTopic() {
261                 return topic;
262         }
263
264         /**
265          * @return the decoderFilters
266          */
267         public List<PotentialCoderFilter> getCoderFilters() {
268                 return coderFilters;
269         }
270         
271         /**
272          * @return the customGsonCoder
273          */
274         public CustomGsonCoder getCustomGsonCoder() {
275                 return customGsonCoder;
276         }
277
278         /**
279          * @param customGsonCoder the customGsonCoder to set
280          */
281         public void setCustomGsonCoder(CustomGsonCoder customGsonCoder) {
282                 this.customGsonCoder = customGsonCoder;
283         }
284
285         /**
286          * @return the customJacksonCoder
287          */
288         public CustomJacksonCoder getCustomJacksonCoder() {
289                 return customJacksonCoder;
290         }
291
292         /**
293          * @param customJacksonCoder the customJacksonCoder to set
294          */
295         public void setCustomJacksonCoder(CustomJacksonCoder customJacksonCoder) {
296                 this.customJacksonCoder = customJacksonCoder;
297         }
298
299         @Override
300         public String toString() {
301                 StringBuilder builder = new StringBuilder();
302                 builder.append("TopicCoderFilterConfiguration [topic=").append(topic).append(", coderFilters=")
303                                 .append(coderFilters).append(", customGsonCoder=").append(customGsonCoder)
304                                 .append(", customJacksonCoder=").append(customJacksonCoder).append("]");
305                 return builder.toString();
306         }
307
308
309 }