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

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