2d69453004bff19806bcaee902f62940f930a9da
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-management
4  * ================================================================================
5  * Copyright (C) 2017-2020 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     /**
149      * Coder/Decoder class and Filter container. The decoder class is potential, in order to be
150      * operational needs to be fetched from an available class loader.
151      */
152     public static class PotentialCoderFilter {
153
154         /* decoder class (pending from being able to be fetched and found in some class loader) */
155         protected String codedClass;
156
157         /* filters to apply to the selection of the decodedClass */
158         protected JsonProtocolFilter filter;
159
160         /**
161          * constructor.
162          *
163          * @param codedClass decoder class
164          * @param filter filters to apply
165          */
166         public PotentialCoderFilter(String codedClass, JsonProtocolFilter filter) {
167             this.codedClass = codedClass;
168             this.filter = filter;
169         }
170
171         /**
172          * Get coded class.
173          *
174          * @return the decodedClass
175          **/
176         public String getCodedClass() {
177             return codedClass;
178         }
179
180         /** Set coded class.
181          *
182          * @param decodedClass the decodedClass to set
183          **/
184         public void setCodedClass(String decodedClass) {
185             this.codedClass = decodedClass;
186         }
187
188         /**
189          * Get filter.
190          *
191          * @return the filter
192          **/
193         public JsonProtocolFilter getFilter() {
194             return filter;
195         }
196
197         /**
198          * Set filter.
199          *
200          * @param filter the filter to set
201          **/
202         public void setFilter(JsonProtocolFilter filter) {
203             this.filter = filter;
204         }
205
206         @Override
207         public String toString() {
208             StringBuilder builder = new StringBuilder();
209             builder
210                 .append("PotentialCoderFilter [codedClass=")
211                 .append(codedClass)
212                 .append(", filter=")
213                 .append(filter)
214                 .append("]");
215             return builder.toString();
216         }
217     }
218
219     /* the source topic */
220     protected final String topic;
221
222     /* List of decoder -> filters */
223     protected final List<PotentialCoderFilter> coderFilters;
224
225     /* custom gson coder that this controller prefers to use instead of the framework ones */
226     protected CustomGsonCoder customGsonCoder;
227
228     /**
229      * Constructor.
230      *
231      * @param topic the topic
232      * @param decoderFilters list of decoders and associated filters
233      * @param customGsonCoder GSON coder
234      */
235     public TopicCoderFilterConfiguration(
236             String topic,
237             List<PotentialCoderFilter> decoderFilters,
238             CustomGsonCoder customGsonCoder) {
239         this.coderFilters = decoderFilters;
240         this.topic = topic;
241         this.customGsonCoder = customGsonCoder;
242     }
243
244     /**
245      * Get topic.
246      * @return the topic
247      **/
248     public String getTopic() {
249         return topic;
250     }
251
252     /** Get coder filters.
253      *
254      * @return the decoderFilters
255      **/
256     public List<PotentialCoderFilter> getCoderFilters() {
257         return coderFilters;
258     }
259
260     /**
261      * Get custom gson coder.
262      *
263      * @return the customGsonCoder
264      **/
265     public CustomGsonCoder getCustomGsonCoder() {
266         return customGsonCoder;
267     }
268
269     /**
270      * Set custom gson coder.
271      *
272      * @param customGsonCoder the customGsonCoder to set
273      **/
274     public void setCustomGsonCoder(CustomGsonCoder customGsonCoder) {
275         this.customGsonCoder = customGsonCoder;
276     }
277
278     @Override
279     public String toString() {
280         StringBuilder builder = new StringBuilder();
281         builder
282             .append("TopicCoderFilterConfiguration [topic=")
283             .append(topic)
284             .append(", coderFilters=")
285             .append(coderFilters)
286             .append(", customGsonCoder=")
287             .append(customGsonCoder)
288             .append("]");
289         return builder.toString();
290     }
291 }