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 lombok.ToString;
26 import org.onap.policy.drools.controller.DroolsController;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Protocol Coder that does its best attempt to decode/encode, selecting the best class and best fitted json parsing
35 class MultiplexorEventProtocolCoder implements EventProtocolCoder {
40 private static final Logger logger = LoggerFactory.getLogger(MultiplexorEventProtocolCoder.class);
45 protected EventProtocolDecoder decoders = new EventProtocolDecoder();
50 protected EventProtocolEncoder encoders = new EventProtocolEncoder();
56 public void addDecoder(EventProtocolParams eventProtocolParams) {
58 "{}: add-decoder {}:{}:{}:{}:{}:{}:{}",
60 eventProtocolParams.getGroupId(),
61 eventProtocolParams.getArtifactId(),
62 eventProtocolParams.getTopic(),
63 eventProtocolParams.getEventClass(),
64 eventProtocolParams.getProtocolFilter(),
65 eventProtocolParams.getCustomGsonCoder(),
66 eventProtocolParams.getModelClassLoaderHash());
67 this.decoders.add(eventProtocolParams);
73 * @param eventProtocolParams parameter object for event encoder
76 public void addEncoder(EventProtocolParams eventProtocolParams) {
78 "{}: add-decoder {}:{}:{}:{}:{}:{}:{}",
80 eventProtocolParams.getGroupId(),
81 eventProtocolParams.getArtifactId(),
82 eventProtocolParams.getTopic(),
83 eventProtocolParams.getEventClass(),
84 eventProtocolParams.getProtocolFilter(),
85 eventProtocolParams.getCustomGsonCoder(),
86 eventProtocolParams.getModelClassLoaderHash());
87 this.encoders.add(eventProtocolParams);
94 public void removeDecoders(String groupId, String artifactId, String topic) {
95 logger.info("{}: remove-decoder {}:{}:{}", this, groupId, artifactId, topic);
96 this.decoders.remove(groupId, artifactId, topic);
103 public void removeEncoders(String groupId, String artifactId, String topic) {
104 logger.info("{}: remove-encoder {}:{}:{}", this, groupId, artifactId, topic);
105 this.encoders.remove(groupId, artifactId, topic);
112 public boolean isDecodingSupported(String groupId, String artifactId, String topic) {
113 return this.decoders.isCodingSupported(groupId, artifactId, topic);
120 public boolean isEncodingSupported(String groupId, String artifactId, String topic) {
121 return this.encoders.isCodingSupported(groupId, artifactId, topic);
128 public Object decode(String groupId, String artifactId, String topic, String json) {
129 logger.debug("{}: decode {}:{}:{}:{}", this, groupId, artifactId, topic, json); // NOSONAR
132 * It seems that sonar declares the previous logging line as a security vulnerability
133 * when logging the topic variable. The static code analysis indicates that
134 * the path starts in org.onap.policy.drools.server.restful.RestManager::decode(),
135 * but the request is rejected if the topic contains invalid characters (the sonar description
136 * mentions "/r/n/t" characters) which are validated against in the checkValidNameInput(topic).
137 * Furthermore production instances are assumed not to have debug enabled, nor the REST telemetry API
138 * should be published externally. An additional note is that Path URLs containing spaces and newlines
139 * will be failed earlier at the HTTP protocol libraries (jetty, etc ..) so an URL of the form
140 * "https://../to\npic" won't even make it here.
142 return this.decoders.decode(groupId, artifactId, topic, json);
149 public String encode(String groupId, String artifactId, String topic, Object event) {
150 logger.debug("{}: encode {}:{}:{}:{}", this, groupId, artifactId, topic, event);
151 return this.encoders.encode(groupId, artifactId, topic, event);
158 public String encode(String topic, Object event) {
159 logger.debug("{}: encode {}:{}", this, topic, event); // NOSONAR
162 * See explanation for decode(String groupId, String artifactId, String topic, String json).
163 * The same applies here as it is called from
164 * org.onap.policy.drools.server.restful.RestManager::encode(),
166 return this.encoders.encode(topic, event);
173 public String encode(String topic, Object event, DroolsController droolsController) {
174 logger.debug("{}: encode {}:{}:{}", this, topic, event, droolsController);
175 return this.encoders.encode(topic, event, droolsController);
182 public List<CoderFilters> getDecoderFilters(String groupId, String artifactId, String topic) {
183 return this.decoders.getFilters(groupId, artifactId, topic);
190 public CoderFilters getDecoderFilters(
191 String groupId, String artifactId, String topic, String classname) {
192 return this.decoders.getFilters(groupId, artifactId, topic, classname);
199 public List<CoderFilters> getDecoderFilters(String groupId, String artifactId) {
200 return this.decoders.getFilters(groupId, artifactId);
207 public ProtocolCoderToolset getDecoders(String groupId, String artifactId, String topic) {
208 ProtocolCoderToolset decoderToolsets =
209 this.decoders.getCoders(groupId, artifactId, topic);
210 if (decoderToolsets == null) {
211 throw new IllegalArgumentException(
212 "Decoders not found for " + groupId + ":" + artifactId + ":" + topic);
215 return decoderToolsets;
219 * get all deocders by maven coordinates and topic.
221 * @param groupId group id
222 * @param artifactId artifact id
223 * @return list of decoders
224 * @throws IllegalArgumentException if invalid input
227 public List<ProtocolCoderToolset> getDecoders(String groupId, String artifactId) {
229 List<ProtocolCoderToolset> decoderToolsets =
230 this.decoders.getCoders(groupId, artifactId);
231 if (decoderToolsets == null) {
232 throw new IllegalArgumentException("Decoders not found for " + groupId + ":" + artifactId);
235 return new ArrayList<>(decoderToolsets);
242 public List<CoderFilters> getEncoderFilters(String groupId, String artifactId, String topic) {
243 return this.encoders.getFilters(groupId, artifactId, topic);
250 public CoderFilters getEncoderFilters(
251 String groupId, String artifactId, String topic, String classname) {
252 return this.encoders.getFilters(groupId, artifactId, topic, classname);
259 public List<CoderFilters> getEncoderFilters(String groupId, String artifactId) {
260 return this.encoders.getFilters(groupId, artifactId);
267 public List<CoderFilters> getReverseEncoderFilters(String topic, String encodedClass) {
268 return this.encoders.getReverseFilters(topic, encodedClass);
275 public DroolsController getDroolsController(String topic, Object encodedClass) {
276 return this.encoders.getDroolsController(topic, encodedClass);
283 public List<DroolsController> getDroolsControllers(String topic, Object encodedClass) {
284 return this.encoders.getDroolsControllers(topic, encodedClass);