05c09b4b1679c8ed1fb51073b03c8121f5be96fe
[transportpce.git] / tests / honeynode / 2.2.1 / honeynode-plugin-impl / src / main / java / io / fd / honeycomb / transportpce / device / read / DeviceReaderFactory.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.org.openroadm.device.rev181019.org.openroadm.device.container.OrgOpenroadmDevice;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev181019.org.openroadm.device.container.OrgOpenroadmDeviceBuilder;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.StreamsBuilder;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.google.common.util.concurrent.FluentFuture;
35 import com.google.common.util.concurrent.Futures;
36 import com.google.inject.Inject;
37 import com.google.inject.name.Named;
38
39 import io.fd.honeycomb.translate.read.ReaderFactory;
40 import io.fd.honeycomb.translate.read.registry.ModifiableReaderRegistryBuilder;
41 import io.fd.honeycomb.translate.util.read.BindingBrokerReader;
42 import io.fd.honeycomb.transportpce.device.configuration.DeviceConfiguration;
43 import io.fd.honeycomb.transportpce.device.configuration.NetconfConfiguration;
44
45 /**
46  * @author Martial COULIBALY ( martial.coulibaly@gfi.com ) on behalf of Orange
47  */
48 public final class DeviceReaderFactory implements ReaderFactory {
49
50     private static final Logger LOG = LoggerFactory.getLogger(DeviceReaderFactory.class);
51     public static final InstanceIdentifier<OrgOpenroadmDevice> DEVICE_CONTAINER_ID = InstanceIdentifier
52             .create(OrgOpenroadmDevice.class);
53     private static final String YANG_MODELS = "yang";
54
55     @Inject
56     @Named("device-databroker")
57     private DataBroker dataBroker;
58
59     @Inject
60     private DeviceConfiguration deviceConfiguration;
61
62     @Inject
63     private NetconfConfiguration netconfConfiguration;
64
65     @Override
66     public void init(final ModifiableReaderRegistryBuilder registry) {
67         registry.add(new BindingBrokerReader<>(DEVICE_CONTAINER_ID, dataBroker, LogicalDatastoreType.OPERATIONAL,
68                 OrgOpenroadmDeviceBuilder.class));
69         if (writeXMLDataToOper()) {
70             writeNetconfStream();
71             loadConfigData();
72         }
73     }
74
75     /**
76      * Write xml data from {@link DeviceConfiguration} to operational data.
77      *
78      */
79     public boolean writeXMLDataToOper() {
80         Boolean res = false;
81         LOG.info("writting xml file data to oper datastore");
82         OrgOpenroadmDevice device = this.deviceConfiguration.getDataDevice();
83         if (device != null) {
84             String deviceId = device.getInfo().getNodeId().getValue();
85             LOG.info("Getting device info from xml file for device '{}'", deviceId);
86             OrgOpenroadmDeviceBuilder result = new OrgOpenroadmDeviceBuilder(device);
87             InstanceIdentifier<OrgOpenroadmDevice> iid = InstanceIdentifier.create(OrgOpenroadmDevice.class);
88             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
89             if (writeTx != null) {
90                 LOG.info("WriteTransaction is ok, copy device info to oper datastore");
91                 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, result.build());
92                 FluentFuture< ? extends @NonNull CommitInfo> future = writeTx.commit();
93                 try {
94                     Futures.getChecked(future, ExecutionException.class);
95                     LOG.info("device '{}' writed to oper datastore", deviceId);
96                     res = true;
97                 } catch (ExecutionException e) {
98                     LOG.error("Failed to write Element '{}' to oper datastore", deviceId);
99                 }
100             } else {
101                 LOG.error("WriteTransaction object is null");
102             }
103         } else {
104             LOG.error("device data operation gets from xml file is null !");
105         }
106         return res;
107     }
108
109     /**
110      * Load data to config device datastore.
111      *
112      */
113     private boolean loadConfigData() {
114         Boolean res = false;
115         LOG.info("loading device configuration info from xml file...");
116         String xml = this.deviceConfiguration.getConfigDevice();
117         LOG.info("device info gets from xml file !");
118         if (xml != null) {
119             OrgOpenroadmDevice result = this.deviceConfiguration.getDeviceFromXML(xml);
120             if (result != null) {
121                 LOG.info("OrgOpenroadmDevice info gets : {}", result.getInfo().getNodeId());
122                 WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
123                 if (writeTx != null) {
124                     LOG.info("WriteTransaction is ok, copy device info to config datastore");
125                     writeTx.put(LogicalDatastoreType.CONFIGURATION, DEVICE_CONTAINER_ID, result);
126                     FluentFuture< ? extends @NonNull CommitInfo> future = writeTx.commit();
127                     try {
128                         Futures.getChecked(future, ExecutionException.class);
129                         LOG.info("device writed to config datastore");
130                     } catch (ExecutionException e) {
131                         LOG.error("Failed to write device to config datastore");
132                     }
133                 } else {
134                     LOG.error("WriteTransaction object is null");
135                 }
136             } else {
137                 LOG.error("device gets from xml is null !!");
138             }
139         } else {
140             LOG.error("device ID from input is not the same from xml file");
141         }
142         return res;
143     }
144
145     /**
146      * write {@link Streams} data to operational device datastore.
147      *
148      * @return result {@link Boolean}
149      */
150     public boolean writeNetconfStream() {
151         Boolean result = false;
152         LOG.info("writting netconf stream to oper datastore");
153         Streams streams = this.netconfConfiguration.getNetconfStreamsData();
154         if (streams != null) {
155             LOG.info("Netconf Data gets from xml file is present");
156             InstanceIdentifier<Streams> iid = InstanceIdentifier.create(Netconf.class).child(Streams.class);
157 //            StreamNameType name = new StreamNameType("OPENROADM");
158 //            Streams netconfStreams = new StreamsBuilder()
159 //                    .setStream(Arrays.asList(new StreamBuilder()
160 //                            .setKey(new StreamKey(name))
161 //                            .setName(name)
162 //                            .build()))
163 //                    .build();
164             Streams netconfStreams = new StreamsBuilder(streams).build();
165             WriteTransaction writeTx = this.dataBroker.newWriteOnlyTransaction();
166             if (writeTx != null) {
167                 LOG.info("WriteTransaction is ok");
168                 writeTx.put(LogicalDatastoreType.OPERATIONAL, iid, netconfStreams);
169                 FluentFuture< ? extends @NonNull CommitInfo> future = writeTx.commit();
170                 try {
171                     Futures.getChecked(future, ExecutionException.class);
172                     LOG.info("netconf stream writed to oper datastore");
173                     result = true;
174                 } catch (ExecutionException e) {
175                     LOG.error("Failed to write netconf stream to oper datastore");
176                 }
177             } else {
178                 LOG.error("WriteTransaction object is null");
179             }
180         } else {
181             LOG.error("Netconf data gets from xml file is null !");
182         }
183         return result;
184     }
185 }