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