Merge "Bug 1333: Regression Test suite."
[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 javassist.ClassPool;
11
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.binding.impl.ForwardedBindingDataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
16 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMDataBrokerImpl;
17 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
18 import org.opendaylight.controller.sal.binding.test.util.MockSchemaService;
19 import org.opendaylight.controller.sal.core.api.model.SchemaService;
20 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
21 import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
22 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24
25 import com.google.common.collect.ImmutableMap;
26 import com.google.common.util.concurrent.ListeningExecutorService;
27 import com.google.common.util.concurrent.MoreExecutors;
28
29 public class DataBrokerTestCustomizer {
30
31     private DOMDataBroker domDataBroker;
32     private final RuntimeGeneratedMappingServiceImpl mappingService;
33     private final MockSchemaService schemaService;
34     private ImmutableMap<LogicalDatastoreType, DOMStore> datastores;
35
36     public ImmutableMap<LogicalDatastoreType, DOMStore> createDatastores() {
37         return ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
38                 .put(LogicalDatastoreType.OPERATIONAL, createOperationalDatastore())
39                 .put(LogicalDatastoreType.CONFIGURATION,createConfigurationDatastore())
40                 .build();
41     }
42
43     public DataBrokerTestCustomizer() {
44         schemaService = new MockSchemaService();
45         mappingService = new RuntimeGeneratedMappingServiceImpl(ClassPool.getDefault());
46     }
47
48     public DOMStore createConfigurationDatastore() {
49         InMemoryDOMDataStore store = new InMemoryDOMDataStore("CFG", MoreExecutors.sameThreadExecutor());
50         schemaService.registerSchemaContextListener(store);
51         return store;
52     }
53
54     public DOMStore createOperationalDatastore() {
55         InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", MoreExecutors.sameThreadExecutor());
56         schemaService.registerSchemaContextListener(store);
57         return store;
58     }
59
60     public DOMDataBroker createDOMDataBroker() {
61         return new DOMDataBrokerImpl(getDatastores(), getCommitCoordinatorExecutor());
62     }
63
64     public ListeningExecutorService getCommitCoordinatorExecutor() {
65         return MoreExecutors.sameThreadExecutor();
66     }
67
68     public DataBroker createDataBroker() {
69         return new ForwardedBindingDataBroker(getDOMDataBroker(), getMappingService(), getSchemaService());
70     }
71
72     private SchemaService getSchemaService() {
73         return schemaService;
74     }
75
76     private BindingIndependentMappingService getMappingService() {
77         return mappingService;
78     }
79
80     private DOMDataBroker getDOMDataBroker() {
81         if(domDataBroker == null) {
82             domDataBroker = createDOMDataBroker();
83         }
84         return domDataBroker;
85     }
86
87     private ImmutableMap<LogicalDatastoreType, DOMStore> getDatastores() {
88         if(datastores == null) {
89             datastores = createDatastores();
90         }
91         return datastores;
92     }
93
94     public void updateSchema(final SchemaContext ctx) {
95         schemaService.changeSchema(ctx);
96         mappingService.onGlobalContextUpdated(ctx);
97     }
98
99 }