4dd132b92517b9aab4c882964e62acbf44803f0f
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * policy-management
4  * ================================================================================
5  * Copyright (C) 2017-2021 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 import lombok.AllArgsConstructor;
25 import lombok.Getter;
26 import lombok.Setter;
27 import lombok.ToString;
28
29 @Getter
30 @ToString
31 public class TopicCoderFilterConfiguration {
32
33     /**
34      * Custom coder, contains class and static field to access parser that the controller desires to
35      * use instead of the framework provided parser.
36      */
37     @Getter
38     @Setter
39     @ToString
40     public abstract static class CustomCoder {
41         protected String classContainer;
42         protected String staticCoderField;
43
44         /**
45          * create custom coder from raw string in the following format (typically embedded in a property
46          * file):
47          *
48          * <p>Note this is to support decoding/encoding of partial structures that are only known by the
49          * model.
50          *
51          * @param rawCustomCoder with format: &lt;class-containing-custom-coder&gt;,&lt;static-coder-field&gt.
52          */
53         protected CustomCoder(String rawCustomCoder) {
54             if (rawCustomCoder != null && !rawCustomCoder.isEmpty()) {
55
56                 this.classContainer = rawCustomCoder.substring(0, rawCustomCoder.indexOf(','));
57                 if (this.classContainer == null || this.classContainer.isEmpty()) {
58                     throw new IllegalArgumentException(
59                             "No classname to create CustomCoder cannot be created");
60                 }
61
62                 this.staticCoderField = rawCustomCoder.substring(rawCustomCoder.indexOf(',') + 1);
63                 if (this.staticCoderField == null || this.staticCoderField.isEmpty()) {
64                     throw new IllegalArgumentException(
65                             "No staticCoderField to create CustomCoder cannot be created for class " + classContainer);
66                 }
67             }
68         }
69
70         /**
71          * Constructor.
72          *
73          * @param className class name
74          * @param staticCoderField static coder field
75          */
76         protected CustomCoder(String className, String staticCoderField) {
77             if (className == null || className.isEmpty()) {
78                 throw new IllegalArgumentException("No classname to create CustomCoder cannot be created");
79             }
80
81             if (staticCoderField == null || staticCoderField.isEmpty()) {
82                 throw new IllegalArgumentException(
83                         "No staticCoderField to create CustomCoder cannot be created for class " + className);
84             }
85
86             this.classContainer = className;
87             this.staticCoderField = staticCoderField;
88         }
89     }
90
91     @ToString(callSuper = true)
92     public static class CustomGsonCoder extends CustomCoder {
93
94         public CustomGsonCoder(String className, String staticCoderField) {
95             super(className, staticCoderField);
96         }
97
98         public CustomGsonCoder(String customGson) {
99             super(customGson);
100         }
101     }
102
103     /**
104      * Coder/Decoder class and Filter container. The decoder class is potential, in order to be
105      * operational needs to be fetched from an available class loader.
106      */
107     @Getter
108     @Setter
109     @ToString
110     @AllArgsConstructor
111     public static class PotentialCoderFilter {
112
113         /* decoder class (pending from being able to be fetched and found in some class loader) */
114         protected String codedClass;
115
116         /* filters to apply to the selection of the decodedClass */
117         protected JsonProtocolFilter filter;
118     }
119
120     /* the source topic */
121     protected final String topic;
122
123     /* List of decoder -> filters */
124     protected final List<PotentialCoderFilter> coderFilters;
125
126     /* custom gson coder that this controller prefers to use instead of the framework ones */
127     @Setter
128     protected CustomGsonCoder customGsonCoder;
129
130     /**
131      * Constructor.
132      *
133      * @param topic the topic
134      * @param decoderFilters list of decoders and associated filters
135      * @param customGsonCoder GSON coder
136      */
137     public TopicCoderFilterConfiguration(
138             String topic,
139             List<PotentialCoderFilter> decoderFilters,
140             CustomGsonCoder customGsonCoder) {
141         this.coderFilters = decoderFilters;
142         this.topic = topic;
143         this.customGsonCoder = customGsonCoder;
144     }
145 }