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

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