Fix a race PingPongTransactionChain
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / osgi / NetconfMonitoringServiceImpl.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 package org.opendaylight.controller.netconf.impl.osgi;
9
10 import com.google.common.base.Function;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Collections2;
13 import com.google.common.collect.Lists;
14 import com.google.common.collect.Sets;
15 import io.netty.util.internal.ConcurrentSet;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.Set;
19 import javax.annotation.Nonnull;
20 import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
21 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
22 import org.opendaylight.controller.netconf.mapping.api.Capability;
23 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
24 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
25 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.Yang;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.SchemasBuilder;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Sessions;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.SessionsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.SchemaBuilder;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.SchemaKey;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.Session;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class NetconfMonitoringServiceImpl implements NetconfMonitoringService, SessionMonitoringService {
40
41     private static final Logger LOG = LoggerFactory.getLogger(NetconfMonitoringServiceImpl.class);
42
43     private final Set<NetconfManagementSession> sessions = new ConcurrentSet<>();
44     private final NetconfOperationProvider netconfOperationProvider;
45
46     public NetconfMonitoringServiceImpl(NetconfOperationProvider netconfOperationProvider) {
47         this.netconfOperationProvider = netconfOperationProvider;
48     }
49
50     @Override
51     public void onSessionUp(NetconfManagementSession session) {
52         LOG.debug("Session {} up", session);
53         Preconditions.checkState(!sessions.contains(session), "Session %s was already added", session);
54         sessions.add(session);
55     }
56
57     @Override
58     public void onSessionDown(NetconfManagementSession session) {
59         LOG.debug("Session {} down", session);
60         Preconditions.checkState(sessions.contains(session), "Session %s not present", session);
61         sessions.remove(session);
62     }
63
64     @Override
65     public Sessions getSessions() {
66         return new SessionsBuilder().setSession(transformSessions(sessions)).build();
67     }
68
69     @Override
70     public Schemas getSchemas() {
71         // capabilities should be split from operations (it will allow to move getSchema operation to monitoring module)
72         try (NetconfOperationServiceSnapshot snapshot = netconfOperationProvider.openSnapshot("netconf-monitoring")) {
73             return transformSchemas(snapshot.getServices());
74         } catch (RuntimeException e) {
75             throw e;
76         } catch (Exception e) {
77             throw new IllegalStateException("Exception while closing", e);
78         }
79     }
80
81     private Schemas transformSchemas(Set<NetconfOperationService> services) {
82         Set<Capability> caps = Sets.newHashSet();
83
84         List<Schema> schemas = Lists.newArrayList();
85
86
87         for (NetconfOperationService netconfOperationService : services) {
88             // TODO check for duplicates ? move capability merging to snapshot
89             // Split capabilities from operations first and delete this duplicate code
90             caps.addAll(netconfOperationService.getCapabilities());
91         }
92
93         for (Capability cap : caps) {
94             SchemaBuilder builder = new SchemaBuilder();
95
96             if (cap.getCapabilitySchema().isPresent() == false) {
97                 continue;
98             }
99
100             Preconditions.checkState(cap.getModuleNamespace().isPresent());
101             builder.setNamespace(new Uri(cap.getModuleNamespace().get()));
102
103             Preconditions.checkState(cap.getRevision().isPresent());
104             String version = cap.getRevision().get();
105             builder.setVersion(version);
106
107             Preconditions.checkState(cap.getModuleName().isPresent());
108             String identifier = cap.getModuleName().get();
109             builder.setIdentifier(identifier);
110
111             builder.setFormat(Yang.class);
112
113             builder.setLocation(transformLocations(cap.getLocation().or(Collections.<String>emptyList())));
114
115             builder.setKey(new SchemaKey(Yang.class, identifier, version));
116
117             schemas.add(builder.build());
118         }
119
120         return new SchemasBuilder().setSchema(schemas).build();
121     }
122
123     private List<Schema.Location> transformLocations(List<String> locations) {
124         List<Schema.Location> monitoringLocations = Lists.newArrayList();
125         monitoringLocations.add(new Schema.Location(Schema.Location.Enumeration.NETCONF));
126
127         for (String location : locations) {
128             monitoringLocations.add(new Schema.Location(new Uri(location)));
129         }
130
131         return monitoringLocations;
132     }
133
134     private List<Session> transformSessions(Set<NetconfManagementSession> sessions) {
135         return Lists.newArrayList(Collections2.transform(sessions, new Function<NetconfManagementSession, Session>() {
136             @Override
137             public Session apply(@Nonnull NetconfManagementSession input) {
138                 return input.toManagementSession();
139             }
140         }));
141     }
142 }

©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.