2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.drools.protocol.coders;
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;
30 * Protocol Coder that does its best attempt to decode/encode, selecting the best class and best fitted json parsing
33 class MultiplexorEventProtocolCoder implements EventProtocolCoder {
38 private static final Logger logger = LoggerFactory.getLogger(MultiplexorEventProtocolCoder.class);
43 protected EventProtocolDecoder decoders = new EventProtocolDecoder();
48 protected EventProtocolEncoder encoders = new EventProtocolEncoder();
54 public void addDecoder(EventProtocolParams eventProtocolParams) {
56 "{}: add-decoder {}:{}:{}:{}:{}:{}:{}",
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);
71 * @param eventProtocolParams parameter object for event encoder
74 public void addEncoder(EventProtocolParams eventProtocolParams) {
76 "{}: add-decoder {}:{}:{}:{}:{}:{}:{}",
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);
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);
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);
110 public boolean isDecodingSupported(String groupId, String artifactId, String topic) {
111 return this.decoders.isCodingSupported(groupId, artifactId, topic);
118 public boolean isEncodingSupported(String groupId, String artifactId, String topic) {
119 return this.encoders.isCodingSupported(groupId, artifactId, topic);
126 public Object decode(String groupId, String artifactId, String topic, String json) {
127 logger.debug("{}: decode {}:{}:{}:{}", this, groupId, artifactId, topic, json); // NOSONAR
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.
140 return this.decoders.decode(groupId, artifactId, topic, json);
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);
156 public String encode(String topic, Object event) {
157 logger.debug("{}: encode {}:{}", this, topic, event); // NOSONAR
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(),
164 return this.encoders.encode(topic, event);
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);
180 public List<CoderFilters> getDecoderFilters(String groupId, String artifactId, String topic) {
181 return this.decoders.getFilters(groupId, artifactId, topic);
188 public CoderFilters getDecoderFilters(
189 String groupId, String artifactId, String topic, String classname) {
190 return this.decoders.getFilters(groupId, artifactId, topic, classname);
197 public List<CoderFilters> getDecoderFilters(String groupId, String artifactId) {
198 return this.decoders.getFilters(groupId, artifactId);
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);
213 return decoderToolsets;
217 * get all deocders by maven coordinates and topic.
219 * @param groupId group id
220 * @param artifactId artifact id
221 * @return list of decoders
222 * @throws IllegalArgumentException if invalid input
225 public List<ProtocolCoderToolset> getDecoders(String groupId, String artifactId) {
227 List<ProtocolCoderToolset> decoderToolsets =
228 this.decoders.getCoders(groupId, artifactId);
229 if (decoderToolsets == null) {
230 throw new IllegalArgumentException("Decoders not found for " + groupId + ":" + artifactId);
233 return new ArrayList<>(decoderToolsets);
240 public List<CoderFilters> getEncoderFilters(String groupId, String artifactId, String topic) {
241 return this.encoders.getFilters(groupId, artifactId, topic);
248 public CoderFilters getEncoderFilters(
249 String groupId, String artifactId, String topic, String classname) {
250 return this.encoders.getFilters(groupId, artifactId, topic, classname);
257 public List<CoderFilters> getEncoderFilters(String groupId, String artifactId) {
258 return this.encoders.getFilters(groupId, artifactId);
265 public List<CoderFilters> getReverseEncoderFilters(String topic, String encodedClass) {
266 return this.encoders.getReverseFilters(topic, encodedClass);
273 public DroolsController getDroolsController(String topic, Object encodedClass) {
274 return this.encoders.getDroolsController(topic, encodedClass);
281 public List<DroolsController> getDroolsControllers(String topic, Object encodedClass) {
282 return this.encoders.getDroolsControllers(topic, encodedClass);
289 public String toString() {
290 return "MultiplexorEventProtocolCoder [decoders="