<forwardingrulesmanager.version>0.5.1-SNAPSHOT</forwardingrulesmanager.version>
<statisticsmanager.version>0.5.1-SNAPSHOT</statisticsmanager.version>
<clustering.services.version>0.5.1-SNAPSHOT</clustering.services.version>
- <configuration.version>0.4.2-SNAPSHOT</configuration.version>
+ <configuration.version>0.4.3-SNAPSHOT</configuration.version>
<topologymanager.version>0.4.2-SNAPSHOT</topologymanager.version>
<protocol_plugin.stub.version>0.4.2-SNAPSHOT</protocol_plugin.stub.version>
<clustering.stub.version>0.4.2-SNAPSHOT</clustering.stub.version>
package org.opendaylight.protocol.framework;
import io.netty.channel.Channel;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.concurrent.Promise;
* @param <S> Protocol session type, has to extend ProtocolSession<M>
*/
public abstract class AbstractSessionNegotiator<M, S extends AbstractProtocolSession<?>> extends ChannelInboundHandlerAdapter implements SessionNegotiator<S> {
- private final Logger logger = LoggerFactory.getLogger(AbstractSessionNegotiator.class);
+ private final Logger LOG = LoggerFactory.getLogger(AbstractSessionNegotiator.class);
private final Promise<S> promise;
protected final Channel channel;
protected abstract void handleMessage(M msg) throws Exception;
protected final void negotiationSuccessful(final S session) {
- logger.debug("Negotiation on channel {} successful with session {}", channel, session);
+ LOG.debug("Negotiation on channel {} successful with session {}", channel, session);
channel.pipeline().replace(this, "session", session);
promise.setSuccess(session);
}
protected final void negotiationFailed(final Throwable cause) {
- logger.debug("Negotiation on channel {} failed", channel, cause);
+ LOG.debug("Negotiation on channel {} failed", channel, cause);
channel.close();
promise.setFailure(cause);
}
+ /**
+ * Send a message to peer and fail negotiation if it does not reach
+ * the peer.
+ *
+ * @param msg Message which should be sent.
+ */
+ protected final void sendMessage(final M msg) {
+ this.channel.writeAndFlush(msg).addListener(
+ new ChannelFutureListener() {
+ @Override
+ public void operationComplete(final ChannelFuture f) {
+ if (!f.isSuccess()) {
+ LOG.info("Failed to send message {}", msg, f.cause());
+ negotiationFailed(f.cause());
+ } else {
+ LOG.trace("Message {} sent to socket", msg);
+ }
+ }
+ });
+ }
+
@Override
public final void channelActive(final ChannelHandlerContext ctx) {
- logger.debug("Starting session negotiation on channel {}", channel);
+ LOG.debug("Starting session negotiation on channel {}", channel);
try {
startNegotiation();
} catch (Exception e) {
- logger.warn("Unexpected negotiation failure", e);
+ LOG.warn("Unexpected negotiation failure", e);
negotiationFailed(e);
}
}
@Override
public final void channelRead(final ChannelHandlerContext ctx, final Object msg) {
- logger.debug("Negotiation read invoked on channel {}", channel);
+ LOG.debug("Negotiation read invoked on channel {}", channel);
try {
handleMessage((M)msg);
} catch (Exception e) {
- logger.debug("Unexpected error while handling negotiation message {}", msg, e);
+ LOG.debug("Unexpected error while handling negotiation message {}", msg, e);
negotiationFailed(e);
}
}
@Override
public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause) {
- logger.info("Unexpected error during negotiation", cause);
+ LOG.info("Unexpected error during negotiation", cause);
negotiationFailed(cause);
}
}
<groupId>org.opendaylight.yangtools</groupId>
<artifactId>mockito-configuration</artifactId>
</dependency>
+
+ <!--Dependencies regardign RuntimeGeneratedMappingService-->
+ <dependency>
+ <groupId>org.opendaylight.yangtools</groupId>
+ <artifactId>binding-generator-impl</artifactId>
+ </dependency>
+ <!--End-->
+
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>config-util</artifactId>
this.service = service;
CodecRegistry codecRegistry = service.getCodecRegistry();
logger.debug("Codec registry acquired {}", codecRegistry);
- activator.initConfigManager(ctx, codecRegistry);
+// activator.initConfigManager(ctx, codecRegistry);
return service;
}
package org.opendaylight.controller.config.manager.impl.osgi;
import java.lang.management.ManagementFactory;
+import java.util.Collection;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanServer;
import org.opendaylight.controller.config.manager.impl.ConfigRegistryImpl;
import org.opendaylight.controller.config.manager.impl.jmx.ConfigRegistryJMXRegistrator;
+import org.opendaylight.controller.config.manager.impl.osgi.mapping.ModuleInfoBundleTracker;
+import org.opendaylight.controller.config.manager.impl.osgi.mapping.RuntimeGeneratedMappingServiceActivator;
import org.opendaylight.controller.config.spi.ModuleFactory;
-import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
+import org.opendaylight.yangtools.concepts.Registration;
+import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.slf4j.LoggerFactory;
public class ConfigManagerActivator implements BundleActivator {
- private static final Logger logger = LoggerFactory
- .getLogger(ConfigManagerActivator.class);
+ private static final Logger logger = LoggerFactory.getLogger(ConfigManagerActivator.class);
- private ExtenderBundleTracker extenderBundleTracker;
+ private ExtensibleBundleTracker<Collection<Registration<YangModuleInfo>>> bundleTracker;
private ConfigRegistryImpl configRegistry;
private ConfigRegistryJMXRegistrator configRegistryJMXRegistrator;
private ServiceRegistration configRegistryServiceRegistration;
- private ServiceTracker<BindingIndependentMappingService, BindingIndependentMappingService> tracker;
+ private final MBeanServer configMBeanServer = ManagementFactory.getPlatformMBeanServer();
+
+ private RuntimeGeneratedMappingServiceActivator mappingServiceActivator;
@Override
public void start(BundleContext context) throws Exception {
- BindingIndependentMappingServiceTracker mappingServiceTracker = new BindingIndependentMappingServiceTracker(
- context, this);
- tracker = new ServiceTracker<BindingIndependentMappingService, BindingIndependentMappingService>(
- context, BindingIndependentMappingService.class, mappingServiceTracker);
-
- logger.debug("Waiting for codec registry");
-
- tracker.open();
- }
- void initConfigManager(BundleContext context, CodecRegistry codecRegistry) {
- BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver =
- new BundleContextBackedModuleFactoriesResolver(context);
- MBeanServer configMBeanServer = ManagementFactory.getPlatformMBeanServer();
+ // track bundles containing YangModuleInfo
+ ModuleInfoBundleTracker moduleInfoBundleTracker = new ModuleInfoBundleTracker();
+ mappingServiceActivator = new RuntimeGeneratedMappingServiceActivator(moduleInfoBundleTracker);
+ CodecRegistry codecRegistry = mappingServiceActivator.startRuntimeMappingService(context).getCodecRegistry();
+ // start config registry
+ BundleContextBackedModuleFactoriesResolver bundleContextBackedModuleFactoriesResolver = new BundleContextBackedModuleFactoriesResolver(
+ context);
+ configRegistry = new ConfigRegistryImpl(bundleContextBackedModuleFactoriesResolver, configMBeanServer,
+ codecRegistry);
- // TODO push codecRegistry/IdentityCodec to dependencyResolver
+ // track bundles containing factories
+ BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker(
+ configRegistry);
+ ModuleFactoryBundleTracker moduleFactoryBundleTracker = new ModuleFactoryBundleTracker(
+ blankTransactionServiceTracker);
- configRegistry = new ConfigRegistryImpl(
- bundleContextBackedModuleFactoriesResolver, configMBeanServer, codecRegistry);
+ // start extensible tracker
+ bundleTracker = new ExtensibleBundleTracker<>(context, moduleInfoBundleTracker, moduleFactoryBundleTracker);
+ bundleTracker.open();
// register config registry to OSGi
configRegistryServiceRegistration = context.registerService(ConfigRegistryImpl.class, configRegistry, null);
throw new RuntimeException("Config Registry was already registered to JMX", e);
}
- // track bundles containing factories
- BlankTransactionServiceTracker blankTransactionServiceTracker = new BlankTransactionServiceTracker(configRegistry);
- extenderBundleTracker = new ExtenderBundleTracker(context, blankTransactionServiceTracker);
- extenderBundleTracker.open();
-
- ServiceTracker<?, ?> serviceTracker = new ServiceTracker(context, ModuleFactory.class, blankTransactionServiceTracker);
+ ServiceTracker<ModuleFactory, Object> serviceTracker = new ServiceTracker<>(context, ModuleFactory.class,
+ blankTransactionServiceTracker);
serviceTracker.open();
}
@Override
public void stop(BundleContext context) throws Exception {
- try {
- tracker.close();
- } catch (Exception e) {
- logger.warn("Exception while closing tracker", e);
- }
try {
configRegistry.close();
} catch (Exception e) {
logger.warn("Exception while closing config registry", e);
}
try {
- extenderBundleTracker.close();
+ bundleTracker.close();
} catch (Exception e) {
logger.warn("Exception while closing extender", e);
}
} catch (Exception e) {
logger.warn("Exception while unregistering config registry", e);
}
+ try {
+ mappingServiceActivator.close();
+ } catch (Exception e) {
+ logger.warn("Exception while closing mapping service", e);
+ }
}
}
--- /dev/null
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.config.manager.impl.osgi;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.util.tracker.BundleTracker;
+import org.osgi.util.tracker.BundleTrackerCustomizer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ *
+ * Extensible bundle tracker. Takes several BundleTrackerCustomizers and propagates bundle events to all of them.
+ * Primary customizer
+ *
+ * @param <T>
+ */
+public final class ExtensibleBundleTracker<T> extends BundleTracker<T> {
+
+ private final BundleTrackerCustomizer<T> primaryTracker;
+ private final BundleTrackerCustomizer<?>[] additionalTrackers;
+
+ private static final Logger logger = LoggerFactory.getLogger(ExtensibleBundleTracker.class);
+
+ public ExtensibleBundleTracker(BundleContext context, BundleTrackerCustomizer<T> primaryBundleTrackerCustomizer,
+ BundleTrackerCustomizer<?>... additionalBundleTrackerCustomizers) {
+ this(context, Bundle.ACTIVE, primaryBundleTrackerCustomizer, additionalBundleTrackerCustomizers);
+ }
+
+ public ExtensibleBundleTracker(BundleContext context, int bundleState,
+ BundleTrackerCustomizer<T> primaryBundleTrackerCustomizer,
+ BundleTrackerCustomizer<?>... additionalBundleTrackerCustomizers) {
+ super(context, bundleState, null);
+ this.primaryTracker = primaryBundleTrackerCustomizer;
+ this.additionalTrackers = additionalBundleTrackerCustomizers;
+ logger.trace("Registered as extender with context {} and bundle state {}", context, bundleState);
+ }
+
+ @Override
+ public T addingBundle(final Bundle bundle, final BundleEvent event) {
+ T primaryTrackerRetVal = primaryTracker.addingBundle(bundle, event);
+
+ forEachAdditionalBundle(new BundleStrategy() {
+ @Override
+ public void execute(BundleTrackerCustomizer<?> tracker) {
+ tracker.addingBundle(bundle, event);
+ }
+ });
+
+ return primaryTrackerRetVal;
+ }
+
+ @Override
+ public void modifiedBundle(final Bundle bundle, final BundleEvent event, final T object) {
+ primaryTracker.modifiedBundle(bundle, event, object);
+
+ forEachAdditionalBundle(new BundleStrategy() {
+ @Override
+ public void execute(BundleTrackerCustomizer<?> tracker) {
+ tracker.modifiedBundle(bundle, event, null);
+ }
+ });
+
+ }
+
+ @Override
+ public void removedBundle(final Bundle bundle, final BundleEvent event, final T object) {
+ primaryTracker.removedBundle(bundle, event, object);
+
+ forEachAdditionalBundle(new BundleStrategy() {
+ @Override
+ public void execute(BundleTrackerCustomizer<?> tracker) {
+ tracker.removedBundle(bundle, event, null);
+ }
+ });
+ }
+
+ private void forEachAdditionalBundle(BundleStrategy lambda) {
+ for (BundleTrackerCustomizer<?> trac : additionalTrackers) {
+ lambda.execute(trac);
+ }
+ }
+
+ private static interface BundleStrategy {
+ void execute(BundleTrackerCustomizer<?> tracker);
+ }
+
+}
import org.apache.commons.io.IOUtils;
import org.opendaylight.controller.config.spi.ModuleFactory;
import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
import org.osgi.framework.ServiceRegistration;
-import org.osgi.util.tracker.BundleTracker;
+import org.osgi.util.tracker.BundleTrackerCustomizer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
* the services are unregistered automatically.
* Code based on http://www.toedter.com/blog/?p=236
*/
-public class ExtenderBundleTracker extends BundleTracker<Object> {
+public class ModuleFactoryBundleTracker implements BundleTrackerCustomizer<Object> {
private final BlankTransactionServiceTracker blankTransactionServiceTracker;
- private static final Logger logger = LoggerFactory.getLogger(ExtenderBundleTracker.class);
+ private static final Logger logger = LoggerFactory.getLogger(ModuleFactoryBundleTracker.class);
- public ExtenderBundleTracker(BundleContext context, BlankTransactionServiceTracker blankTransactionServiceTracker) {
- super(context, Bundle.ACTIVE, null);
+ public ModuleFactoryBundleTracker(BlankTransactionServiceTracker blankTransactionServiceTracker) {
this.blankTransactionServiceTracker = blankTransactionServiceTracker;
- logger.trace("Registered as extender with context {}", context);
}
@Override
return bundle;
}
+ @Override
+ public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
+ // NOOP
+ }
+
@Override
public void removedBundle(Bundle bundle, BundleEvent event, Object object) {
- super.removedBundle(bundle,event,object);
// workaround for service tracker not getting removed service event
blankTransactionServiceTracker.blankTransaction();
}
--- /dev/null
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.config.manager.impl.osgi.mapping;
+
+import org.apache.commons.io.IOUtils;
+import org.opendaylight.yangtools.concepts.Registration;
+import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
+import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
+import org.opendaylight.yangtools.yang.binding.YangModelBindingProvider;
+import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleEvent;
+import org.osgi.util.tracker.BundleTrackerCustomizer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
+import java.net.URL;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+
+import static java.lang.String.format;
+
+/**
+ * Tracks bundles and attempts to retrieve YangModuleInfo.
+ */
+public final class ModuleInfoBundleTracker implements BundleTrackerCustomizer<Collection<Registration<YangModuleInfo>>> {
+
+ private static final Logger logger = LoggerFactory.getLogger(ModuleInfoBundleTracker.class);
+ public static final String GET_MODULE_INFO_METHOD = "getModuleInfo";
+
+ public static final String MODULE_INFO_PROVIDER_PATH_PREFIX = "META-INF/services/";
+
+ private ModuleInfoBackedContext moduleInfoLoadingStrategy = ModuleInfoBackedContext.create();
+
+ public GeneratedClassLoadingStrategy getModuleInfoLoadingStrategy() {
+ return moduleInfoLoadingStrategy;
+ }
+
+ @Override
+ public Collection<Registration<YangModuleInfo>> addingBundle(Bundle bundle, BundleEvent event) {
+ URL resource = bundle.getEntry(MODULE_INFO_PROVIDER_PATH_PREFIX + YangModelBindingProvider.class.getName());
+
+ if(resource==null) {
+ return null;
+ }
+
+ List<Registration<YangModuleInfo>> registrations = new LinkedList<>();
+
+ try (InputStream inputStream = resource.openStream()) {
+ List<String> lines = IOUtils.readLines(inputStream);
+ for (String moduleInfoName : lines) {
+ YangModuleInfo moduleInfo = retrieveModuleInfo(moduleInfoName, bundle);
+ registrations.add(moduleInfoLoadingStrategy.registerModuleInfo(moduleInfo));
+ }
+
+
+ } catch (Exception e) {
+ logger.error("Error while reading {}", resource, e);
+ throw new RuntimeException(e);
+ }
+
+ return registrations;
+ }
+
+ @Override
+ public void modifiedBundle(Bundle bundle, BundleEvent event, Collection<Registration<YangModuleInfo>> object) {
+ // NOOP
+ }
+
+ @Override
+ public void removedBundle(Bundle bundle, BundleEvent event, Collection<Registration<YangModuleInfo>> regs) {
+ if(regs == null) {
+ return;
+ }
+
+ for (Registration<YangModuleInfo> reg : regs) {
+ try {
+ reg.close();
+ } catch (Exception e) {
+ throw new RuntimeException("Unable to unregister YangModuleInfo " + reg.getInstance(), e);
+ }
+ }
+ }
+
+ private static YangModuleInfo retrieveModuleInfo(String moduleInfoClass, Bundle bundle) {
+ String errorMessage;
+ Class<?> clazz = loadClass(moduleInfoClass, bundle);
+
+ if (YangModelBindingProvider.class.isAssignableFrom(clazz) == false) {
+ errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz, YangModelBindingProvider.class, bundle);
+ throw new IllegalStateException(errorMessage);
+ }
+
+ try {
+ Object instance = clazz.newInstance();
+ Object result = clazz.getMethod(GET_MODULE_INFO_METHOD).invoke(instance);
+
+ if (YangModuleInfo.class.isAssignableFrom(result.getClass()) == false) {
+ errorMessage = logMessage("Error invoking method not found {} in bundle {}, reason {}",
+ GET_MODULE_INFO_METHOD, bundle, "Not assignable from " + YangModuleInfo.class);
+ } else {
+ return (YangModuleInfo) result;
+ }
+
+ } catch (InstantiationException e) {
+ errorMessage = logMessage("Could not instantiate {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
+ } catch (IllegalAccessException e) {
+ errorMessage = logMessage("Illegal access during instatiation of class {} in bundle {}, reason {}",
+ moduleInfoClass, bundle, e);
+ } catch (NoSuchMethodException e) {
+ errorMessage = logMessage("Method not found {} in bundle {}, reason {}", GET_MODULE_INFO_METHOD, bundle, e);
+ } catch (InvocationTargetException e) {
+ errorMessage = logMessage("Error invoking method {} in bundle {}, reason {}", GET_MODULE_INFO_METHOD,
+ bundle, e);
+ }
+
+ throw new IllegalStateException(errorMessage);
+ }
+
+ private static Class<?> loadClass(String moduleInfoClass, Bundle bundle) {
+ try {
+ return bundle.loadClass(moduleInfoClass);
+ } catch (ClassNotFoundException e) {
+ String errorMessage = logMessage("Could not find class {} in bunde {}, reason {}", moduleInfoClass, bundle, e);
+ throw new IllegalStateException(errorMessage);
+ }
+ }
+
+ public static String logMessage(String slfMessage, Object... params) {
+ logger.info(slfMessage, params);
+ String formatMessage = slfMessage.replaceAll("\\{\\}", "%s");
+ return format(formatMessage, params);
+ }
+}
/*
- * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-package org.opendaylight.controller.sal.binding.osgi;
+package org.opendaylight.controller.config.manager.impl.osgi.mapping;
-import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder;
+import javassist.ClassPool;
import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
import org.opendaylight.yangtools.yang.model.api.SchemaServiceListener;
-import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import java.util.Hashtable;
-public class Activator implements BundleActivator {
+public class RuntimeGeneratedMappingServiceActivator implements AutoCloseable {
+
+ private static final ClassPool CLASS_POOL = ClassPool.getDefault();
- private ServiceRegistration<?> reg;
private ServiceRegistration<SchemaServiceListener> listenerReg;
private ServiceRegistration<BindingIndependentMappingService> mappingReg;
+ private ModuleInfoBundleTracker moduleInfoBundleTracker;
- @Override
- public void start(BundleContext context) throws Exception {
- RuntimeGeneratedMappingServiceImpl service = new RuntimeGeneratedMappingServiceImpl();
- service.setPool(SingletonHolder.CLASS_POOL);
+ public RuntimeGeneratedMappingServiceActivator(ModuleInfoBundleTracker moduleInfoBundleTracker) {
+ this.moduleInfoBundleTracker = moduleInfoBundleTracker;
+ }
+
+ public RuntimeGeneratedMappingServiceImpl startRuntimeMappingService(BundleContext context) {
+ RuntimeGeneratedMappingServiceImpl service = new RuntimeGeneratedMappingServiceImpl(moduleInfoBundleTracker.getModuleInfoLoadingStrategy());
+ service.setPool(CLASS_POOL);
service.init();
startRuntimeMappingService(service, context);
+ return service;
}
private void startRuntimeMappingService(RuntimeGeneratedMappingServiceImpl service, BundleContext context) {
Hashtable<String, String> properties = new Hashtable<String, String>();
listenerReg = context.registerService(SchemaServiceListener.class, service, properties);
mappingReg = context.registerService(BindingIndependentMappingService.class, service, properties);
-
+
}
@Override
- public void stop(BundleContext context) throws Exception {
- if(listenerReg != null) {
- listenerReg.unregister();
- }
- if(mappingReg != null) {
- mappingReg.unregister();
- }
+ public void close() throws Exception {
+ mappingReg.unregister();
+ listenerReg.unregister();
}
}
*/
package org.opendaylight.controller.config.yang.netty.eventexecutor;
-import io.netty.util.concurrent.AbstractEventExecutor;
+import com.google.common.reflect.AbstractInvocationHandler;
+import com.google.common.reflect.Reflection;
import io.netty.util.concurrent.EventExecutor;
-import io.netty.util.concurrent.EventExecutorGroup;
-import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GlobalEventExecutor;
-import io.netty.util.concurrent.ScheduledFuture;
-import java.util.concurrent.Callable;
+import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
-/**
-*
-*/
public final class GlobalEventExecutorModule extends
org.opendaylight.controller.config.yang.netty.eventexecutor.AbstractGlobalEventExecutorModule {
@Override
public java.lang.AutoCloseable createInstance() {
- return new GlobalEventExecutorCloseable(GlobalEventExecutor.INSTANCE);
+ final CloseableGlobalEventExecutorMixin closeableGlobalEventExecutorMixin =
+ new CloseableGlobalEventExecutorMixin(GlobalEventExecutor.INSTANCE);
+ return Reflection.newProxy(AutoCloseableEventExecutor.class, new AbstractInvocationHandler() {
+ @Override
+ protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
+ if (method.getName().equals("close")) {
+ closeableGlobalEventExecutorMixin.close();
+ return null;
+ } else {
+ return method.invoke(GlobalEventExecutor.INSTANCE, args);
+ }
+ }
+ });
}
- static final private class GlobalEventExecutorCloseable extends AbstractEventExecutor implements AutoCloseable {
-
- private EventExecutor executor;
-
- public GlobalEventExecutorCloseable(EventExecutor executor) {
- this.executor = executor;
- }
-
- @Override
- public EventExecutorGroup parent() {
- return this.executor.parent();
- }
-
- @Override
- public boolean inEventLoop(Thread thread) {
- return this.executor.inEventLoop(thread);
- }
-
- @Override
- public boolean isShuttingDown() {
- return this.executor.isShuttingDown();
- }
-
- @Override
- public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) {
- return this.executor.shutdownGracefully(quietPeriod, timeout, unit);
- }
-
- @Override
- public Future<?> terminationFuture() {
- return this.executor.terminationFuture();
- }
-
- @Override
- public boolean isShutdown() {
- return this.executor.isShutdown();
- }
+ public static interface AutoCloseableEventExecutor extends EventExecutor, AutoCloseable {
- @Override
- public boolean isTerminated() {
- return this.executor.isTerminated();
- }
-
- @Override
- public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
- return this.executor.awaitTermination(timeout, unit);
- }
-
- @Override
- public void execute(Runnable command) {
- this.executor.execute(command);
- }
-
- @Override
- public void close() throws Exception {
- shutdownGracefully();
- }
-
- @SuppressWarnings("deprecation")
- @Override
- public void shutdown() {
- this.executor.shutdown();
- }
-
- @Override
- public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) {
- return this.executor.scheduleWithFixedDelay(command, initialDelay, delay, unit);
- }
+ }
- @Override
- public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
- return this.executor.schedule(command, delay, unit);
- }
+ public static class CloseableGlobalEventExecutorMixin implements AutoCloseable {
+ private final GlobalEventExecutor eventExecutor;
- @Override
- public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
- return this.executor.schedule(callable, delay, unit);
+ public CloseableGlobalEventExecutorMixin(GlobalEventExecutor eventExecutor) {
+ this.eventExecutor = eventExecutor;
}
@Override
- public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) {
- return this.executor.scheduleAtFixedRate(command, initialDelay, period, unit);
+ public void close() {
+ eventExecutor.shutdownGracefully(0, 1, TimeUnit.SECONDS);
}
}
}
*/
/**
-* Generated file
-
-* Generated from: yang module name: netty-event-executor yang module local name: netty-global-event-executor
-* Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator
-* Generated at: Tue Nov 12 10:44:21 CET 2013
-*
-* Do not modify this file unless it is present under src/main directory
-*/
+ * Generated file
+
+ * Generated from: yang module name: netty-event-executor yang module local name: netty-global-event-executor
+ * Generated by: org.opendaylight.controller.config.yangjmxgenerator.plugin.JMXGenerator
+ * Generated at: Tue Nov 12 10:44:21 CET 2013
+ *
+ * Do not modify this file unless it is present under src/main directory
+ */
package org.opendaylight.controller.config.yang.netty.eventexecutor;
-/**
-*
-*/
-public class GlobalEventExecutorModuleFactory extends org.opendaylight.controller.config.yang.netty.eventexecutor.AbstractGlobalEventExecutorModuleFactory
-{
+import org.opendaylight.controller.config.api.DependencyResolver;
+import org.osgi.framework.BundleContext;
+
+import static com.google.common.base.Preconditions.checkArgument;
+
+public class GlobalEventExecutorModuleFactory extends org.opendaylight.controller.config.yang.netty.eventexecutor.AbstractGlobalEventExecutorModuleFactory {
+ public static final String SINGLETON_NAME = "singleton";
+
+ @Override
+ public GlobalEventExecutorModule instantiateModule(String instanceName, DependencyResolver dependencyResolver, GlobalEventExecutorModule oldModule, AutoCloseable oldInstance, BundleContext bundleContext) {
+ checkArgument(SINGLETON_NAME.equals(instanceName),"Illegal instance name '" + instanceName + "', only allowed name is " + SINGLETON_NAME);
+ return super.instantiateModule(instanceName, dependencyResolver, oldModule, oldInstance, bundleContext);
+ }
+ @Override
+ public GlobalEventExecutorModule instantiateModule(String instanceName, DependencyResolver dependencyResolver, BundleContext bundleContext) {
+ checkArgument(SINGLETON_NAME.equals(instanceName),"Illegal instance name '" + instanceName + "', only allowed name is " + SINGLETON_NAME);
+ return super.instantiateModule(instanceName, dependencyResolver, bundleContext);
+ }
}
package org.opendaylight.controller.config.yang.netty.eventexecutor;
-import javax.management.InstanceAlreadyExistsException;
-import javax.management.ObjectName;
-
import org.junit.Before;
import org.junit.Test;
import org.opendaylight.controller.config.api.ConflictingVersionException;
import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.ObjectName;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
public class GlobalEventExecutorModuleTest extends AbstractConfigTest {
private GlobalEventExecutorModuleFactory factory;
- private final String instanceName = "netty1";
+ private final String instanceName = GlobalEventExecutorModuleFactory.SINGLETON_NAME;
@Before
public void setUp() {
ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
createInstance(transaction, instanceName);
- createInstance(transaction, instanceName + 2);
+
transaction.validateConfig();
CommitStatus status = transaction.commit();
- assertBeanCount(2, factory.getImplementationName());
- assertStatus(status, 2, 0, 0);
+ assertBeanCount(1, factory.getImplementationName());
+ assertStatus(status, 1, 0, 0);
+ }
+
+ @Test
+ public void testConflictingName() throws Exception {
+ ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
+ try {
+ createInstance(transaction, instanceName + "x");
+ fail();
+ }catch(IllegalArgumentException e){
+ assertTrue(e.getMessage() + " failure", e.getMessage().contains("only allowed name is singleton"));
+ }
}
@Test
import org.opendaylight.controller.config.api.JmxAttributeValidationException;
+import java.util.concurrent.TimeUnit;
+
/**
*
*/
@Override
public void close() throws Exception {
- shutdownGracefully();
+ shutdownGracefully(0, 1, TimeUnit.SECONDS);
}
}
}
class StopSystemBundleThread extends Thread {
private static final Logger logger = LoggerFactory.getLogger(StopSystemBundleThread.class);
+ public static final String CONFIG_MANAGER_SYMBOLIC_NAME = "org.opendaylight.controller.config-manager";
private final Bundle systemBundle;
StopSystemBundleThread(Bundle systemBundle) {
try {
// wait so that JMX response is received
Thread.sleep(1000);
+ // first try to stop config-manager
+ Bundle configManager = findConfigManager();
+ if (configManager != null){
+ logger.debug("Stopping config-manager");
+ configManager.stop();
+ Thread.sleep(1000);
+ }
+ logger.debug("Stopping system bundle");
systemBundle.stop();
} catch (BundleException e) {
logger.warn("Can not stop OSGi server", e);
logger.warn("Shutdown process interrupted", e);
}
}
+
+ private Bundle findConfigManager() {
+ for(Bundle bundle: systemBundle.getBundleContext().getBundles()){
+ if (CONFIG_MANAGER_SYMBOLIC_NAME.equals(bundle.getSymbolicName())) {
+ return bundle;
+ }
+ }
+ return null;
+ }
+
}
class CallSystemExitThread extends Thread {
import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
import org.osgi.framework.Bundle;
+import javax.management.InstanceNotFoundException;
import javax.management.JMX;
import javax.management.ObjectName;
import java.util.Collections;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.opendaylight.controller.config.yang.shutdown.impl.ShutdownModuleFactory.NAME;
public class ShutdownTest extends AbstractConfigTest {
private final ShutdownModuleFactory factory = new ShutdownModuleFactory();
@Mock
- private Bundle mockedSysBundle;
+ private Bundle mockedSysBundle, mockedConfigManager;
+
@Before
public void setUp() throws Exception {
doReturn(mockedSysBundle).when(mockedContext).getBundle(0);
mockedContext.getBundle(0);
doNothing().when(mockedSysBundle).stop();
+ doNothing().when(mockedConfigManager).stop();
+ doReturn(mockedContext).when(mockedSysBundle).getBundleContext();
+ doReturn(new Bundle[]{mockedSysBundle, mockedConfigManager}).when(mockedContext).getBundles();
+ doReturn("system bundle").when(mockedSysBundle).getSymbolicName();
+ doReturn(StopSystemBundleThread.CONFIG_MANAGER_SYMBOLIC_NAME).when(mockedConfigManager).getSymbolicName();
+
+
ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
// initialize default instance
transaction.commit();
@Test
public void testWithSecret() throws Exception {
+ String secret = "secret";
+ setSecret(secret);
+ shutdownViaRuntimeJMX(secret);
+ }
+
+ private void setSecret(String secret) throws InstanceNotFoundException {
ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
ObjectName on = transaction.lookupConfigBean(NAME, NAME);
ShutdownModuleMXBean proxy = transaction.newMXBeanProxy(on, ShutdownModuleMXBean.class);
- String secret = "secret";
proxy.setSecret(secret);
transaction.commit();
- shutdownViaRuntimeJMX(secret);
+ }
+
+ @Test
+ public void testWrongSecret() throws Exception {
+ setSecret("secret");
try {
ShutdownRuntimeMXBean runtime = JMX.newMXBeanProxy(platformMBeanServer, runtimeON, ShutdownRuntimeMXBean.class);
runtime.shutdown("foo", 60000L, null);
assertStopped();
}
-
private void assertStopped() throws Exception {
- Thread.sleep(2000); // happens on another thread
+ Thread.sleep(3000); // happens on another thread
+ verify(mockedConfigManager).stop();
verify(mockedSysBundle).stop();
- verifyNoMoreInteractions(mockedSysBundle);
- reset(mockedSysBundle);
- doNothing().when(mockedSysBundle).stop();
}
}
</Private-Package>
<Export-Package>
org.opendaylight.controller.config.threadpool.util,
- org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.threadpool.impl.rev130405.*,
+ org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.threadpool.impl.*,
</Export-Package>
</instructions>
</configuration>
config:java-name-prefix IdentityTest;
}
- identity test-identity1 {
-
- }
-
- identity test-identity2 {
- base test-identity1;
- }
-
augment "/config:modules/config:module/config:configuration" {
case impl-identity-test {
when "/config:modules/config:module/config:type = 'impl-identity-test'";
leaf afi {
type identityref {
- base test-identity1;
+ base tt:test-identity1;
}
}
container identities-container {
leaf afi {
type identityref {
- base test-identity1;
+ base tt:test-identity1;
}
}
}
list identities {
leaf afi {
type identityref {
- base test-identity1;
+ base tt:test-identity1;
}
}
leaf safi {
type identityref {
- base test-identity1;
+ base tt:test-identity1;
}
}
container identities-inner {
leaf afi {
type identityref {
- base test-identity1;
+ base tt:test-identity1;
}
}
}
}
}
+
+
+ identity test-identity1 {
+
+ }
+
+ identity test-identity2 {
+ base test-identity1;
+ }
}
import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.test.impl.rev130403.TestIdentity1;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.test.impl.rev130403.TestIdentity2;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
import org.opendaylight.yangtools.yang.data.impl.codec.IdentityCodec;
</scm>
<artifactId>configuration</artifactId>
- <version>0.4.2-SNAPSHOT</version>
+ <version>0.4.3-SNAPSHOT</version>
<packaging>bundle</packaging>
<build>
public interface IConfigurationContainerService extends IConfigurationServiceCommon {
/**
- * Bundle will call this function to ask ContainerConfigurationService to provide the
- * directory location of container
+ * This function returns the path to the configuration directory of the
+ * current container.
*
- * @return The path to active container directory
+ * @return The path to active container's configuration directory
*/
- String getConfigurationRoot();
+ public String getConfigurationRoot();
+
+ /**
+ * Function checks whether there exists a saved configuration for this
+ * container (This is essentially checking whether the container's root
+ * configuration directory exists)
+ *
+ * @return True iff container config has been saved at least once
+ */
+ public boolean hasBeenSaved();
+
}
</scm>
<artifactId>configuration.implementation</artifactId>
- <version>0.4.2-SNAPSHOT</version>
+ <version>0.4.3-SNAPSHOT</version>
<packaging>bundle</packaging>
<build>
public void init() {
logger.info("ConfigurationService Manager init");
+
+ // Create the default startup directory, so that container unaware apps can initiate save
+ createContainerDirectory(ROOT + GlobalConstants.DEFAULT.toString());
}
public void start() {
List<String> containerList = new ArrayList<String>();
for (IConfigurationAware configurationAware : this.configurationAwareList) {
if (configurationAware instanceof IConfigurationContainerService) {
- String containerFilePath = ((ContainerConfigurationService)configurationAware).getConfigurationRoot();
+ String containerFilePath = ((IConfigurationContainerService)configurationAware).getConfigurationRoot();
containerList.add(containerFilePath);
}
}
}
private void createContainerDirectory(IConfigurationAware configurationAware) {
- String containerFilePath = ((ContainerConfigurationService) configurationAware).getConfigurationRoot();
- if (!new File(containerFilePath).exists()) {
- boolean created = new File(containerFilePath).mkdir();
- if (!created) {
- logger.error("Failed to create startup config directory: {}", containerFilePath);
+ String containerFilePath = ((IConfigurationContainerService) configurationAware).getConfigurationRoot();
+ createContainerDirectory(containerFilePath);
+ }
+
+ private void createContainerDirectory(String containerFilePath) {
+
+ try {
+ if (!new File(containerFilePath).exists()) {
+ boolean created = new File(containerFilePath).mkdir();
+ if (!created) {
+ logger.error("Failed to create config directory: {}", containerFilePath);
+ }
}
+ } catch (Exception e) {
+ logger.error("Failed to create config directory: {} ({})", containerFilePath, e.getMessage());
}
}
package org.opendaylight.controller.configuration.internal;
+import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Dictionary;
.synchronizedSet(new HashSet<IConfigurationContainerAware>());
private ObjectReader objReader;
private ObjectWriter objWriter;
+ private String containerName;
public void addConfigurationContainerAware(
IConfigurationContainerAware configurationAware) {
void init(Component c) {
Dictionary<?, ?> props = c.getServiceProperties();
- String containerName = (props != null) ? (String) props.get("containerName") :
+ containerName = (props != null) ? (String) props.get("containerName") :
GlobalConstants.DEFAULT.toString();
root = String.format("%s%s/", GlobalConstants.STARTUPHOME.toString(), containerName);
}
Status status = configurationAware.saveConfiguration();
if (!status.isSuccess()) {
success = false;
- logger.warn("Failed to save config for {}", configurationAware.getClass().getSimpleName());
+ logger.warn("Failed to save config for {} ({})", configurationAware.getClass().getSimpleName(),
+ status.getDescription());
}
}
if (success) {
return new Status(StatusCode.SUCCESS);
} else {
- return new Status(StatusCode.INTERNALERROR, "Failed to Save All Configurations");
+ return new Status(StatusCode.INTERNALERROR, "Failed to save one or more configurations");
}
}
@Override
public Status persistConfiguration(List<ConfigurationObject> config, String fileName) {
+ if (!hasBeenSaved()) {
+ return new Status(StatusCode.NOTALLOWED,
+ String.format("Container %s has not been saved yet", containerName));
+ }
String destination = String.format("%s%s", root, fileName);
return objWriter.write(config, destination);
}
}
return (List<ConfigurationObject>) obj;
}
+
+ @Override
+ public boolean hasBeenSaved() {
+ try {
+ File configRoot = new File(this.getConfigurationRoot());
+ return configRoot.exists();
+ } catch (Exception e) {
+ return false;
+ }
+
+ }
}
# Embedded Tomcat configuration File
org.eclipse.gemini.web.tomcat.config.path=configuration/tomcat-server.xml
+org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true
# Open Flow related system parameters
# TCP port on which the controller is listening (default 6633)
</module>
<module>
<type xmlns:netty="urn:opendaylight:params:xml:ns:yang:controller:netty:eventexecutor">netty:netty-global-event-executor</type>
- <name>global-event-executor</name>
+ <name>singleton</name>
</module>
</modules>
<type xmlns:netty="urn:opendaylight:params:xml:ns:yang:controller:netty">netty:netty-event-executor</type>
<instance>
<name>global-event-executor</name>
- <provider>/modules/module[type='netty-global-event-executor'][name='global-event-executor']</provider>
+ <provider>/modules/module[type='netty-global-event-executor'][name='singleton']</provider>
</instance>
</service>
<service>
--- /dev/null
+/**
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.sal.compatibility;
+
+import org.eclipse.xtend2.lib.StringConcatenation;
+import org.eclipse.xtext.xbase.lib.Conversions;
+import org.eclipse.xtext.xbase.lib.IterableExtensions;
+import org.opendaylight.controller.sal.core.Node;
+import org.opendaylight.controller.sal.core.NodeConnector;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
+
+import java.util.List;
+
+@SuppressWarnings("all")
+public class InventoryMapping {
+ public static NodeConnector toAdNodeConnector(final InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector> identifier) {
+ final List<PathArgument> path = identifier.getPath();
+ final PathArgument lastPathArgument = IterableExtensions.<PathArgument>last(path);
+ final NodeConnectorKey tpKey = ((IdentifiableItem<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector,NodeConnectorKey>) lastPathArgument).getKey();
+ return InventoryMapping.nodeConnectorFromId(tpKey.getId().getValue());
+ }
+
+ public static Node toAdNode(final InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node> identifier) {
+ final List<PathArgument> path = identifier.getPath();
+ final PathArgument lastPathArgument = IterableExtensions.<PathArgument>last(path);
+ final NodeKey tpKey = ((IdentifiableItem<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node,NodeKey>) lastPathArgument).getKey();
+ return InventoryMapping.nodeFromNodeId(tpKey.getId().getValue());
+ }
+
+ public static NodeRef toNodeRef(final Node node) {
+ final NodeId nodeId = new NodeId(InventoryMapping.toNodeId(node));
+ final NodeKey nodeKey = new NodeKey(nodeId);
+ final InstanceIdentifierBuilder<? extends Object> builder = InstanceIdentifier.builder();
+ final InstanceIdentifierBuilder<Nodes> nodes = builder.<Nodes>node(Nodes.class);
+ final InstanceIdentifierBuilder<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node> child =
+ nodes.<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node, NodeKey>child(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node.class, nodeKey);
+ final InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node> path = child.toInstance();
+ return new NodeRef(path);
+ }
+
+ public static NodeKey toNodeKey(final Node node) {
+ final NodeId nodeId = new NodeId(InventoryMapping.toNodeId(node));
+ return new NodeKey(nodeId);
+ }
+
+ public static NodeConnectorKey toNodeConnectorKey(final NodeConnector nc) {
+ final NodeConnectorId nodeConnectorId = new NodeConnectorId(InventoryMapping.toNodeConnectorId(nc));
+ return new NodeConnectorKey(nodeConnectorId);
+ }
+
+ public static String toNodeId(final Node node) {
+ final StringConcatenation builder = new StringConcatenation();
+ builder.append("ad-sal:");
+ builder.append(node.getType(), "");
+ builder.append("::");
+ builder.append(node.getNodeIDString(), "");
+ return builder.toString();
+ }
+
+ public static String toNodeConnectorId(final NodeConnector nc) {
+ final StringConcatenation builder = new StringConcatenation();
+ builder.append(InventoryMapping.toNodeId(nc.getNode()), "");
+ builder.append("::");
+ builder.append(nc.getNodeConnectorIDString(), "");
+ return builder.toString();
+ }
+
+ public static Node nodeFromNodeId(final String nodeId) {
+ final String[] split = nodeId.split("::");
+ return InventoryMapping.nodeFromString(split);
+ }
+
+ public static NodeConnector nodeConnectorFromId(final String invId) {
+ final String[] split = invId.split("::");
+ return InventoryMapping.nodeConnectorFromString(split);
+ }
+
+ private static NodeConnector nodeConnectorFromString(final String[] string) {
+ final List<String> subList = ((List<String>)Conversions.doWrapArray(string)).subList(0, 1);
+ final Node node = InventoryMapping.nodeFromString(((String[])Conversions.unwrapArray(subList, String.class)));
+ final String index3 = string[2];
+ return NodeConnector.fromStringNoNode(index3, node);
+ }
+
+ private static Node nodeFromString(final String[] strings) {
+ String index0 = strings[0];
+ final String type = index0.substring(6);
+ String id = strings[1];
+ return Node.fromString(type, id);
+ }
+}
+++ /dev/null
-/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.controller.sal.compatibility
-
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef
-
-class InventoryMapping {
-
- static def org.opendaylight.controller.sal.core.NodeConnector toAdNodeConnector(
- InstanceIdentifier<NodeConnector> identifier) {
- val tpKey = (identifier.path.last as IdentifiableItem<NodeConnector,NodeConnectorKey>).key;
- return nodeConnectorFromId(tpKey.id.value);
- }
-
- static def org.opendaylight.controller.sal.core.Node toAdNode(InstanceIdentifier<Node> identifier) {
- val tpKey = (identifier.path.last as IdentifiableItem<Node,NodeKey>).key;
- return nodeFromNodeId(tpKey.id.value);
- }
-
-
- static def NodeRef toNodeRef(org.opendaylight.controller.sal.core.Node node) {
- val nodeId = new NodeKey(new NodeId(node.toNodeId))
- val path = InstanceIdentifier.builder().node(Nodes).child(Node,nodeId).toInstance;
- return new NodeRef(path);
- }
-
- static def NodeKey toNodeKey(org.opendaylight.controller.sal.core.Node node) {
- val nodeId = new NodeId(node.toNodeId)
- return new NodeKey(nodeId);
- }
-
- static def NodeConnectorKey toNodeConnectorKey(org.opendaylight.controller.sal.core.NodeConnector nc) {
- val nodeId = new NodeConnectorId(nc.toNodeConnectorId)
- return new NodeConnectorKey(nodeId);
- }
-
- static def String toNodeId(org.opendaylight.controller.sal.core.Node node) {
- '''ad-sal:«node.type»::«node.nodeIDString»'''
- }
-
- static def String toNodeConnectorId(org.opendaylight.controller.sal.core.NodeConnector nc) {
- '''«nc.node.toNodeId»::«nc.nodeConnectorIDString»'''
- }
-
- static def org.opendaylight.controller.sal.core.Node nodeFromNodeId(String nodeId) {
- return nodeFromString(nodeId.split("::"))
- }
-
- static def nodeConnectorFromId(String invId) {
- return nodeConnectorFromString(invId.split("::"));
- }
-
- private static def org.opendaylight.controller.sal.core.NodeConnector nodeConnectorFromString(String[] string) {
- val node = nodeFromString(string.subList(0, 1));
- return org.opendaylight.controller.sal.core.NodeConnector.fromStringNoNode(string.get(2), node);
- }
-
- private static def org.opendaylight.controller.sal.core.Node nodeFromString(String[] strings) {
- val type = strings.get(0).substring(6);
- org.opendaylight.controller.sal.core.Node.fromString(type, strings.get(1))
- }
-
-}
package org.opendaylight.controller.sal.compatibility;
+import java.util.List;
+
import org.opendaylight.controller.sal.binding.api.data.DataChangeListener;
import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
import org.opendaylight.controller.sal.inventory.IPluginOutInventoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.List;
-
public class InventoryNotificationProvider implements AutoCloseable{
private ListenerRegistration<DataChangeListener> nodeConnectorDataChangeListenerRegistration;
&& inventoryPublisher!= null){
if(nodeConnectorDataChangeListener == null){
- InstanceIdentifier nodeConnectorPath = InstanceIdentifier.builder(Nodes.class).child(Node.class).child(NodeConnector.class).build();
+ InstanceIdentifier<NodeConnector> nodeConnectorPath = InstanceIdentifier.builder(Nodes.class).child(Node.class).child(NodeConnector.class).build();
nodeConnectorDataChangeListener = new NodeConnectorDataChangeListener();
nodeConnectorDataChangeListener.setInventoryPublisher(inventoryPublisher);
nodeConnectorDataChangeListenerRegistration = dataProviderService.registerDataChangeListener(nodeConnectorPath, nodeConnectorDataChangeListener);
--- /dev/null
+/**
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.sal.compatibility;
+
+import com.google.common.base.Objects;
+import com.google.common.base.Preconditions;
+import org.eclipse.xtext.xbase.lib.Exceptions;
+import org.opendaylight.controller.sal.common.util.Arguments;
+import org.opendaylight.controller.sal.core.AdvertisedBandwidth;
+import org.opendaylight.controller.sal.core.Bandwidth;
+import org.opendaylight.controller.sal.core.Buffers;
+import org.opendaylight.controller.sal.core.Capabilities;
+import org.opendaylight.controller.sal.core.Config;
+import org.opendaylight.controller.sal.core.ConstructionException;
+import org.opendaylight.controller.sal.core.MacAddress;
+import org.opendaylight.controller.sal.core.Name;
+import org.opendaylight.controller.sal.core.NodeConnector.NodeConnectorIDType;
+import org.opendaylight.controller.sal.core.PeerBandwidth;
+import org.opendaylight.controller.sal.core.Property;
+import org.opendaylight.controller.sal.core.SupportedBandwidth;
+import org.opendaylight.controller.sal.core.Tables;
+import org.opendaylight.controller.sal.core.TimeStamp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FeatureCapability;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityArpMatchIp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityFlowStats;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityIpReasm;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityPortStats;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityQueueStats;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityStp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityTableStats;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowNodeConnector;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SwitchFeatures;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.flow.capable.port.State;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
+import org.opendaylight.yangtools.yang.binding.Identifier;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
+
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+
+@SuppressWarnings("all")
+public class NodeMapping {
+ public final static String MD_SAL_TYPE = "MD_SAL";
+
+ private final static Class<Node> NODE_CLASS = Node.class;
+
+ private final static Class<NodeConnector> NODECONNECTOR_CLASS = NodeConnector.class;
+
+ private NodeMapping() {
+ throw new UnsupportedOperationException("Utility class. Instantiation is not allowed.");
+ }
+
+ public static org.opendaylight.controller.sal.core.Node toADNode(final InstanceIdentifier<? extends Object> node) throws ConstructionException {
+ NodeId nodeId = NodeMapping.toNodeId(node);
+ return NodeMapping.toADNode(nodeId);
+ }
+
+ public static org.opendaylight.controller.sal.core.Node toADNode(final NodeId id) {
+ try {
+ String aDNodeId = NodeMapping.toADNodeId(id);
+ return new org.opendaylight.controller.sal.core.Node(NodeMapping.MD_SAL_TYPE, aDNodeId);
+ } catch (Throwable e) {
+ throw Exceptions.sneakyThrow(e);
+ }
+ }
+
+ public static NodeId toNodeId(final InstanceIdentifier<? extends Object> node) {
+ Preconditions.<InstanceIdentifier<? extends Object>>checkNotNull(node);
+ List<PathArgument> path = node.getPath();
+ Preconditions.<List<PathArgument>>checkNotNull(path);
+ int size = path.size();
+ Preconditions.checkArgument(size >= 2);
+ final PathArgument arg = path.get(1);
+ final IdentifiableItem item = Arguments.<IdentifiableItem>checkInstanceOf(arg, IdentifiableItem.class);
+ Identifier key = item.getKey();
+ final NodeKey nodeKey = Arguments.<NodeKey>checkInstanceOf(key, NodeKey.class);
+ return nodeKey.getId();
+ }
+
+ public static String toADNodeId(final NodeId nodeId) {
+ Preconditions.<NodeId>checkNotNull(nodeId);
+ return nodeId.getValue();
+ }
+
+ public static org.opendaylight.controller.sal.core.NodeConnector toADNodeConnector(final NodeConnectorRef source) throws ConstructionException {
+ Preconditions.<NodeConnectorRef>checkNotNull(source);
+ final InstanceIdentifier<?> path = Preconditions.<InstanceIdentifier<? extends Object>>checkNotNull(source.getValue());
+ Preconditions.checkArgument(path.getPath().size() >= 3);
+ final PathArgument arg = path.getPath().get(2);
+ final IdentifiableItem item = Arguments.<IdentifiableItem>checkInstanceOf(arg,IdentifiableItem.class);
+ final NodeConnectorKey connectorKey = Arguments.<NodeConnectorKey>checkInstanceOf(item.getKey(), NodeConnectorKey.class);
+ return NodeMapping.toADNodeConnector(connectorKey.getId(), NodeMapping.toNodeId(path));
+ }
+
+ public static org.opendaylight.controller.sal.core.NodeConnector toADNodeConnector(final NodeConnectorId ncid, final NodeId nid) {
+ try {
+ String nodeConnectorType = NodeMapping.toNodeConnectorType(ncid, nid);
+ Object aDNodeConnectorId = NodeMapping.toADNodeConnectorId(ncid, nid);
+ org.opendaylight.controller.sal.core.Node aDNode = NodeMapping.toADNode(nid);
+ return new org.opendaylight.controller.sal.core.NodeConnector(nodeConnectorType, aDNodeConnectorId, aDNode);
+ } catch (Throwable e) {
+ throw Exceptions.sneakyThrow(e);
+ }
+ }
+
+ public static String toNodeConnectorType(final NodeConnectorId ncId, final NodeId nodeId) {
+ if (ncId.equals(toLocalNodeConnectorId(nodeId))) {
+ return NodeConnectorIDType.SWSTACK;
+ } else if (ncId.equals(toNormalNodeConnectorId(nodeId))) {
+ return NodeConnectorIDType.HWPATH;
+ } else if (ncId.equals(toControllerNodeConnectorId(nodeId))) {
+ return NodeConnectorIDType.CONTROLLER;
+ }
+ return MD_SAL_TYPE;
+ }
+
+ public static Object toADNodeConnectorId(final NodeConnectorId nodeConnectorId, final NodeId nodeId) {
+ if (nodeConnectorId.equals(toLocalNodeConnectorId(nodeId)) ||
+ nodeConnectorId.equals(toNormalNodeConnectorId(nodeId)) ||
+ nodeConnectorId.equals(toControllerNodeConnectorId(nodeId))) {
+ return org.opendaylight.controller.sal.core.NodeConnector.SPECIALNODECONNECTORID;
+ }
+ return nodeConnectorId.getValue();
+ }
+
+ public static NodeConnectorId toControllerNodeConnectorId(final NodeId node) {
+ return new NodeConnectorId(node.getValue() + ":" + 4294967293L);
+ }
+
+ public static NodeConnectorId toLocalNodeConnectorId(final NodeId node) {
+ return new NodeConnectorId(node.getValue() + ":" + 4294967294L);
+ }
+
+ public static NodeConnectorId toNormalNodeConnectorId(final NodeId node) {
+ return new NodeConnectorId(node.getValue() + ":" + 4294967290L);
+ }
+
+ public static NodeRef toNodeRef(final org.opendaylight.controller.sal.core.Node node) {
+ Preconditions.checkArgument(MD_SAL_TYPE.equals(node.getType()));
+ final String nodeId = Arguments.<String>checkInstanceOf(node.getID(), String.class);
+ final NodeKey nodeKey = new NodeKey(new NodeId(nodeId));
+ final InstanceIdentifier<Node> nodePath = InstanceIdentifier.builder().node(Nodes.class).child(NODE_CLASS, nodeKey).toInstance();
+ return new NodeRef(nodePath);
+ }
+
+ public static NodeConnectorRef toNodeConnectorRef(final org.opendaylight.controller.sal.core.NodeConnector nodeConnector) {
+
+ final NodeRef node = NodeMapping.toNodeRef(nodeConnector.getNode());
+ final InstanceIdentifier<Node> nodePath = ((InstanceIdentifier<Node>) node.getValue());
+ NodeConnectorId nodeConnectorId = null;
+
+ if (nodeConnector.getID().equals(org.opendaylight.controller.sal.core.NodeConnector.SPECIALNODECONNECTORID)) {
+ final NodeId nodeId = toNodeId(nodePath);
+ final String nodeConnectorType = nodeConnector.getType();
+ if (nodeConnectorType.equals(NodeConnectorIDType.SWSTACK)) {
+ nodeConnectorId = toLocalNodeConnectorId(nodeId);
+ } else if (nodeConnectorType.equals(NodeConnectorIDType.HWPATH)) {
+ nodeConnectorId = toNormalNodeConnectorId(nodeId);
+ } else if (nodeConnectorType.equals(NodeConnectorIDType.CONTROLLER)) {
+ nodeConnectorId = toControllerNodeConnectorId(nodeId);
+ }
+ } else {
+ nodeConnectorId = new NodeConnectorId(Arguments.<String>checkInstanceOf(nodeConnector.getID(), String.class));
+ }
+ final NodeConnectorKey connectorKey = new NodeConnectorKey(nodeConnectorId);
+ final InstanceIdentifier<NodeConnector> path = InstanceIdentifier.builder(nodePath).child(NODECONNECTOR_CLASS, connectorKey).toInstance();
+ return new NodeConnectorRef(path);
+ }
+
+ public static org.opendaylight.controller.sal.core.Node toADNode(final NodeRef node) throws ConstructionException {
+ return NodeMapping.toADNode(node.getValue());
+ }
+
+ public static HashSet<Property> toADNodeConnectorProperties(final NodeConnectorUpdated nc) {
+ final FlowCapableNodeConnectorUpdated fcncu = nc.<FlowCapableNodeConnectorUpdated>getAugmentation(FlowCapableNodeConnectorUpdated.class);
+ if (!Objects.equal(fcncu, null)) {
+ return NodeMapping.toADNodeConnectorProperties(fcncu);
+ }
+ return new HashSet<Property>();
+ }
+
+ public static HashSet<Property> toADNodeConnectorProperties(final NodeConnector nc) {
+ final FlowCapableNodeConnector fcnc = nc.<FlowCapableNodeConnector>getAugmentation(FlowCapableNodeConnector.class);
+ if (!Objects.equal(fcnc, null)) {
+ return NodeMapping.toADNodeConnectorProperties(fcnc);
+ }
+ return new HashSet<Property>();
+ }
+
+ public static HashSet<Property> toADNodeConnectorProperties(final FlowNodeConnector fcncu) {
+
+ final HashSet<org.opendaylight.controller.sal.core.Property> props = new HashSet<>();
+ if (fcncu != null) {
+ if (fcncu.getCurrentFeature() != null && toAdBandwidth(fcncu.getCurrentFeature()) != null) {
+ props.add(toAdBandwidth(fcncu.getCurrentFeature()));
+ }
+ if (fcncu.getAdvertisedFeatures() != null && toAdAdvertizedBandwidth(fcncu.getAdvertisedFeatures()) != null) {
+ props.add(toAdAdvertizedBandwidth(fcncu.getAdvertisedFeatures()));
+ }
+ if (fcncu.getSupported() != null && toAdSupportedBandwidth(fcncu.getSupported()) != null) {
+ props.add(toAdSupportedBandwidth(fcncu.getSupported()));
+ }
+ if (fcncu.getPeerFeatures() != null && toAdPeerBandwidth(fcncu.getPeerFeatures()) != null) {
+ props.add(toAdPeerBandwidth(fcncu.getPeerFeatures()));
+ }
+ if (fcncu.getName() != null && toAdName(fcncu.getName()) != null) {
+ props.add(toAdName(fcncu.getName()));
+ }
+ if (fcncu.getConfiguration() != null && toAdConfig(fcncu.getConfiguration()) != null) {
+ props.add(toAdConfig(fcncu.getConfiguration()));
+ }
+ if (fcncu.getState() != null && toAdState(fcncu.getState()) != null) {
+ props.add(toAdState(fcncu.getState()));
+ }
+ }
+ return props;
+ }
+
+ public static Name toAdName(final String name) {
+ return new Name(name);
+ }
+
+ public static Config toAdConfig(final PortConfig pc) {
+ Config config = null;
+ if (pc.isPORTDOWN()) {
+ config = new Config(Config.ADMIN_DOWN);
+ } else {
+ config = new Config(Config.ADMIN_UP);
+ }
+ return config;
+ }
+
+ public static org.opendaylight.controller.sal.core.State toAdState(final State s) {
+
+ org.opendaylight.controller.sal.core.State state = null;
+ if (s.isLinkDown()) {
+ state = new org.opendaylight.controller.sal.core.State(org.opendaylight.controller.sal.core.State.EDGE_DOWN);
+ } else {
+ state = new org.opendaylight.controller.sal.core.State(org.opendaylight.controller.sal.core.State.EDGE_UP);
+ }
+ return state;
+ }
+
+ public static Bandwidth toAdBandwidth(final PortFeatures pf) {
+ Bandwidth bw = null;
+ if (pf.isTenMbHd() || pf.isTenMbFd()) {
+ bw = new Bandwidth(Bandwidth.BW10Mbps);
+ } else if (pf.isHundredMbHd() || pf.isHundredMbFd()) {
+ bw = new Bandwidth(Bandwidth.BW100Mbps);
+ } else if (pf.isOneGbHd() || pf.isOneGbFd()) {
+ bw = new Bandwidth(Bandwidth.BW1Gbps);
+ } else if (pf.isOneGbFd()) {
+ bw = new Bandwidth(Bandwidth.BW10Gbps);
+ } else if (pf.isTenGbFd()) {
+ bw = new Bandwidth(Bandwidth.BW10Gbps);
+ } else if (pf.isFortyGbFd()) {
+ bw = new Bandwidth(Bandwidth.BW40Gbps);
+ } else if (pf.isHundredGbFd()) {
+ bw = new Bandwidth(Bandwidth.BW100Gbps);
+ } else if (pf.isOneTbFd()) {
+ bw = new Bandwidth(Bandwidth.BW1Tbps);
+ }
+ return bw;
+ }
+
+ public static AdvertisedBandwidth toAdAdvertizedBandwidth(final PortFeatures pf) {
+ AdvertisedBandwidth abw = null;
+ final Bandwidth bw = toAdBandwidth(pf);
+ if (bw != null) {
+ abw = new AdvertisedBandwidth(bw.getValue());
+ }
+ return abw;
+ }
+
+ public static SupportedBandwidth toAdSupportedBandwidth(final PortFeatures pf) {
+ SupportedBandwidth sbw = null;
+ final Bandwidth bw = toAdBandwidth(pf);
+ if (bw != null) {
+ sbw = new SupportedBandwidth(bw.getValue());
+ }
+ return sbw;
+ }
+
+ public static PeerBandwidth toAdPeerBandwidth(final PortFeatures pf) {
+ PeerBandwidth pbw = null;
+ final Bandwidth bw = toAdBandwidth(pf);
+ if (bw != null) {
+ pbw = new PeerBandwidth(bw.getValue());
+ }
+ return pbw;
+ }
+
+ public static HashSet<Property> toADNodeProperties(final NodeUpdated nu) {
+ final FlowCapableNodeUpdated fcnu = nu.getAugmentation(FlowCapableNodeUpdated.class);
+ if (fcnu != null) {
+ return toADNodeProperties(fcnu, nu.getId());
+ }
+ return new HashSet<org.opendaylight.controller.sal.core.Property>();
+ }
+
+ public static HashSet<Property> toADNodeProperties(final FlowNode fcnu, final NodeId id) {
+
+ final HashSet<org.opendaylight.controller.sal.core.Property> props = new HashSet<>();
+
+ if (fcnu != null) {
+ props.add(toADTimestamp());
+
+ // props.add(fcnu.supportedActions.toADActions) - TODO
+ if (id != null) {
+ props.add(toADMacAddress(id));
+ }
+ SwitchFeatures switchFeatures = fcnu.getSwitchFeatures();
+ if (switchFeatures != null) {
+ if (switchFeatures.getMaxTables() != null) {
+ props.add(toADTables(switchFeatures.getMaxTables()));
+ }
+ if (switchFeatures.getCapabilities() != null) {
+ props.add(toADCapabiliities(switchFeatures.getCapabilities()));
+ }
+ if (switchFeatures.getMaxBuffers() != null) {
+ props.add(toADBuffers(switchFeatures.getMaxBuffers()));
+ }
+ }
+ }
+ return props;
+ }
+
+ public static TimeStamp toADTimestamp() {
+ final Date date = new Date();
+ final TimeStamp timestamp = new TimeStamp(date.getTime(), "connectedSince");
+ return timestamp;
+ }
+
+ public static MacAddress toADMacAddress(final NodeId id) {
+ final String nodeId = id.getValue().replaceAll("openflow:", "");
+ long lNodeId = Long.parseLong(nodeId);
+ lNodeId = Long.valueOf(lNodeId).longValue();
+ byte[] bytesFromDpid = ToSalConversionsUtils.bytesFromDpid(lNodeId);
+ return new MacAddress(bytesFromDpid);
+ }
+
+ public static Tables toADTables(final Short tables) {
+ return new Tables(tables.byteValue());
+ }
+
+ public static Capabilities toADCapabiliities(final List<Class<? extends FeatureCapability>> capabilities) {
+
+ int b = 0;
+ for (Class<? extends FeatureCapability> capability : capabilities) {
+ if (capability.equals(FlowFeatureCapabilityFlowStats.class)) {
+ b = Capabilities.CapabilitiesType.FLOW_STATS_CAPABILITY.getValue() | b;
+ } else if (capability.equals(FlowFeatureCapabilityTableStats.class)) {
+ b = Capabilities.CapabilitiesType.TABLE_STATS_CAPABILITY.getValue() | b;
+ } else if (capability.equals(FlowFeatureCapabilityPortStats.class)) {
+ b = Capabilities.CapabilitiesType.PORT_STATS_CAPABILITY.getValue() | b;
+ } else if (capability.equals(FlowFeatureCapabilityStp.class)) {
+ b = Capabilities.CapabilitiesType.STP_CAPABILITY.getValue() | b;
+ } else if (capability.equals(FlowFeatureCapabilityIpReasm.class)) {
+ b = Capabilities.CapabilitiesType.IP_REASSEM_CAPABILITY.getValue() | b;
+ } else if (capability.equals(FlowFeatureCapabilityQueueStats.class)) {
+ b = Capabilities.CapabilitiesType.QUEUE_STATS_CAPABILITY.getValue() | b;
+ } else if (capability.equals(FlowFeatureCapabilityArpMatchIp.class)) {
+ b = Capabilities.CapabilitiesType.ARP_MATCH_IP_CAPABILITY.getValue() | b;
+ }
+ }
+ return new Capabilities(b);
+ }
+
+ public static Buffers toADBuffers(final Long buffers) {
+ return new Buffers(buffers.intValue());
+ }
+}
+++ /dev/null
-/*
- * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.controller.sal.compatibility
-
-import org.opendaylight.controller.sal.core.Node
-import org.opendaylight.controller.sal.core.NodeConnector
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem
-
-import static com.google.common.base.Preconditions.*;
-import static extension org.opendaylight.controller.sal.common.util.Arguments.*;
-import static extension org.opendaylight.controller.sal.compatibility.ToSalConversionsUtils.*;
-
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey
-import org.opendaylight.controller.sal.core.ConstructionException
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures
-import org.opendaylight.controller.sal.core.Bandwidth
-import org.opendaylight.controller.sal.core.AdvertisedBandwidth
-import org.opendaylight.controller.sal.core.SupportedBandwidth
-import org.opendaylight.controller.sal.core.PeerBandwidth
-import org.opendaylight.controller.sal.core.Name
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig
-import org.opendaylight.controller.sal.core.Config
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.flow.capable.port.State
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated
-import java.util.HashSet
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdated
-import org.opendaylight.controller.sal.core.Tables
-import java.util.List
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FeatureCapability
-import org.opendaylight.controller.sal.core.Buffers
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityFlowStats
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityTableStats
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityIpReasm
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityPortStats
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityStp
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityQueueStats
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityArpMatchIp
-import org.opendaylight.controller.sal.core.Capabilities
-import org.opendaylight.controller.sal.core.MacAddress
-import java.util.Date
-import org.opendaylight.controller.sal.core.TimeStamp
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowNodeConnector
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowNode
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector
-
-public class NodeMapping {
-
- public static val MD_SAL_TYPE = "MD_SAL";
- private static val NODE_CLASS = org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
- private static val NODECONNECTOR_CLASS = org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.
- NodeConnector;
-
- private new() {
- throw new UnsupportedOperationException("Utility class. Instantiation is not allowed.");
- }
-
- public static def toADNode(InstanceIdentifier<?> node) throws ConstructionException {
- return node.toNodeId.toADNode
- }
-
- public static def toADNode(NodeId id) {
- return new Node(MD_SAL_TYPE, id.toADNodeId);
- }
-
- public static def toNodeId(InstanceIdentifier<?> node) {
- checkNotNull(node);
- checkNotNull(node.getPath());
- checkArgument(node.getPath().size() >= 2);
- val arg = node.getPath().get(1);
- val item = arg.checkInstanceOf(IdentifiableItem);
- val nodeKey = item.getKey().checkInstanceOf(NodeKey);
- return nodeKey.id
- }
-
- public static def toADNodeId(NodeId nodeId) {
- checkNotNull(nodeId);
- return nodeId.value
- }
-
- public static def toADNodeConnector(NodeConnectorRef source) throws ConstructionException {
- checkNotNull(source);
- val InstanceIdentifier<?> path = checkNotNull(source.getValue());
- checkArgument(path.path.size() >= 3);
- val arg = path.getPath().get(2);
- val item = arg.checkInstanceOf(IdentifiableItem);
- val connectorKey = item.getKey().checkInstanceOf(NodeConnectorKey);
- return connectorKey.id.toADNodeConnector(path.toNodeId)
- }
-
- public static def toADNodeConnector(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId ncid,
- org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId nid) {
- return new NodeConnector(ncid.toNodeConnectorType(nid),
- ncid.toADNodeConnectorId(nid), nid.toADNode);
- }
-
- public static def toNodeConnectorType(NodeConnectorId ncId, NodeId nodeId) {
- if (ncId.equals(nodeId.toLocalNodeConnectorId)) {
- return NodeConnector.NodeConnectorIDType.SWSTACK
- } else if (ncId.equals(nodeId.toNormalNodeConnectorId)) {
- return NodeConnector.NodeConnectorIDType.HWPATH
- } else if (ncId.equals(nodeId.toControllerNodeConnectorId)) {
- return NodeConnector.NodeConnectorIDType.CONTROLLER
- }
- return MD_SAL_TYPE
- }
-
- public static def toADNodeConnectorId(NodeConnectorId nodeConnectorId, NodeId nodeId) {
- if (nodeConnectorId.equals(nodeId.toLocalNodeConnectorId) ||
- nodeConnectorId.equals(nodeId.toNormalNodeConnectorId) ||
- nodeConnectorId.equals(nodeId.toControllerNodeConnectorId)) {
- return NodeConnector.SPECIALNODECONNECTORID
- }
- return nodeConnectorId.value
- }
-
- public static def toControllerNodeConnectorId(NodeId node) {
- return new NodeConnectorId(node.value + ":" + 4294967293L)
- }
-
- public static def toLocalNodeConnectorId(NodeId node) {
- return new NodeConnectorId(node.value + ":" + 4294967294L)
- }
-
- public static def toNormalNodeConnectorId(NodeId node) {
- return new NodeConnectorId(node.value + ":" + 4294967290L)
- }
-
- public static def toNodeRef(Node node) {
- checkArgument(MD_SAL_TYPE.equals(node.getType()));
- var nodeId = node.ID.checkInstanceOf(String)
- val nodeKey = new NodeKey(new NodeId(nodeId));
- val nodePath = InstanceIdentifier.builder().node(Nodes).child(NODE_CLASS, nodeKey).toInstance();
- return new NodeRef(nodePath);
- }
-
- public static def toNodeConnectorRef(NodeConnector nodeConnector) {
- val node = nodeConnector.node.toNodeRef();
- val nodePath = node.getValue() as InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node>
- var NodeConnectorId nodeConnectorId
- if (nodeConnector.ID.equals(NodeConnector.SPECIALNODECONNECTORID)) {
- if (nodeConnector.type.equals(NodeConnector.NodeConnectorIDType.SWSTACK)) {
- nodeConnectorId = nodePath.toNodeId.toLocalNodeConnectorId
- } else if (nodeConnector.type.equals(NodeConnector.NodeConnectorIDType.HWPATH)) {
- nodeConnectorId = nodePath.toNodeId.toNormalNodeConnectorId
- } else if (nodeConnector.type.equals(NodeConnector.NodeConnectorIDType.CONTROLLER)) {
- nodeConnectorId = nodePath.toNodeId.toControllerNodeConnectorId
- }
- } else {
- nodeConnectorId = new NodeConnectorId(nodeConnector.ID.checkInstanceOf(String))
- }
- val connectorKey = new NodeConnectorKey(nodeConnectorId);
- val path = InstanceIdentifier.builder(nodePath).child(NODECONNECTOR_CLASS, connectorKey).toInstance();
- return new NodeConnectorRef(path);
- }
-
- public static def toADNode(NodeRef node) throws ConstructionException {
- return toADNode(node.getValue());
- }
-
- public static def toADNodeConnectorProperties(NodeConnectorUpdated nc) {
- val fcncu = nc.getAugmentation(FlowCapableNodeConnectorUpdated)
- if (fcncu != null) {
- return fcncu.toADNodeConnectorProperties
- }
- return new HashSet<org.opendaylight.controller.sal.core.Property>();
- }
-
- public static def toADNodeConnectorProperties(org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector nc) {
- val fcnc = nc.getAugmentation(FlowCapableNodeConnector)
- if (fcnc != null) {
- return fcnc.toADNodeConnectorProperties
- }
- return new HashSet<org.opendaylight.controller.sal.core.Property>();
- }
-
- public static def toADNodeConnectorProperties(FlowNodeConnector fcncu) {
- val props = new HashSet<org.opendaylight.controller.sal.core.Property>();
- if (fcncu != null) {
- if (fcncu.currentFeature != null && fcncu.currentFeature.toAdBandwidth != null) {
- props.add(fcncu.currentFeature.toAdBandwidth)
- }
- if (fcncu.advertisedFeatures != null && fcncu.advertisedFeatures.toAdAdvertizedBandwidth != null) {
- props.add(fcncu.advertisedFeatures.toAdAdvertizedBandwidth)
- }
- if (fcncu.supported != null && fcncu.supported.toAdSupportedBandwidth != null) {
- props.add(fcncu.supported.toAdSupportedBandwidth)
- }
- if (fcncu.peerFeatures != null && fcncu.peerFeatures.toAdPeerBandwidth != null) {
- props.add(fcncu.peerFeatures.toAdPeerBandwidth)
- }
- if (fcncu.name != null && fcncu.name.toAdName != null) {
- props.add(fcncu.name.toAdName)
- }
- if (fcncu.configuration != null && fcncu.configuration.toAdConfig != null) {
- props.add(fcncu.configuration.toAdConfig)
- }
- if (fcncu.state != null && fcncu.state.toAdState != null) {
- props.add(fcncu.state.toAdState)
- }
- }
- return props
- }
-
- public static def toAdName(String name) {
- return new Name(name)
- }
-
- public static def toAdConfig(PortConfig pc) {
- var Config config;
- if (pc.PORTDOWN) {
- config = new Config(Config.ADMIN_DOWN)
- } else {
- config = new Config(Config.ADMIN_UP)
- }
- return config
- }
-
- public static def toAdState(State s) {
- var org.opendaylight.controller.sal.core.State state
- if (s.linkDown) {
- state = new org.opendaylight.controller.sal.core.State(org.opendaylight.controller.sal.core.State.EDGE_DOWN)
- } else {
- state = new org.opendaylight.controller.sal.core.State(org.opendaylight.controller.sal.core.State.EDGE_UP)
- }
- return state
- }
-
- public static def toAdBandwidth(PortFeatures pf) {
- var Bandwidth bw = null
- if (pf.isTenMbHd || pf.isTenMbFd) {
- bw = new Bandwidth(Bandwidth.BW10Mbps)
- } else if (pf.isHundredMbHd || pf.isHundredMbFd) {
- bw = new Bandwidth(Bandwidth.BW100Mbps)
- } else if (pf.isOneGbHd || pf.isOneGbFd) {
- bw = new Bandwidth(Bandwidth.BW1Gbps)
- } else if (pf.isOneGbFd) {
- bw = new Bandwidth(Bandwidth.BW10Gbps)
- } else if (pf.isTenGbFd) {
- bw = new Bandwidth(Bandwidth.BW10Gbps)
- } else if (pf.isFortyGbFd) {
- bw = new Bandwidth(Bandwidth.BW40Gbps)
- } else if (pf.isHundredGbFd) {
- bw = new Bandwidth(Bandwidth.BW100Gbps)
- } else if (pf.isOneTbFd) {
- bw = new Bandwidth(Bandwidth.BW1Tbps)
- }
- return bw;
- }
-
- public static def toAdAdvertizedBandwidth(PortFeatures pf) {
- var AdvertisedBandwidth abw
- val bw = pf.toAdBandwidth
- if (bw != null) {
- abw = new AdvertisedBandwidth(bw.value)
- }
- return abw
- }
-
- public static def toAdSupportedBandwidth(PortFeatures pf) {
- var SupportedBandwidth sbw
- val bw = pf.toAdBandwidth
- if (bw != null) {
- sbw = new SupportedBandwidth(bw.value)
- }
- return sbw
- }
-
- public static def toAdPeerBandwidth(PortFeatures pf) {
- var PeerBandwidth pbw
- val bw = pf.toAdBandwidth
- if (bw != null) {
- pbw = new PeerBandwidth(bw.value)
- }
- return pbw
- }
-
- public static def toADNodeProperties(NodeUpdated nu) {
- val fcnu = nu.getAugmentation(FlowCapableNodeUpdated)
- if (fcnu != null) {
- return fcnu.toADNodeProperties(nu.id)
- }
- return new HashSet<org.opendaylight.controller.sal.core.Property>();
-
- }
-
- public static def toADNodeProperties(FlowNode fcnu, NodeId id) {
- val props = new HashSet<org.opendaylight.controller.sal.core.Property>();
- if (fcnu != null) {
- props.add(toADTimestamp)
-
- // props.add(fcnu.supportedActions.toADActions) - TODO
- if (id != null) {
- props.add(id.toADMacAddress)
- }
- if (fcnu.switchFeatures != null) {
- if (fcnu.switchFeatures.maxTables != null) {
- props.add(fcnu.switchFeatures.maxTables.toADTables)
- }
- if (fcnu.switchFeatures.capabilities != null) {
- props.add(fcnu.switchFeatures.capabilities.toADCapabiliities)
- }
- if (fcnu.switchFeatures.maxBuffers != null) {
- props.add(fcnu.switchFeatures.maxBuffers.toADBuffers)
- }
- }
- }
- return props;
- }
-
- public static def toADTimestamp() {
- val date = new Date();
- val timestamp = new TimeStamp(date.time, "connectedSince")
- return timestamp;
- }
-
- public static def toADMacAddress(NodeId id) {
- return new MacAddress(Long.parseLong(id.value.replaceAll("openflow:", "")).longValue.bytesFromDpid)
- }
-
- public static def toADTables(Short tables) {
- return new Tables(tables.byteValue)
- }
-
- public static def toADCapabiliities(List<Class<? extends FeatureCapability>> capabilities) {
- var int b
- for (capability : capabilities) {
- if (capability.equals(FlowFeatureCapabilityFlowStats)) {
- b = Capabilities.CapabilitiesType.FLOW_STATS_CAPABILITY.value.bitwiseOr(b)
- } else if (capability.equals(FlowFeatureCapabilityTableStats)) {
- b = Capabilities.CapabilitiesType.TABLE_STATS_CAPABILITY.value.bitwiseOr(b)
- } else if (capability.equals(FlowFeatureCapabilityPortStats)) {
- b = Capabilities.CapabilitiesType.PORT_STATS_CAPABILITY.value.bitwiseOr(b)
- } else if (capability.equals(FlowFeatureCapabilityStp)) {
- b = Capabilities.CapabilitiesType.STP_CAPABILITY.value.bitwiseOr(b)
- } else if (capability.equals(FlowFeatureCapabilityIpReasm)) {
- b = Capabilities.CapabilitiesType.IP_REASSEM_CAPABILITY.value.bitwiseOr(b)
- } else if (capability.equals(FlowFeatureCapabilityQueueStats)) {
- b = Capabilities.CapabilitiesType.QUEUE_STATS_CAPABILITY.value.bitwiseOr(b)
- } else if (capability.equals(FlowFeatureCapabilityArpMatchIp)) {
- b = Capabilities.CapabilitiesType.ARP_MATCH_IP_CAPABILITY.value.bitwiseOr(b)
- }
- }
- return new Capabilities(b)
- }
-
- public static def toADBuffers(Long buffers) {
- return new Buffers(buffers.intValue)
- }
-
-}
module flow-capable-transaction {
- namespace "urn:opendaylight:flow:transaction";
+ namespace "urn:opendaylight:flow:transaction";
prefix type;
import opendaylight-inventory {prefix inv; revision-date "2013-08-19";}
import ietf-inet-types {prefix inet; revision-date "2010-09-24";}
import yang-ext {prefix ext; revision-date "2013-07-09";}
-
+
revision "2013-11-03" {
description "Initial revision";
}
typedef transaction-id {
- type uint64;
+ type uint64;
}
// This refers to MD-SAL transaction reference.
grouping transaction-metadata {
type inet:uri;
}
}
-
+
grouping transaction-aware {
leaf transaction-id {
type transaction-id;
}
}
+ grouping multipart-transaction-aware {
+ uses transaction-aware;
+
+ leaf moreReplies {
+ type boolean;
+ default false;
+ }
+ }
+
rpc get-next-transaction-id {
input {
leaf node {
}
}
output {
- uses transaction-aware;
+ uses transaction-aware;
}
}
}
}
}
-}
\ No newline at end of file
+}
notification table-updated {
uses "inv:node-context-ref";
- uses tr:transaction-aware;
- leaf moreReplies {
- type boolean;
- }
+ uses tr:multipart-transaction-aware;
uses table-type:table-features;
}
}
import flow-node-inventory {prefix flow-node;revision-date "2013-08-19";}
import flow-capable-transaction {prefix tr;}
import ietf-inet-types {prefix inet; revision-date "2010-09-24";}
-
+
revision "2013-08-19" {
description "Initial revision of flow statistics service";
}
-
- //Augment flow statistics data to the flow-capable-node->table->flow
- augment "/inv:nodes/inv:node/flow-node:table/flow-node:flow" {
+
+ //Augment flow statistics data to the flow-capable-node->table->flow
+ augment "/inv:nodes/inv:node/flow-node:table/flow-node:flow" {
ext:augment-identifier "flow-statistics-data";
uses flow-statistics;
}
-
- grouping flow-statistics {
+
+ grouping flow-statistics {
container flow-statistics {
//config "false";
- uses flow-types:flow;
- uses stat-types:generic-statistics;
+ uses flow-types:flow;
+ uses stat-types:generic-statistics;
+ }
+ }
+
+ typedef flow-id {
+ description "flow id";
+ type inet:uri;
+ }
+
+ grouping flow-and-statistics-map-list {
+ description "List of flow and statistics map";
+ list flow-and-statistics-map-list {
+ key "flow-id";
+ leaf flow-id {
+ type flow-id;
+ }
+ uses flow-and-statistics-map;
}
- }
-
- typedef flow-id {
- description "flow id";
- type inet:uri;
- }
-
- grouping flow-and-statistics-map-list {
- description "List of flow and statistics map";
- list flow-and-statistics-map-list {
- key "flow-id";
- leaf flow-id {
- type flow-id;
- }
- uses flow-and-statistics-map;
- }
- }
-
- grouping flow-and-statistics-map{
- description "Mapping between flow and its statistics";
- uses flow-types:flow;
- uses stat-types:generic-statistics;
- }
-
+ }
+
+ grouping flow-and-statistics-map{
+ description "Mapping between flow and its statistics";
+ uses flow-types:flow;
+ uses stat-types:generic-statistics;
+ }
+
// RPC calls to fetch flow statistics
rpc get-all-flows-statistics-from-all-flow-tables {
- description "Fetch statistics of all the flow present in all the flow tables of the switch";
+ description "Fetch statistics of all the flow present in all the flow tables of the switch";
input {
uses inv:node-context-ref;
}
uses flow-and-statistics-map-list;
uses tr:transaction-aware;
}
-
+
}
rpc get-all-flow-statistics-from-flow-table {
- description "Fetch statistics of all the flow present in the specific flow table of the switch";
+ description "Fetch statistics of all the flow present in the specific flow table of the switch";
input {
uses inv:node-context-ref;
leaf table-id {
- type table-types:table-id;
+ type table-types:table-id;
}
}
output {
}
rpc get-flow-statistics-from-flow-table {
- description "Fetch statistics of the specific flow present in the specific flow table of the switch";
+ description "Fetch statistics of the specific flow present in the specific flow table of the switch";
input {
uses inv:node-context-ref;
uses flow-types:flow;
}
notification flows-statistics-update {
- description "Flows statistics sent by switch";
- leaf moreReplies {
+ description "Flows statistics sent by switch";
+ leaf moreReplies {
type boolean;
}
uses inv:node;
- uses flow-and-statistics-map-list;
- uses tr:transaction-aware;
+ uses flow-and-statistics-map-list;
+ uses tr:transaction-aware;
}
- //Models for aggregate flow statistics collection
- augment "/inv:nodes/inv:node/flow-node:table" {
+ //Models for aggregate flow statistics collection
+ augment "/inv:nodes/inv:node/flow-node:table" {
ext:augment-identifier "aggregate-flow-statistics-data";
uses aggregate-flow-statistics;
}
-
- grouping aggregate-flow-statistics {
+
+ grouping aggregate-flow-statistics {
container aggregate-flow-statistics {
//config "false";
- uses stat-types:aggregate-flow-statistics;
+ uses stat-types:aggregate-flow-statistics;
}
- }
-
+ }
+
// RPC calls to fetch aggregate flow statistics
rpc get-aggregate-flow-statistics-from-flow-table-for-all-flows {
- description "Fetch aggregate statistics for all the flows present in the specific flow table of the switch";
+ description "Fetch aggregate statistics for all the flows present in the specific flow table of the switch";
input {
uses inv:node-context-ref;
leaf table-id {
- type table-types:table-id;
+ type table-types:table-id;
}
}
output {
- uses stat-types:aggregate-flow-statistics;
+ uses stat-types:aggregate-flow-statistics;
uses tr:transaction-aware;
}
}
rpc get-aggregate-flow-statistics-from-flow-table-for-given-match {
- description "Fetch aggregate statistics for all the flow matches to the given match from the given table of the switch";
+ description "Fetch aggregate statistics for all the flow matches to the given match from the given table of the switch";
input {
uses inv:node-context-ref;
uses flow-types:flow;
}
output {
- uses stat-types:aggregate-flow-statistics;
+ uses stat-types:aggregate-flow-statistics;
uses tr:transaction-aware;
}
}
notification aggregate-flow-statistics-update {
- description "Aggregate flow statistics for a table, sent by switch";
- leaf moreReplies {
- type boolean;
- }
+ description "Aggregate flow statistics for a table, sent by switch";
uses inv:node;
- uses stat-types:aggregate-flow-statistics;
- uses tr:transaction-aware;
+ uses stat-types:aggregate-flow-statistics;
+ uses tr:multipart-transaction-aware;
}
}
namespace "urn:opendaylight:flow:table:statistics";
prefix flowtablestat;
- import flow-capable-transaction {prefix tr;}
+ import flow-capable-transaction {prefix tr;}
import yang-ext {prefix ext; revision-date "2013-07-09";}
import opendaylight-inventory {prefix inv;revision-date "2013-08-19";}
import flow-node-inventory {prefix flow-node;revision-date "2013-08-19";}
}
//Augment flow table statistics data to the table
- augment "/inv:nodes/inv:node/flow-node:table" {
+ augment "/inv:nodes/inv:node/flow-node:table" {
ext:augment-identifier "flow-table-statistics-data";
uses flow-table-statistics;
}
-
- grouping flow-table-statistics {
+
+ grouping flow-table-statistics {
container flow-table-statistics {
//config "false";
- uses stat-types:generic-table-statistics;
+ uses stat-types:generic-table-statistics;
}
- }
+ }
//RPC calls to fetch flow table statistics
grouping flow-table-and-statistics-map {
- list flow-table-and-statistics-map {
- key "table-id";
- leaf table-id {
- type table-types:table-id;
- }
- uses stat-types:generic-table-statistics;
- }
+ list flow-table-and-statistics-map {
+ key "table-id";
+ leaf table-id {
+ type table-types:table-id;
+ }
+ uses stat-types:generic-table-statistics;
+ }
}
rpc get-flow-tables-statistics {
- description "Fetch statistics of all the flow tables present on the tarnet node";
- input {
- uses inv:node-context-ref;
- }
- output {
- uses flow-table-and-statistics-map;
- uses tr:transaction-aware;
- }
+ description "Fetch statistics of all the flow tables present on the tarnet node";
+ input {
+ uses inv:node-context-ref;
+ }
+ output {
+ uses flow-table-and-statistics-map;
+ uses tr:transaction-aware;
+ }
}
//Notification to receive table statistics update
notification flow-table-statistics-update {
- description "Receive flow table statistics update";
-
- leaf moreReplies {
- type boolean;
- }
+ description "Receive flow table statistics update";
+
uses inv:node;
- uses flow-table-and-statistics-map;
- uses tr:transaction-aware;
+ uses flow-table-and-statistics-map;
+ uses tr:multipart-transaction-aware;
}
}
description "Initial revision of group statistics service";
}
- grouping group-statistics {
+ grouping group-statistics {
container group-statistics {
//config "false";
uses group-types:group-statistics;
}
- }
+ }
augment "/inv:nodes/inv:node/fni:group" {
ext:augment-identifier "node-group-statistics";
uses group-statistics;
}
- grouping group-desc {
+ grouping group-desc {
container group-desc {
//config "false";
uses group-types:group;
}
- }
+ }
augment "/inv:nodes/inv:node/fni:group" {
ext:augment-identifier "node-group-desc-stats";
uses group-desc;
}
- grouping group-features {
- container group-features {
+ grouping group-features {
+ container group-features {
//config "false";
uses group-types:group-features-reply;
}
}
-
+
augment "/inv:nodes/inv:node" {
ext:augment-identifier "node-group-features";
uses group-features;
//Notification calls
notification group-statistics-updated {
- leaf moreReplies {
- type boolean;
- }
uses inv:node;
uses group-types:group-statistics-reply;
- uses tr:transaction-aware;
+ uses tr:multipart-transaction-aware;
}
notification group-desc-stats-updated {
- leaf moreReplies {
- type boolean;
- }
uses inv:node;
uses group-types:group-desc-stats-reply;
- uses tr:transaction-aware;
+ uses tr:multipart-transaction-aware;
}
notification group-features-updated {
- leaf moreReplies {
- type boolean;
- }
uses inv:node;
uses group-types:group-features-reply;
- uses tr:transaction-aware;
+ uses tr:multipart-transaction-aware;
}
}
//Notification calls
notification meter-statistics-updated {
- leaf moreReplies {
- type boolean;
- }
-
uses inv:node;
uses meter-types:meter-statistics-reply;
- uses tr:transaction-aware;
+ uses tr:multipart-transaction-aware;
}
notification meter-config-stats-updated {
- leaf moreReplies {
- type boolean;
- }
-
uses inv:node;
uses meter-types:meter-config-stats-reply;
- uses tr:transaction-aware;
+ uses tr:multipart-transaction-aware;
}
notification meter-features-updated {
- leaf moreReplies {
- type boolean;
- }
-
uses inv:node;
uses meter-types:meter-features-reply;
- uses tr:transaction-aware;
+ uses tr:multipart-transaction-aware;
}
}
namespace "urn:opendaylight:port:statistics";
prefix portstat;
- import flow-capable-transaction {prefix tr;}
+ import flow-capable-transaction {prefix tr;}
import yang-ext {prefix ext; revision-date "2013-07-09";}
import opendaylight-inventory {prefix inv;revision-date "2013-08-19";}
import opendaylight-statistics-types {prefix stat-types;revision-date "2013-09-25";}
revision "2013-12-14" {
description "Initial revision of port statistics model";
}
-
+
//Augment port statistics data to the flow-capable-node-connector
- augment "/inv:nodes/inv:node/inv:node-connector" {
+ augment "/inv:nodes/inv:node/inv:node-connector" {
ext:augment-identifier "flow-capable-node-connector-statistics-data";
uses flow-capable-node-connector-statistics;
}
-
- grouping flow-capable-node-connector-statistics {
+
+ grouping flow-capable-node-connector-statistics {
container flow-capable-node-connector-statistics {
//config "false";
uses stat-types:node-connector-statistics;
}
- }
-
+ }
+
// RPC calls
rpc get-all-node-connectors-statistics {
- description "Get statistics for all node connectors from the node";
+ description "Get statistics for all node connectors from the node";
input {
uses inv:node-context-ref;
}
output {
- uses node-connector-statistics-and-port-number-map;
+ uses node-connector-statistics-and-port-number-map;
uses tr:transaction-aware;
}
}
-
+
rpc get-node-connector-statistics {
- description "Get statistics for given node connector from the node";
- input {
- uses inv:node-context-ref;
- leaf node-connector-id {
- type inv:node-connector-id;
- }
- }
- output {
+ description "Get statistics for given node connector from the node";
+ input {
+ uses inv:node-context-ref;
+ leaf node-connector-id {
+ type inv:node-connector-id;
+ }
+ }
+ output {
uses stat-types:node-connector-statistics;
uses tr:transaction-aware;
- }
+ }
}
-
- //Notification for node connector statistics update
- grouping node-connector-statistics-and-port-number-map {
- description "List of map - node connectors and their statistics";
- list node-connector-statistics-and-port-number-map {
- key "node-connector-id";
- leaf node-connector-id {
- type inv:node-connector-id;
- }
- uses stat-types:node-connector-statistics;
- }
- }
- notification node-connector-statistics-update {
- leaf moreReplies {
- type boolean;
+ //Notification for node connector statistics update
+ grouping node-connector-statistics-and-port-number-map {
+ description "List of map - node connectors and their statistics";
+ list node-connector-statistics-and-port-number-map {
+ key "node-connector-id";
+ leaf node-connector-id {
+ type inv:node-connector-id;
+ }
+ uses stat-types:node-connector-statistics;
}
+ }
+
+ notification node-connector-statistics-update {
uses inv:node;
- uses node-connector-statistics-and-port-number-map;
- uses tr:transaction-aware;
+ uses node-connector-statistics-and-port-number-map;
+ uses tr:multipart-transaction-aware;
}
}
namespace "urn:opendaylight:queue:statistics";
prefix queuestat;
- import flow-capable-transaction {prefix tr;}
+ import flow-capable-transaction {prefix tr;}
import yang-ext {prefix ext; revision-date "2013-07-09";}
import opendaylight-inventory {prefix inv;revision-date "2013-08-19";}
import flow-node-inventory {prefix flow-node;revision-date "2013-08-19";}
}
//Augment queue statistics data to the flow-capable-node-connector
- augment "/inv:nodes/inv:node/inv:node-connector/flow-node:queue" {
+ augment "/inv:nodes/inv:node/inv:node-connector/flow-node:queue" {
ext:augment-identifier "flow-capable-node-connector-queue-statistics-data";
uses flow-capable-node-connector-queue-statistics;
}
-
- grouping flow-capable-node-connector-queue-statistics {
+
+ grouping flow-capable-node-connector-queue-statistics {
container flow-capable-node-connector-queue-statistics {
//config "false";
uses stat-types:generic-queue-statistics;
}
- }
-
- //RPC calls to fetch queue statistics
+ }
+
+ //RPC calls to fetch queue statistics
grouping queue-id-and-statistics-map {
- list queue-id-and-statistics-map {
- key "queue-id node-connector-id";
- leaf queue-id {
- type queue-types:queue-id;
- }
- leaf node-connector-id {
- type inv:node-connector-id;
- }
-
+ list queue-id-and-statistics-map {
+ key "queue-id node-connector-id";
+ leaf queue-id {
+ type queue-types:queue-id;
+ }
+ leaf node-connector-id {
+ type inv:node-connector-id;
+ }
+
uses stat-types:generic-queue-statistics;
- }
+ }
}
rpc get-all-queues-statistics-from-all-ports {
- description "Get statistics for all the queues attached to all the ports from the node";
+ description "Get statistics for all the queues attached to all the ports from the node";
input {
uses inv:node-context-ref;
}
}
rpc get-all-queues-statistics-from-given-port {
- description "Get statistics for all queues for given port of the node";
- input {
- uses inv:node-context-ref;
- leaf node-connector-id {
- type inv:node-connector-id;
- }
- }
- output {
+ description "Get statistics for all queues for given port of the node";
+ input {
+ uses inv:node-context-ref;
+ leaf node-connector-id {
+ type inv:node-connector-id;
+ }
+ }
+ output {
uses queue-id-and-statistics-map;
uses tr:transaction-aware;
- }
+ }
}
rpc get-queue-statistics-from-given-port {
- description "Get statistics for given queues from given port of the node";
- input {
- uses inv:node-context-ref;
- leaf node-connector-id {
- type inv:node-connector-id;
- }
- leaf queue-id {
- type queue-types:queue-id;
- }
- }
- output {
+ description "Get statistics for given queues from given port of the node";
+ input {
+ uses inv:node-context-ref;
+ leaf node-connector-id {
+ type inv:node-connector-id;
+ }
+ leaf queue-id {
+ type queue-types:queue-id;
+ }
+ }
+ output {
uses queue-id-and-statistics-map;
uses tr:transaction-aware;
- }
+ }
}
//Notification for port statistics update
- notification queue-statistics-update {
- leaf moreReplies {
- type boolean;
- }
+ notification queue-statistics-update {
uses inv:node;
- uses queue-id-and-statistics-map;
- uses tr:transaction-aware;
+ uses queue-id-and-statistics-map;
+ uses tr:multipart-transaction-aware;
}
}
<configuration>
<instructions>
<Bundle-Name>${project.groupId}.${project.artifactId}</Bundle-Name>
- <Bundle-Activator>org.opendaylight.controller.sal.binding.osgi.Activator</Bundle-Activator>
<Export-package>
org.opendaylight.controller.sal.binding.spi.*,
</Export-package>
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
import javassist.ClassPool;
public class SingletonHolder {
- public static final ClassPool CLASS_POOL = new ClassPool();
+ public static final ClassPool CLASS_POOL = ClassPool.getDefault();
public static final org.opendaylight.controller.sal.binding.codegen.impl.RuntimeCodeGenerator RPC_GENERATOR_IMPL = new org.opendaylight.controller.sal.binding.codegen.impl.RuntimeCodeGenerator(
CLASS_POOL);
public static final RuntimeCodeGenerator RPC_GENERATOR = RPC_GENERATOR_IMPL;
public static final NotificationInvokerFactory INVOKER_FACTORY = RPC_GENERATOR_IMPL.getInvokerFactory();
+
+ public static final int CORE_NOTIFICATION_THREADS = 4;
+ public static final int MAX_NOTIFICATION_THREADS = 32;
+ public static final int NOTIFICATION_THREAD_LIFE = 15;
+
private static ListeningExecutorService NOTIFICATION_EXECUTOR = null;
private static ListeningExecutorService COMMIT_EXECUTOR = null;
private static ListeningExecutorService CHANGE_EVENT_EXECUTOR = null;
+ /**
+ * @deprecated This method is only used from configuration modules and thus callers of it
+ * should use service injection to make the executor configurable.
+ */
+ @Deprecated
public static synchronized final ListeningExecutorService getDefaultNotificationExecutor() {
if (NOTIFICATION_EXECUTOR == null) {
- NOTIFICATION_EXECUTOR = createNamedExecutor("md-sal-binding-notification-%d");
+ ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-notification-%d").build();
+ ExecutorService executor = new ThreadPoolExecutor(CORE_NOTIFICATION_THREADS, MAX_NOTIFICATION_THREADS,
+ NOTIFICATION_THREAD_LIFE, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), factory);
+ NOTIFICATION_EXECUTOR = MoreExecutors.listeningDecorator(executor);
}
return NOTIFICATION_EXECUTOR;
}
public static synchronized final ListeningExecutorService getDefaultCommitExecutor() {
if (COMMIT_EXECUTOR == null) {
ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-commit-%d").build();
- /*
- * FIXME: this used to be newCacheThreadPool(), but MD-SAL does not have transaction
- * ordering guarantees, which means that using a concurrent threadpool results
- * in application data being committed in random order, potentially resulting
- * in inconsistent data being present. Once proper primitives are introduced,
- * concurrency can be reintroduced.
- */
+ /*
+ * FIXME: this used to be newCacheThreadPool(), but MD-SAL does not have transaction
+ * ordering guarantees, which means that using a concurrent threadpool results
+ * in application data being committed in random order, potentially resulting
+ * in inconsistent data being present. Once proper primitives are introduced,
+ * concurrency can be reintroduced.
+ */
ExecutorService executor = Executors.newSingleThreadExecutor(factory);
COMMIT_EXECUTOR = MoreExecutors.listeningDecorator(executor);
}
return COMMIT_EXECUTOR;
}
- private static ListeningExecutorService createNamedExecutor(String format) {
- ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat(format).build();
- ExecutorService executor = Executors.newCachedThreadPool(factory);
- return MoreExecutors.listeningDecorator(executor);
- }
-
public static ExecutorService getDefaultChangeEventExecutor() {
if (CHANGE_EVENT_EXECUTOR == null) {
ThreadFactory factory = new ThreadFactoryBuilder().setDaemon(true).setNameFormat("md-sal-binding-change-%d").build();
package org.opendaylight.controller.sal.binding.impl\r
\r
import com.google.common.collect.HashMultimap\r
+import com.google.common.collect.ImmutableSet\r
import com.google.common.collect.Multimap\r
-import java.util.Collection\r
+import com.google.common.collect.Multimaps\r
import java.util.Collections\r
import java.util.concurrent.Callable\r
import java.util.concurrent.ExecutorService\r
+import java.util.concurrent.Future\r
+import java.util.Set\r
import org.opendaylight.controller.sal.binding.api.NotificationListener\r
import org.opendaylight.controller.sal.binding.api.NotificationProviderService\r
+import org.opendaylight.controller.sal.binding.api.NotificationProviderService.NotificationInterestListener\r
+import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder\r
import org.opendaylight.controller.sal.binding.spi.NotificationInvokerFactory.NotificationInvoker\r
import org.opendaylight.yangtools.concepts.AbstractObjectRegistration\r
import org.opendaylight.yangtools.concepts.ListenerRegistration\r
import org.opendaylight.yangtools.concepts.Registration\r
+import org.opendaylight.yangtools.concepts.util.ListenerRegistry\r
import org.opendaylight.yangtools.yang.binding.Notification\r
import org.slf4j.LoggerFactory\r
-import org.opendaylight.controller.sal.binding.codegen.impl.SingletonHolder\rimport com.google.common.collect.Multimaps\r
-import org.opendaylight.yangtools.concepts.util.ListenerRegistry\r
-import org.opendaylight.controller.sal.binding.api.NotificationProviderService.NotificationInterestListener\rimport java.util.Set\r
-import java.util.Set\r
-import com.google.common.collect.ImmutableSet\r
-import java.util.concurrent.Future\r
\r
class NotificationBrokerImpl implements NotificationProviderService, AutoCloseable {\r
\r
notification.class.interfaces.filter[it != Notification && Notification.isAssignableFrom(it)]\r
}\r
\r
- @SuppressWarnings("unchecked")\r
- private def notifyAll(Collection<NotificationListener<?>> listeners, Notification notification) {\r
- listeners.forEach[(it as NotificationListener).onNotification(notification)]\r
- }\r
-\r
@Deprecated\r
override addNotificationListener(org.opendaylight.yangtools.yang.binding.NotificationListener listener) {\r
throw new UnsupportedOperationException("Deprecated method. Use registerNotificationListener instead.");\r
*/
package org.opendaylight.controller.sal.binding.impl.util
-import java.util.Map.Entry
-import org.opendaylight.yangtools.concepts.Path
-import java.util.Map
-import java.util.Set
+import com.google.common.collect.Multimap
import java.util.Collection
import java.util.HashSet
-import com.google.common.collect.Multimap
+import java.util.Map.Entry
+import org.opendaylight.yangtools.concepts.Path
class MapUtils {
public interface SimpleInput extends DataObject,Augmentable<SimpleInput> {
@RoutingContext(BaseIdentity.class)
- InstanceIdentifier getIdentifier();
+ InstanceIdentifier<?> getIdentifier();
}
public static final String CONTROLLER_MODELS = "org.opendaylight.controller.model";
public static final String YANGTOOLS_MODELS = "org.opendaylight.yangtools.model";
- private static final String OPENDAYLIGHT_SNAPSHOT = "http://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/";
- private static final String OPENDAYLIGHT_RELEASE = "http://nexus.opendaylight.org/content/repositories/opendaylight.release/";
public static Option mdSalCoreBundles() {
return new DefaultCompositeOption( //
*/
package org.opendaylight.controller.test.sal.binding.it;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
import java.util.concurrent.Future;
import org.junit.Before;
import org.junit.Test;
import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
-import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
+import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
import org.opendaylight.controller.sal.core.api.Broker;
public class DataServiceTest extends AbstractTest {
protected DataBrokerService consumerDataService;
-
-
+
+
@Inject
Broker broker2;
assertNotNull(consumerDataService);
-
+
DataModificationTransaction transaction = consumerDataService.beginTransaction();
assertNotNull(transaction);
-
+
NodeRef node1 = createNodeRef("0");
DataObject node = consumerDataService.readConfigurationData(node1.getValue());
assertNull(node);
Node nodeData1 = createNode("0");
-
+
transaction.putConfigurationData(node1.getValue(), nodeData1);
Future<RpcResult<TransactionStatus>> commitResult = transaction.commit();
assertNotNull(commitResult);
-
+
RpcResult<TransactionStatus> result = commitResult.get();
-
+
assertNotNull(result);
assertNotNull(result.getResult());
assertEquals(TransactionStatus.COMMITED, result.getResult());
-
+
Node readedData = (Node) consumerDataService.readConfigurationData(node1.getValue());
assertNotNull(readedData);
assertEquals(nodeData1.getKey(), readedData.getKey());
-
-
+
+
DataModificationTransaction transaction2 = consumerDataService.beginTransaction();
assertNotNull(transaction);
-
+
transaction2.removeConfigurationData(node1.getValue());
-
+
Future<RpcResult<TransactionStatus>> commitResult2 = transaction2.commit();
assertNotNull(commitResult2);
-
+
RpcResult<TransactionStatus> result2 = commitResult2.get();
-
+
assertNotNull(result2);
assertNotNull(result2.getResult());
assertEquals(TransactionStatus.COMMITED, result2.getResult());
-
+
DataObject readedData2 = consumerDataService.readConfigurationData(node1.getValue());
assertNull(readedData2);
-
-
+
+
}
-
+
private static NodeRef createNodeRef(String string) {
NodeKey key = new NodeKey(new NodeId(string));
- InstanceIdentifier<Node> path = InstanceIdentifier.builder().node(Nodes.class).node(Node.class, key)
- .toInstance();
+ InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class).child(Node.class, key).build();
return new NodeRef(path);
}
-
+
private static Node createNode(String string) {
NodeBuilder ret = new NodeBuilder();
NodeId id = new NodeId(string);
*/
private static NodeRef createNodeRef(String string) {
NodeKey key = new NodeKey(new NodeId(string));
- InstanceIdentifier<Node> path = InstanceIdentifier.builder().node(Nodes.class).node(Node.class, key)
- .toInstance();
+ InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class).child(Node.class, key).build();
return new NodeRef(path);
}
import java.util.Map;
import java.util.Set;
-// FIXME: After 0.6 Release of YANGTools refactor to use Path marker interface for arguments.
-// import org.opendaylight.yangtools.concepts.Path;
+import org.opendaylight.yangtools.concepts.Path;
-public interface DataChange<P/* extends Path<P> */, D> {
+public interface DataChange<P extends Path<P>, D> {
/**
* Returns a map of paths and newly created objects
- *
+ *
* @return map of paths and newly created objects
*/
Map<P, D> getCreatedOperationalData();
/**
* Returns a map of paths and newly created objects
- *
+ *
* @return map of paths and newly created objects
*/
Map<P, D> getCreatedConfigurationData();
/**
* Returns a map of paths and respective updated objects after update.
- *
+ *
* Original state of the object is in
* {@link #getOriginalOperationalData()}
- *
+ *
* @return map of paths and newly created objects
*/
Map<P, D> getUpdatedOperationalData();
/**
* Returns a map of paths and respective updated objects after update.
- *
+ *
* Original state of the object is in
* {@link #getOriginalConfigurationData()}
- *
+ *
* @return map of paths and newly created objects
*/
Map<P, D> getUpdatedConfigurationData();
/**
* Returns a set of paths of removed objects.
- *
+ *
* Original state of the object is in
* {@link #getOriginalConfigurationData()}
- *
+ *
* @return map of paths and newly created objects
*/
Set<P> getRemovedConfigurationData();
/**
* Returns a set of paths of removed objects.
- *
+ *
* Original state of the object is in
* {@link #getOriginalOperationalData()}
- *
+ *
* @return map of paths and newly created objects
*/
Set<P> getRemovedOperationalData();
/**
* Return a map of paths and original state of updated and removed objectd.
- *
+ *
* @return map of paths and original state of updated and removed objectd.
*/
Map<P, D> getOriginalConfigurationData();
/**
* Return a map of paths and original state of updated and removed objectd.
- *
+ *
* @return map of paths and original state of updated and removed objectd.
*/
Map<P, D> getOriginalOperationalData();
package org.opendaylight.controller.md.sal.common.api.data;
import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.concepts.Path;
-public interface DataChangeEvent<P,D> extends DataChange<P, D>, Immutable {
+public interface DataChangeEvent<P extends Path<P>,D> extends DataChange<P, D>, Immutable {
/**
* Returns a orignal subtree of data, which starts at the path
* where listener was registered.
- *
+ *
*/
D getOriginalConfigurationSubtree();
/**
* Returns a new subtree of data, which starts at the path
* where listener was registered.
- *
+ *
*/
D getOriginalOperationalSubtree();
-
-
-
+
+
+
/**
* Returns a updated subtree of data, which starts at the path
* where listener was registered.
- *
+ *
*/
D getUpdatedConfigurationSubtree();
/**
* Returns a udpated subtree of data, which starts at the path
* where listener was registered.
- *
+ *
*/
D getUpdatedOperationalSubtree();
}
import java.util.EventListener;
-// FIXME: After 0.6 Release of YANGTools refactor to use Path marker interface for arguments.
-// import org.opendaylight.yangtools.concepts.Path;
+import org.opendaylight.yangtools.concepts.Path;
-public interface DataChangeListener<P/* extends Path<P> */,D> extends EventListener {
+public interface DataChangeListener<P extends Path<P>, D> extends EventListener {
void onDataChanged(DataChangeEvent<P, D> change);
}
*/
package org.opendaylight.controller.md.sal.common.api.data;
-
import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.concepts.Path;
-// FIXME: After 0.6 Release of YANGTools refactor to use Path marker interface for arguments.
-// import org.opendaylight.yangtools.concepts.Path;
-public interface DataChangePublisher<P/* extends Path<P> */,D, L extends DataChangeListener<P,D>> {
+public interface DataChangePublisher<P extends Path<P>, D, L extends DataChangeListener<P,D>> {
ListenerRegistration<L> registerDataChangeListener(P path, L listener);
package org.opendaylight.controller.md.sal.common.api.data;
import org.opendaylight.controller.sal.common.DataStoreIdentifier;
-// FIXME: After 0.6 Release of YANGTools refactor to use Path marker interface for arguments.
-// import org.opendaylight.yangtools.concepts.Path;
+import org.opendaylight.yangtools.concepts.Path;
import org.opendaylight.yangtools.yang.common.RpcResult;
/**
* Two phase commit handler (cohort) of the two-phase commit protocol of data.
- *
+ *
* <p>
* The provider should expose the implementation of DataCommitHandler if it's
* functionality depends on any subset of data stored in data repositories, in
* order to participate in {@link DataBrokerService#commit(DataStoreIdentifier)
* operation.
- *
+ *
* <p>
* Operations of two-phase commit handlers should not change data in data store,
* this is responsibility of the coordinator (broker or some component of the
* broker).
- *
+ *
* The commit handlers are responsible for changing the internal state of the
* provider to reflect the commited changes in data.
- *
+ *
* <h3>Two-phase commit</h3>
- *
+ *
* <h4>Commit Request Phase</h4>
- *
+ *
* <ol>
* <li> <code>Consumer</code> edits data by invocation of
* <code>DataBrokerService.editCandidateData(DataStoreIdentifier, DataRoot)</code>
* </ol>
* <li><code>Broker</code> starts a commit finish phase
* </ol>
- *
+ *
* <h4>Commit Finish Phase</h4>
- *
+ *
* <ol>
* <li>For each <code>CommitTransaction</code> from Commit Request phase
* <ol>
* <li>If error occured, the broker starts a commit rollback phase.
* </ul>
* </ol>
- *
+ *
* <h4>Commit Rollback Phase</h4>
* <li>For each <code>DataCommitTransaction</code> from Commit Request phase
* <ol>
* <li>The provider rollbacks a commit and returns an {@link RpcResult} of
* rollback. </ol>
* <li>Broker returns a error result to the consumer.
- *
+ *
* @param <P> Class representing a path
* @param <D> Superclass from which all data objects are derived from.
*/
-public interface DataCommitHandler<P/* extends Path<P> */,D> {
+public interface DataCommitHandler<P extends Path<P>, D> {
+
-
DataCommitTransaction<P, D> requestCommit(DataModification<P,D> modification);
- public interface DataCommitTransaction<P/* extends Path<P> */,D> {
+ public interface DataCommitTransaction<P extends Path<P>, D> {
DataModification<P,D> getModification();
/**
- *
+ *
* Finishes a commit.
- *
+ *
* This callback is invoked by commit coordinator to finish commit action.
- *
+ *
* The implementation is required to finish transaction or return unsuccessful
* rpc result if something went wrong.
- *
+ *
* The provider (commit handler) should apply all changes to its state
* which are a result of data change-
- *
+ *
* @return
*/
RpcResult<Void> finish() throws IllegalStateException;
/**
* Rollbacks a commit.
- *
+ *
* This callback is invoked by commit coordinator to finish commit action.
- *
+ *
* The provider (commit handler) should rollback all changes to its state
* which were a result of previous request commit.
- *
+ *
* @return
* @throws IllegalStateException
* If the method is invoked after {@link #finish()}
import java.util.concurrent.Future;
import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
+import org.opendaylight.yangtools.concepts.Path;
import org.opendaylight.yangtools.yang.common.RpcResult;
-//FIXME: After 0.6 Release of YANGTools refactor to use Path marker interface for arguments.
-//import org.opendaylight.yangtools.concepts.Path;
-public interface DataModification<P/* extends Path<P> */, D> extends DataChange<P, D>, DataReader<P, D> {
+public interface DataModification<P extends Path<P>, D> extends DataChange<P, D>, DataReader<P, D> {
/**
* Returns transaction identifier
*/
package org.opendaylight.controller.md.sal.common.api.data;
-// FIXME: After 0.6 Release of YANGTools refactor to use Path marker interface for arguments.
-// import org.opendaylight.yangtools.concepts.Path;
+import org.opendaylight.yangtools.concepts.Path;
-public interface DataModificationTransactionFactory<P/* extends Path<P> */,D> {
+public interface DataModificationTransactionFactory<P extends Path<P> ,D> {
DataModification<P, D> beginTransaction();
}
*/
package org.opendaylight.controller.md.sal.common.api.data;
-// FIXME: After 0.6 Release of YANGTools refactor to use Path marker interface for arguments.
-// import org.opendaylight.yangtools.concepts.Path;
+import org.opendaylight.yangtools.concepts.Path;
-public interface DataProvider<P/* extends Path<P> */,D> extends DataReader<P, D> {
+public interface DataProvider<P extends Path<P>, D> extends DataReader<P, D> {
}
*/
package org.opendaylight.controller.md.sal.common.api.data;
-// FIXME: After 0.6 Release of YANGTools refactor to use Path marker interface for arguments.
-// import org.opendaylight.yangtools.concepts.Path;
+import org.opendaylight.yangtools.concepts.Path;
/**
* Reader for reading YANG subtrees based on their path.
- *
+ *
* Reader is requested to return object at specified path and all it's subnodes
* known to the reader or null if node is not found in this reader.
*
* @param <P> Path Type
* @param <D> Data Type
*/
-public interface DataReader<P/* extends Path<P> */,D> {
+public interface DataReader<P extends Path<P> ,D> {
/**
* Reads data from Operational data store located at provided path
- *
+ *
* @param path Path to data
* @return
*/
*/
package org.opendaylight.controller.md.sal.common.api.data;
-public interface DataStore<P, D> extends //
+import org.opendaylight.yangtools.concepts.Path;
+
+public interface DataStore<P extends Path<P>, D> extends //
DataReader<P, D>, //
DataModificationTransactionFactory<P, D> {
*/
package org.opendaylight.controller.md.sal.common.api.data;
+import org.opendaylight.yangtools.concepts.Path;
+
/**
* A chain of transactions. Transactions in a chain need to be committed in sequence and each
* transaction should see the effects of previous transactions as if they happened. A chain
* makes no guarantees of atomicity, in fact transactions are committed as soon as possible.
*/
-public interface TransactionChain<P/* extends Path<P> */, D> extends AutoCloseable {
+public interface TransactionChain<P extends Path<P>, D> extends AutoCloseable {
/**
* Create a new transaction which will continue the chain. The previous transaction
* has to be either COMMITTED or CANCELLED.
*/
package org.opendaylight.controller.md.sal.common.api.data;
+import org.opendaylight.yangtools.concepts.Path;
+
/**
* Interface for creating transaction chains.
*/
-public interface TransactionChainFactory<P/* extends Path<P> */, D> {
+public interface TransactionChainFactory<P extends Path<P>, D> {
/**
* Create a new transaction chain. The chain will be initialized to read
* from its backing datastore, with no outstanding transaction. Listener
package org.opendaylight.controller.md.sal.common.api.routing;
import org.opendaylight.yangtools.concepts.Mutable;
-// FIXME: After 0.6 Release of YANGTools refactor to use Path marker interface for arguments.
-// import org.opendaylight.yangtools.concepts.Path;
+import org.opendaylight.yangtools.concepts.Path;
-public interface MutableRoutingTable<C,P/* extends Path<P> */,T> extends RoutingTable<C,P,T>, Mutable {
+public interface MutableRoutingTable<C, P extends Path<P>, T> extends RoutingTable<C,P,T>, Mutable {
void setDefaultRoute(T target);
void updateRoute(P path,T target);
*/
package org.opendaylight.controller.md.sal.common.api.routing;
-// FIXME: After 0.6 Release of YANGTools refactor to use Path marker interface for arguments.
-// import org.opendaylight.yangtools.concepts.Path;
+import org.opendaylight.yangtools.concepts.Path;
import org.opendaylight.yangtools.concepts.Registration;
-public interface RoutedRegistration<C,P/* extends Path<P> */,S> extends Registration<S> {
+public interface RoutedRegistration<C, P extends Path<P>, S> extends Registration<S> {
void registerPath(C context, P path);
void unregisterPath(C context, P path);
import java.util.Map;
-// FIXME: After 0.6 Release of YANGTools refactor to use Path marker interface for arguments.
-// import org.opendaylight.yangtools.concepts.Path;
+import org.opendaylight.yangtools.concepts.Path;
-public interface RoutingTable<C,P/* extends Path<P> */,T> {
+public interface RoutingTable<C, P extends Path<P>, T> {
C getIdentifier();
import com.google.common.collect.ImmutableSet;
public class RoutingUtils {
-
+
public static <C,P> RouteChange<C,P> removalChange(C context,P path) {
final ImmutableMap<C, Set<P>> announcements = ImmutableMap.<C,Set<P>>of();
final ImmutableMap<C, Set<P>> removals = ImmutableMap.<C,Set<P>>of(context, ImmutableSet.of(path));
return new RouteChangeImpl<C,P>(announcements, removals);
}
-
+
public static <C,P> RouteChange<C,P> announcementChange(C context,P path) {
final ImmutableMap<C, Set<P>> announcements = ImmutableMap.<C,Set<P>>of(context, ImmutableSet.of(path));
final ImmutableMap<C, Set<P>> removals = ImmutableMap.<C,Set<P>>of();
return new RouteChangeImpl<C,P>(announcements, removals);
}
-
-
+
+
public static <C,P> RouteChange<C,P> change(Map<C, Set<P>> announcements,
Map<C, Set<P>> removals) {
final ImmutableMap<C, Set<P>> immutableAnnouncements = ImmutableMap.<C,Set<P>>copyOf(announcements);
final ImmutableMap<C, Set<P>> immutableRemovals = ImmutableMap.<C,Set<P>>copyOf(removals);
return new RouteChangeImpl<C,P>(immutableAnnouncements, immutableRemovals);
}
-
-
+
+
private static class RouteChangeImpl<C,P> implements RouteChange<C, P> {
private final Map<C, Set<P>> removal;
private final Map<C, Set<P>> announcement;
public Map<C, Set<P>> getAnnouncements() {
return announcement;
}
-
+
@Override
public Map<C, Set<P>> getRemovals() {
return removal;
if (getClass() != obj.getClass()) {
return false;
}
- RouteChangeImpl other = (RouteChangeImpl) obj;
+ RouteChangeImpl<?, ?> other = (RouteChangeImpl<?, ?>) obj;
if (announcement == null) {
if (other.announcement != null)
return false;
}
-
+
}
Preconditions.checkNotNull(transaction);
transaction.changeStatus(TransactionStatus.SUBMITED);
final TwoPhaseCommit<P, D, DCL> task = new TwoPhaseCommit<P, D, DCL>(transaction, this);
- ;
+
this.getSubmittedTransactionsCount().getAndIncrement();
return this.getExecutor().submit(task);
}
log.trace("Transaction: {} Affected Subtrees:", transactionId, changedPaths);
+ // The transaction has no effects, let's just shortcut it
+ if (changedPaths.isEmpty()) {
+ dataBroker.getFinishedTransactionsCount().getAndIncrement();
+ transaction.changeStatus(TransactionStatus.COMMITED);
+
+ log.trace("Transaction: {} Finished successfully (no effects).", transactionId);
+
+ return Rpcs.<TransactionStatus> getRpcResult(true, TransactionStatus.COMMITED,
+ Collections.<RpcError> emptySet());
+ }
+
final ImmutableList.Builder<ListenerStateCapture<P, D, DCL>> listenersBuilder = ImmutableList.builder();
listenersBuilder.addAll(dataBroker.affectedListeners(changedPaths));
filterProbablyAffectedListeners(dataBroker.probablyAffectedListeners(changedPaths),listenersBuilder);
@Override
public void run() {
for (final ListenerStateCapture<P, D, DCL> listenerSet : listeners) {
- {
- DataChangeEvent<P, D> changeEvent = listenerSet.createEvent(transaction);
- for (final DataChangeListenerRegistration<P, D, DCL> listener : listenerSet.getListeners()) {
- try {
- listener.getInstance().onDataChanged(changeEvent);
- } catch (Exception e) {
- log.error("Unhandled exception when invoking listener {}", listener);
- }
+ DataChangeEvent<P, D> changeEvent = listenerSet.createEvent(transaction);
+ for (final DataChangeListenerRegistration<P, D, DCL> listener : listenerSet.getListeners()) {
+ try {
+ listener.getInstance().onDataChanged(changeEvent);
+ } catch (Exception e) {
+ log.error("Unhandled exception when invoking listener {}", listener, e);
}
}
}
package org.opendaylight.controller.sal.dom.broker;
import java.util.Collections
-import java.util.HashMap
import java.util.HashSet
-import java.util.Map
import java.util.Set
import java.util.concurrent.Callable
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
import java.util.concurrent.Future
+import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener
import org.opendaylight.controller.sal.core.api.Broker
-import org.opendaylight.controller.sal.core.api.BrokerService
import org.opendaylight.controller.sal.core.api.Consumer
import org.opendaylight.controller.sal.core.api.Provider
-import org.opendaylight.controller.sal.core.spi.BrokerModule
+import org.opendaylight.controller.sal.core.api.RpcImplementation
+import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry
+import org.opendaylight.controller.sal.core.api.RpcRegistrationListener
+import org.opendaylight.controller.sal.core.api.RpcRoutingContext
+import org.opendaylight.controller.sal.dom.broker.spi.RpcRouter
import org.opendaylight.yangtools.yang.common.QName
import org.opendaylight.yangtools.yang.common.RpcResult
import org.opendaylight.yangtools.yang.data.api.CompositeNode
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
import org.osgi.framework.BundleContext
import org.slf4j.LoggerFactory
-import org.opendaylight.controller.sal.dom.broker.spi.RpcRouter
-import org.opendaylight.yangtools.concepts.ListenerRegistration
-import org.opendaylight.controller.sal.core.api.RpcRegistrationListener
-import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry
-import org.opendaylight.controller.sal.core.api.RpcImplementation
-import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener
-import org.opendaylight.controller.sal.core.api.RpcRoutingContext
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
public class BrokerImpl implements Broker, RpcProvisionRegistry, AutoCloseable {
private static val log = LoggerFactory.getLogger(BrokerImpl);
*/
package org.opendaylight.controller.sal.dom.broker.impl
-import org.opendaylight.controller.md.sal.common.impl.routing.AbstractDataReadRouter
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
-import org.opendaylight.yangtools.yang.data.api.CompositeNode
-import org.opendaylight.controller.md.sal.common.api.data.DataReader
-import org.opendaylight.yangtools.yang.common.QName
import java.net.URI
-import java.util.List
-import org.opendaylight.yangtools.yang.data.api.Node
import java.util.ArrayList
-import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl
-import java.util.Map
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier
-import org.opendaylight.yangtools.yang.data.api.SimpleNode
+import java.util.Collection
import java.util.Collections
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates
import java.util.HashMap
-import static com.google.common.base.Preconditions.*;
-import java.util.Collection
-import java.util.Set
+import java.util.Map
import java.util.Map.Entry
-import org.slf4j.LoggerFactory
+import java.util.Set
+import org.opendaylight.controller.md.sal.common.impl.routing.AbstractDataReadRouter
+import org.opendaylight.yangtools.yang.common.QName
+import org.opendaylight.yangtools.yang.data.api.CompositeNode
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument
+import org.opendaylight.yangtools.yang.data.api.Node
+import org.opendaylight.yangtools.yang.data.api.SimpleNode
import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl
+import org.slf4j.LoggerFactory
+
+import static com.google.common.base.Preconditions.*
class DataReaderRouter extends AbstractDataReadRouter<InstanceIdentifier, CompositeNode> {
private static val LOG = LoggerFactory.getLogger(DataReaderRouter);
var name = pathArgument?.nodeType;
val nodes = new ArrayList<Node<?>>();
val keyNodes = new HashMap<QName, SimpleNode<?>>();
- val iterator = data.iterator;
for(dataBit : data) {
try {
if(pathArgument != null && dataBit != null) {
import org.opendaylight.yangtools.yang.data.api.CompositeNode
import org.opendaylight.controller.sal.core.api.data.DataStore
import java.util.HashSet
+import org.slf4j.LoggerFactory
+import org.slf4j.Logger
-class HashMapDataStore implements DataStore, AutoCloseable {
-
+final class HashMapDataStore implements DataStore, AutoCloseable {
+ private val Logger LOG = LoggerFactory.getLogger(HashMapDataStore)
val Map<InstanceIdentifier, CompositeNode> configuration = new ConcurrentHashMap();
val Map<InstanceIdentifier, CompositeNode> operational = new ConcurrentHashMap();
override containsConfigurationPath(InstanceIdentifier path) {
- throw new UnsupportedOperationException("TODO: auto-generated method stub")
-
+ return configuration.containsKey(path)
}
override containsOperationalPath(InstanceIdentifier path) {
- throw new UnsupportedOperationException("TODO: auto-generated method stub")
+ return operational.containsKey(path)
}
override getStoredConfigurationPaths() {
}
override readConfigurationData(InstanceIdentifier path) {
+ LOG.trace("Reading configuration path {}", path)
configuration.get(path);
}
override readOperationalData(InstanceIdentifier path) {
+ LOG.trace("Reading operational path {}", path)
operational.get(path);
}
def RpcResult<Void> finish(HashMapDataStoreTransaction transaction) {
val modification = transaction.modification;
for (removal : modification.removedConfigurationData) {
+ LOG.trace("Removing configuration path {}", removal)
remove(configuration,removal);
}
for (removal : modification.removedOperationalData) {
+ LOG.trace("Removing operational path {}", removal)
remove(operational,removal);
}
+ if (LOG.isTraceEnabled()) {
+ for (a : modification.updatedConfigurationData.keySet) {
+ LOG.trace("Adding configuration path {}", a)
+ }
+ for (a : modification.updatedOperationalData.keySet) {
+ LOG.trace("Adding operational path {}", a)
+ }
+ }
configuration.putAll(modification.updatedConfigurationData);
operational.putAll(modification.updatedOperationalData);
}
}
for(pathToRemove : affected) {
+ LOG.trace("Removed path {}", pathToRemove)
map.remove(pathToRemove);
}
protected CompositeNode mergeData(InstanceIdentifier path, CompositeNode stored, CompositeNode modified,
boolean config) {
- long startTime = System.nanoTime();
+ // long startTime = System.nanoTime();
try {
DataSchemaNode node = schemaNodeFor(path);
return YangDataOperations.merge(node, stored, modified, config);
throw new IllegalArgumentException("Supplied node is not data node container.");
}
- private def static checkConfigurational(DataSchemaNode node, boolean config) {
- if (config) {
- checkArgument(node.configuration, "Supplied composite node is not configurational.");
- }
- }
-
private static dispatch def Iterable<? extends Node<?>> mergeMultiple(LeafSchemaNode node, List<Node<?>> original,
List<Node<?>> modified, boolean configurational) {
checkArgument(original.size === 1);
import java.util.List
import java.util.Set
import java.util.concurrent.atomic.AtomicInteger
+import org.opendaylight.controller.netconf.api.NetconfMessage
import org.opendaylight.controller.sal.common.util.Rpcs
+import org.opendaylight.yangtools.yang.common.QName
+import org.opendaylight.yangtools.yang.common.RpcResult
import org.opendaylight.yangtools.yang.data.api.CompositeNode
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument
-import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl
-import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode
-import java.util.Collections
-import java.util.List
-import java.util.Set
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates
-import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument
import org.opendaylight.yangtools.yang.data.api.Node
import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl
+import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode
+import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl
import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils
import org.opendaylight.yangtools.yang.model.api.NotificationDefinition
import org.opendaylight.yangtools.yang.model.api.SchemaContext
import org.w3c.dom.Document
import org.w3c.dom.Element
-import org.opendaylight.yangtools.yang.common.QName
-import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl
-import org.opendaylight.controller.netconf.api.NetconfMessage
-import org.opendaylight.yangtools.yang.common.RpcResult
class NetconfMapping {
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
+import javax.ws.rs.Encoded;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("/config/{identifier:.+}")
@Produces({Draft02.MediaTypes.DATA+JSON,Draft02.MediaTypes.DATA+XML,
MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_XML})
- public StructuredData readConfigurationData(@PathParam("identifier") String identifier);
+ public StructuredData readConfigurationData(@Encoded @PathParam("identifier") String identifier);
@GET
@Path("/operational/{identifier:.+}")
@Produces({Draft02.MediaTypes.DATA+JSON,Draft02.MediaTypes.DATA+XML,
MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_XML})
- public StructuredData readOperationalData(@PathParam("identifier") String identifier);
+ public StructuredData readOperationalData(@Encoded @PathParam("identifier") String identifier);
@PUT
@Path("/config/{identifier:.+}")
@Consumes({Draft02.MediaTypes.DATA+JSON,Draft02.MediaTypes.DATA+XML,
MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_XML})
- public Response updateConfigurationData(@PathParam("identifier") String identifier, CompositeNode payload);
+ public Response updateConfigurationData(@Encoded @PathParam("identifier") String identifier, CompositeNode payload);
@POST
@Path("/config/{identifier:.+}")
@Consumes({Draft02.MediaTypes.DATA+JSON,Draft02.MediaTypes.DATA+XML,
MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.TEXT_XML})
- public Response createConfigurationData(@PathParam("identifier") String identifier, CompositeNode payload);
+ public Response createConfigurationData(@Encoded @PathParam("identifier") String identifier, CompositeNode payload);
@POST
@Path("/config")
@DELETE
@Path("/config/{identifier:.+}")
- public Response deleteConfigurationData(@PathParam("identifier") String identifier);
+ public Response deleteConfigurationData(@Encoded @PathParam("identifier") String identifier);
@GET
@Path("/streams/stream/{identifier:.+}")
- public Response subscribeToStream(@PathParam("identifier") String identifier, @Context UriInfo uriInfo);
+ public Response subscribeToStream(@Encoded @PathParam("identifier") String identifier, @Context UriInfo uriInfo);
}
val static MOUNT_MODULE = "yang-ext"
val static MOUNT_NODE = "mount"
public val static MOUNT = "yang-ext:mount"
+ val static URI_ENCODING_CHAR_SET = "ISO-8859-1"
+ val static URI_SLASH_PLACEHOLDER = "%2F";
@Property
var SchemaContext globalSchema;
private def InstanceIdWithSchemaNode toIdentifier(String restconfInstance, boolean toMountPointIdentifier) {
checkPreconditions
- val pathArgs = Lists.newArrayList(Splitter.on("/").split(restconfInstance))
+ val encodedPathArgs = Lists.newArrayList(Splitter.on("/").split(restconfInstance))
+ val pathArgs = urlPathArgsDecode(encodedPathArgs)
pathArgs.omitFirstAndLastEmptyString
if (pathArgs.empty) {
return null;
private def toUriString(Object object) {
if(object === null) return "";
- return URLEncoder.encode(object.toString)
+// return object.toString.replace("/",URI_SLASH_PLACEHOLDER)
+ return URLEncoder.encode(object.toString,URI_ENCODING_CHAR_SET)
}
private def InstanceIdWithSchemaNode collectPathArguments(InstanceIdentifierBuilder builder, List<String> strings,
}
override onGlobalContextUpdated(SchemaContext context) {
- this.globalSchema = context;
- for (operation : context.operations) {
- val qname = operation.QName;
- qnameToRpc.put(qname, operation);
+ if (context !== null) {
+ qnameToRpc.clear
+ this.globalSchema = context;
+ for (operation : context.operations) {
+ val qname = operation.QName;
+ qnameToRpc.put(qname, operation);
+ }
}
}
+
+ def urlPathArgsDecode(List<String> strings) {
+ val List<String> decodedPathArgs = new ArrayList();
+ for (pathArg : strings) {
+ decodedPathArgs.add(URLDecoder.decode(pathArg, URI_ENCODING_CHAR_SET))
+ }
+ return decodedPathArgs
+ }
+
}
import static org.mockito.Mockito.when;
import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.JSON;
import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
-import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
import java.io.IOException;
import java.io.InputStream;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.UriInfo;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
public void testPostOperationsWithInputDataMediaTypes() throws UnsupportedEncodingException {
String uriPrefix = "/operations/";
String uriPath = "ietf-interfaces:interfaces";
- String uri = createUri(uriPrefix, uriPath);
+ String uri = uriPrefix + uriPath;
when(restconfService.invokeRpc(eq(uriPath), any(CompositeNode.class))).thenReturn(null);
post(uri, Draft02.MediaTypes.OPERATION+JSON, Draft02.MediaTypes.OPERATION+JSON, jsonData);
verify(restconfService, times(1)).invokeRpc(eq(uriPath), any(CompositeNode.class));
public void testGetConfigMediaTypes() throws UnsupportedEncodingException {
String uriPrefix = "/config/";
String uriPath = "ietf-interfaces:interfaces";
- String uri = createUri(uriPrefix, uriPath);
+ String uri = uriPrefix + uriPath;
when(restconfService.readConfigurationData(uriPath)).thenReturn(null);
get(uri, Draft02.MediaTypes.DATA+JSON);
verify(restconfService, times(1)).readConfigurationData(uriPath);
public void testGetOperationalMediaTypes() throws UnsupportedEncodingException {
String uriPrefix = "/operational/";
String uriPath = "ietf-interfaces:interfaces";
- String uri = createUri(uriPrefix, uriPath);
+ String uri = uriPrefix + uriPath;
when(restconfService.readOperationalData(uriPath)).thenReturn(null);
get(uri, Draft02.MediaTypes.DATA+JSON);
verify(restconfService, times(1)).readOperationalData(uriPath);
public void testPutConfigMediaTypes() throws UnsupportedEncodingException {
String uriPrefix = "/config/";
String uriPath = "ietf-interfaces:interfaces";
- String uri = createUri(uriPrefix, uriPath);
+ String uri = uriPrefix + uriPath;
when(restconfService.updateConfigurationData(eq(uriPath), any(CompositeNode.class))).thenReturn(null);
put(uri, null, Draft02.MediaTypes.DATA+JSON, jsonData);
verify(restconfService, times(1)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
public void testPostConfigWithPathMediaTypes() throws UnsupportedEncodingException {
String uriPrefix = "/config/";
String uriPath = "ietf-interfaces:interfaces";
- String uri = createUri(uriPrefix, uriPath);
+ String uri = uriPrefix + uriPath;
when(restconfService.createConfigurationData(eq(uriPath), any(CompositeNode.class))).thenReturn(null);
post(uri, null, Draft02.MediaTypes.DATA+JSON, jsonData);
verify(restconfService, times(1)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
@Test
public void testPostConfigMediaTypes() throws UnsupportedEncodingException {
String uriPrefix = "/config/";
- String uri = createUri(uriPrefix, "");
+ String uri = uriPrefix;
when(restconfService.createConfigurationData(any(CompositeNode.class))).thenReturn(null);
post(uri, null, Draft02.MediaTypes.DATA+JSON, jsonData);
verify(restconfService, times(1)).createConfigurationData(any(CompositeNode.class));
public void testDeleteConfigMediaTypes() throws UnsupportedEncodingException {
String uriPrefix = "/config/";
String uriPath = "ietf-interfaces:interfaces";
- String uri = createUri(uriPrefix, uriPath);
+ String uri = uriPrefix + uriPath;
when(restconfService.deleteConfigurationData(eq(uriPath))).thenReturn(null);
target(uri).request("fooMediaType").delete();
verify(restconfService, times(1)).deleteConfigurationData(uriPath);
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
@Test
public void deleteConfigStatusCodes() throws UnsupportedEncodingException {
- String uri = createUri("/config/", "test-interface:interfaces");
+ String uri = "/config/test-interface:interfaces";
Future<RpcResult<TransactionStatus>> dummyFuture = createFuture(TransactionStatus.COMMITED);
when(brokerFacade.commitConfigurationDataDelete(any(InstanceIdentifier.class))).thenReturn(dummyFuture);
Response response = target(uri).request(MediaType.APPLICATION_XML).delete();
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
@Test
public void getOperationalStatusCodes() throws UnsupportedEncodingException {
mockReadOperationalDataMethod();
- String uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0");
+ String uri = "/operational/ietf-interfaces:interfaces/interface/eth0";
assertEquals(200, get(uri, MediaType.APPLICATION_XML));
- uri = createUri("/operational/", "wrong-module:interfaces/interface/eth0");
+ uri = "/operational/wrong-module:interfaces/interface/eth0";
assertEquals(400, get(uri, MediaType.APPLICATION_XML));
}
@Test
public void getConfigStatusCodes() throws UnsupportedEncodingException {
mockReadConfigurationDataMethod();
- String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
+ String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
assertEquals(200, get(uri, MediaType.APPLICATION_XML));
- uri = createUri("/config/", "wrong-module:interfaces/interface/eth0");
+ uri = "/config/wrong-module:interfaces/interface/eth0";
assertEquals(400, get(uri, MediaType.APPLICATION_XML));
}
ControllerContext.getInstance().setMountService(mockMountService);
- String uri = createUri("/config/",
- "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont/cont1");
+ String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont/cont1";
assertEquals(200, get(uri, MediaType.APPLICATION_XML));
- uri = createUri("/config/", "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1");
+ uri = "/config/ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
assertEquals(200, get(uri, MediaType.APPLICATION_XML));
}
ControllerContext.getInstance().setMountService(mockMountService);
- String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/yang-ext:mount/");
+ String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/";
assertEquals(200, get(uri, MediaType.APPLICATION_XML));
}
public void getModulesTest() throws UnsupportedEncodingException, FileNotFoundException {
ControllerContext.getInstance().setGlobalSchema(schemaContextModules);
- String uri = createUri("/modules", "");
+ String uri = "/modules";
Response response = target(uri).request("application/yang.api+json").get();
validateModulesResponseJson(response);
public void getModuleTest() throws FileNotFoundException, UnsupportedEncodingException {
ControllerContext.getInstance().setGlobalSchema(schemaContextModules);
- String uri = createUri("/modules/module/module2/2014-01-02", "");
+ String uri = "/modules/module/module2/2014-01-02";
Response response = target(uri).request("application/yang.api+xml").get();
assertEquals(200, response.getStatus());
public void getOperationsTest() throws FileNotFoundException, UnsupportedEncodingException {
ControllerContext.getInstance().setGlobalSchema(schemaContextModules);
- String uri = createUri("/operations", "");
+ String uri = "/operations";
Response response = target(uri).request("application/yang.api+xml").get();
assertEquals(200, response.getStatus());
controllerContext.setMountService(mockMountService);
- String uri = createUri("/operations/", "ietf-interfaces:interfaces/interface/0/yang-ext:mount/");
+ String uri = "/operations/ietf-interfaces:interfaces/interface/0/yang-ext:mount/";
Response response = target(uri).request("application/yang.api+xml").get();
assertEquals(200, response.getStatus());
controllerContext.setMountService(mockMountService);
- String uri = createUri("/modules/", "ietf-interfaces:interfaces/interface/0/yang-ext:mount/");
+ String uri = "/modules/ietf-interfaces:interfaces/interface/0/yang-ext:mount/";
Response response = target(uri).request("application/yang.api+json").get();
assertEquals(200, response.getStatus());
controllerContext.setMountService(mockMountService);
- String uri = createUri("/modules/module/",
- "ietf-interfaces:interfaces/interface/0/yang-ext:mount/module1-behind-mount-point/2014-02-03");
+ String uri = "/modules/module/ietf-interfaces:interfaces/interface/0/yang-ext:mount/module1-behind-mount-point/2014-02-03";
Response response = target(uri).request("application/yang.api+json").get();
assertEquals(200, response.getStatus());
*/
package org.opendaylight.controller.sal.restconf.impl.test;
-import java.io.UnsupportedEncodingException;
-import java.net.URI;
-import java.net.URLEncoder;
-
-import com.google.common.base.Charsets;
-
public class RestOperationUtils {
public static final String JSON = "+json";
private RestOperationUtils() {
}
-
- public static String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
- return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
- }
}
public void postOperationsStatusCodes() throws UnsupportedEncodingException {
controllerContext.setSchemas(schemaContextTestModule);
mockInvokeRpc(cnSnDataOutput, true);
- String uri = createUri("/operations/", "test-module:rpc-test");
+ String uri = "/operations/test-module:rpc-test";
assertEquals(200, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
mockInvokeRpc(null, true);
mockInvokeRpc(null, false);
assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
- uri = createUri("/operations/", "test-module:rpc-wrongtest");
+ uri = "/operations/test-module:rpc-wrongtest";
assertEquals(404, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
}
public void postConfigOnlyStatusCodes() throws UnsupportedEncodingException {
controllerContext.setSchemas(schemaContextYangsIetf);
mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
- String uri = createUri("/config", "");
+ String uri = "/config";
assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
mockCommitConfigurationDataPostMethod(null);
public void postConfigStatusCodes() throws UnsupportedEncodingException {
controllerContext.setSchemas(schemaContextYangsIetf);
mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
- String uri = createUri("/config/", "ietf-interfaces:interfaces");
+ String uri = "/config/ietf-interfaces:interfaces";
assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
mockCommitConfigurationDataPostMethod(null);
ControllerContext.getInstance().setMountService(mockMountService);
- String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/");
+ String uri = "/config/ietf-interfaces:interfaces/interface/0/";
assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
- uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont");
+ uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
}
ArgumentCaptor<InstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(InstanceIdentifier.class);
ArgumentCaptor<CompositeNode> compNodeCaptor = ArgumentCaptor.forClass(CompositeNode.class);
- String URI_1 = createUri("/config", "");
+ String URI_1 = "/config";
assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());
- String URI_2 = createUri("/config/", "test-interface:interfaces");
+ String URI_2 = "/config/test-interface:interfaces";
assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
verify(brokerFacade, times(2))
.commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
.thenReturn(null);
- String URI_1 = createUri("/config", "");
+ String URI_1 = "/config";
assertEquals(202, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
- String URI_2 = createUri("/config/", "test-interface:interfaces");
+ String URI_2 = "/config/test-interface:interfaces";
assertEquals(202, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
}
- private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
- return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
- }
-
private static void initMocking() {
controllerContext = ControllerContext.getInstance();
controllerContext.setSchemas(schemaContext);
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
import java.io.FileNotFoundException;
import java.io.IOException;
*/
@Test
public void putConfigStatusCodes() throws UnsupportedEncodingException {
- String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
+ String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
ControllerContext.getInstance().setMountService(mockMountService);
- String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont");
+ String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
- uri = createUri("/config/", "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont");
+ uri = "/config/ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
}
ControllerContext.getInstance().setMountService(mockMountService);
- String uri = createUri("/config/", "ietf-interfaces:interfaces/yang-ext:mount");
+ String uri = "/config/ietf-interfaces:interfaces/yang-ext:mount";
assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData3));
}
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
-import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
@Test
public void testCallRpcCallGet() throws UnsupportedEncodingException, InterruptedException {
- String uri = createUri("/operations/", "sal-remote:create-data-change-event-subscription");
+ String uri = "/operations/sal-remote:create-data-change-event-subscription";
Response responseWithStreamName = post(uri, MediaType.APPLICATION_XML, getRpcInput());
String xmlResponse = responseWithStreamName.readEntity(String.class);
assertNotNull(xmlResponse);
assertTrue(xmlResponse.contains("<stream-name>ietf-interfaces:interfaces/ietf-interfaces:interface/eth0</stream-name>"));
- uri = createUri("/streams/stream/", "ietf-interfaces:interfaces/ietf-interfaces:interface/eth0");
+ uri = "/streams/stream/ietf-interfaces:interfaces/ietf-interfaces:interface/eth0";
Response responseWithRedirectionUri = get(uri, MediaType.APPLICATION_XML);
final URI websocketServerUri = responseWithRedirectionUri.getLocation();
assertNotNull(websocketServerUri);
import org.opendaylight.controller.netconf.util.xml.XmlElement;
import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
import org.opendaylight.controller.netconf.util.xml.XmlUtil;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.test.impl.rev130403.TestIdentity1;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.test.impl.rev130403.TestIdentity2;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
import org.opendaylight.yangtools.yang.data.impl.codec.IdentityCodec;
import org.opendaylight.yangtools.yang.model.api.Module;
private static void registerAsJMXListener(MBeanServerConnection mBeanServerConnection, ConfigPersisterNotificationListener listener) {
logger.trace("Called registerAsJMXListener");
try {
- mBeanServerConnection.addNotificationListener(DefaultCommitOperationMXBean.objectName, listener, null, null);
+ mBeanServerConnection.addNotificationListener(DefaultCommitOperationMXBean.OBJECT_NAME, listener, null, null);
} catch (InstanceNotFoundException | IOException e) {
throw new RuntimeException("Cannot register as JMX listener to netconf", e);
}
@Override
public synchronized void close() {
// unregister from JMX
- ObjectName on = DefaultCommitOperationMXBean.objectName;
+ ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME;
try {
if (mBeanServerConnection.isRegistered(on)) {
mBeanServerConnection.removeNotificationListener(on, listener);
private final long sessionId;
private boolean up = false;
- protected final Channel channel;
+ private final Channel channel;
protected AbstractNetconfSession(L sessionListener, Channel channel, long sessionId) {
this.sessionListener = sessionListener;
this.channel = channel;
this.sessionId = sessionId;
- logger.debug("Session {} created", toString());
+ logger.debug("Session {} created", sessionId);
}
protected abstract S thisInstance();
package org.opendaylight.controller.netconf.api;
public class NetconfSessionPreferences {
- protected final NetconfMessage helloMessage;
+ private final NetconfMessage helloMessage;
public NetconfSessionPreferences(final NetconfMessage helloMessage) {
this.helloMessage = helloMessage;
private final Element configSnapshot;
- private static final String afterCommitMessageTemplate = "Commit successful: %s";
+ private static final String AFTER_COMMIT_MESSAGE_TEMPLATE = "Commit successful: %s";
private final Set<String> capabilities;
CommitJMXNotification(NotificationBroadcasterSupport source, String message, Element cfgSnapshot,
Set<String> capabilities) {
- super(TransactionProviderJMXNotificationType.commit, source, String.format(afterCommitMessageTemplate, message));
+ super(TransactionProviderJMXNotificationType.commit, source, String.format(AFTER_COMMIT_MESSAGE_TEMPLATE, message));
this.configSnapshot = cfgSnapshot;
this.capabilities = capabilities;
}
public interface DefaultCommitOperationMXBean {
- static String typeName = "NetconfNotificationProvider";
- public static ObjectName objectName = ObjectNameUtil.createONWithDomainAndType(typeName);
+ String TYPE_NAME = "NetconfNotificationProvider";
+ ObjectName OBJECT_NAME = ObjectNameUtil.createONWithDomainAndType(TYPE_NAME);
}
private final MBeanServer mbeanServer;
- private final ObjectName on = DefaultCommitOperationMXBean.objectName;
+ private final ObjectName on = DefaultCommitOperationMXBean.OBJECT_NAME;
public DefaultCommitNotificationProducer(MBeanServer mBeanServer) {
this.mbeanServer = mBeanServer;
logger.debug("Additional header from hello parsed as {} from {}", parsedHeader, additionalHeader);
- return new NetconfServerSession(sessionListener, channel, sessionPreferences.getSessionId(), parsedHeader);
+ return new NetconfServerSession(sessionListener, channel, getSessionPreferences().getSessionId(), parsedHeader);
}
}
import java.net.InetSocketAddress;
import java.util.Dictionary;
import java.util.Hashtable;
+import java.util.concurrent.TimeUnit;
public class NetconfImplActivator implements BundleActivator {
logger.info("Shutting down netconf because YangStoreService service was removed");
commitNot.close();
- eventLoopGroup.shutdownGracefully();
+ eventLoopGroup.shutdownGracefully(0, 1, TimeUnit.SECONDS);
timer.stop();
regMonitoring.unregister();
package org.opendaylight.controller.netconf.impl;
-import org.junit.Before;
-import org.junit.Test;
-import org.opendaylight.controller.netconf.util.messages.NetconfMessageHeader;
-
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
-public class MessageHeaderTest {
-
- private NetconfMessageHeader header = null;
-
- @Before
- public void setUp() {
- this.header = new NetconfMessageHeader();
- }
+import org.junit.Test;
+import org.opendaylight.controller.netconf.util.messages.NetconfMessageHeader;
+public class MessageHeaderTest {
@Test
public void testFromBytes() {
final byte[] raw = new byte[] { (byte) 0x0a, (byte) 0x23, (byte) 0x35, (byte) 0x38, (byte) 0x0a };
- this.header.fromBytes(raw);
- assertEquals(58, this.header.getLength());
+ NetconfMessageHeader header = NetconfMessageHeader.fromBytes(raw);
+ assertEquals(58, header.getLength());
}
@Test
public void testToBytes() {
- this.header.setLength(123);
+ NetconfMessageHeader header = new NetconfMessageHeader(123);
assertArrayEquals(new byte[] { (byte) 0x0a, (byte) 0x23, (byte) 0x31, (byte) 0x32, (byte) 0x33, (byte) 0x0a },
- this.header.toBytes());
+ header.toBytes());
}
}
import org.junit.Before;
import org.junit.Test;
import org.opendaylight.controller.netconf.api.NetconfMessage;
+import org.opendaylight.controller.netconf.util.handler.ChunkedFramingMechanismEncoder;
import org.opendaylight.controller.netconf.util.handler.FramingMechanismHandlerFactory;
-import org.opendaylight.controller.netconf.util.handler.NetconfMessageAggregator;
-import org.opendaylight.controller.netconf.util.handler.NetconfMessageChunkDecoder;
+import org.opendaylight.controller.netconf.util.handler.NetconfChunkAggregator;
+import org.opendaylight.controller.netconf.util.handler.NetconfEOMAggregator;
import org.opendaylight.controller.netconf.util.handler.NetconfMessageToXMLEncoder;
import org.opendaylight.controller.netconf.util.handler.NetconfXMLToMessageDecoder;
import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK),
new NetconfMessageToXMLEncoder(),
- new NetconfMessageAggregator(FramingMechanism.CHUNK), new NetconfMessageChunkDecoder(),
+ new NetconfChunkAggregator(),
new NetconfXMLToMessageDecoder());
testChunkChannel.writeOutbound(this.msg);
enc.encode(null, msg, out);
int msgLength = out.readableBytes();
- int chunkCount = msgLength / NetconfMessageConstants.MAX_CHUNK_SIZE;
- if ((msgLength % NetconfMessageConstants.MAX_CHUNK_SIZE) != 0) {
+ int chunkCount = msgLength / ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
+ if ((msgLength % ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE) != 0) {
chunkCount++;
}
for (int i = 1; i <= chunkCount; i++) {
ByteBuf recievedOutbound = (ByteBuf) messages.poll();
- int exptHeaderLength = NetconfMessageConstants.MAX_CHUNK_SIZE;
+ int exptHeaderLength = ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE;
if (i == chunkCount) {
- exptHeaderLength = msgLength - (NetconfMessageConstants.MAX_CHUNK_SIZE * (i - 1));
- byte[] eom = new byte[NetconfMessageConstants.endOfChunk.length];
- recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.endOfChunk.length,
+ exptHeaderLength = msgLength - (ChunkedFramingMechanismEncoder.DEFAULT_CHUNK_SIZE * (i - 1));
+ byte[] eom = new byte[NetconfMessageConstants.END_OF_CHUNK.length];
+ recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.END_OF_CHUNK.length,
eom);
- assertArrayEquals(NetconfMessageConstants.endOfChunk, eom);
+ assertArrayEquals(NetconfMessageConstants.END_OF_CHUNK, eom);
}
byte[] header = new byte[String.valueOf(exptHeaderLength).length()
+ NetconfMessageConstants.MIN_HEADER_LENGTH - 1];
recievedOutbound.getBytes(0, header);
- NetconfMessageHeader messageHeader = new NetconfMessageHeader();
- messageHeader.fromBytes(header);
+ NetconfMessageHeader messageHeader = NetconfMessageHeader.fromBytes(header);
assertEquals(exptHeaderLength, messageHeader.getLength());
testChunkChannel.writeInbound(recievedOutbound);
public void testEOMFramingMechanismOnPipeline() throws Exception {
EmbeddedChannel testChunkChannel = new EmbeddedChannel(
FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM),
- new NetconfMessageToXMLEncoder(), new NetconfMessageAggregator(
- FramingMechanism.EOM), new NetconfXMLToMessageDecoder());
+ new NetconfMessageToXMLEncoder(), new NetconfEOMAggregator(), new NetconfXMLToMessageDecoder());
testChunkChannel.writeOutbound(this.msg);
ByteBuf recievedOutbound = (ByteBuf) testChunkChannel.readOutbound();
- byte[] eom = new byte[NetconfMessageConstants.endOfMessage.length];
- recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.endOfMessage.length, eom);
- assertArrayEquals(NetconfMessageConstants.endOfMessage, eom);
+ byte[] eom = new byte[NetconfMessageConstants.END_OF_MESSAGE.length];
+ recievedOutbound.getBytes(recievedOutbound.readableBytes() - NetconfMessageConstants.END_OF_MESSAGE.length, eom);
+ assertArrayEquals(NetconfMessageConstants.END_OF_MESSAGE, eom);
testChunkChannel.writeInbound(recievedOutbound);
NetconfMessage receivedMessage = (NetconfMessage) testChunkChannel.readInbound();
*/
package org.opendaylight.controller.netconf.it;
-import com.google.common.collect.Lists;
-import com.google.common.collect.Sets;
+import static junit.framework.Assert.assertEquals;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyLong;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
import io.netty.channel.ChannelFuture;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.InetSocketAddress;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+import javax.management.InstanceNotFoundException;
+import javax.management.Notification;
+import javax.management.NotificationListener;
+
import org.apache.commons.lang3.StringUtils;
import org.junit.After;
import org.junit.Assert;
import org.opendaylight.controller.netconf.persist.impl.ConfigPersisterNotificationHandler;
import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
import org.opendaylight.controller.netconf.util.xml.XmlUtil;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
-import javax.management.InstanceNotFoundException;
-import javax.management.Notification;
-import javax.management.NotificationListener;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.InetSocketAddress;
-import java.util.Collection;
-import java.util.List;
-import java.util.Set;
-import java.util.regex.Pattern;
-
-import static junit.framework.Assert.assertEquals;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyLong;
-import static org.mockito.Mockito.doAnswer;
-import static org.mockito.Mockito.doNothing;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
public class NetconfConfigPersisterITTest extends AbstractNetconfConfigTest {
- private static final Logger logger = LoggerFactory.getLogger(NetconfConfigPersisterITTest.class);
-
private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
private VerifyingNotificationListener createCommitNotificationListener() throws InstanceNotFoundException {
VerifyingNotificationListener listener = new VerifyingNotificationListener();
- platformMBeanServer.addNotificationListener(DefaultCommitNotificationProducer.objectName, listener, null, null);
+ platformMBeanServer.addNotificationListener(DefaultCommitNotificationProducer.OBJECT_NAME, listener, null, null);
return listener;
}
public VerifyingPersister() throws IOException {
Persister mockedAggregator = mock(Persister.class);
- doAnswer(new Answer() {
+ doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
ConfigSnapshotHolder configSnapshot = (ConfigSnapshotHolder) invocation.getArguments()[0];
*/
package org.opendaylight.controller.netconf.it;
-import com.google.common.base.Charsets;
-import com.google.common.base.Optional;
-import com.google.common.collect.Sets;
+import static org.mockito.Matchers.anyLong;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
import io.netty.channel.ChannelFuture;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+
import junit.framework.Assert;
+
import org.junit.Before;
import org.junit.Test;
import org.junit.matchers.JUnitMatchers;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.net.InetSocketAddress;
-import java.net.Socket;
-import java.util.Collection;
-import java.util.List;
-import java.util.Set;
-
-import static org.mockito.Matchers.anyLong;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
+import com.google.common.base.Charsets;
+import com.google.common.base.Optional;
+import com.google.common.collect.Sets;
public class NetconfMonitoringITTest extends AbstractNetconfConfigTest {
}
}
+ sock.close();
+
org.junit.Assert.assertThat(responseBuilder.toString(), JUnitMatchers.containsString("<capability>urn:ietf:params:netconf:capability:candidate:1.0</capability>"));
org.junit.Assert.assertThat(responseBuilder.toString(), JUnitMatchers.containsString("<username>tomas</username>"));
}
import org.opendaylight.controller.netconf.api.NetconfSession;
import org.opendaylight.controller.netconf.util.handler.FramingMechanismHandlerFactory;
+import org.opendaylight.controller.netconf.util.handler.NetconfEOMAggregator;
import org.opendaylight.controller.netconf.util.handler.NetconfHelloMessageToXMLEncoder;
-import org.opendaylight.controller.netconf.util.handler.NetconfMessageAggregator;
import org.opendaylight.controller.netconf.util.handler.NetconfXMLToHelloMessageDecoder;
import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
public static final String NETCONF_SESSION_NEGOTIATOR = "negotiator";
public void initialize(SocketChannel ch, Promise<S> promise) {
- ch.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfMessageAggregator(FramingMechanism.EOM));
+ ch.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfEOMAggregator());
initializeMessageDecoder(ch);
ch.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER, FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM));
initializeMessageEncoder(ch);
package org.opendaylight.controller.netconf.util;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelHandler;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.ssl.SslHandler;
+import io.netty.util.Timeout;
+import io.netty.util.Timer;
+import io.netty.util.TimerTask;
+import io.netty.util.concurrent.Future;
+import io.netty.util.concurrent.GenericFutureListener;
+import io.netty.util.concurrent.Promise;
+
import java.util.concurrent.TimeUnit;
+import io.netty.channel.ChannelInboundHandlerAdapter;
import org.opendaylight.controller.netconf.api.AbstractNetconfSession;
import org.opendaylight.controller.netconf.api.NetconfMessage;
import org.opendaylight.controller.netconf.api.NetconfSessionListener;
import org.opendaylight.controller.netconf.api.NetconfSessionPreferences;
import org.opendaylight.controller.netconf.util.handler.FramingMechanismHandlerFactory;
-import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
-import org.opendaylight.controller.netconf.util.handler.NetconfMessageAggregator;
-import org.opendaylight.controller.netconf.util.handler.NetconfMessageChunkDecoder;
+import org.opendaylight.controller.netconf.util.handler.NetconfChunkAggregator;
import org.opendaylight.controller.netconf.util.handler.NetconfMessageToXMLEncoder;
import org.opendaylight.controller.netconf.util.handler.NetconfXMLToMessageDecoder;
import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
+import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
import org.opendaylight.controller.netconf.util.xml.XmlUtil;
import org.opendaylight.protocol.framework.AbstractSessionNegotiator;
import org.slf4j.Logger;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
-import io.netty.channel.Channel;
-import io.netty.channel.ChannelHandler;
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.handler.ssl.SslHandler;
-import io.netty.util.Timeout;
-import io.netty.util.Timer;
-import io.netty.util.TimerTask;
-import io.netty.util.concurrent.Future;
-import io.netty.util.concurrent.GenericFutureListener;
-import io.netty.util.concurrent.Promise;
-
public abstract class AbstractNetconfSessionNegotiator<P extends NetconfSessionPreferences, S extends AbstractNetconfSession<S, L>, L extends NetconfSessionListener<S>>
extends AbstractSessionNegotiator<NetconfHelloMessage, S> {
private static final Logger logger = LoggerFactory.getLogger(AbstractNetconfSessionNegotiator.class);
public static final String NAME_OF_EXCEPTION_HANDLER = "lastExceptionHandler";
- public static final String CHUNK_DECODER_CHANNEL_HANDLER_KEY = "chunkDecoder";
- protected final P sessionPreferences;
+ private final P sessionPreferences;
private final L sessionListener;
private Timeout timeout;
Future<Channel> future = sslHandler.get().handshakeFuture();
future.addListener(new GenericFutureListener<Future<? super Channel>>() {
@Override
- public void operationComplete(Future<? super Channel> future) throws Exception {
+ public void operationComplete(Future<? super Channel> future) {
Preconditions.checkState(future.isSuccess(), "Ssl handshake was not successful");
logger.debug("Ssl handshake complete");
start();
}
});
- } else
+ } else {
start();
+ }
}
private static Optional<SslHandler> getSslHandler(Channel channel) {
return sslHandler == null ? Optional.<SslHandler> absent() : Optional.of(sslHandler);
}
+ public P getSessionPreferences() {
+ return sessionPreferences;
+ }
+
private void start() {
final NetconfMessage helloMessage = this.sessionPreferences.getHelloMessage();
logger.debug("Session negotiation started with hello message {}", XmlUtil.toString(helloMessage.getDocument()));
- channel.pipeline().addLast(NAME_OF_EXCEPTION_HANDLER, new ChannelHandler() {
- @Override
- public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
- }
-
- @Override
- public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
- }
-
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
- logger.warn("An exception occurred during negotiation on channel {}", channel.localAddress(), cause);
- cancelTimeout();
- negotiationFailed(cause);
- changeState(State.FAILED);
- }
- });
+ channel.pipeline().addLast(NAME_OF_EXCEPTION_HANDLER, new ExceptionHandlingInboundChannelHandler());
timeout = this.timer.newTimeout(new TimerTask() {
@Override
- public void run(final Timeout timeout) throws Exception {
+ public void run(final Timeout timeout) {
synchronized (this) {
if (state != State.ESTABLISHED) {
logger.debug("Connection timeout after {}, session is in state {}", timeout, state);
}
}, connectionTimeoutMillis, TimeUnit.MILLISECONDS);
- sendMessage(helloMessage);
+ // FIXME, make sessionPreferences return HelloMessage, move NetconfHelloMessage to API
+ sendMessage((NetconfHelloMessage)helloMessage);
changeState(State.OPEN_WAIT);
}
private void cancelTimeout() {
- if(timeout!=null)
+ if(timeout!=null) {
timeout.cancel();
- }
-
- private void sendMessage(NetconfMessage message) {
- this.channel.writeAndFlush(message);
+ }
}
@Override
protected void handleMessage(NetconfHelloMessage netconfMessage) {
- final Document doc = netconfMessage.getDocument();
+ Preconditions.checkNotNull(netconfMessage != null, "netconfMessage");
- // Only Hello message should arrive during negotiation
- if (netconfMessage instanceof NetconfHelloMessage) {
+ final Document doc = netconfMessage.getDocument();
- replaceHelloMessageHandlers();
+ replaceHelloMessageHandlers();
- if (shouldUseChunkFraming(doc)) {
- insertChunkFramingToPipeline();
- }
+ if (shouldUseChunkFraming(doc)) {
+ insertChunkFramingToPipeline();
+ }
- changeState(State.ESTABLISHED);
- S session = getSession(sessionListener, channel, (NetconfHelloMessage)netconfMessage);
+ changeState(State.ESTABLISHED);
+ S session = getSession(sessionListener, channel, netconfMessage);
- negotiationSuccessful(session);
- } else {
- final IllegalStateException cause = new IllegalStateException(
- "Received message was not hello as expected, but was " + XmlUtil.toString(doc));
- logger.warn("Negotiation of netconf session failed", cause);
- negotiationFailed(cause);
- }
+ negotiationSuccessful(session);
}
/**
replaceChannelHandler(channel, AbstractChannelInitializer.NETCONF_MESSAGE_FRAME_ENCODER,
FramingMechanismHandlerFactory.createHandler(FramingMechanism.CHUNK));
replaceChannelHandler(channel, AbstractChannelInitializer.NETCONF_MESSAGE_AGGREGATOR,
- new NetconfMessageAggregator(FramingMechanism.CHUNK));
- channel.pipeline().addAfter(AbstractChannelInitializer.NETCONF_MESSAGE_AGGREGATOR,
- CHUNK_DECODER_CHANNEL_HANDLER_KEY, new NetconfMessageChunkDecoder());
+ new NetconfChunkAggregator());
}
private boolean shouldUseChunkFraming(Document doc) {
}
private static boolean isStateChangePermitted(State state, State newState) {
- if (state == State.IDLE && newState == State.OPEN_WAIT)
+ if (state == State.IDLE && newState == State.OPEN_WAIT) {
return true;
- if (state == State.OPEN_WAIT && newState == State.ESTABLISHED)
+ }
+ if (state == State.OPEN_WAIT && newState == State.ESTABLISHED) {
return true;
- if (state == State.OPEN_WAIT && newState == State.FAILED)
+ }
+ if (state == State.OPEN_WAIT && newState == State.FAILED) {
return true;
+ }
logger.debug("Transition from {} to {} is not allowed", state, newState);
return false;
}
+
+ /**
+ * Handler to catch exceptions in pipeline during negotiation
+ */
+ private final class ExceptionHandlingInboundChannelHandler extends ChannelInboundHandlerAdapter {
+
+ @Override
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
+ logger.warn("An exception occurred during negotiation on channel {}", channel.localAddress(), cause);
+ cancelTimeout();
+ negotiationFailed(cause);
+ changeState(State.FAILED);
+ }
+ }
}
import java.io.IOException;
import java.io.InputStream;
-public class NetconfUtil {
+public final class NetconfUtil {
private static final Logger logger = LoggerFactory.getLogger(NetconfUtil.class);
+ private NetconfUtil() {}
+
public static NetconfMessage createMessage(final File f) {
Preconditions.checkNotNull(f, "File parameter was null");
try {
package org.opendaylight.controller.netconf.util.handler;
-import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
-import org.opendaylight.controller.netconf.util.messages.NetconfMessageHeader;
-
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
+import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
+import org.opendaylight.controller.netconf.util.messages.NetconfMessageHeader;
+
+import com.google.common.base.Preconditions;
+
public class ChunkedFramingMechanismEncoder extends MessageToByteEncoder<ByteBuf> {
+ public static final int DEFAULT_CHUNK_SIZE = 8192;
+ public static final int MIN_CHUNK_SIZE = 128;
+ public static final int MAX_CHUNK_SIZE = 16 * 1024 * 1024;
- private NetconfMessageHeader messageHeader = new NetconfMessageHeader();
+ private final int chunkSize;
- private final static int MAX_CHUNK_SIZE = NetconfMessageConstants.MAX_CHUNK_SIZE;
+ public ChunkedFramingMechanismEncoder() {
+ this(DEFAULT_CHUNK_SIZE);
+ }
+
+ public ChunkedFramingMechanismEncoder(int chunkSize) {
+ Preconditions.checkArgument(chunkSize > MIN_CHUNK_SIZE);
+ Preconditions.checkArgument(chunkSize < MAX_CHUNK_SIZE);
+ this.chunkSize = chunkSize;
+ }
+
+ public final int getChunkSize() {
+ return chunkSize;
+ }
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
- while (msg.readableBytes() > MAX_CHUNK_SIZE) {
- ByteBuf chunk = Unpooled.buffer(MAX_CHUNK_SIZE);
- chunk.writeBytes(createChunkHeader(MAX_CHUNK_SIZE));
- chunk.writeBytes(msg.readBytes(MAX_CHUNK_SIZE));
+ while (msg.readableBytes() > chunkSize) {
+ ByteBuf chunk = Unpooled.buffer(chunkSize);
+ chunk.writeBytes(createChunkHeader(chunkSize));
+ chunk.writeBytes(msg.readBytes(chunkSize));
ctx.write(chunk);
}
out.writeBytes(createChunkHeader(msg.readableBytes()));
out.writeBytes(msg.readBytes(msg.readableBytes()));
- out.writeBytes(NetconfMessageConstants.endOfChunk);
+ out.writeBytes(NetconfMessageConstants.END_OF_CHUNK);
}
private ByteBuf createChunkHeader(int chunkSize) {
- messageHeader.setLength(chunkSize);
- return Unpooled.wrappedBuffer(messageHeader.toBytes());
+ return Unpooled.wrappedBuffer(NetconfMessageHeader.toBytes(chunkSize));
}
-
}
package org.opendaylight.controller.netconf.util.handler;
-import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
-
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
-public class EOMFramingMechanismEncoder extends MessageToByteEncoder<ByteBuf> {
-
- private byte[] eom = NetconfMessageConstants.endOfMessage;
+import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
+public class EOMFramingMechanismEncoder extends MessageToByteEncoder<ByteBuf> {
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
out.writeBytes(msg);
- out.writeBytes(eom);
+ out.writeBytes(NetconfMessageConstants.END_OF_MESSAGE);
}
-
}
public class FramingMechanismHandlerFactory {
- private final static Logger logger = LoggerFactory.getLogger(FramingMechanismHandlerFactory.class);
+ private static final Logger logger = LoggerFactory.getLogger(FramingMechanismHandlerFactory.class);
+
+ private FramingMechanismHandlerFactory() {}
public static MessageToByteEncoder<ByteBuf> createHandler(FramingMechanism framingMechanism) {
logger.debug("{} framing mechanism was selected.", framingMechanism);
--- /dev/null
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.netconf.util.handler;
+
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.CompositeByteBuf;
+import io.netty.buffer.Unpooled;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+
+public class NetconfChunkAggregator extends ByteToMessageDecoder {
+ private final static Logger logger = LoggerFactory.getLogger(NetconfChunkAggregator.class);
+ public static final int DEFAULT_MAXIMUM_CHUNK_SIZE = 16 * 1024 * 1024;
+
+ private static enum State {
+ HEADER_ONE, // \n
+ HEADER_TWO, // #
+ HEADER_LENGTH_FIRST, // [1-9]
+ HEADER_LENGTH_OTHER, // [0-9]*\n
+ DATA,
+ FOOTER_ONE, // \n
+ FOOTER_TWO, // #
+ FOOTER_THREE, // #
+ FOOTER_FOUR, // \n
+ }
+
+ private final int maxChunkSize = DEFAULT_MAXIMUM_CHUNK_SIZE;
+ private State state = State.HEADER_ONE;
+ private long chunkSize;
+ private CompositeByteBuf chunk;
+
+ @Override
+ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
+ while (in.isReadable()) {
+ switch (state) {
+ case HEADER_ONE:
+ {
+ final byte b = in.readByte();
+ if (b != '\n') {
+ logger.debug("Got byte {} while waiting for {}", b, (byte)'\n');
+ throw new IllegalStateException("Malformed chunk header encountered (byte 0)");
+ }
+
+ state = State.HEADER_TWO;
+
+ initChunk();
+ break;
+ }
+ case HEADER_TWO:
+ {
+ final byte b = in.readByte();
+ if (b != '#') {
+ logger.debug("Got byte {} while waiting for {}", b, (byte)'#');
+ throw new IllegalStateException("Malformed chunk header encountered (byte 1)");
+ }
+
+ state = State.HEADER_LENGTH_FIRST;
+ break;
+ }
+ case HEADER_LENGTH_FIRST:
+ {
+ final byte b = in.readByte();
+ chunkSize = processHeaderLengthFirst(b);
+ state = State.HEADER_LENGTH_OTHER;
+ break;
+ }
+ case HEADER_LENGTH_OTHER:
+ {
+ final byte b = in.readByte();
+ if (b == '\n') {
+ state = State.DATA;
+ break;
+ }
+
+ if (b < '0' || b > '9') {
+ logger.debug("Got byte {} while waiting for {}-{}", b, (byte)'0', (byte)'9');
+ throw new IllegalStateException("Invalid chunk size encountered");
+ }
+
+ chunkSize *= 10;
+ chunkSize += b - '0';
+
+ if (chunkSize > maxChunkSize) {
+ logger.debug("Parsed chunk size {}, maximum allowed is {}", chunkSize, maxChunkSize);
+ throw new IllegalStateException("Maximum chunk size exceeded");
+ }
+ break;
+ }
+ case DATA:
+ /*
+ * FIXME: this gathers all data into one big chunk before passing
+ * it on. Make sure the pipeline can work with partial data
+ * and then change this piece to pass the data on as it
+ * comes through.
+ */
+ if (in.readableBytes() < chunkSize) {
+ logger.debug("Buffer has {} bytes, need {} to complete chunk", in.readableBytes(), chunkSize);
+ in.discardReadBytes();
+ return;
+ }
+
+ aggregateChunks(in.readBytes((int) chunkSize));
+ state = State.FOOTER_ONE;
+ break;
+ case FOOTER_ONE:
+ {
+ final byte b = in.readByte();
+ if (b != '\n') {
+ logger.debug("Got byte {} while waiting for {}", b, (byte)'\n');
+ throw new IllegalStateException("Malformed chunk footer encountered (byte 0)");
+ }
+
+ state = State.FOOTER_TWO;
+ chunkSize = 0;
+ break;
+ }
+ case FOOTER_TWO:
+ {
+ final byte b = in.readByte();
+
+ if (b != '#') {
+ logger.debug("Got byte {} while waiting for {}", b, (byte)'#');
+ throw new IllegalStateException("Malformed chunk footer encountered (byte 1)");
+ }
+
+ state = State.FOOTER_THREE;
+ break;
+ }
+ case FOOTER_THREE:
+ {
+ final byte b = in.readByte();
+
+ // In this state, either header-of-new-chunk or message-end is expected
+ // Depends on the next character
+
+ if (isHeaderLengthFirst(b)) {
+ // Extract header length#1 from new chunk
+ chunkSize = processHeaderLengthFirst(b);
+ // Proceed with next chunk processing
+ state = State.HEADER_LENGTH_OTHER;
+ } else if (b == '#') {
+ state = State.FOOTER_FOUR;
+ } else {
+ logger.debug("Got byte {} while waiting for {} or {}-{}", b, (byte) '#', (byte) '1', (byte) '9');
+ throw new IllegalStateException("Malformed chunk footer encountered (byte 2)");
+ }
+
+ break;
+ }
+ case FOOTER_FOUR:
+ {
+ final byte b = in.readByte();
+ if (b != '\n') {
+ logger.debug("Got byte {} while waiting for {}", b, (byte)'\n');
+ throw new IllegalStateException("Malformed chunk footer encountered (byte 3)");
+ }
+
+ state = State.HEADER_ONE;
+ out.add(chunk);
+ chunk = null;
+ break;
+ }
+ }
+ }
+
+ in.discardReadBytes();
+ }
+
+ private void initChunk() {
+ chunk = Unpooled.compositeBuffer();
+ }
+
+ private void aggregateChunks(ByteBuf newChunk) {
+ chunk.addComponent(chunk.numComponents(), newChunk);
+
+ // Update writer index, addComponent does not update it
+ chunk.writerIndex(chunk.writerIndex() + newChunk.readableBytes());
+ }
+
+ private static int processHeaderLengthFirst(byte b) {
+ if (isHeaderLengthFirst(b) == false) {
+ logger.debug("Got byte {} while waiting for {}-{}", b, (byte)'1', (byte)'9');
+ throw new IllegalStateException("Invalid chunk size encountered (byte 0)");
+ }
+
+ return b - '0';
+ }
+
+ private static boolean isHeaderLengthFirst(byte b) {
+ return b >= '1' && b <= '9';
+ }
+}
package org.opendaylight.controller.netconf.util.handler;
-import com.google.common.base.Charsets;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
-import org.opendaylight.controller.netconf.util.messages.FramingMechanism;
-import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import java.util.List;
-public class NetconfMessageAggregator extends ByteToMessageDecoder {
-
- private final static Logger logger = LoggerFactory.getLogger(NetconfMessageAggregator.class);
+import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
- private byte[] eom = NetconfMessageConstants.endOfMessage;
+import com.google.common.base.Charsets;
- public NetconfMessageAggregator(FramingMechanism framingMechanism) {
- if (framingMechanism == FramingMechanism.CHUNK) {
- eom = NetconfMessageConstants.endOfChunk;
- }
- }
+public class NetconfEOMAggregator extends ByteToMessageDecoder {
+ private final static Logger logger = LoggerFactory.getLogger(NetconfEOMAggregator.class);
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
- int index = indexOfSequence(in, eom);
+ int index = indexOfSequence(in, NetconfMessageConstants.END_OF_MESSAGE);
if (index == -1) {
logger.debug("Message is not complete, read again.");
if (logger.isTraceEnabled()) {
ctx.read();
} else {
ByteBuf msg = in.readBytes(index);
- in.readBytes(eom.length);
+ in.readBytes(NetconfMessageConstants.END_OF_MESSAGE.length);
in.discardReadBytes();
logger.debug("Message is complete.");
out.add(msg);
*/
package org.opendaylight.controller.netconf.util.handler;
-import java.nio.ByteBuffer;
+import io.netty.buffer.ByteBuf;
+import io.netty.channel.ChannelHandlerContext;
+
+import java.io.IOException;
+
+import javax.xml.transform.TransformerException;
import org.opendaylight.controller.netconf.api.NetconfMessage;
import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
+import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
* </pre>
*/
public final class NetconfHelloMessageToXMLEncoder extends NetconfMessageToXMLEncoder {
-
@Override
- protected ByteBuffer encodeMessage(NetconfMessage msg) {
+ @VisibleForTesting
+ public void encode(ChannelHandlerContext ctx, NetconfMessage msg, ByteBuf out) throws IOException, TransformerException {
Preconditions.checkState(msg instanceof NetconfHelloMessage, "Netconf message of type %s expected, was %s",
NetconfHelloMessage.class, msg.getClass());
Optional<NetconfHelloMessageAdditionalHeader> headerOptional = ((NetconfHelloMessage) msg)
// If additional header present, serialize it along with netconf hello
// message
if (headerOptional.isPresent()) {
- byte[] bytesFromHeader = headerOptional.get().toFormattedString().getBytes(Charsets.UTF_8);
- byte[] bytesFromMessage = xmlToString(msg.getDocument()).getBytes(Charsets.UTF_8);
-
- ByteBuffer byteBuffer = ByteBuffer.allocate(bytesFromHeader.length + bytesFromMessage.length)
- .put(bytesFromHeader).put(bytesFromMessage);
- byteBuffer.flip();
- return byteBuffer;
+ out.writeBytes(headerOptional.get().toFormattedString().getBytes(Charsets.UTF_8));
}
- return super.encodeMessage(msg);
+ super.encode(ctx, msg, out);
}
}
+++ /dev/null
-/*
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.controller.netconf.util.handler;
-
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.Unpooled;
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.handler.codec.ByteToMessageDecoder;
-
-import java.nio.charset.Charset;
-import java.util.List;
-
-import org.opendaylight.controller.netconf.api.NetconfDeserializerException;
-import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class NetconfMessageChunkDecoder extends ByteToMessageDecoder {
-
- private final static Logger logger = LoggerFactory.getLogger(NetconfMessageChunkDecoder.class);
-
- @Override
- protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
- ByteBuf byteBufMsg = Unpooled.buffer(in.readableBytes());
- int chunkSize = -1;
- boolean isParsed = false;
- while (in.isReadable()) {
- try {
- if (!isParsed) {
- chunkSize = readHeader(in);
- isParsed = true;
- }
- if (chunkSize != -1 && isParsed) {
- in.readBytes(byteBufMsg, chunkSize);
- isParsed = false;
- } else {
- throw new NetconfDeserializerException("Unable to parse chunked data or header.");
- }
- } catch (Exception e) {
- logger.error("Failed to decode chunked message.", e);
- this.exceptionCaught(ctx, e);
- }
- }
- out.add(byteBufMsg);
- isParsed = false;
- }
-
- private int readHeader(ByteBuf in) {
- ByteBuf chunkSize = Unpooled.buffer(NetconfMessageConstants.MIN_HEADER_LENGTH,
- NetconfMessageConstants.MAX_HEADER_LENGTH);
- byte b = in.readByte();
- if (b != 10)
- return -1;
- b = in.readByte();
- if (b != 35)
- return -1;
- while ((b = in.readByte()) != 10) {
- chunkSize.writeByte(b);
- }
- return Integer.parseInt(chunkSize.toString(Charset.forName("UTF-8")));
- }
-
-}
package org.opendaylight.controller.netconf.util.handler;
import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufOutputStream;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
-import java.nio.ByteBuffer;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
import org.opendaylight.controller.netconf.api.NetconfMessage;
-import org.opendaylight.controller.netconf.util.xml.XmlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Comment;
-import org.w3c.dom.Document;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Charsets;
import com.google.common.base.Optional;
public class NetconfMessageToXMLEncoder extends MessageToByteEncoder<NetconfMessage> {
private static final Logger LOG = LoggerFactory.getLogger(NetconfMessageToXMLEncoder.class);
+ private static final TransformerFactory FACTORY = TransformerFactory.newInstance();
private final Optional<String> clientId;
@Override
@VisibleForTesting
- public void encode(ChannelHandlerContext ctx, NetconfMessage msg, ByteBuf out) throws Exception {
+ public void encode(ChannelHandlerContext ctx, NetconfMessage msg, ByteBuf out) throws IOException, TransformerException {
LOG.debug("Sent to encode : {}", msg);
if (clientId.isPresent()) {
msg.getDocument().appendChild(comment);
}
- final ByteBuffer msgBytes = encodeMessage(msg);
-
- LOG.trace("Putting message \n{}", xmlToString(msg.getDocument()));
- out.writeBytes(msgBytes);
- }
-
- protected ByteBuffer encodeMessage(NetconfMessage msg) {
- return Charsets.UTF_8.encode(xmlToString(msg.getDocument()));
- }
+ try (OutputStream os = new ByteBufOutputStream(out)) {
+ Transformer transformer = FACTORY.newTransformer();
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes");
+ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
- protected String xmlToString(Document doc) {
- return XmlUtil.toString(doc, false);
+ StreamResult result = new StreamResult(new OutputStreamWriter(os));
+ DOMSource source = new DOMSource(msg.getDocument());
+ transformer.transform(source, result);
+ }
}
}
*/
package org.opendaylight.controller.netconf.util.handler;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+
+import java.io.ByteArrayInputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import org.opendaylight.controller.netconf.api.NetconfMessage;
import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
+import org.opendaylight.controller.netconf.util.xml.XmlUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
+import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
* {@link org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage}
* . Used by netconf server to retrieve information about session metadata.
*/
-public class NetconfXMLToHelloMessageDecoder extends NetconfXMLToMessageDecoder {
+public final class NetconfXMLToHelloMessageDecoder extends ByteToMessageDecoder {
+ private static final Logger LOG = LoggerFactory.getLogger(NetconfXMLToHelloMessageDecoder.class);
private static final List<byte[]> POSSIBLE_ENDS = ImmutableList.of(
new byte[] { ']', '\n' },
new byte[] { '\r', '\n', '[' },
new byte[] { '\n', '[' });
- private String additionalHeaderCache;
-
@Override
- protected byte[] preprocessMessageBytes(byte[] bytes) {
- // Extract bytes containing header with additional metadata
-
- if (startsWithAdditionalHeader(bytes)) {
- // Auth information containing username, ip address... extracted for monitoring
- int endOfAuthHeader = getAdditionalHeaderEndIndex(bytes);
- if (endOfAuthHeader > -1) {
- byte[] additionalHeaderBytes = Arrays.copyOfRange(bytes, 0, endOfAuthHeader + 2);
- additionalHeaderCache = additionalHeaderToString(additionalHeaderBytes);
- bytes = Arrays.copyOfRange(bytes, endOfAuthHeader + 2, bytes.length);
- }
+ @VisibleForTesting
+ public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
+ if (in.readableBytes() == 0) {
+ LOG.debug("No more content in incoming buffer.");
+ return;
}
- return bytes;
- }
+ in.markReaderIndex();
+ try {
+ LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
+ byte[] bytes = new byte[in.readableBytes()];
+ in.readBytes(bytes);
+
+ logMessage(bytes);
+
+ // Extract bytes containing header with additional metadata
+ String additionalHeader = null;
+ if (startsWithAdditionalHeader(bytes)) {
+ // Auth information containing username, ip address... extracted for monitoring
+ int endOfAuthHeader = getAdditionalHeaderEndIndex(bytes);
+ if (endOfAuthHeader > -1) {
+ byte[] additionalHeaderBytes = Arrays.copyOfRange(bytes, 0, endOfAuthHeader + 2);
+ additionalHeader = additionalHeaderToString(additionalHeaderBytes);
+ bytes = Arrays.copyOfRange(bytes, endOfAuthHeader + 2, bytes.length);
+ }
+ }
- @Override
- protected void cleanUpAfterDecode() {
- additionalHeaderCache = null;
- }
+ Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes));
- @Override
- protected NetconfMessage buildNetconfMessage(Document doc) {
- return new NetconfHelloMessage(doc, additionalHeaderCache == null ? null
- : NetconfHelloMessageAdditionalHeader.fromString(additionalHeaderCache));
+ final NetconfMessage message;
+ if (additionalHeader != null) {
+ message = new NetconfHelloMessage(doc, NetconfHelloMessageAdditionalHeader.fromString(additionalHeader));
+ } else {
+ message = new NetconfHelloMessage(doc);
+ }
+ out.add(message);
+ } finally {
+ in.discardReadBytes();
+ }
}
private int getAdditionalHeaderEndIndex(byte[] bytes) {
return -1;
}
+
+ private void logMessage(byte[] bytes) {
+ String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
+ LOG.debug("Parsing message \n{}", s);
+ }
+
private boolean startsWithAdditionalHeader(byte[] bytes) {
for (byte[] possibleStart : POSSIBLE_STARTS) {
int i = 0;
for (byte b : possibleStart) {
- if(bytes[i++] != b)
+ if(bytes[i++] != b) {
break;
+ }
- if(i == possibleStart.length)
+ if(i == possibleStart.length) {
return true;
+ }
}
}
*/
package org.opendaylight.controller.netconf.util.handler;
-import java.io.ByteArrayInputStream;
-import java.nio.ByteBuffer;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufInputStream;
+import io.netty.buffer.ByteBufUtil;
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.handler.codec.ByteToMessageDecoder;
+
import java.util.List;
-import org.opendaylight.controller.netconf.api.NetconfDeserializerException;
import org.opendaylight.controller.netconf.api.NetconfMessage;
import org.opendaylight.controller.netconf.util.xml.XmlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.w3c.dom.Document;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Charsets;
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.ByteBufUtil;
-import io.netty.channel.ChannelHandlerContext;
-import io.netty.handler.codec.ByteToMessageDecoder;
-
-public class NetconfXMLToMessageDecoder extends ByteToMessageDecoder {
+public final class NetconfXMLToMessageDecoder extends ByteToMessageDecoder {
private static final Logger LOG = LoggerFactory.getLogger(NetconfXMLToMessageDecoder.class);
@Override
@VisibleForTesting
public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
- if (in.readableBytes() == 0) {
- LOG.debug("No more content in incoming buffer.");
- return;
- }
-
- in.markReaderIndex();
- try {
+ if (in.readableBytes() != 0) {
LOG.trace("Received to decode: {}", ByteBufUtil.hexDump(in));
- byte[] bytes = new byte[in.readableBytes()];
- in.readBytes(bytes);
-
- logMessage(bytes);
-
- bytes = preprocessMessageBytes(bytes);
- NetconfMessage message;
- try {
- Document doc = XmlUtil.readXmlToDocument(new ByteArrayInputStream(bytes));
- message = buildNetconfMessage(doc);
- } catch (Exception e) {
- throw new NetconfDeserializerException("Could not parse message from " + new String(bytes), e);
- }
-
- out.add(message);
- } finally {
- in.discardReadBytes();
- cleanUpAfterDecode();
+ out.add(new NetconfMessage(XmlUtil.readXmlToDocument(new ByteBufInputStream(in))));
+ } else {
+ LOG.debug("No more content in incoming buffer.");
}
}
-
- protected void cleanUpAfterDecode() {}
-
- protected NetconfMessage buildNetconfMessage(Document doc) {
- return new NetconfMessage(doc);
- }
-
- protected byte[] preprocessMessageBytes(byte[] bytes) {
- return bytes;
- }
-
- private void logMessage(byte[] bytes) {
- String s = Charsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
- LOG.debug("Parsing message \n{}", s);
- }
-
}
* stops at instance of this class. All downstream events are handed of to wrapped {@link org.opendaylight.controller.netconf.util.handler.ssh.client.SshClientAdapter};
*/
public class SshHandler extends ChannelOutboundHandlerAdapter {
+ private static final String SOCKET = "socket";
+
private final VirtualSocket virtualSocket = new VirtualSocket();
private final SshClientAdapter sshClientAdapter;
@Override
public void handlerAdded(ChannelHandlerContext ctx){
- if (ctx.channel().pipeline().get("socket") == null) {
- ctx.channel().pipeline().addFirst("socket", virtualSocket);
+ if (ctx.channel().pipeline().get(SOCKET) == null) {
+ ctx.channel().pipeline().addFirst(SOCKET, virtualSocket);
}
}
@Override
- public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
- if (ctx.channel().pipeline().get("socket") != null) {
- ctx.channel().pipeline().remove("socket");
+ public void handlerRemoved(ChannelHandlerContext ctx) {
+ if (ctx.channel().pipeline().get(SOCKET) != null) {
+ ctx.channel().pipeline().remove(SOCKET);
}
}
@Override
- public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
+ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws IOException {
this.sshClientAdapter.write((ByteBuf) msg);
}
public void connect(final ChannelHandlerContext ctx,
SocketAddress remoteAddress,
SocketAddress localAddress,
- ChannelPromise promise) throws Exception {
+ ChannelPromise promise) {
ctx.connect(remoteAddress, localAddress, promise);
promise.addListener(new ChannelFutureListener() {
- public void operationComplete(ChannelFuture channelFuture) throws Exception {
+ public void operationComplete(ChannelFuture channelFuture) {
sshClientAdapter.start(ctx);
}}
);
}
@Override
- public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
+ public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) {
sshClientAdapter.stop(promise);
}
}
public void authenticate(Connection connection) throws IOException {
boolean isAuthenticated = connection.authenticateWithPassword(username, password);
- if (isAuthenticated == false)
+ if (isAuthenticated == false) {
throw new IOException("Authentication failed.");
+ }
}
}
private Invoker(){}
protected boolean isInvoked() {
+ // TODO invoked is always false
return invoked;
}
return new Invoker() {
@Override
void invoke(SshSession session) throws IOException {
- if (isInvoked() == true) throw new IllegalStateException("Already invoked.");
+ if (isInvoked()) {
+ throw new IllegalStateException("Already invoked.");
+ }
session.startSubSystem(subsystem);
}
*/
public class SshClient {
private final VirtualSocket socket;
- private final Map<Integer, SshSession> openSessions = new HashMap();
+ private final Map<Integer, SshSession> openSessions = new HashMap<>();
private final AuthenticationHandler authenticationHandler;
private Connection connection;
}
public SshSession openSession() throws IOException {
- if (connection == null)
+ if (connection == null) {
connect();
+ }
Session session = connection.openSession();
SshSession sshSession = new SshSession(session);
public void closeSession(SshSession session) {
if (session.getState() == Channel.STATE_OPEN || session.getState() == Channel.STATE_OPENING) {
- session.session.close();
+ session.close();
}
}
openSessions.clear();
- if (connection != null)
+ if (connection != null) {
connection.close();
+ }
}
}
* pipeline.
*/
public class SshClientAdapter implements Runnable {
+ private static final int BUFFER_SIZE = 1024;
+
private final SshClient sshClient;
private final Invoker invoker;
- private SshSession session;
- private InputStream stdOut;
- private InputStream stdErr;
private OutputStream stdIn;
- private Queue<ByteBuf> postponned = new LinkedList<>();
-
+ private Queue<ByteBuf> postponed = new LinkedList<>();
private ChannelHandlerContext ctx;
private ChannelPromise disconnectPromise;
public void run() {
try {
- session = sshClient.openSession();
+ SshSession session = sshClient.openSession();
invoker.invoke(session);
- stdOut = session.getStdout();
- stdErr = session.getStderr();
+ InputStream stdOut = session.getStdout();
+ session.getStderr();
synchronized (lock) {
stdIn = session.getStdin();
- ByteBuf message = null;
- while ((message = postponned.poll()) != null) {
+ ByteBuf message;
+ while ((message = postponed.poll()) != null) {
writeImpl(message);
}
}
while (stopRequested.get() == false) {
- byte[] readBuff = new byte[1024];
+ byte[] readBuff = new byte[BUFFER_SIZE];
int c = stdOut.read(readBuff);
if (c == -1) {
continue;
sshClient.close();
synchronized (lock) {
- if (disconnectPromise != null)
+ if (disconnectPromise != null) {
ctx.disconnect(disconnectPromise);
+ }
}
}
}
public void write(ByteBuf message) throws IOException {
synchronized (lock) {
if (stdIn == null) {
- postponned.add(message);
+ postponed.add(message);
return;
}
writeImpl(message);
}
public void start(ChannelHandlerContext ctx) {
- if (this.ctx != null)
- return; // context is already associated.
+ if (this.ctx != null) {
+ // context is already associated.
+ return;
+ }
this.ctx = ctx;
new Thread(this).start();
}
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
+import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Wrapper class for proprietary SSH sessions implementations
*/
-public class SshSession {
- final Session session;
+public class SshSession implements Closeable {
+ private final Session session;
public SshSession(Session session) {
this.session = session;
return session.waitUntilDataAvailable(timeout);
}
- public int waitForCondition(int condition_set, long timeout) {
- return session.waitForCondition(condition_set, timeout);
+ public int waitForCondition(int conditionSet, long timeout) {
+ return session.waitForCondition(conditionSet, timeout);
}
public Integer getExitStatus() {
public String getExitSignal() {
return session.getExitSignal();
}
+
+ @Override
+ public void close() {
+ session.close();
+ }
}
b[off] = (byte)c;
- if(this.bb.readableBytes() == 0) return bytesRead;
+ if(this.bb.readableBytes() == 0) {
+ return bytesRead;
+ }
int ltr = len-1;
ltr = (ltr <= bb.readableBytes()) ? ltr : bb.readableBytes();
}
}
- public void channelRegistered(ChannelHandlerContext ctx)
- throws Exception {
+ public void channelRegistered(ChannelHandlerContext ctx) {
ctx.fireChannelRegistered();
}
- public void channelUnregistered(ChannelHandlerContext ctx)
- throws Exception {
+ public void channelUnregistered(ChannelHandlerContext ctx) {
ctx.fireChannelUnregistered();
}
- public void channelActive(ChannelHandlerContext ctx)
- throws Exception {
+ public void channelActive(ChannelHandlerContext ctx) {
ctx.fireChannelActive();
}
- public void channelInactive(ChannelHandlerContext ctx)
- throws Exception {
+ public void channelInactive(ChannelHandlerContext ctx) {
ctx.fireChannelInactive();
}
- public void channelRead(ChannelHandlerContext ctx, Object o)
- throws Exception {
+ public void channelRead(ChannelHandlerContext ctx, Object o) {
synchronized(lock) {
this.bb.discardReadBytes();
this.bb.writeBytes((ByteBuf) o);
}
}
- public void channelReadComplete(ChannelHandlerContext ctx)
- throws Exception {
+ public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.fireChannelReadComplete();
}
- public void userEventTriggered(ChannelHandlerContext ctx, Object o)
- throws Exception {
+ public void userEventTriggered(ChannelHandlerContext ctx, Object o) {
ctx.fireUserEventTriggered(o);
}
- public void channelWritabilityChanged(ChannelHandlerContext ctx)
- throws Exception {
+ public void channelWritabilityChanged(ChannelHandlerContext ctx) {
ctx.fireChannelWritabilityChanged();
}
- public void handlerAdded(ChannelHandlerContext ctx)
- throws Exception {
+ public void handlerAdded(ChannelHandlerContext ctx) {
}
- public void handlerRemoved(ChannelHandlerContext ctx)
- throws Exception {
+ public void handlerRemoved(ChannelHandlerContext ctx) {
}
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable)
- throws Exception {
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) {
ctx.fireExceptionCaught(throwable);
}
}
import io.netty.channel.ChannelOutboundHandler;
import io.netty.channel.ChannelPromise;
-import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketAddress;
private ChannelHandlerContext ctx;
@Override
- public void flush() throws IOException {
+ public void flush() {
synchronized(lock) {
ctx.writeAndFlush(buff).awaitUninterruptibly();
buff = Unpooled.buffer();
}
@Override
- public void write(int b) throws IOException {
+ public void write(int b) {
synchronized(lock) {
buff.writeByte(b);
}
}
public void bind(ChannelHandlerContext ctx, SocketAddress localAddress,
- ChannelPromise promise) throws Exception {
+ ChannelPromise promise) {
ctx.bind(localAddress, promise);
}
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress,
- SocketAddress localAddress, ChannelPromise promise)
- throws Exception {
+ SocketAddress localAddress, ChannelPromise promise) {
this.ctx = ctx;
ctx.connect(remoteAddress, localAddress, promise);
}
- public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise)
- throws Exception {
+ public void disconnect(ChannelHandlerContext ctx, ChannelPromise promise) {
ctx.disconnect(promise);
}
- public void close(ChannelHandlerContext ctx, ChannelPromise promise)
- throws Exception {
+ public void close(ChannelHandlerContext ctx, ChannelPromise promise) {
ctx.close(promise);
}
- public void deregister(ChannelHandlerContext ctx, ChannelPromise channelPromise)
- throws Exception {
+ public void deregister(ChannelHandlerContext ctx, ChannelPromise channelPromise) {
ctx.deregister(channelPromise);
}
- public void read(ChannelHandlerContext ctx)
- throws Exception {
+ public void read(ChannelHandlerContext ctx) {
ctx.read();
}
- public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
- throws Exception {
+ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
// pass
}
- public void flush(ChannelHandlerContext ctx)
- throws Exception {
+ public void flush(ChannelHandlerContext ctx) {
// pass
}
throws Exception {
}
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
- throws Exception {
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
ctx.fireExceptionCaught(cause);
}
}
* are able to use full potential of NIO environment.
*/
public class VirtualSocket extends Socket implements ChannelHandler {
+ private static final String INPUT_STREAM = "inputStream";
+ private static final String OUTPUT_STREAM = "outputStream";
+
private final ChannelInputStream chis = new ChannelInputStream();
private final ChannelOutputStream chos = new ChannelOutputStream();
private ChannelHandlerContext ctx;
return this.chos;
}
- public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
+ public void handlerAdded(ChannelHandlerContext ctx) {
this.ctx = ctx;
- if (ctx.channel().pipeline().get("outputStream") == null) {
- ctx.channel().pipeline().addFirst("outputStream", chos);
+ if (ctx.channel().pipeline().get(OUTPUT_STREAM) == null) {
+ ctx.channel().pipeline().addFirst(OUTPUT_STREAM, chos);
}
- if (ctx.channel().pipeline().get("inputStream") == null) {
- ctx.channel().pipeline().addFirst("inputStream", chis);
+ if (ctx.channel().pipeline().get(INPUT_STREAM) == null) {
+ ctx.channel().pipeline().addFirst(INPUT_STREAM, chis);
}
}
- public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
- if (ctx.channel().pipeline().get("outputStream") != null) {
- ctx.channel().pipeline().remove("outputStream");
+ public void handlerRemoved(ChannelHandlerContext ctx) {
+ if (ctx.channel().pipeline().get(OUTPUT_STREAM) != null) {
+ ctx.channel().pipeline().remove(OUTPUT_STREAM);
}
- if (ctx.channel().pipeline().get("inputStream") != null) {
- ctx.channel().pipeline().remove("inputStream");
+ if (ctx.channel().pipeline().get(INPUT_STREAM) != null) {
+ ctx.channel().pipeline().remove(INPUT_STREAM);
}
}
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) throws Exception {
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable throwable) {
+ // TODO exceptionCaught is deprecated transform this handler
ctx.fireExceptionCaught(throwable);
}
public InetAddress getInetAddress() {
InetSocketAddress isa = getInetSocketAddress();
- if (isa == null) throw new VirtualSocketException();
+ if (isa == null) {
+ throw new VirtualSocketException();
+ }
return getInetSocketAddress().getAddress();
}
* session's connection. Provided information can be reported via netconf
* monitoring.
* <pre>
- * It has pattern "[username; host-address:port; transport; session-identifier;]"
+ * It has PATTERN "[username; host-address:port; transport; session-identifier;]"
* username - name of account on a remote
* host-address - client's IP address
* port - port number
}
// TODO IPv6
- private static final Pattern pattern = Pattern
+ private static final Pattern PATTERN = Pattern
.compile("\\[(?<username>[^;]+);(?<address>[0-9\\.]+)[:/](?<port>[0-9]+);(?<transport>[a-z]+)[^\\]]+\\]");
- private static final Pattern customHeaderPattern = Pattern
+ private static final Pattern CUSTOM_HEADER_PATTERN = Pattern
.compile("\\[(?<username>[^;]+);(?<address>[0-9\\.]+)[:/](?<port>[0-9]+);(?<transport>[a-z]+);(?<sessionIdentifier>[a-z]+)[^\\]]+\\]");
/**
* Parse additional header from a formatted string
*/
public static NetconfHelloMessageAdditionalHeader fromString(String additionalHeader) {
- additionalHeader = additionalHeader.trim();
- Matcher matcher = pattern.matcher(additionalHeader);
- Matcher matcher2 = customHeaderPattern.matcher(additionalHeader);
+ String additionalHeaderTrimmed = additionalHeader.trim();
+ Matcher matcher = PATTERN.matcher(additionalHeaderTrimmed);
+ Matcher matcher2 = CUSTOM_HEADER_PATTERN.matcher(additionalHeaderTrimmed);
Preconditions.checkArgument(matcher.matches(), "Additional header in wrong format %s, expected %s",
- additionalHeader, pattern);
+ additionalHeaderTrimmed, PATTERN);
String username = matcher.group("username");
String address = matcher.group("address");
import com.google.common.base.Charsets;
public class NetconfMessageConstants {
+ /**
+ * The NETCONF 1.0 old-style message separator. This is framing mechanism
+ * is used by default.
+ */
+ public static final byte[] END_OF_MESSAGE = "]]>]]>".getBytes(Charsets.UTF_8);
- public static final byte[] endOfMessage = "]]>]]>".getBytes(Charsets.UTF_8);
+ // bytes
- public static final byte[] endOfChunk = "\n##\n".getBytes(Charsets.UTF_8);
+ public static final int MIN_HEADER_LENGTH = 4;
- public static final int MAX_CHUNK_SIZE = 1024; // bytes
+ // bytes
- public static final int MIN_HEADER_LENGTH = 4; // bytes
+ public static final int MAX_HEADER_LENGTH = 13;
- public static final int MAX_HEADER_LENGTH = 13; // bytes
-}
\ No newline at end of file
+ public static final byte[] END_OF_CHUNK = "\n##\n".getBytes(Charsets.UTF_8);
+}
package org.opendaylight.controller.netconf.util.messages;
+import java.nio.ByteBuffer;
+
import com.google.common.base.Charsets;
import com.google.common.base.Preconditions;
-import java.nio.ByteBuffer;
-
/**
* Netconf message header is used only when chunked framing mechanism is
* supported. The header consists of only the length field.
*/
+@Deprecated
public final class NetconfMessageHeader {
-
- private long length;
-
// \n#<length>\n
- private static final byte[] headerBegin = new byte[] { (byte) 0x0a, (byte) 0x23 };
+ private static final byte[] HEADER_START = new byte[] { (byte) 0x0a, (byte) 0x23 };
+ private static final byte HEADER_END = (byte) 0x0a;
+ private final long length;
- private static final byte headerEnd = (byte) 0x0a;
-
- private boolean parsed = false;
-
- public NetconfMessageHeader() {
-
- }
-
- public NetconfMessageHeader fromBytes(final byte[] bytes) {
- // the length is variable therefore bytes between headerBegin and
- // headerEnd mark the length
- // the length should be only numbers and therefore easily parsed with
- // ASCII
- this.length = Long.parseLong(Charsets.US_ASCII.decode(
- ByteBuffer.wrap(bytes, headerBegin.length, bytes.length - headerBegin.length - 1)).toString());
- Preconditions.checkState(this.length < Integer.MAX_VALUE && this.length > 0);
- this.parsed = true;
- return this;
+ public NetconfMessageHeader(final long length) {
+ Preconditions.checkArgument(length < Integer.MAX_VALUE && length > 0);
+ this.length = length;
}
public byte[] toBytes() {
- final byte[] l = String.valueOf(this.length).getBytes(Charsets.US_ASCII);
- final byte[] h = new byte[headerBegin.length + l.length + 1];
- System.arraycopy(headerBegin, 0, h, 0, headerBegin.length);
- System.arraycopy(l, 0, h, headerBegin.length, l.length);
- System.arraycopy(new byte[] { headerEnd }, 0, h, headerBegin.length + l.length, 1);
- return h;
+ return toBytes(this.length);
}
// FIXME: improve precision to long
return (int) this.length;
}
- public void setLength(final int length) {
- this.length = length;
- }
+ public static NetconfMessageHeader fromBytes(final byte[] bytes) {
+ // the length is variable therefore bytes between headerBegin and
+ // headerEnd mark the length
+ // the length should be only numbers and therefore easily parsed with
+ // ASCII
+ long length = Long.parseLong(Charsets.US_ASCII.decode(
+ ByteBuffer.wrap(bytes, HEADER_START.length, bytes.length - HEADER_START.length - 1)).toString());
- /**
- * @return the parsed
- */
- public boolean isParsed() {
- return this.parsed;
+ return new NetconfMessageHeader(length);
}
- /**
- * @param parsed
- * the parsed to set
- */
- public void setParsed() {
- this.parsed = false;
+ public static byte[] toBytes(final long length) {
+ final byte[] l = String.valueOf(length).getBytes(Charsets.US_ASCII);
+ final byte[] h = new byte[HEADER_START.length + l.length + 1];
+ System.arraycopy(HEADER_START, 0, h, 0, HEADER_START.length);
+ System.arraycopy(l, 0, h, HEADER_START.length, l.length);
+ System.arraycopy(new byte[] { HEADER_END }, 0, h, HEADER_START.length + l.length, 1);
+ return h;
}
}
import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
import org.w3c.dom.Document;
-public class NetconfMessageUtil {
+public final class NetconfMessageUtil {
+
+ private NetconfMessageUtil() {}
public static boolean isOKMessage(NetconfMessage message) {
return isOKMessage(message.getDocument());
import java.io.InputStream;
import java.util.Map.Entry;
-public class SendErrorExceptionUtil {
+public final class SendErrorExceptionUtil {
private static final Logger logger = LoggerFactory.getLogger(SendErrorExceptionUtil.class);
+ private SendErrorExceptionUtil() {}
+
public static void sendErrorMessage(final NetconfSession session,
final NetconfDocumentedException sendErrorException) {
logger.trace("Sending error {}", sendErrorException.getMessage(), sendErrorException);
for (int i = 0; i < incomingAttributes.getLength(); i++) {
final Attr attr = (Attr) incomingAttributes.item(i);
// skip namespace
- if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY))
+ if (attr.getNodeName().equals(XmlUtil.XMLNS_ATTRIBUTE_KEY)) {
continue;
+ }
rpcReply.setAttributeNode((Attr) errorDocument.importNode(attr, true));
}
} catch (final Exception e) {
import static com.google.common.base.Preconditions.checkNotNull;
-public class NetconfConfigUtil {
+public final class NetconfConfigUtil {
private static final Logger logger = LoggerFactory.getLogger(NetconfConfigUtil.class);
private static final String PREFIX_PROP = "netconf.";
-
+ private NetconfConfigUtil() {}
private enum InfixProp {
tcp, ssh
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
-public class XMLNetconfUtil {
+public final class XMLNetconfUtil {
+
+ private XMLNetconfUtil() {}
public static XPathExpression compileXPath(String xPath) {
final XPathFactory xPathfactory = XPathFactory.newInstance();
import java.util.List;
import java.util.Map;
-public class XmlElement {
+public final class XmlElement {
- public final Element element;
+ private final Element element;
private XmlElement(Element element) {
this.element = element;
public void appendChild(Element element) {
this.element.appendChild(element);
- // Element newElement = (Element) element.cloneNode(true);
- // newElement.appendChild(configElement);
- // return XmlElement.fromDomElement(newElement);
}
public Element getDomElement() {
final List<XmlElement> result = new ArrayList<>();
for (int i = 0; i < childNodes.getLength(); i++) {
Node item = childNodes.item(i);
- if (item instanceof Element == false)
+ if (item instanceof Element == false) {
continue;
- if (strat.accept((Element) item))
+ }
+ if (strat.accept((Element) item)) {
result.add(new XmlElement((Element) item));
+ }
}
return result;
public String getNamespace() {
String namespaceURI = element.getNamespaceURI();
Preconditions.checkState(namespaceURI != null, "No namespace defined for %s", this);
- return namespaceURI.toString();
+ return namespaceURI;
}
@Override
public String toString() {
- final StringBuffer sb = new StringBuffer("XmlElement{");
+ final StringBuilder sb = new StringBuilder("XmlElement{");
sb.append("name='").append(getName()).append('\'');
if (element.getNamespaceURI() != null) {
sb.append(", namespace='").append(getNamespace()).append('\'');
public Map.Entry<String/* prefix */, String/* namespace */> findNamespaceOfTextContent() {
Map<String, String> namespaces = extractNamespaces(element);
String textContent = getTextContent();
- int indexOfColon = textContent.indexOf(":");
+ int indexOfColon = textContent.indexOf(':');
String prefix;
if (indexOfColon > -1) {
prefix = textContent.substring(0, indexOfColon);
@Override
public boolean equals(Object o) {
- if (this == o)
+ if (this == o) {
return true;
- if (o == null || getClass() != o.getClass())
+ }
+ if (o == null || getClass() != o.getClass()) {
return false;
+ }
XmlElement that = (XmlElement) o;
- if (!element.isEqualNode(that.element))
+ if (!element.isEqualNode(that.element)) {
return false;
+ }
return true;
}
return true;
}
- private static interface ElementFilteringStrategy {
+ private interface ElementFilteringStrategy {
boolean accept(Element e);
}
}
*/
package org.opendaylight.controller.netconf.util.xml;
-public class XmlNetconfConstants {
+public final class XmlNetconfConstants {
+
+ private XmlNetconfConstants() {}
public static final String MOUNTPOINTS = "mountpoints";
public static final String MOUNTPOINT = "mountpoint";
import com.google.common.base.Preconditions;
-public class XmlNetconfValidator {
- static final Schema schema;
+public final class XmlNetconfValidator {
+
+ private static final Schema SCHEMA;
+
+ private XmlNetconfValidator() {}
static {
final InputStream xmlSchema = XmlNetconfValidator.class.getResourceAsStream("/xml.xsd");
final InputStream rfc4714Schema = XmlNetconfValidator.class.getResourceAsStream("/rfc4741.xsd");
Preconditions.checkNotNull(rfc4714Schema, "Cannot find rfc4741.xsd");
- schema = XmlUtil.loadSchema(xmlSchema, rfc4714Schema);
+ SCHEMA = XmlUtil.loadSchema(xmlSchema, rfc4714Schema);
}
public static void validate(Document inputDocument) throws SAXException, IOException {
- final Validator validator = schema.newValidator();
+ final Validator validator = SCHEMA.newValidator();
final Source source = new DOMSource(inputDocument);
validator.validate(source);
}
import com.google.common.base.Charsets;
-public class XmlUtil {
+public final class XmlUtil {
public static final String XMLNS_ATTRIBUTE_KEY = "xmlns";
+ private static final DocumentBuilderFactory BUILDERFACTORY;
+
+ static {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ factory.setNamespaceAware(true);
+ factory.setCoalescing(true);
+ factory.setIgnoringElementContentWhitespace(true);
+ factory.setIgnoringComments(true);
+ BUILDERFACTORY = factory;
+ }
+
+ private XmlUtil() {}
public static Element readXmlToElement(String xmlContent) throws SAXException, IOException {
Document doc = readXmlToDocument(xmlContent);
return readXmlToDocument(new ByteArrayInputStream(xmlContent.getBytes(Charsets.UTF_8)));
}
+ // TODO improve exceptions throwing
+ // along with XmlElement
+
public static Document readXmlToDocument(InputStream xmlContent) throws SAXException, IOException {
- DocumentBuilderFactory factory = getDocumentBuilderFactory();
DocumentBuilder dBuilder;
try {
- dBuilder = factory.newDocumentBuilder();
+ dBuilder = BUILDERFACTORY.newDocumentBuilder();
} catch (ParserConfigurationException e) {
- throw new RuntimeException(e);
+ throw new RuntimeException("Failed to parse XML document", e);
}
Document doc = dBuilder.parse(xmlContent);
return readXmlToDocument(new FileInputStream(xmlFile)).getDocumentElement();
}
- private static final DocumentBuilderFactory getDocumentBuilderFactory() {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- factory.setNamespaceAware(true);
- factory.setCoalescing(true);
- factory.setIgnoringElementContentWhitespace(true);
- factory.setIgnoringComments(true);
- return factory;
- }
-
public static Document newDocument() {
- DocumentBuilderFactory factory = getDocumentBuilderFactory();
try {
- DocumentBuilder builder = factory.newDocumentBuilder();
+ DocumentBuilder builder = BUILDERFACTORY.newDocumentBuilder();
Document document = builder.newDocument();
return document;
} catch (ParserConfigurationException e) {
- throw new RuntimeException(e);
+ throw new RuntimeException("Failed to create document", e);
}
}
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
- transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, addXmlDeclaration == true ? "no" : "yes");
+ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, addXmlDeclaration ? "no" : "yes");
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(xml);
transformer.transform(source, result);
- String xmlString = result.getWriter().toString();
- return xmlString;
+ return result.getWriter().toString();
} catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) {
throw new RuntimeException("Unable to serialize xml element " + xml, e);
}
--- /dev/null
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.netconf.util.handler;
+
+import com.google.common.base.Charsets;
+import com.google.common.collect.Lists;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import junit.framework.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.util.List;
+
+public class NetconfChunkAggregatorTest {
+
+ private static final String CHUNKED_MESSAGE = "\n#4\n" +
+ "<rpc" +
+ "\n#18\n" +
+ " message-id=\"102\"\n" +
+ "\n#79\n" +
+ " xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
+ " <close-session/>\n" +
+ "</rpc>" +
+ "\n##\n";
+
+ public static final String EXPECTED_MESSAGE = "<rpc message-id=\"102\"\n" +
+ " xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
+ " <close-session/>\n" +
+ "</rpc>";
+
+ private static final String CHUNKED_MESSAGE_ONE = "\n#101\n" + EXPECTED_MESSAGE + "\n##\n";
+
+ private static NetconfChunkAggregator agr;
+
+ @BeforeClass
+ public static void setUp() throws Exception {
+ agr = new NetconfChunkAggregator();
+ }
+
+ @Test
+ public void testMultipleChunks() throws Exception {
+ List<Object> output = Lists.newArrayList();
+ ByteBuf input = Unpooled.copiedBuffer(CHUNKED_MESSAGE.getBytes(Charsets.UTF_8));
+ agr.decode(null, input, output);
+
+ Assert.assertEquals(1, output.size());
+ ByteBuf chunk = (ByteBuf) output.get(0);
+
+ Assert.assertEquals(EXPECTED_MESSAGE, chunk.toString(Charsets.UTF_8));
+ }
+
+ @Test
+ public void testOneChunks() throws Exception {
+ List<Object> output = Lists.newArrayList();
+ ByteBuf input = Unpooled.copiedBuffer(CHUNKED_MESSAGE_ONE.getBytes(Charsets.UTF_8));
+ agr.decode(null, input, output);
+
+ Assert.assertEquals(1, output.size());
+ ByteBuf chunk = (ByteBuf) output.get(0);
+
+ Assert.assertEquals(EXPECTED_MESSAGE, chunk.toString(Charsets.UTF_8));
+ }
+
+
+}
import org.junit.Test;
import org.opendaylight.controller.netconf.util.handler.NetconfXMLToHelloMessageDecoder;
-import org.opendaylight.controller.netconf.util.handler.NetconfXMLToMessageDecoder;
import com.google.common.io.Files;
public class NetconfMessageFactoryTest {
@Test
public void testAuth() throws Exception {
- NetconfXMLToMessageDecoder parser = new NetconfXMLToHelloMessageDecoder();
+ NetconfXMLToHelloMessageDecoder parser = new NetconfXMLToHelloMessageDecoder();
File authHelloFile = new File(getClass().getResource("/netconfMessages/client_hello_with_auth.xml").getFile());
final List<Object> out = new ArrayList<>();
</type>
<name>id-test</name>
<identities>
- <afi xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:test:impl">prefix:test-identity1</afi>
- <safi xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:test:impl">prefix:test-identity2</safi>
+ <afi xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:config:test:types">prefix:test-identity1</afi>
+ <safi xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:config:test:types">prefix:test-identity2</safi>
</identities>
<identities>
- <afi xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:test:impl">prefix:test-identity2</afi>
- <safi xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:test:impl">prefix:test-identity1</safi>
+ <afi xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:config:test:types">prefix:test-identity2</afi>
+ <safi xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:config:test:types">prefix:test-identity1</safi>
</identities>
<identities-container>
- <afi xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:test:impl">prefix:test-identity2</afi>
+ <afi xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:config:test:types">prefix:test-identity2</afi>
</identities-container>
- <afi xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:test:impl">prefix:test-identity1</afi>
+ <afi xmlns:prefix="urn:opendaylight:params:xml:ns:yang:controller:config:test:types">prefix:test-identity1</afi>
</module>
</modules>
<version>1.1-SNAPSHOT</version>
<packaging>bundle</packaging>
- <properties>
- <ganymed.version>build209</ganymed.version>
- </properties>
-
<dependencies>
<dependency>
<groupId>org.osgi</groupId>