f13248ec52e8d1cb2d6febcaeaca638342bec7a3
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.event.carrier.grpc;
22
23 import lombok.Getter;
24 import lombok.Setter;
25 import org.apache.commons.lang3.StringUtils;
26 import org.onap.policy.apex.service.engine.event.ApexEventException;
27 import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters;
28
29 // @formatter:off
30 /**
31  * Apex parameters for gRPC as an event carrier technology.
32  *
33  * <p>The parameters for this plugin are:
34  * <ol>
35  * <li>host: The host on which CDS is running. This parameter is mandatory
36  * <li>port: The port on the CDS host to connect to for CDS. This parameter is mandatory.
37  * <li>username: The username for basic authentication to connect to CDS. This parameter is mandatory.
38  * <li>password: The password for basic authentication to connect to CDS. This parameter is mandatory.
39  * <li>timeout: The timeout in seconds for CDS requests. This parameter is mandatory.
40  * </ol>
41  *
42  * @author Ajith Sreekumar(ajith.sreekumar@est.tech)
43  */
44 //@formatter:on
45 @Getter
46 @Setter
47 public class GrpcCarrierTechnologyParameters extends CarrierTechnologyParameters {
48     // @formatter:off
49     private static final int MIN_USER_PORT =  1024;
50     private static final int MAX_USER_PORT = 65535;
51
52     /** The label of this carrier technology. */
53     public static final String GRPC_CARRIER_TECHNOLOGY_LABEL = "GRPC";
54
55     /** The producer plugin class for the grpc carrier technology. */
56     public static final String GRPC_EVENT_PRODUCER_PLUGIN_CLASS = ApexGrpcProducer.class.getName();
57
58     /** The consumer plugin class for the gRPC carrier technology. */
59     public static final String GRPC_EVENT_CONSUMER_PLUGIN_CLASS = ApexGrpcConsumer.class.getName();
60
61     private int timeout;
62     private int port;
63     private String host;
64     private String username;
65     private String password;
66
67
68     /**
69      * Constructor to create a gRPC carrier technology parameters instance and register the instance with the
70      * parameter service.
71      */
72     public GrpcCarrierTechnologyParameters() {
73         super();
74         // Set the carrier technology properties for the gRPC carrier technology
75         this.setLabel(GRPC_CARRIER_TECHNOLOGY_LABEL);
76         this.setEventProducerPluginClass(GRPC_EVENT_PRODUCER_PLUGIN_CLASS);
77         this.setEventConsumerPluginClass(GRPC_EVENT_CONSUMER_PLUGIN_CLASS);
78     }
79
80     /**
81      * The method validates the gRPC parameters. Host details are specified as parameters only for a gRPC producer.
82      *
83      * @param isProducer if the parameters specified are for the gRPC producer or consumer
84      * @throws ApexEventException exception thrown when invalid parameters are provided
85      */
86     public void validateGrpcParameters(boolean isProducer) throws ApexEventException {
87         StringBuilder errorMessage = new StringBuilder();
88         if (isProducer) {
89             if (timeout < 1) {
90                 errorMessage.append("timeout should have a positive value.\n");
91             }
92             if (MIN_USER_PORT > port || MAX_USER_PORT < port) {
93                 errorMessage.append("port range should be between ").append(MIN_USER_PORT).append(" and ")
94                 .append(MAX_USER_PORT).append("\n");
95             }
96             if (StringUtils.isEmpty(host)) {
97                 errorMessage.append("host should be specified.\n");
98             }
99             if (StringUtils.isEmpty(username)) {
100                 errorMessage.append("username should be specified.\n");
101             }
102             if (StringUtils.isEmpty(password)) {
103                 errorMessage.append("password should be specified.\n");
104             }
105         }
106         if (errorMessage.length() > 0) {
107             errorMessage.insert(0, "Issues in specifying gRPC Producer parameters:\n");
108             throw new ApexEventException(errorMessage.toString());
109         }
110     }
111 }