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

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