5870ee1e45f6ab2d7bafa90c80ef858b4c5b2028
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Simulator
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
21 package org.onap.netconfsimulator.websocket;
22
23
24 import java.util.Optional;
25 import javax.websocket.CloseReason;
26 import javax.websocket.Endpoint;
27 import javax.websocket.EndpointConfig;
28 import javax.websocket.RemoteEndpoint;
29 import javax.websocket.Session;
30
31 import org.onap.netconfsimulator.kafka.listener.KafkaListenerEntry;
32 import org.onap.netconfsimulator.kafka.listener.KafkaListenerHandler;
33 import org.onap.netconfsimulator.websocket.message.NetconfMessageListener;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.kafka.listener.AbstractMessageListenerContainer;
38 import org.springframework.kafka.listener.MessageListener;
39 import org.springframework.stereotype.Component;
40
41 //instance of this class is created every each websocket request
42 @Component
43 class NetconfEndpoint extends Endpoint {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(NetconfEndpoint.class);
46     private static final String TOPIC_NAME = "config";
47
48     private KafkaListenerHandler kafkaListenerHandler;
49
50     public Optional<KafkaListenerEntry> getEntry() {
51         return entry;
52     }
53
54     public void setEntry(Optional<KafkaListenerEntry> entry) {
55         this.entry = entry;
56     }
57
58     private Optional<KafkaListenerEntry> entry = Optional.empty();
59
60
61     @Autowired
62     NetconfEndpoint(KafkaListenerHandler listenerHandler) {
63         this.kafkaListenerHandler = listenerHandler;
64     }
65
66     @Override
67     public void onOpen(Session session, EndpointConfig endpointConfig) {
68         RemoteEndpoint.Basic basicRemote = session.getBasicRemote();
69
70         addKafkaListener(basicRemote);
71         entry.ifPresent(x -> LOGGER.info("Session with client: {} established", x.getClientId()));
72     }
73
74     @Override
75     public void onError(Session session, Throwable throwable) {
76         LOGGER.error("Unexpected error occurred", throwable);
77     }
78
79     @Override
80     public void onClose(Session session, CloseReason closeReason) {
81         entry.ifPresent(x -> x.getListenerContainer().stop());
82         entry.ifPresent(x -> LOGGER.info("Closing connection for client: {}", x.getClientId()));
83     }
84
85
86     private void addKafkaListener(RemoteEndpoint.Basic remoteEndpoint) {
87         MessageListener messageListener = new NetconfMessageListener(remoteEndpoint);
88
89         KafkaListenerEntry kafkaListener = kafkaListenerHandler.createKafkaListener(messageListener, TOPIC_NAME);
90
91         AbstractMessageListenerContainer listenerContainer = kafkaListener.getListenerContainer();
92         listenerContainer.start();
93         entry = Optional.of(kafkaListener);
94     }
95 }