8e5c1a7da3e1b947f1de1429f11bddcc81fb6cb6
[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.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableList.Builder;
16 import com.google.common.collect.Maps;
17 import io.netty.util.internal.ConcurrentSet;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Set;
23 import java.util.concurrent.ConcurrentHashMap;
24 import javax.annotation.Nonnull;
25 import org.opendaylight.controller.netconf.api.Capability;
26 import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession;
27 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
28 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.Yang;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.SchemasBuilder;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Sessions;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.SessionsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.SchemaBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.SchemaKey;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.Session;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class NetconfMonitoringServiceImpl implements NetconfMonitoringService {
43
44     private static final Schema.Location NETCONF_LOCATION = new Schema.Location(Schema.Location.Enumeration.NETCONF);
45     private static final List<Schema.Location> NETCONF_LOCATIONS = ImmutableList.of(NETCONF_LOCATION);
46     private static final Logger LOG = LoggerFactory.getLogger(NetconfMonitoringServiceImpl.class);
47     private static final Function<NetconfManagementSession, Session> SESSION_FUNCTION = new Function<NetconfManagementSession, Session>() {
48         @Override
49         public Session apply(@Nonnull final NetconfManagementSession input) {
50             return input.toManagementSession();
51         }
52     };
53
54     private final Set<NetconfManagementSession> sessions = new ConcurrentSet<>();
55     private final NetconfOperationServiceFactory netconfOperationProvider;
56     private final Map<String, Capability> capabilities = new ConcurrentHashMap<>();
57     private static final Function<Capability, String> CAPABILITY_TO_URI = new Function<Capability, String>() {
58         @Override
59         public String apply(final Capability input) {
60             return input.getCapabilityUri();
61         }
62     };
63
64     // FIXME check threadsafety
65
66     public NetconfMonitoringServiceImpl(final NetconfOperationServiceFactory netconfOperationProvider) {
67         this.netconfOperationProvider = netconfOperationProvider;
68         netconfOperationProvider.registerCapabilityListener(this);
69     }
70
71     @Override
72     public void onSessionUp(final NetconfManagementSession session) {
73         LOG.debug("Session {} up", session);
74         Preconditions.checkState(!sessions.contains(session), "Session %s was already added", session);
75         sessions.add(session);
76     }
77
78     @Override
79     public void onSessionDown(final NetconfManagementSession session) {
80         LOG.debug("Session {} down", session);
81         Preconditions.checkState(sessions.contains(session), "Session %s not present", session);
82         sessions.remove(session);
83     }
84
85     @Override
86     public Sessions getSessions() {
87         return new SessionsBuilder().setSession(ImmutableList.copyOf(Collections2.transform(sessions, SESSION_FUNCTION))).build();
88     }
89
90     @Override
91     public Schemas getSchemas() {
92         try {
93             return transformSchemas(netconfOperationProvider.getCapabilities());
94         } catch (final RuntimeException e) {
95             throw e;
96         } catch (final Exception e) {
97             throw new IllegalStateException("Exception while closing", e);
98         }
99     }
100
101     @Override
102     public String getSchemaForCapability(final String moduleName, final Optional<String> revision) {
103
104         // FIXME not effective at all
105
106         Map<String, Map<String, String>> mappedModulesToRevisionToSchema = Maps.newHashMap();
107
108         final Collection<Capability> caps = capabilities.values();
109
110         for (Capability cap : caps) {
111             if (!cap.getModuleName().isPresent()
112                     || !cap.getRevision().isPresent()
113                     || !cap.getCapabilitySchema().isPresent()){
114                 continue;
115             }
116
117             final String currentModuleName = cap.getModuleName().get();
118             Map<String, String> revisionMap = mappedModulesToRevisionToSchema.get(currentModuleName);
119             if (revisionMap == null) {
120                 revisionMap = Maps.newHashMap();
121                 mappedModulesToRevisionToSchema.put(currentModuleName, revisionMap);
122             }
123
124             String currentRevision = cap.getRevision().get();
125             revisionMap.put(currentRevision, cap.getCapabilitySchema().get());
126         }
127
128         Map<String, String> revisionMapRequest = mappedModulesToRevisionToSchema.get(moduleName);
129         Preconditions.checkState(revisionMapRequest != null, "Capability for module %s not present, " + ""
130                 + "available modules : %s", moduleName, Collections2.transform(caps, CAPABILITY_TO_URI));
131
132         if (revision.isPresent()) {
133             String schema = revisionMapRequest.get(revision.get());
134
135             Preconditions.checkState(schema != null,
136                     "Capability for module %s:%s not present, available revisions for module: %s", moduleName,
137                     revision.get(), revisionMapRequest.keySet());
138
139             return schema;
140         } else {
141             Preconditions.checkState(revisionMapRequest.size() == 1,
142                     "Expected 1 capability for module %s, available revisions : %s", moduleName,
143                     revisionMapRequest.keySet());
144             return revisionMapRequest.values().iterator().next();
145         }
146     }
147
148     @Override
149     public Set<String> getCapabilities() {
150         return capabilities.keySet();
151     }
152
153     private static Schemas transformSchemas(final Set<Capability> caps) {
154         final List<Schema> schemas = new ArrayList<>(caps.size());
155         for (final Capability cap : caps) {
156             if (cap.getCapabilitySchema().isPresent()) {
157                 final SchemaBuilder builder = new SchemaBuilder();
158                 Preconditions.checkState(cap.getModuleNamespace().isPresent());
159                 builder.setNamespace(new Uri(cap.getModuleNamespace().get()));
160
161                 Preconditions.checkState(cap.getRevision().isPresent());
162                 final String version = cap.getRevision().get();
163                 builder.setVersion(version);
164
165                 Preconditions.checkState(cap.getModuleName().isPresent());
166                 final String identifier = cap.getModuleName().get();
167                 builder.setIdentifier(identifier);
168
169                 builder.setFormat(Yang.class);
170
171                 builder.setLocation(transformLocations(cap.getLocation()));
172
173                 builder.setKey(new SchemaKey(Yang.class, identifier, version));
174
175                 schemas.add(builder.build());
176             }
177         }
178
179         return new SchemasBuilder().setSchema(schemas).build();
180     }
181
182     private static List<Schema.Location> transformLocations(final Collection<String> locations) {
183         if (locations.isEmpty()) {
184             return NETCONF_LOCATIONS;
185         }
186
187         final Builder<Schema.Location> b = ImmutableList.builder();
188         b.add(NETCONF_LOCATION);
189
190         for (final String location : locations) {
191             b.add(new Schema.Location(new Uri(location)));
192         }
193
194         return b.build();
195     }
196
197     @Override
198     public void onCapabilitiesAdded(final Set<Capability> addedCaps) {
199         // FIXME what check for duplicates
200         this.capabilities.putAll(Maps.uniqueIndex(addedCaps, CAPABILITY_TO_URI));
201     }
202
203     @Override
204     public void onCapabilitiesRemoved(final Set<Capability> addedCaps) {
205         for (final Capability addedCap : addedCaps) {
206             capabilities.remove(addedCap.getCapabilityUri());
207         }
208     }
209 }

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