8d34cbd2cd6f7b5df33b2008e9bb806e0780f9fb
[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 java.util.concurrent.Executors;
14 import javassist.ClassPool;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
17 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
18 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMDataBrokerAdapter;
19 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMNotificationPublishServiceAdapter;
20 import org.opendaylight.controller.md.sal.binding.impl.BindingDOMNotificationServiceAdapter;
21 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
24 import org.opendaylight.controller.md.sal.dom.broker.impl.DOMNotificationRouter;
25 import org.opendaylight.controller.md.sal.dom.broker.impl.SerializedDOMDataBroker;
26 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
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.newDirectExecutorService());
65         schemaService.registerSchemaContextListener(store);
66         return store;
67     }
68
69     public DOMStore createOperationalDatastore() {
70         final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", MoreExecutors.newDirectExecutorService());
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     }
82
83     public NotificationPublishService createNotificationPublishService() {
84         return new BindingDOMNotificationPublishServiceAdapter(bindingToNormalized, domNotificationRouter);
85     }
86
87     public ListeningExecutorService getCommitCoordinatorExecutor() {
88         return MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
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.