a8f3c3a32d3deedc2bd0d5e81f135af127e16fb7
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019 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.ArrayList;
24 import java.util.List;
25 import org.onap.policy.drools.controller.DroolsController;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Protocol Coder that does its best attempt to decode/encode, selecting the best class and best fitted json parsing
31  * tools.
32  */
33 class MultiplexorEventProtocolCoder implements EventProtocolCoder {
34
35     /**
36      * Logger.
37      */
38     private static Logger logger = LoggerFactory.getLogger(MultiplexorEventProtocolCoder.class);
39
40     /**
41      * Decoders.
42      */
43     protected EventProtocolDecoder decoders = new EventProtocolDecoder();
44
45     /**
46      * Encoders.
47      */
48     protected EventProtocolEncoder encoders = new EventProtocolEncoder();
49
50     /**
51      * {@inheritDoc}.
52      */
53     @Override
54     public void addDecoder(EventProtocolParams eventProtocolParams) {
55         logger.info(
56                 "{}: add-decoder {}:{}:{}:{}:{}:{}:{}",
57                 this,
58                 eventProtocolParams.getGroupId(),
59                 eventProtocolParams.getArtifactId(),
60                 eventProtocolParams.getTopic(),
61                 eventProtocolParams.getEventClass(),
62                 eventProtocolParams.getProtocolFilter(),
63                 eventProtocolParams.getCustomGsonCoder(),
64                 eventProtocolParams.getModelClassLoaderHash());
65         this.decoders.add(eventProtocolParams);
66     }
67
68     /**
69      * {@inheritDoc}.
70      *
71      * @param eventProtocolParams parameter object for event encoder
72      */
73     @Override
74     public void addEncoder(EventProtocolParams eventProtocolParams) {
75         logger.info(
76                 "{}: add-decoder {}:{}:{}:{}:{}:{}:{}",
77                 this,
78                 eventProtocolParams.getGroupId(),
79                 eventProtocolParams.getArtifactId(),
80                 eventProtocolParams.getTopic(),
81                 eventProtocolParams.getEventClass(),
82                 eventProtocolParams.getProtocolFilter(),
83                 eventProtocolParams.getCustomGsonCoder(),
84                 eventProtocolParams.getModelClassLoaderHash());
85         this.encoders.add(eventProtocolParams);
86     }
87
88     /**
89      * {@inheritDoc}.
90      */
91     @Override
92     public void removeDecoders(String groupId, String artifactId, String topic) {
93         logger.info("{}: remove-decoder {}:{}:{}", this, groupId, artifactId, topic);
94         this.decoders.remove(groupId, artifactId, topic);
95     }
96
97     /**
98      * {@inheritDoc}.
99      */
100     @Override
101     public void removeEncoders(String groupId, String artifactId, String topic) {
102         logger.info("{}: remove-encoder {}:{}:{}", this, groupId, artifactId, topic);
103         this.encoders.remove(groupId, artifactId, topic);
104     }
105
106     /**
107      * {@inheritDoc}.
108      */
109     @Override
110     public boolean isDecodingSupported(String groupId, String artifactId, String topic) {
111         return this.decoders.isCodingSupported(groupId, artifactId, topic);
112     }
113
114     /**
115      * {@inheritDoc}.
116      */
117     @Override
118     public boolean isEncodingSupported(String groupId, String artifactId, String topic) {
119         return this.encoders.isCodingSupported(groupId, artifactId, topic);
120     }
121
122     /**
123      * {@inheritDoc}.
124      */
125     @Override
126     public Object decode(String groupId, String artifactId, String topic, String json) {
127         logger.debug("{}: decode {}:{}:{}:{}", this, groupId, artifactId, topic, json);
128         return this.decoders.decode(groupId, artifactId, topic, json);
129     }
130
131     /**
132      * {@inheritDoc}.
133      */
134     @Override
135     public String encode(String groupId, String artifactId, String topic, Object event) {
136         logger.debug("{}: encode {}:{}:{}:{}", this, groupId, artifactId, topic, event);
137         return this.encoders.encode(groupId, artifactId, topic, event);
138     }
139
140     /**
141      * {@inheritDoc}.
142      */
143     @Override
144     public String encode(String topic, Object event) {
145         logger.debug("{}: encode {}:{}", this, topic, event);
146         return this.encoders.encode(topic, event);
147     }
148
149     /**
150      * {@inheritDoc}.
151      */
152     @Override
153     public String encode(String topic, Object event, DroolsController droolsController) {
154         logger.debug("{}: encode {}:{}:{}", this, topic, event, droolsController);
155         return this.encoders.encode(topic, event, droolsController);
156     }
157
158     /**
159      * {@inheritDoc}.
160      */
161     @Override
162     public List<CoderFilters> getDecoderFilters(String groupId, String artifactId, String topic) {
163         return this.decoders.getFilters(groupId, artifactId, topic);
164     }
165
166     /**
167      * {@inheritDoc}.
168      */
169     @Override
170     public CoderFilters getDecoderFilters(
171             String groupId, String artifactId, String topic, String classname) {
172         return this.decoders.getFilters(groupId, artifactId, topic, classname);
173     }
174
175     /**
176      * {@inheritDoc}.
177      */
178     @Override
179     public List<CoderFilters> getDecoderFilters(String groupId, String artifactId) {
180         return this.decoders.getFilters(groupId, artifactId);
181     }
182
183     /**
184      * {@inheritDoc}.
185      */
186     @Override
187     public ProtocolCoderToolset getDecoders(String groupId, String artifactId, String topic) {
188         ProtocolCoderToolset decoderToolsets =
189                 this.decoders.getCoders(groupId, artifactId, topic);
190         if (decoderToolsets == null) {
191             throw new IllegalArgumentException(
192                     "Decoders not found for " + groupId + ":" + artifactId + ":" + topic);
193         }
194
195         return decoderToolsets;
196     }
197
198     /**
199      * get all deocders by maven coordinates and topic.
200      *
201      * @param groupId    group id
202      * @param artifactId artifact id
203      * @return list of decoders
204      * @throws IllegalArgumentException if invalid input
205      */
206     @Override
207     public List<ProtocolCoderToolset> getDecoders(String groupId, String artifactId) {
208
209         List<ProtocolCoderToolset> decoderToolsets =
210                 this.decoders.getCoders(groupId, artifactId);
211         if (decoderToolsets == null) {
212             throw new IllegalArgumentException("Decoders not found for " + groupId + ":" + artifactId);
213         }
214
215         return new ArrayList<>(decoderToolsets);
216     }
217
218     /**
219      * {@inheritDoc}.
220      */
221     @Override
222     public List<CoderFilters> getEncoderFilters(String groupId, String artifactId, String topic) {
223         return this.encoders.getFilters(groupId, artifactId, topic);
224     }
225
226     /**
227      * {@inheritDoc}.
228      */
229     @Override
230     public CoderFilters getEncoderFilters(
231             String groupId, String artifactId, String topic, String classname) {
232         return this.encoders.getFilters(groupId, artifactId, topic, classname);
233     }
234
235     /**
236      * {@inheritDoc}.
237      */
238     @Override
239     public List<CoderFilters> getEncoderFilters(String groupId, String artifactId) {
240         return this.encoders.getFilters(groupId, artifactId);
241     }
242
243     /**
244      * {@inheritDoc}.
245      */
246     @Override
247     public List<CoderFilters> getReverseEncoderFilters(String topic, String encodedClass) {
248         return this.encoders.getReverseFilters(topic, encodedClass);
249     }
250
251     /**
252      * {@inheritDoc}.
253      */
254     @Override
255     public DroolsController getDroolsController(String topic, Object encodedClass) {
256         return this.encoders.getDroolsController(topic, encodedClass);
257     }
258
259     /**
260      * {@inheritDoc}.
261      */
262     @Override
263     public List<DroolsController> getDroolsControllers(String topic, Object encodedClass) {
264         return this.encoders.getDroolsControllers(topic, encodedClass);
265     }
266
267     /**
268      * {@inheritDoc}.
269      */
270     @Override
271     public String toString() {
272         return "MultiplexorEventProtocolCoder [decoders="
273                 + decoders
274                 + ", encoders="
275                 + encoders
276                 + "]";
277     }
278 }