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

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