2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.netconfsimulator.websocket;
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;
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;
41 //instance of this class is created every each websocket request
43 class NetconfEndpoint extends Endpoint {
45 private static final Logger LOGGER = LoggerFactory.getLogger(NetconfEndpoint.class);
46 private static final String TOPIC_NAME = "config";
48 private KafkaListenerHandler kafkaListenerHandler;
50 public Optional<KafkaListenerEntry> getEntry() {
54 public void setEntry(Optional<KafkaListenerEntry> entry) {
58 private Optional<KafkaListenerEntry> entry = Optional.empty();
62 NetconfEndpoint(KafkaListenerHandler listenerHandler) {
63 this.kafkaListenerHandler = listenerHandler;
67 public void onOpen(Session session, EndpointConfig endpointConfig) {
68 RemoteEndpoint.Basic basicRemote = session.getBasicRemote();
70 addKafkaListener(basicRemote);
71 entry.ifPresent(x -> LOGGER.info("Session with client: {} established", x.getClientId()));
75 public void onError(Session session, Throwable throwable) {
76 LOGGER.error("Unexpected error occurred", throwable);
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()));
86 private void addKafkaListener(RemoteEndpoint.Basic remoteEndpoint) {
87 MessageListener messageListener = new NetconfMessageListener(remoteEndpoint);
89 KafkaListenerEntry kafkaListener = kafkaListenerHandler.createKafkaListener(messageListener, TOPIC_NAME);
91 AbstractMessageListenerContainer listenerContainer = kafkaListener.getListenerContainer();
92 listenerContainer.start();
93 entry = Optional.of(kafkaListener);