X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fimpl%2Fosgi%2FNetconfMonitoringServiceImpl.java;h=efbe3ad68fe4615334c08206054cc755e20ff87b;hb=d92bf9525c23d35df3befc1048e9293fefda6618;hp=505c74714a71c269ad9721497cdbffaedd4fb706;hpb=4043d42c401e0ad6369c9ec35f2c926dcc18c80d;p=controller.git diff --git a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/osgi/NetconfMonitoringServiceImpl.java b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/osgi/NetconfMonitoringServiceImpl.java index 505c74714a..efbe3ad68f 100644 --- a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/osgi/NetconfMonitoringServiceImpl.java +++ b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/osgi/NetconfMonitoringServiceImpl.java @@ -10,13 +10,21 @@ package org.opendaylight.controller.netconf.impl.osgi; import com.google.common.base.Function; import com.google.common.base.Preconditions; import com.google.common.collect.Collections2; -import com.google.common.collect.Lists; -import com.google.common.collect.Sets; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableList.Builder; import io.netty.util.internal.ConcurrentSet; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import javax.annotation.Nonnull; import org.opendaylight.controller.netconf.api.monitoring.NetconfManagementSession; import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService; import org.opendaylight.controller.netconf.mapping.api.Capability; +import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider; import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService; +import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.Yang; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas; @@ -30,106 +38,104 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.mon import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.annotation.Nullable; -import java.util.Collections; -import java.util.List; -import java.util.Set; - public class NetconfMonitoringServiceImpl implements NetconfMonitoringService, SessionMonitoringService { - - private static final Logger logger = LoggerFactory.getLogger(NetconfMonitoringServiceImpl.class); + private static final Schema.Location NETCONF_LOCATION = new Schema.Location(Schema.Location.Enumeration.NETCONF); + private static final List NETCONF_LOCATIONS = ImmutableList.of(NETCONF_LOCATION); + private static final Logger LOG = LoggerFactory.getLogger(NetconfMonitoringServiceImpl.class); + private static final Function SESSION_FUNCTION = new Function() { + @Override + public Session apply(@Nonnull final NetconfManagementSession input) { + return input.toManagementSession(); + } + }; private final Set sessions = new ConcurrentSet<>(); - private final NetconfOperationServiceFactoryListener factoriesListener; + private final NetconfOperationProvider netconfOperationProvider; - public NetconfMonitoringServiceImpl(NetconfOperationServiceFactoryListener factoriesListener) { - this.factoriesListener = factoriesListener; + public NetconfMonitoringServiceImpl(final NetconfOperationProvider netconfOperationProvider) { + this.netconfOperationProvider = netconfOperationProvider; } @Override - public void onSessionUp(NetconfManagementSession session) { - logger.debug("Session {} up", session); - Preconditions.checkState(sessions.contains(session) == false, "Session %s was already added", session); + public void onSessionUp(final NetconfManagementSession session) { + LOG.debug("Session {} up", session); + Preconditions.checkState(!sessions.contains(session), "Session %s was already added", session); sessions.add(session); } @Override - public void onSessionDown(NetconfManagementSession session) { - logger.debug("Session {} down", session); - Preconditions.checkState(sessions.contains(session) == true, "Session %s not present", session); + public void onSessionDown(final NetconfManagementSession session) { + LOG.debug("Session {} down", session); + Preconditions.checkState(sessions.contains(session), "Session %s not present", session); sessions.remove(session); } @Override public Sessions getSessions() { - return new SessionsBuilder().setSession(transformSessions(sessions)).build(); + return new SessionsBuilder().setSession(ImmutableList.copyOf(Collections2.transform(sessions, SESSION_FUNCTION))).build(); } @Override public Schemas getSchemas() { - // FIXME, session ID // capabilities should be split from operations (it will allow to move getSchema operation to monitoring module) - return transformSchemas(factoriesListener.getSnapshot(0)); + try (NetconfOperationServiceSnapshot snapshot = netconfOperationProvider.openSnapshot("netconf-monitoring")) { + return transformSchemas(snapshot.getServices()); + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + throw new IllegalStateException("Exception while closing", e); + } } - private Schemas transformSchemas(NetconfOperationServiceSnapshot snapshot) { - Set caps = Sets.newHashSet(); - - List schemas = Lists.newArrayList(); - - for (NetconfOperationService netconfOperationService : snapshot.getServices()) { + private static Schemas transformSchemas(final Set services) { + // FIXME: Capability implementations do not have hashcode/equals! + final Set caps = new HashSet<>(); + for (NetconfOperationService netconfOperationService : services) { // TODO check for duplicates ? move capability merging to snapshot // Split capabilities from operations first and delete this duplicate code caps.addAll(netconfOperationService.getCapabilities()); } + final List schemas = new ArrayList<>(caps.size()); for (Capability cap : caps) { - SchemaBuilder builder = new SchemaBuilder(); - - if(cap.getCapabilitySchema().isPresent() == false) - continue; - - Preconditions.checkState(cap.getModuleNamespace().isPresent()); - builder.setNamespace(new Uri(cap.getModuleNamespace().get())); + if (cap.getCapabilitySchema().isPresent()) { + SchemaBuilder builder = new SchemaBuilder(); + Preconditions.checkState(cap.getModuleNamespace().isPresent()); + builder.setNamespace(new Uri(cap.getModuleNamespace().get())); - Preconditions.checkState(cap.getRevision().isPresent()); - String version = cap.getRevision().get(); - builder.setVersion(version); + Preconditions.checkState(cap.getRevision().isPresent()); + String version = cap.getRevision().get(); + builder.setVersion(version); - Preconditions.checkState(cap.getModuleName().isPresent()); - String identifier = cap.getModuleName().get(); - builder.setIdentifier(identifier); + Preconditions.checkState(cap.getModuleName().isPresent()); + String identifier = cap.getModuleName().get(); + builder.setIdentifier(identifier); - builder.setFormat(Yang.class); + builder.setFormat(Yang.class); - builder.setLocation(transformLocations(cap.getLocation().or(Collections. emptyList()))); + builder.setLocation(transformLocations(cap.getLocation())); - builder.setKey(new SchemaKey(Yang.class, identifier, version)); + builder.setKey(new SchemaKey(Yang.class, identifier, version)); - schemas.add(builder.build()); + schemas.add(builder.build()); + } } return new SchemasBuilder().setSchema(schemas).build(); } - private List transformLocations(List locations) { - List monitoringLocations = Lists.newArrayList(); - monitoringLocations.add(new Schema.Location(Schema.Location.Enumeration.NETCONF)); + private static List transformLocations(final Collection locations) { + if (locations.isEmpty()) { + return NETCONF_LOCATIONS; + } + + final Builder b = ImmutableList.builder(); + b.add(NETCONF_LOCATION); for (String location : locations) { - monitoringLocations.add(new Schema.Location(new Uri(location))); + b.add(new Schema.Location(new Uri(location))); } - return monitoringLocations; - } - - private List transformSessions(Set sessions) { - return Lists.newArrayList(Collections2.transform(sessions, new Function() { - @Nullable - @Override - public Session apply(@Nullable NetconfManagementSession input) { - return input.toManagementSession(); - } - })); + return b.build(); } }