3fea6821332fd0dc77591c2c5b05b8b163990274
[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 lombok.ToString;
26 import org.onap.policy.drools.controller.DroolsController;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Protocol Coder that does its best attempt to decode/encode, selecting the best class and best fitted json parsing
32  * tools.
33  */
34 @ToString
35 class MultiplexorEventProtocolCoder implements EventProtocolCoder {
36
37     /**
38      * Logger.
39      */
40     private static final Logger logger = LoggerFactory.getLogger(MultiplexorEventProtocolCoder.class);
41
42     /**
43      * Decoders.
44      */
45     protected EventProtocolDecoder decoders = new EventProtocolDecoder();
46
47     /**
48      * Encoders.
49      */
50     protected EventProtocolEncoder encoders = new EventProtocolEncoder();
51
52     /**
53      * {@inheritDoc}.
54      */
55     @Override
56     public void addDecoder(EventProtocolParams eventProtocolParams) {
57         logger.info(
58                 "{}: add-decoder {}:{}:{}:{}:{}:{}:{}",
59                 this,
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);
68     }
69
70     /**
71      * {@inheritDoc}.
72      *
73      * @param eventProtocolParams parameter object for event encoder
74      */
75     @Override
76     public void addEncoder(EventProtocolParams eventProtocolParams) {
77         logger.info(
78                 "{}: add-decoder {}:{}:{}:{}:{}:{}:{}",
79                 this,
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);
88     }
89
90     /**
91      * {@inheritDoc}.
92      */
93     @Override
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);
97     }
98
99     /**
100      * {@inheritDoc}.
101      */
102     @Override
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);
106     }
107
108     /**
109      * {@inheritDoc}.
110      */
111     @Override
112     public boolean isDecodingSupported(String groupId, String artifactId, String topic) {
113         return this.decoders.isCodingSupported(groupId, artifactId, topic);
114     }
115
116     /**
117      * {@inheritDoc}.
118      */
119     @Override
120     public boolean isEncodingSupported(String groupId, String artifactId, String topic) {
121         return this.encoders.isCodingSupported(groupId, artifactId, topic);
122     }
123
124     /**
125      * {@inheritDoc}.
126      */
127     @Override
128     public Object decode(String groupId, String artifactId, String topic, String json) {
129         logger.debug("{}: decode {}:{}:{}:{}", this, groupId, artifactId, topic, json);  // NOSONAR
130
131         /*
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.
141          */
142         return this.decoders.decode(groupId, artifactId, topic, json);
143     }
144
145     /**
146      * {@inheritDoc}.
147      */
148     @Override
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);
152     }
153
154     /**
155      * {@inheritDoc}.
156      */
157     @Override
158     public String encode(String topic, Object event) {
159         logger.debug("{}: encode {}:{}", this, topic, event);  // NOSONAR
160
161         /*
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(),
165          */
166         return this.encoders.encode(topic, event);
167     }
168
169     /**
170      * {@inheritDoc}.
171      */
172     @Override
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);
176     }
177
178     /**
179      * {@inheritDoc}.
180      */
181     @Override
182     public List<CoderFilters> getDecoderFilters(String groupId, String artifactId, String topic) {
183         return this.decoders.getFilters(groupId, artifactId, topic);
184     }
185
186     /**
187      * {@inheritDoc}.
188      */
189     @Override
190     public CoderFilters getDecoderFilters(
191             String groupId, String artifactId, String topic, String classname) {
192         return this.decoders.getFilters(groupId, artifactId, topic, classname);
193     }
194
195     /**
196      * {@inheritDoc}.
197      */
198     @Override
199     public List<CoderFilters> getDecoderFilters(String groupId, String artifactId) {
200         return this.decoders.getFilters(groupId, artifactId);
201     }
202
203     /**
204      * {@inheritDoc}.
205      */
206     @Override
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);
213         }
214
215         return decoderToolsets;
216     }
217
218     /**
219      * get all deocders by maven coordinates and topic.
220      *
221      * @param groupId    group id
222      * @param artifactId artifact id
223      * @return list of decoders
224      * @throws IllegalArgumentException if invalid input
225      */
226     @Override
227     public List<ProtocolCoderToolset> getDecoders(String groupId, String artifactId) {
228
229         List<ProtocolCoderToolset> decoderToolsets =
230                 this.decoders.getCoders(groupId, artifactId);
231         if (decoderToolsets == null) {
232             throw new IllegalArgumentException("Decoders not found for " + groupId + ":" + artifactId);
233         }
234
235         return new ArrayList<>(decoderToolsets);
236     }
237
238     /**
239      * {@inheritDoc}.
240      */
241     @Override
242     public List<CoderFilters> getEncoderFilters(String groupId, String artifactId, String topic) {
243         return this.encoders.getFilters(groupId, artifactId, topic);
244     }
245
246     /**
247      * {@inheritDoc}.
248      */
249     @Override
250     public CoderFilters getEncoderFilters(
251             String groupId, String artifactId, String topic, String classname) {
252         return this.encoders.getFilters(groupId, artifactId, topic, classname);
253     }
254
255     /**
256      * {@inheritDoc}.
257      */
258     @Override
259     public List<CoderFilters> getEncoderFilters(String groupId, String artifactId) {
260         return this.encoders.getFilters(groupId, artifactId);
261     }
262
263     /**
264      * {@inheritDoc}.
265      */
266     @Override
267     public List<CoderFilters> getReverseEncoderFilters(String topic, String encodedClass) {
268         return this.encoders.getReverseFilters(topic, encodedClass);
269     }
270
271     /**
272      * {@inheritDoc}.
273      */
274     @Override
275     public DroolsController getDroolsController(String topic, Object encodedClass) {
276         return this.encoders.getDroolsController(topic, encodedClass);
277     }
278
279     /**
280      * {@inheritDoc}.
281      */
282     @Override
283     public List<DroolsController> getDroolsControllers(String topic, Object encodedClass) {
284         return this.encoders.getDroolsControllers(topic, encodedClass);
285     }
286 }