2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.netconf.impl.osgi;
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.ImmutableList;
14 import com.google.common.collect.ImmutableList.Builder;
15 import io.netty.util.internal.ConcurrentSet;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.HashSet;
19 import java.util.List;
21 import javax.annotation.Nonnull;
22 import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
23 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
24 import org.opendaylight.controller.netconf.mapping.api.Capability;
25 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
26 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
27 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.Yang;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.SchemasBuilder;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Sessions;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.SessionsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.SchemaBuilder;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.SchemaKey;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.Session;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class NetconfMonitoringServiceImpl implements NetconfMonitoringService, SessionMonitoringService {
42 private static final Schema.Location NETCONF_LOCATION = new Schema.Location(Schema.Location.Enumeration.NETCONF);
43 private static final List<Schema.Location> NETCONF_LOCATIONS = ImmutableList.of(NETCONF_LOCATION);
44 private static final Logger LOG = LoggerFactory.getLogger(NetconfMonitoringServiceImpl.class);
45 private static final Function<NetconfManagementSession, Session> SESSION_FUNCTION = new Function<NetconfManagementSession, Session>() {
47 public Session apply(@Nonnull final NetconfManagementSession input) {
48 return input.toManagementSession();
52 private final Set<NetconfManagementSession> sessions = new ConcurrentSet<>();
53 private final NetconfOperationProvider netconfOperationProvider;
55 public NetconfMonitoringServiceImpl(final NetconfOperationProvider netconfOperationProvider) {
56 this.netconfOperationProvider = netconfOperationProvider;
60 public void onSessionUp(final NetconfManagementSession session) {
61 LOG.debug("Session {} up", session);
62 Preconditions.checkState(!sessions.contains(session), "Session %s was already added", session);
63 sessions.add(session);
67 public void onSessionDown(final NetconfManagementSession session) {
68 LOG.debug("Session {} down", session);
69 Preconditions.checkState(sessions.contains(session), "Session %s not present", session);
70 sessions.remove(session);
74 public Sessions getSessions() {
75 return new SessionsBuilder().setSession(ImmutableList.copyOf(Collections2.transform(sessions, SESSION_FUNCTION))).build();
79 public Schemas getSchemas() {
80 // capabilities should be split from operations (it will allow to move getSchema operation to monitoring module)
81 try (NetconfOperationServiceSnapshot snapshot = netconfOperationProvider.openSnapshot("netconf-monitoring")) {
82 return transformSchemas(snapshot.getServices());
83 } catch (RuntimeException e) {
85 } catch (Exception e) {
86 throw new IllegalStateException("Exception while closing", e);
90 private static Schemas transformSchemas(final Set<NetconfOperationService> services) {
91 // FIXME: Capability implementations do not have hashcode/equals!
92 final Set<Capability> caps = new HashSet<>();
93 for (NetconfOperationService netconfOperationService : services) {
94 // TODO check for duplicates ? move capability merging to snapshot
95 // Split capabilities from operations first and delete this duplicate code
96 caps.addAll(netconfOperationService.getCapabilities());
99 final List<Schema> schemas = new ArrayList<>(caps.size());
100 for (Capability cap : caps) {
101 if (cap.getCapabilitySchema().isPresent()) {
102 SchemaBuilder builder = new SchemaBuilder();
103 Preconditions.checkState(cap.getModuleNamespace().isPresent());
104 builder.setNamespace(new Uri(cap.getModuleNamespace().get()));
106 Preconditions.checkState(cap.getRevision().isPresent());
107 String version = cap.getRevision().get();
108 builder.setVersion(version);
110 Preconditions.checkState(cap.getModuleName().isPresent());
111 String identifier = cap.getModuleName().get();
112 builder.setIdentifier(identifier);
114 builder.setFormat(Yang.class);
116 builder.setLocation(transformLocations(cap.getLocation()));
118 builder.setKey(new SchemaKey(Yang.class, identifier, version));
120 schemas.add(builder.build());
124 return new SchemasBuilder().setSchema(schemas).build();
127 private static List<Schema.Location> transformLocations(final Collection<String> locations) {
128 if (locations.isEmpty()) {
129 return NETCONF_LOCATIONS;
132 final Builder<Schema.Location> b = ImmutableList.builder();
133 b.add(NETCONF_LOCATION);
135 for (String location : locations) {
136 b.add(new Schema.Location(new Uri(location)));