Honeynode test tool
[transportpce.git] / tests / honeynode / minimal-distribution-core / src / main / java / io / fd / honeycomb / infra / distro / data / PersistingDataTreeProvider.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.infra.distro.data;
18
19 import com.google.inject.Inject;
20 import com.google.inject.name.Named;
21
22 import io.fd.honeycomb.binding.init.ProviderTrait;
23 import io.fd.honeycomb.data.impl.PersistingDataTreeAdapter;
24 import io.fd.honeycomb.infra.distro.cfgattrs.HoneycombConfiguration;
25 import io.fd.honeycomb.infra.distro.data.context.ContextPipelineModule;
26
27 import java.nio.file.Paths;
28
29 import org.opendaylight.controller.sal.core.api.model.SchemaService;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public abstract class PersistingDataTreeProvider extends ProviderTrait<DataTree> {
36
37     private static final Logger LOG = LoggerFactory.getLogger(PersistingDataTreeProvider.class);
38
39     @Inject
40     private SchemaService schemaService;
41     @Inject
42     protected HoneycombConfiguration config;
43
44     @Override
45     public DataTree create() {
46         return isEnabled()
47                 ? new PersistingDataTreeAdapter(getDelegate(), schemaService, Paths.get(getPath()))
48                 : getDelegate();
49     }
50
51     public abstract String getPath();
52
53     public abstract TreeType getType();
54
55     public abstract DataTree getDelegate();
56
57     protected abstract boolean isEnabled();
58
59     public static final class ConfigPersistingDataTreeProvider extends PersistingDataTreeProvider {
60
61         @Inject
62         @Named(ConfigAndOperationalPipelineModule.HONEYCOMB_CONFIG_NONPERSIST)
63         private DataTree delegate;
64
65         @Override
66         public String getPath() {
67             return config.peristConfigPath;
68         }
69
70         @Override
71         public TreeType getType() {
72             return TreeType.CONFIGURATION;
73         }
74
75         @Override
76         public DataTree getDelegate() {
77             return delegate;
78         }
79
80         @Override
81         protected boolean isEnabled() {
82             return config.isConfigPersistenceEnabled();
83         }
84     }
85
86     public static final class ContextPersistingDataTreeProvider extends PersistingDataTreeProvider {
87
88         @Inject
89         @Named(ContextPipelineModule.HONEYCOMB_CONTEXT_NOPERSIST)
90         private DataTree delegate;
91
92         @Override
93         public String getPath() {
94             return config.peristContextPath;
95         }
96
97         @Override
98         public TreeType getType() {
99             return TreeType.OPERATIONAL;
100         }
101
102         @Override
103         public DataTree getDelegate() {
104             return delegate;
105         }
106
107         @Override
108         protected boolean isEnabled() {
109             return config.isContextPersistenceEnabled();
110         }
111
112     }
113 }