Bug 2364: Migrated Binding MD-SAL to not use composites nodes
[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.BindingToNormalizedNodeCodec;
18 import org.opendaylight.controller.md.sal.binding.impl.ForwardedBindingDataBroker;
19 import org.opendaylight.controller.md.sal.binding.impl.ForwardedNotificationPublishService;
20 import org.opendaylight.controller.md.sal.binding.impl.ForwardedNotificationService;
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.impl.RuntimeGeneratedMappingServiceImpl;
35 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37
38 public class DataBrokerTestCustomizer {
39
40     private DOMDataBroker domDataBroker;
41     private final DOMNotificationRouter domNotificationRouter;
42     private final RuntimeGeneratedMappingServiceImpl mappingService;
43     private final MockSchemaService schemaService;
44     private ImmutableMap<LogicalDatastoreType, DOMStore> datastores;
45     private final BindingToNormalizedNodeCodec bindingToNormalized;
46
47     public ImmutableMap<LogicalDatastoreType, DOMStore> createDatastores() {
48         return ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
49                 .put(LogicalDatastoreType.OPERATIONAL, createOperationalDatastore())
50                 .put(LogicalDatastoreType.CONFIGURATION,createConfigurationDatastore())
51                 .build();
52     }
53
54     public DataBrokerTestCustomizer() {
55         schemaService = new MockSchemaService();
56         final ClassPool pool = ClassPool.getDefault();
57         mappingService = new RuntimeGeneratedMappingServiceImpl(pool);
58         final DataObjectSerializerGenerator generator = StreamWriterGenerator.create(JavassistUtils.forClassPool(pool));
59         final BindingNormalizedNodeCodecRegistry codecRegistry = new BindingNormalizedNodeCodecRegistry(generator);
60         final GeneratedClassLoadingStrategy loading = GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
61         bindingToNormalized = new BindingToNormalizedNodeCodec(loading, mappingService, codecRegistry);
62         schemaService.registerSchemaContextListener(bindingToNormalized);
63         domNotificationRouter = DOMNotificationRouter.create(16);
64     }
65
66     public DOMStore createConfigurationDatastore() {
67         final InMemoryDOMDataStore store = new InMemoryDOMDataStore("CFG", MoreExecutors.sameThreadExecutor());
68         schemaService.registerSchemaContextListener(store);
69         return store;
70     }
71
72     public DOMStore createOperationalDatastore() {
73         final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", MoreExecutors.sameThreadExecutor());
74         schemaService.registerSchemaContextListener(store);
75         return store;
76     }
77
78     public DOMDataBroker createDOMDataBroker() {
79         return new SerializedDOMDataBroker(getDatastores(), getCommitCoordinatorExecutor());
80     }
81
82     public NotificationService createNotificationService() {
83         return new ForwardedNotificationService(bindingToNormalized.getCodecRegistry(), domNotificationRouter,
84                 SingletonHolder.INVOKER_FACTORY);
85     }
86
87     public NotificationPublishService createNotificationPublishService() {
88         return new ForwardedNotificationPublishService(bindingToNormalized.getCodecRegistry(), domNotificationRouter);
89     }
90
91
92     public ListeningExecutorService getCommitCoordinatorExecutor() {
93         return MoreExecutors.sameThreadExecutor();
94     }
95
96     public DataBroker createDataBroker() {
97         return new ForwardedBindingDataBroker(getDOMDataBroker(), bindingToNormalized);
98     }
99
100     public BindingToNormalizedNodeCodec getBindingToNormalized() {
101         return bindingToNormalized;
102     }
103
104     public SchemaService getSchemaService() {
105         return schemaService;
106     }
107
108     private DOMDataBroker getDOMDataBroker() {
109         if(domDataBroker == null) {
110             domDataBroker = createDOMDataBroker();
111         }
112         return domDataBroker;
113     }
114
115     private synchronized ImmutableMap<LogicalDatastoreType, DOMStore> getDatastores() {
116         if (datastores == null) {
117             datastores = createDatastores();
118         }
119         return datastores;
120     }
121
122     public void updateSchema(final SchemaContext ctx) {
123         schemaService.changeSchema(ctx);
124         mappingService.onGlobalContextUpdated(ctx);
125     }
126
127     public DOMNotificationRouter getDomNotificationRouter() {
128         return domNotificationRouter;
129     }
130 }

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