Rework SslHandlerFactory
[netconf.git] / apps / callhome-provider / src / main / java / org / opendaylight / netconf / topology / callhome / AbstractCallHomeSessionContextManager.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech s.r.o. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netconf.topology.callhome;
9
10 import java.util.concurrent.ConcurrentHashMap;
11 import java.util.concurrent.ConcurrentMap;
12
13 public abstract class AbstractCallHomeSessionContextManager<T extends CallHomeSessionContext>
14         implements CallHomeSessionContextManager<T> {
15     protected final ConcurrentMap<String, T> contexts = new ConcurrentHashMap<>();
16
17     @Override
18     public void register(final T context) {
19         contexts.put(context.id(), context);
20     }
21
22     @Override
23     public boolean exists(final String id) {
24         return contexts.containsKey(id);
25     }
26
27     @Override
28     public void remove(final String id) {
29         final var context = contexts.remove(id);
30         if (context != null) {
31             context.close();
32             if (!context.settableFuture().isDone()) {
33                 context.settableFuture().cancel(true);
34             }
35         }
36     }
37
38     @Override
39     public void close() throws Exception {
40         for (var it = contexts.entrySet().iterator(); it.hasNext(); ) {
41             it.next().getValue().close();
42             it.remove();
43         }
44     }
45 }