d341024a6dead207e57436bede49a722ee54f095
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019, 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.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 final 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);  // NOSONAR
128
129         /*
130          * It seems that sonar declares the previous logging line as a security vulnerability
131          * when logging the topic variable.   The static code analysis indicates that
132          * the path starts in org.onap.policy.drools.server.restful.RestManager::decode(),
133          * but the request is rejected if the topic contains invalid characters (the sonar description
134          * mentions "/r/n/t" characters) which are validated against in the checkValidNameInput(topic).
135          * Furthermore production instances are assumed not to have debug enabled, nor the REST telemetry API
136          * should be published externally.  An additional note is that Path URLs containing spaces and newlines
137          * will be failed earlier at the HTTP protocol libraries (jetty, etc ..) so an URL of the form
138          * "https://../to\npic" won't even make it here.
139          */
140         return this.decoders.decode(groupId, artifactId, topic, json);
141     }
142
143     /**
144      * {@inheritDoc}.
145      */
146     @Override
147     public String encode(String groupId, String artifactId, String topic, Object event) {
148         logger.debug("{}: encode {}:{}:{}:{}", this, groupId, artifactId, topic, event);
149         return this.encoders.encode(groupId, artifactId, topic, event);
150     }
151
152     /**
153      * {@inheritDoc}.
154      */
155     @Override
156     public String encode(String topic, Object event) {
157         logger.debug("{}: encode {}:{}", this, topic, event);  // NOSONAR
158
159         /*
160          * See explanation for decode(String groupId, String artifactId, String topic, String json).
161          * The same applies here as it is called from
162          * org.onap.policy.drools.server.restful.RestManager::encode(),
163          */
164         return this.encoders.encode(topic, event);
165     }
166
167     /**
168      * {@inheritDoc}.
169      */
170     @Override
171     public String encode(String topic, Object event, DroolsController droolsController) {
172         logger.debug("{}: encode {}:{}:{}", this, topic, event, droolsController);
173         return this.encoders.encode(topic, event, droolsController);
174     }
175
176     /**
177      * {@inheritDoc}.
178      */
179     @Override
180     public List<CoderFilters> getDecoderFilters(String groupId, String artifactId, String topic) {
181         return this.decoders.getFilters(groupId, artifactId, topic);
182     }
183
184     /**
185      * {@inheritDoc}.
186      */
187     @Override
188     public CoderFilters getDecoderFilters(
189             String groupId, String artifactId, String topic, String classname) {
190         return this.decoders.getFilters(groupId, artifactId, topic, classname);
191     }
192
193     /**
194      * {@inheritDoc}.
195      */
196     @Override
197     public List<CoderFilters> getDecoderFilters(String groupId, String artifactId) {
198         return this.decoders.getFilters(groupId, artifactId);
199     }
200
201     /**
202      * {@inheritDoc}.
203      */
204     @Override
205     public ProtocolCoderToolset getDecoders(String groupId, String artifactId, String topic) {
206         ProtocolCoderToolset decoderToolsets =
207                 this.decoders.getCoders(groupId, artifactId, topic);
208         if (decoderToolsets == null) {
209             throw new IllegalArgumentException(
210                     "Decoders not found for " + groupId + ":" + artifactId + ":" + topic);
211         }
212
213         return decoderToolsets;
214     }
215
216     /**
217      * get all deocders by maven coordinates and topic.
218      *
219      * @param groupId    group id
220      * @param artifactId artifact id
221      * @return list of decoders
222      * @throws IllegalArgumentException if invalid input
223      */
224     @Override
225     public List<ProtocolCoderToolset> getDecoders(String groupId, String artifactId) {
226
227         List<ProtocolCoderToolset> decoderToolsets =
228                 this.decoders.getCoders(groupId, artifactId);
229         if (decoderToolsets == null) {
230             throw new IllegalArgumentException("Decoders not found for " + groupId + ":" + artifactId);
231         }
232
233         return new ArrayList<>(decoderToolsets);
234     }
235
236     /**
237      * {@inheritDoc}.
238      */
239     @Override
240     public List<CoderFilters> getEncoderFilters(String groupId, String artifactId, String topic) {
241         return this.encoders.getFilters(groupId, artifactId, topic);
242     }
243
244     /**
245      * {@inheritDoc}.
246      */
247     @Override
248     public CoderFilters getEncoderFilters(
249             String groupId, String artifactId, String topic, String classname) {
250         return this.encoders.getFilters(groupId, artifactId, topic, classname);
251     }
252
253     /**
254      * {@inheritDoc}.
255      */
256     @Override
257     public List<CoderFilters> getEncoderFilters(String groupId, String artifactId) {
258         return this.encoders.getFilters(groupId, artifactId);
259     }
260
261     /**
262      * {@inheritDoc}.
263      */
264     @Override
265     public List<CoderFilters> getReverseEncoderFilters(String topic, String encodedClass) {
266         return this.encoders.getReverseFilters(topic, encodedClass);
267     }
268
269     /**
270      * {@inheritDoc}.
271      */
272     @Override
273     public DroolsController getDroolsController(String topic, Object encodedClass) {
274         return this.encoders.getDroolsController(topic, encodedClass);
275     }
276
277     /**
278      * {@inheritDoc}.
279      */
280     @Override
281     public List<DroolsController> getDroolsControllers(String topic, Object encodedClass) {
282         return this.encoders.getDroolsControllers(topic, encodedClass);
283     }
284
285     /**
286      * {@inheritDoc}.
287      */
288     @Override
289     public String toString() {
290         return "MultiplexorEventProtocolCoder [decoders="
291                 + decoders
292                 + ", encoders="
293                 + encoders
294                 + "]";
295     }
296 }