2 * ============LICENSE_START=======================================================
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
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 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);
128 return this.decoders.decode(groupId, artifactId, topic, json);
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);
144 public String encode(String topic, Object event) {
145 logger.debug("{}: encode {}:{}", this, topic, event);
146 return this.encoders.encode(topic, event);
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);
162 public List<CoderFilters> getDecoderFilters(String groupId, String artifactId, String topic) {
163 return this.decoders.getFilters(groupId, artifactId, topic);
170 public CoderFilters getDecoderFilters(
171 String groupId, String artifactId, String topic, String classname) {
172 return this.decoders.getFilters(groupId, artifactId, topic, classname);
179 public List<CoderFilters> getDecoderFilters(String groupId, String artifactId) {
180 return this.decoders.getFilters(groupId, artifactId);
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);
195 return decoderToolsets;
199 * get all deocders by maven coordinates and topic.
201 * @param groupId group id
202 * @param artifactId artifact id
203 * @return list of decoders
204 * @throws IllegalArgumentException if invalid input
207 public List<ProtocolCoderToolset> getDecoders(String groupId, String artifactId) {
209 List<ProtocolCoderToolset> decoderToolsets =
210 this.decoders.getCoders(groupId, artifactId);
211 if (decoderToolsets == null) {
212 throw new IllegalArgumentException("Decoders not found for " + groupId + ":" + artifactId);
215 return new ArrayList<>(decoderToolsets);
222 public List<CoderFilters> getEncoderFilters(String groupId, String artifactId, String topic) {
223 return this.encoders.getFilters(groupId, artifactId, topic);
230 public CoderFilters getEncoderFilters(
231 String groupId, String artifactId, String topic, String classname) {
232 return this.encoders.getFilters(groupId, artifactId, topic, classname);
239 public List<CoderFilters> getEncoderFilters(String groupId, String artifactId) {
240 return this.encoders.getFilters(groupId, artifactId);
247 public List<CoderFilters> getReverseEncoderFilters(String topic, String encodedClass) {
248 return this.encoders.getReverseFilters(topic, encodedClass);
255 public DroolsController getDroolsController(String topic, Object encodedClass) {
256 return this.encoders.getDroolsController(topic, encodedClass);
263 public List<DroolsController> getDroolsControllers(String topic, Object encodedClass) {
264 return this.encoders.getDroolsControllers(topic, encodedClass);
271 public String toString() {
272 return "MultiplexorEventProtocolCoder [decoders="