Cleanup DOMStoreReadTransaction/DOMDataReadTransaction exists API documentation
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / test / DataBrokerTestCustomizer.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.md.sal.binding.test;
9
10 import com.google.common.collect.ImmutableMap;
11 import com.google.common.util.concurrent.ListeningExecutorService;
12 import com.google.common.util.concurrent.MoreExecutors;
13 import javassist.ClassPool;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
16 import org.opendaylight.controller.md.sal.binding.impl.ForwardedBindingDataBroker;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
19 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMDataBrokerImpl;
20 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
21 import org.opendaylight.controller.sal.binding.test.util.MockSchemaService;
22 import org.opendaylight.controller.sal.core.api.model.SchemaService;
23 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
24 import org.opendaylight.yangtools.binding.data.codec.gen.impl.DataObjectSerializerGenerator;
25 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
26 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
27 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
28 import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
29 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31
32 public class DataBrokerTestCustomizer {
33
34     private DOMDataBroker domDataBroker;
35     private final RuntimeGeneratedMappingServiceImpl mappingService;
36     private final MockSchemaService schemaService;
37     private ImmutableMap<LogicalDatastoreType, DOMStore> datastores;
38     private final BindingToNormalizedNodeCodec bindingToNormalized ;
39
40     public ImmutableMap<LogicalDatastoreType, DOMStore> createDatastores() {
41         return ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
42                 .put(LogicalDatastoreType.OPERATIONAL, createOperationalDatastore())
43                 .put(LogicalDatastoreType.CONFIGURATION,createConfigurationDatastore())
44                 .build();
45     }
46
47     public DataBrokerTestCustomizer() {
48         schemaService = new MockSchemaService();
49         ClassPool pool = ClassPool.getDefault();
50         mappingService = new RuntimeGeneratedMappingServiceImpl(pool);
51         DataObjectSerializerGenerator generator = StreamWriterGenerator.create(JavassistUtils.forClassPool(pool));
52         BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(generator);
53         GeneratedClassLoadingStrategy loading = GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
54         bindingToNormalized = new BindingToNormalizedNodeCodec(loading, mappingService, codecRegistry);
55         schemaService.registerSchemaContextListener(bindingToNormalized);
56     }
57
58     public DOMStore createConfigurationDatastore() {
59         InMemoryDOMDataStore store = new InMemoryDOMDataStore("CFG",
60                 MoreExecutors.sameThreadExecutor(), MoreExecutors.sameThreadExecutor());
61         schemaService.registerSchemaContextListener(store);
62         return store;
63     }
64
65     public DOMStore createOperationalDatastore() {
66         InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER",
67                 MoreExecutors.sameThreadExecutor(), MoreExecutors.sameThreadExecutor());
68         schemaService.registerSchemaContextListener(store);
69         return store;
70     }
71
72     public DOMDataBroker createDOMDataBroker() {
73         return new DOMDataBrokerImpl(getDatastores(), getCommitCoordinatorExecutor());
74     }
75
76     public ListeningExecutorService getCommitCoordinatorExecutor() {
77         return MoreExecutors.sameThreadExecutor();
78     }
79
80     public DataBroker createDataBroker() {
81         return new ForwardedBindingDataBroker(getDOMDataBroker(), bindingToNormalized, schemaService );
82     }
83
84     public BindingToNormalizedNodeCodec getBindingToNormalized() {
85         return bindingToNormalized;
86     }
87
88     public SchemaService getSchemaService() {
89         return schemaService;
90     }
91
92     private DOMDataBroker getDOMDataBroker() {
93         if(domDataBroker == null) {
94             domDataBroker = createDOMDataBroker();
95         }
96         return domDataBroker;
97     }
98
99     private synchronized ImmutableMap<LogicalDatastoreType, DOMStore> getDatastores() {
100         if (datastores == null) {
101             datastores = createDatastores();
102         }
103         return datastores;
104     }
105
106     public void updateSchema(final SchemaContext ctx) {
107         schemaService.changeSchema(ctx);
108         mappingService.onGlobalContextUpdated(ctx);
109     }
110
111 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.