45511d7f00ef06858cb91e62865727d8ba1c444b
[dcaegen2/services/sdk.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * DCAEGEN2-SERVICES-SDK
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. 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 package org.onap.dcaegen2.services.sdk.services.hvves.client.producer.ct;
21
22 import io.netty.buffer.ByteBuf;
23 import io.netty.handler.ssl.SslContext;
24 import io.vavr.collection.HashSet;
25 import io.vavr.control.Try;
26
27 import java.net.InetSocketAddress;
28 import java.nio.file.Path;
29 import java.nio.file.Paths;
30 import java.time.Duration;
31 import java.util.Optional;
32
33 import org.onap.dcaegen2.services.sdk.security.ssl.*;
34 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.HvVesProducer;
35 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.HvVesProducerFactory;
36 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.options.ImmutableProducerOptions;
37 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.options.ImmutableProducerOptions.Builder;
38 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.options.ImmutableWireFrameVersion;
39 import org.onap.dcaegen2.services.sdk.services.hvves.client.producer.api.options.WireFrameVersion;
40 import org.onap.ves.VesEventOuterClass.VesEvent;
41 import reactor.core.publisher.Flux;
42
43 /**
44  * @author <a href="mailto:piotr.jaszczyk@nokia.com">Piotr Jaszczyk</a>
45  */
46 public class SystemUnderTestWrapper {
47
48     private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(5);
49     private static final String TRUST_CERT_PATH = "/trust.p12";
50     private static final String TRUST_PASSWORD_PATH = "/trust.pass";
51     private static final String CLIENT_CERT_PATH = "/client.p12";
52     private static final String CLIENT_PASSWORD_PATH = "/client.pass";
53     private static final String SERVER_CERT_PATH = "/server.p12";
54     private static final String SERVER_PASSWORD_PATH = "/server.pass";
55
56     private DummyCollector collector;
57     private HvVesProducer cut;
58     private final Duration timeout;
59     private final SslFactory sslFactory = new SslFactory();
60
61     public SystemUnderTestWrapper(Duration timeout) {
62         this.timeout = timeout;
63     }
64
65     public SystemUnderTestWrapper() {
66         this(DEFAULT_TIMEOUT);
67     }
68
69     public void startSecure() {
70         collector = createCollectorWithEnabledSSL();
71
72         final SecurityKeys producerSecurityKeys = ImmutableSecurityKeys.builder()
73                 .keyStore(ImmutableSecurityKeysStore.of(resource(CLIENT_CERT_PATH).get()))
74                 .keyStorePassword(Passwords.fromResource(CLIENT_PASSWORD_PATH))
75                 .trustStore(ImmutableSecurityKeysStore.of(resource(TRUST_CERT_PATH).get()))
76                 .trustStorePassword(Passwords.fromResource(TRUST_PASSWORD_PATH))
77                 .build();
78         start(ImmutableProducerOptions.builder().securityKeys(producerSecurityKeys));
79     }
80
81     public void start() {
82         collector = new DummyCollector(Optional.empty());
83         start(createDefaultOptions());
84     }
85
86     public void start(ImmutableProducerOptions.Builder optionsBuilder) {
87         InetSocketAddress collectorAddress = collector.start();
88         WireFrameVersion WTPVersion = ImmutableWireFrameVersion.builder().build();
89         cut = HvVesProducerFactory.create(
90                 optionsBuilder.collectorAddresses(HashSet.of(collectorAddress))
91                         .wireFrameVersion(WTPVersion).build());
92     }
93
94     public void stop() {
95         collector.stop();
96     }
97
98     public ByteBuf blockingSend(Flux<VesEvent> events) {
99         events.transform(cut::send).subscribe();
100         collector.blockUntilFirstClientIsHandled(timeout);
101         return collector.dataFromFirstClient();
102     }
103
104     private DummyCollector createCollectorWithEnabledSSL() {
105         final SecurityKeys collectorSecurityKeys = ImmutableSecurityKeys.builder()
106                 .keyStore(ImmutableSecurityKeysStore.of(resource(SERVER_CERT_PATH).get()))
107                 .keyStorePassword(Passwords.fromResource(SERVER_PASSWORD_PATH))
108                 .trustStore(ImmutableSecurityKeysStore.of(resource(TRUST_CERT_PATH).get()))
109                 .trustStorePassword(Passwords.fromResource(TRUST_PASSWORD_PATH))
110                 .build();
111         final SslContext collectorSslContext = sslFactory.createSecureServerContext(collectorSecurityKeys);
112         return new DummyCollector(Optional.of(collectorSslContext));
113     }
114
115     private Builder createDefaultOptions() {
116         return ImmutableProducerOptions.builder();
117     }
118
119     private Try<Path> resource(String resource) {
120         return Try.of(() -> Paths.get(Passwords.class.getResource(resource).toURI()));
121     }
122
123 }