2 * Copyright (c) 2018 Orange and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package io.fd.honeycomb.transportpce.device.read;
18 import java.util.concurrent.ExecutionException;
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;
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;
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;
42 * @authors Gilles THOUENON and Christophe BETOULE ( gilles.thouenon@orange.com, christophe.betoule@orange.com )
44 public class OcTerminalDeviceReaderFactory implements ReaderFactory {
46 private static final Logger LOG = LoggerFactory.getLogger(OcTerminalDeviceReaderFactory.class);
47 public static final InstanceIdentifier<TerminalDevice> TERMINAL_DEVICE_ID =
48 InstanceIdentifier.create(TerminalDevice.class);
51 @Named("device-databroker")
52 private DataBroker dataBroker;
55 private OcTerminalDeviceConfiguration ocTerminalDeviceConfiguration;
59 public void init(final ModifiableReaderRegistryBuilder registry) {
60 registry.add(new BindingBrokerReader<>(TERMINAL_DEVICE_ID, dataBroker,LogicalDatastoreType.OPERATIONAL,
61 TerminalDeviceBuilder.class));
62 if (writeXMLDataToOper()) {
68 * Write xml data from {@link terminal-device}
69 * to operational data.
72 public boolean writeXMLDataToOper() {
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();
86 Futures.getChecked(future, ExecutionException.class);
87 LOG.info("oc-terminal-device writed to oper datastore");
89 } catch (ExecutionException e) {
90 LOG.error("Failed to write oc-terminal-device to oper datastore");
93 LOG.error("WriteTransaction object is null");
96 LOG.error("get oc-terminal-device data from xml file is null !");
102 * Load data to config oc-terminal-device datastore.
105 private boolean loadConfigData() {
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 !");
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();
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");
126 LOG.error("WriteTransaction object is null");
129 LOG.error("terminal-device from xml is null !!");
132 LOG.error("no terminal-device obtained from xml file");