ca04f99ba22361d8fcdc09794bcaa9193729483c
[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
14 import javassist.ClassPool;
15
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
18 import org.opendaylight.controller.md.sal.binding.impl.ForwardedBackwardsCompatibleDataBroker;
19 import org.opendaylight.controller.md.sal.binding.impl.ForwardedBindingDataBroker;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
22 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMDataBrokerImpl;
23 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
24 import org.opendaylight.controller.sal.binding.test.util.MockSchemaService;
25 import org.opendaylight.controller.sal.core.api.model.SchemaService;
26 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
27 import org.opendaylight.yangtools.binding.data.codec.gen.impl.DataObjectSerializerGenerator;
28 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
29 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
30 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
31 import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
32 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34
35 public class DataBrokerTestCustomizer {
36
37     private DOMDataBroker domDataBroker;
38     private final RuntimeGeneratedMappingServiceImpl mappingService;
39     private final MockSchemaService schemaService;
40     private ImmutableMap<LogicalDatastoreType, DOMStore> datastores;
41     private final BindingToNormalizedNodeCodec bindingToNormalized ;
42
43     public ImmutableMap<LogicalDatastoreType, DOMStore> createDatastores() {
44         return ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
45                 .put(LogicalDatastoreType.OPERATIONAL, createOperationalDatastore())
46                 .put(LogicalDatastoreType.CONFIGURATION,createConfigurationDatastore())
47                 .build();
48     }
49
50     public DataBrokerTestCustomizer() {
51         schemaService = new MockSchemaService();
52         ClassPool pool = ClassPool.getDefault();
53         mappingService = new RuntimeGeneratedMappingServiceImpl(pool);
54         DataObjectSerializerGenerator generator = StreamWriterGenerator.create(JavassistUtils.forClassPool(pool));
55         BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(generator);
56         GeneratedClassLoadingStrategy loading = GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
57         bindingToNormalized = new BindingToNormalizedNodeCodec(loading, mappingService, codecRegistry);
58         schemaService.registerSchemaContextListener(bindingToNormalized);
59     }
60
61     public DOMStore createConfigurationDatastore() {
62         InMemoryDOMDataStore store = new InMemoryDOMDataStore("CFG",
63                 MoreExecutors.sameThreadExecutor(), MoreExecutors.sameThreadExecutor());
64         schemaService.registerSchemaContextListener(store);
65         return store;
66     }
67
68     public DOMStore createOperationalDatastore() {
69         InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER",
70                 MoreExecutors.sameThreadExecutor(), MoreExecutors.sameThreadExecutor());
71         schemaService.registerSchemaContextListener(store);
72         return store;
73     }
74
75     public DOMDataBroker createDOMDataBroker() {
76         return new DOMDataBrokerImpl(getDatastores(), getCommitCoordinatorExecutor());
77     }
78
79     public ListeningExecutorService getCommitCoordinatorExecutor() {
80         return MoreExecutors.sameThreadExecutor();
81     }
82
83     public DataBroker createDataBroker() {
84         return new ForwardedBindingDataBroker(getDOMDataBroker(), bindingToNormalized, schemaService );
85     }
86
87     public ForwardedBackwardsCompatibleDataBroker createBackwardsCompatibleDataBroker() {
88         return new ForwardedBackwardsCompatibleDataBroker(getDOMDataBroker(), bindingToNormalized, getSchemaService(), MoreExecutors.sameThreadExecutor());
89     }
90
91     private SchemaService getSchemaService() {
92         return schemaService;
93     }
94
95     private DOMDataBroker getDOMDataBroker() {
96         if(domDataBroker == null) {
97             domDataBroker = createDOMDataBroker();
98         }
99         return domDataBroker;
100     }
101
102     private synchronized ImmutableMap<LogicalDatastoreType, DOMStore> getDatastores() {
103         if (datastores == null) {
104             datastores = createDatastores();
105         }
106         return datastores;
107     }
108
109     public void updateSchema(final SchemaContext ctx) {
110         schemaService.changeSchema(ctx);
111         mappingService.onGlobalContextUpdated(ctx);
112     }
113
114 }

©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.