213c7647904fb9aab8a4b9a138b07f3ba8eceb3f
[transportpce.git] / tests / honeynode / 1.2.1 / honeynode-plugin-impl / src / main / java / io / fd / honeycomb / transportpce / device / read / OcTerminalDeviceReaderFactory.java
1 /*
2  * Copyright (c) 2018 Orange 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 package io.fd.honeycomb.transportpce.device.read;
17
18 import java.util.concurrent.ExecutionException;
19
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.mdsal.binding.api.DataBroker;
22 import org.opendaylight.mdsal.binding.api.WriteTransaction;
23 import org.opendaylight.mdsal.common.api.CommitInfo;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.terminal.device.rev170708.terminal.device.top.TerminalDevice;
26 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.terminal.device.rev170708.terminal.device.top.TerminalDeviceBuilder;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.common.util.concurrent.FluentFuture;
32 import com.google.common.util.concurrent.Futures;
33 import com.google.inject.Inject;
34 import com.google.inject.name.Named;
35
36 import io.fd.honeycomb.translate.read.ReaderFactory;
37 import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder;
38 import io.fd.honeycomb.translate.util.read.BindingBrokerReader;
39 import io.fd.honeycomb.transportpce.device.configuration.OcTerminalDeviceConfiguration;
40
41 /**
42  * @authors Gilles THOUENON and Christophe BETOULE ( gilles.thouenon@orange.com, christophe.betoule@orange.com )
43  */
44 public class OcTerminalDeviceReaderFactory implements ReaderFactory {
45
46     private static final Logger LOG = LoggerFactory.getLogger(OcTerminalDeviceReaderFactory.class);
47     public static final InstanceIdentifier<TerminalDevice> TERMINAL_DEVICE_ID =
48             InstanceIdentifier.create(TerminalDevice.class);
49
50     @Inject
51     @Named("device-databroker")
52     private DataBroker dataBroker;
53
54     @Inject
55     private OcTerminalDeviceConfiguration ocTerminalDeviceConfiguration;
56
57
58     @Override
59     public void init(final ModifiableReaderRegistryBuilder registry) {
60         registry.add(new BindingBrokerReader<>(TERMINAL_DEVICE_ID, dataBroker,LogicalDatastoreType.OPERATIONAL,
61                 TerminalDeviceBuilder.class));
62         if (writeXMLDataToOper()) {
63         loadConfigData();
64         };
65     }
66
67     /**
68      * Write xml data from {@link terminal-device}
69      * to operational data.
70      *
71      */
72     public boolean writeXMLDataToOper() {
73         Boolean res = false;
74         LOG.info("writting xml oc-terminal-device file data to oper datastore");
75         TerminalDevice terminalDevice = this.ocTerminalDeviceConfiguration.getDataTerminalDevice();
76         if (terminalDevice !=null) {
77             LOG.info("Getting oc-terminal-device info from xml file");
78             TerminalDeviceBuilder result = new TerminalDeviceBuilder(terminalDevice);
79             InstanceIdentifier<TerminalDevice> iid = InstanceIdentifier.create(TerminalDevice.class);
80             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
81             if (writeTx != null ) {
82                 LOG.info("WriteTransaction is ok, copy oc-terminal-device to oper datastore");
83                 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, result.build());
84                 FluentFuture< ? extends @NonNull CommitInfo> future = writeTx.commit();
85                 try {
86                     Futures.getChecked(future, ExecutionException.class);
87                     LOG.info("oc-terminal-device writed to oper datastore");
88                     res = true;
89                 } catch (ExecutionException e) {
90                     LOG.error("Failed to write oc-terminal-device to oper datastore");
91                 }
92             } else {
93                 LOG.error("WriteTransaction object is null");
94             }
95         } else {
96             LOG.error("get oc-terminal-device data from xml file is null !");
97         }
98         return res;
99     }
100
101     /**
102      * Load data to config oc-terminal-device datastore.
103      *
104      */
105     private boolean loadConfigData() {
106         Boolean res = false;
107         LOG.info("loading oc-terminal-device configuration info from config-xml file...");
108         String xml = this.ocTerminalDeviceConfiguration.getConfigTerminalDevice();
109         LOG.info("get terminal-device data from xml file !");
110         if (xml != null) {
111             TerminalDevice result = this.ocTerminalDeviceConfiguration.getTerminalDeviceFromXML(xml);
112             if (result != null) {
113                 LOG.info("get terminal-device data : {}", result.toString());
114                 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
115                 if (writeTx != null) {
116                     LOG.info("WriteTransaction is ok, copy terminal-device info to config datastore");
117                     writeTx.put(LogicalDatastoreType.CONFIGURATION, TERMINAL_DEVICE_ID, result);
118                     FluentFuture< ? extends @NonNull CommitInfo> future = writeTx.commit();
119                     try {
120                         Futures.getChecked(future, ExecutionException.class);
121                         LOG.info("terminal-device writed to config datastore");
122                     } catch (ExecutionException e) {
123                         LOG.error("Failed to write terminal-device to config datastore");
124                     }
125                 } else {
126                     LOG.error("WriteTransaction object is null");
127                 }
128             } else {
129                 LOG.error("terminal-device from xml is null !!");
130             }
131         } else {
132             LOG.error("no terminal-device obtained from xml file");
133         }
134         return res;
135     }
136
137 }