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;
17 import java.util.concurrent.ExecutionException;
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;
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;
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;
42 * @authors Gilles THOUENON and Christophe BETOULE ( gilles.thouenon@orange.com, christophe.betoule@orange.com )
44 public class OcPlatformReaderFactory implements ReaderFactory {
46 private static final Logger LOG = LoggerFactory.getLogger(OcPlatformReaderFactory.class);
47 public static final InstanceIdentifier<Components> COMPONENTS_ID =
48 InstanceIdentifier.create(Components.class);
51 @Named("device-databroker")
52 private DataBroker dataBroker;
55 private OcPlatformConfiguration ocPlatformConfiguration;
59 public void init(final ModifiableReaderRegistryBuilder registry) {
60 registry.add(new BindingBrokerReader<>(COMPONENTS_ID, dataBroker,LogicalDatastoreType.OPERATIONAL,
61 ComponentsBuilder.class));
62 if (writeXMLDataToOper()) {
68 * Write xml data from {@link PmConfiguration}
69 * to operational data.
72 public boolean writeXMLDataToOper() {
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();
86 Futures.getChecked(future, ExecutionException.class);
87 LOG.info("oc-platform writed to oper datastore");
89 } catch (ExecutionException e) {
90 LOG.error("Failed to write oc-platform to oper datastore");
93 LOG.error("WriteTransaction object is null");
96 LOG.error("get oc-platform data from xml file is null !");
102 * Load data to config oc-platform datastore.
105 private boolean loadConfigData() {
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 !");
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();
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");
127 LOG.error("WriteTransaction object is null");
130 LOG.error("components from xml is null !!");
133 LOG.error("no components obtained from xml file");