9839c9cf04fa60e023e0e29235618f248b0e655d
[netconf.git] / apps / callhome-provider / src / main / java / org / opendaylight / netconf / callhome / server / 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.callhome.server;
9
10 import java.util.Map;
11 import java.util.concurrent.ConcurrentHashMap;
12
13 public abstract class AbstractCallHomeSessionContextManager<T extends CallHomeSessionContext>
14         implements CallHomeSessionContextManager<T> {
15
16     protected final Map<String, T> contexts = new ConcurrentHashMap<>();
17
18     @Override
19     public void register(final T context) {
20         contexts.put(context.id(), context);
21     }
22
23     @Override
24     public boolean exists(final String id) {
25         return contexts.containsKey(id);
26     }
27
28     @Override
29     public void remove(final String id) {
30         final var context = contexts.remove(id);
31         if (context != null) {
32             context.close();
33             if (!context.settableFuture().isDone()) {
34                 context.settableFuture().cancel(true);
35             }
36         }
37     }
38
39     @Override
40     public void close() throws Exception {
41         for (var it = contexts.entrySet().iterator(); it.hasNext(); ) {
42             it.next().getValue().close();
43             it.remove();
44         }
45     }
46 }