Merge "Resolve Bug:593. Persister should communicate via OSGi SR instead of TCP."
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / osgi / NetconfOperationServiceSnapshot.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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
9 package org.opendaylight.controller.netconf.impl.osgi;
10
11 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
12 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.Set;
19
20 public class NetconfOperationServiceSnapshot implements AutoCloseable {
21     private static final Logger logger = LoggerFactory.getLogger(NetconfOperationServiceSnapshot.class);
22
23     private final Set<NetconfOperationService> services;
24     private final String netconfSessionIdForReporting;
25
26     public NetconfOperationServiceSnapshot(Set<NetconfOperationServiceFactory> factories, long sessionId) {
27         Set<NetconfOperationService> services = new HashSet<>();
28         netconfSessionIdForReporting = getNetconfSessionIdForReporting(sessionId);
29         for (NetconfOperationServiceFactory factory : factories) {
30             services.add(factory.createService(netconfSessionIdForReporting));
31         }
32         this.services = Collections.unmodifiableSet(services);
33     }
34
35     private static String getNetconfSessionIdForReporting(long sessionId) {
36         return "netconf session id " + sessionId;
37     }
38
39     public String getNetconfSessionIdForReporting() {
40         return netconfSessionIdForReporting;
41     }
42
43     public Set<NetconfOperationService> getServices() {
44         return services;
45     }
46
47     @Override
48     public void close() {
49         RuntimeException firstException = null;
50         for (NetconfOperationService service : services) {
51             try {
52                 service.close();
53             } catch (RuntimeException e) {
54                 logger.warn("Got exception while closing {}", service, e);
55                 if (firstException == null) {
56                     firstException = e;
57                 } else {
58                     firstException.addSuppressed(e);
59                 }
60             }
61         }
62         if (firstException != null) {
63             throw firstException;
64         }
65     }
66
67     @Override
68     public String toString() {
69         return "NetconfOperationServiceSnapshot{" + netconfSessionIdForReporting + '}';
70     }
71 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.