97dc354495b92b38d9881bcaf261a0b73ae1ceb4
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2020 Bell Canada.
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  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.policy.models.simulators;
20
21 import java.nio.charset.StandardCharsets;
22 import java.util.Base64;
23 import lombok.Getter;
24 import lombok.Setter;
25 import lombok.ToString;
26 import org.onap.policy.common.parameters.GroupValidationResult;
27 import org.onap.policy.common.parameters.ParameterGroup;
28 import org.onap.policy.common.parameters.ParameterRuntimeException;
29 import org.onap.policy.common.parameters.annotations.Max;
30 import org.onap.policy.common.parameters.annotations.Min;
31 import org.onap.policy.common.parameters.annotations.NotNull;
32
33 @Getter
34 @Setter
35 @ToString
36 public class CdsServerParameters implements ParameterGroup {
37
38     // Port range constants
39     private static final int MIN_USER_PORT = 1024;
40     private static final int MAX_USER_PORT = 65535;
41
42     private static final String SERVER_PROPERTIES_TYPE = "CDS gRPC Server Properties";
43
44     // CDS carrier properties
45
46     // Request timeout in seconds
47     @Min(value = 1)
48     private int timeout;
49
50     @Min(value = MIN_USER_PORT)
51     @Max(value = MAX_USER_PORT)
52     private int port;
53
54     @Min(value = 0)
55     private int successRepeatCount;
56
57     private int requestedResponseDelayMs;
58
59     @NotNull
60     private String host;
61
62     @NotNull
63     private String username;
64
65     @NotNull
66     private String password;
67
68     @NotNull
69     private String resourceLocation;
70
71     @Override
72     public String getName() {
73         return SERVER_PROPERTIES_TYPE;
74     }
75
76     @Override
77     public void setName(final String name) {
78         throw new ParameterRuntimeException("The name of this ParameterGroup implementation is always " + getName());
79     }
80
81     @Override
82     public GroupValidationResult validate() {
83         return new GroupValidationResult(this);
84     }
85
86     /**
87      * Generate base64-encoded Authorization header from username and password.
88      *
89      * @return Base64 encoded string
90      */
91     public String getBasicAuth() {
92         String encodedAuth = Base64.getEncoder().encodeToString(
93                 String.format("%s:%s", getUsername(), getPassword()).getBytes(StandardCharsets.UTF_8));
94         // Return encoded basic auth header
95         return "Basic " + encodedAuth;
96     }
97 }