*/
package org.opendaylight.controller.config.manager.impl;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Maps;
+import junit.framework.Assert;
import org.junit.After;
import org.mockito.Matchers;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
import org.opendaylight.controller.config.api.jmx.CommitStatus;
import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver;
import org.opendaylight.controller.config.manager.impl.jmx.BaseJMXRegistrator;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.io.Closeable;
+import java.io.InputStream;
import java.lang.management.ManagementFactory;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
import java.util.Dictionary;
+import java.util.List;
+import java.util.Map;
import java.util.Set;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
-import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
/**
protected BundleContext mockedContext = mock(BundleContext.class);
protected ServiceRegistration<?> mockedServiceRegistration;
+ protected Map<Class, BundleContextServiceRegistrationHandler> getBundleContextServiceRegistrationHandlers() {
+ return Maps.newHashMap();
+ }
+
// this method should be called in @Before
protected void initConfigTransactionManagerImpl(
ModuleFactoriesResolver resolver) {
configRegistryJMXRegistrator = new ConfigRegistryJMXRegistrator(
platformMBeanServer);
- this.mockedServiceRegistration = mock(ServiceRegistration.class);
- doNothing().when(mockedServiceRegistration).unregister();
- doReturn(mockedServiceRegistration).when(mockedContext).registerService(
- Matchers.any(String[].class), any(Closeable.class),
- any(Dictionary.class));
+ initBundleContext();
internalJmxRegistrator = new InternalJMXRegistrator(platformMBeanServer);
baseJmxRegistrator = new BaseJMXRegistrator(internalJmxRegistrator);
configRegistry = new ConfigRegistryImpl(resolver,
platformMBeanServer, baseJmxRegistrator);
+
try {
configRegistryJMXRegistrator.registerToJMX(configRegistry);
} catch (InstanceAlreadyExistsException e) {
configRegistryClient = new ConfigRegistryJMXClient(platformMBeanServer);
}
+ private void initBundleContext() {
+ this.mockedServiceRegistration = mock(ServiceRegistration.class);
+ doNothing().when(mockedServiceRegistration).unregister();
+
+ RegisterServiceAnswer answer = new RegisterServiceAnswer();
+
+ doAnswer(answer).when(mockedContext).registerService(Matchers.any(String[].class),
+ any(Closeable.class), Matchers.<Dictionary<String, ?>>any());
+ doAnswer(answer).when(mockedContext).registerService(Matchers.<Class<Closeable>>any(), any(Closeable.class),
+ Matchers.<Dictionary<String, ?>>any());
+ }
+
+
+ public Collection<InputStream> getFilesAsInputStreams(List<String> paths) {
+ final Collection<InputStream> resources = new ArrayList<>();
+ List<String> failedToFind = new ArrayList<>();
+ for (String path : paths) {
+ InputStream resourceAsStream = getClass().getResourceAsStream(path);
+ if (resourceAsStream == null) {
+ failedToFind.add(path);
+ } else {
+ resources.add(resourceAsStream);
+ }
+ }
+ Assert.assertEquals("Some files were not found", Collections.<String>emptyList(), failedToFind);
+
+ return resources;
+ }
+
@After
public final void cleanUpConfigTransactionManagerImpl() {
configRegistryJMXRegistrator.close();
Class<? extends Module> configBeanClass, String implementationName) {
return new ClassBasedModuleFactory(implementationName, configBeanClass);
}
+
+
+ public static interface BundleContextServiceRegistrationHandler {
+
+ void handleServiceRegistration(Object serviceInstance);
+
+ }
+
+ private class RegisterServiceAnswer implements Answer {
+ @Override
+ public Object answer(InvocationOnMock invocation) throws Throwable {
+ Object[] args = invocation.getArguments();
+
+ Preconditions.checkArgument(args.length == 3);
+
+ Preconditions.checkArgument(args[0] instanceof Class);
+ Class<?> serviceType = (Class<?>) args[0];
+ Object serviceInstance = args[1];
+
+ BundleContextServiceRegistrationHandler serviceRegistrationHandler = getBundleContextServiceRegistrationHandlers()
+ .get(serviceType);
+
+ Preconditions.checkArgument(serviceType.isAssignableFrom(serviceInstance.getClass()));
+
+ if (serviceRegistrationHandler != null) {
+ serviceRegistrationHandler.handleServiceRegistration(serviceType.cast(serviceInstance));
+ }
+
+ return mockedServiceRegistration;
+ }
+ }
}
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
import org.apache.commons.io.IOUtils;
import org.opendaylight.controller.config.yang.store.api.YangStoreException;
import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
-import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public YangStoreSnapshotImpl parseYangFiles(
Collection<? extends InputStream> allInput)
throws YangStoreException {
- YangParserImpl parser = new YangParserImpl();
+ YangParserImpl parser = YangParserWrapper.getYangParserInstance();
- List<InputStream> bufferedInputStreams = new ArrayList<>();
- for (InputStream is : allInput) {
- String content;
- try {
- content = IOUtils.toString(is);
- } catch (IOException e) {
- throw new YangStoreException("Can not get yang as String from "
- + is, e);
- }
- InputStream buf = new ByteArrayInputStream(content.getBytes());
- bufferedInputStreams.add(buf);
- }
-
- Map<InputStream, Module> allYangModules = parser
- .parseYangModelsFromStreamsMapped(bufferedInputStreams);
+ Map<InputStream, Module> allYangModules = YangParserWrapper.parseYangFiles(parser, allInput);
- SchemaContext resolveSchemaContext = parser.resolveSchemaContext(Sets
- .newHashSet(allYangModules.values()));
+ SchemaContext resolveSchemaContext = YangParserWrapper.getSchemaContextFromModules(parser, allYangModules);
// JMX generator
public Map<Module, String> parseYangFilesToString(
Collection<? extends InputStream> allYangs) {
- YangParserImpl parser = new YangParserImpl();
+ YangParserImpl parser = YangParserWrapper.getYangParserInstance();
Map<InputStream, Module> allYangModules = parser
.parseYangModelsFromStreamsMapped(Lists.newArrayList(allYangs));
--- /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.yang.store.impl;
+
+import com.google.common.collect.Sets;
+import org.apache.commons.io.IOUtils;
+import org.opendaylight.controller.config.yang.store.api.YangStoreException;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
+import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
+public class YangParserWrapper {
+
+ /**
+ * throw IllegalStateException if it is unable to parse yang files
+ */
+ public static SchemaContext parseYangFiles(Collection<? extends InputStream> yangFilesAsInputStreams) {
+ YangParserImpl parser = getYangParserInstance();
+ Map<InputStream, Module> mappedYangModules = null;
+ try {
+ mappedYangModules = parseYangFiles(parser, yangFilesAsInputStreams);
+ } catch (YangStoreException e) {
+ throw new IllegalStateException("Unable to parse yang files", e);
+ }
+ return getSchemaContextFromModules(parser, mappedYangModules);
+ }
+
+ static YangParserImpl getYangParserInstance() {
+ return new YangParserImpl();
+ }
+
+ static SchemaContext getSchemaContextFromModules(YangModelParser parser, Map<InputStream, Module> allYangModules) {
+ return parser.resolveSchemaContext(Sets
+ .newHashSet(allYangModules.values()));
+ }
+
+ static Map<InputStream, Module> parseYangFiles(YangModelParser parser, Collection<? extends InputStream> allInput) throws YangStoreException {
+ List<InputStream> bufferedInputStreams = new ArrayList<>();
+ for (InputStream is : allInput) {
+ String content;
+ try {
+ content = IOUtils.toString(is);
+ } catch (IOException e) {
+ throw new YangStoreException("Can not get yang as String from "
+ + is, e);
+ }
+ InputStream buf = new ByteArrayInputStream(content.getBytes());
+ bufferedInputStreams.add(buf);
+ }
+
+ return parser
+ .parseYangModelsFromStreamsMapped(bufferedInputStreams);
+ }
+}