Optimize Capability.getLocation()
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / osgi / NetconfMonitoringServiceImpl.java
index 3f44ff4ff834120d3edb655e55b5419b1569e7e0..efbe3ad68fe4615334c08206054cc755e20ff87b 100644 (file)
@@ -10,10 +10,12 @@ 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.Collections;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 import javax.annotation.Nonnull;
@@ -37,25 +39,32 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class NetconfMonitoringServiceImpl implements NetconfMonitoringService, SessionMonitoringService {
-
+    private static final Schema.Location NETCONF_LOCATION = new Schema.Location(Schema.Location.Enumeration.NETCONF);
+    private static final List<Schema.Location> NETCONF_LOCATIONS = ImmutableList.of(NETCONF_LOCATION);
     private static final Logger LOG = LoggerFactory.getLogger(NetconfMonitoringServiceImpl.class);
+    private static final Function<NetconfManagementSession, Session> SESSION_FUNCTION = new Function<NetconfManagementSession, Session>() {
+        @Override
+        public Session apply(@Nonnull final NetconfManagementSession input) {
+            return input.toManagementSession();
+        }
+    };
 
     private final Set<NetconfManagementSession> sessions = new ConcurrentSet<>();
     private final NetconfOperationProvider netconfOperationProvider;
 
-    public NetconfMonitoringServiceImpl(NetconfOperationProvider netconfOperationProvider) {
+    public NetconfMonitoringServiceImpl(final NetconfOperationProvider netconfOperationProvider) {
         this.netconfOperationProvider = netconfOperationProvider;
     }
 
     @Override
-    public void onSessionUp(NetconfManagementSession 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) {
+    public void onSessionDown(final NetconfManagementSession session) {
         LOG.debug("Session {} down", session);
         Preconditions.checkState(sessions.contains(session), "Session %s not present", session);
         sessions.remove(session);
@@ -63,7 +72,7 @@ public class NetconfMonitoringServiceImpl implements NetconfMonitoringService, S
 
     @Override
     public Sessions getSessions() {
-        return new SessionsBuilder().setSession(transformSessions(sessions)).build();
+        return new SessionsBuilder().setSession(ImmutableList.copyOf(Collections2.transform(sessions, SESSION_FUNCTION))).build();
     }
 
     @Override
@@ -78,65 +87,55 @@ public class NetconfMonitoringServiceImpl implements NetconfMonitoringService, S
         }
     }
 
-    private Schemas transformSchemas(Set<NetconfOperationService> services) {
-        Set<Capability> caps = Sets.newHashSet();
-
-        List<Schema> schemas = Lists.newArrayList();
-
-
+    private static Schemas transformSchemas(final Set<NetconfOperationService> services) {
+        // FIXME: Capability implementations do not have hashcode/equals!
+        final Set<Capability> 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<Schema> schemas = new ArrayList<>(caps.size());
         for (Capability cap : caps) {
-            SchemaBuilder builder = new SchemaBuilder();
-
-            if (cap.getCapabilitySchema().isPresent() == false) {
-                continue;
-            }
+            if (cap.getCapabilitySchema().isPresent()) {
+                SchemaBuilder builder = new SchemaBuilder();
+                Preconditions.checkState(cap.getModuleNamespace().isPresent());
+                builder.setNamespace(new Uri(cap.getModuleNamespace().get()));
 
-            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()));
 
-            builder.setLocation(transformLocations(cap.getLocation().or(Collections.<String>emptyList())));
+                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<Schema.Location> transformLocations(List<String> locations) {
-        List<Schema.Location> monitoringLocations = Lists.newArrayList();
-        monitoringLocations.add(new Schema.Location(Schema.Location.Enumeration.NETCONF));
+    private static List<Schema.Location> transformLocations(final Collection<String> locations) {
+        if (locations.isEmpty()) {
+            return NETCONF_LOCATIONS;
+        }
+
+        final Builder<Schema.Location> 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<Session> transformSessions(Set<NetconfManagementSession> sessions) {
-        return Lists.newArrayList(Collections2.transform(sessions, new Function<NetconfManagementSession, Session>() {
-            @Override
-            public Session apply(@Nonnull NetconfManagementSession input) {
-                return input.toManagementSession();
-            }
-        }));
+        return b.build();
     }
 }