2 * Copyright © 2017 AT&T and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.transportpce.inventory;
10 import static org.opendaylight.transportpce.inventory.utils.StringUtils.getCurrentTimestamp;
11 import static org.opendaylight.transportpce.inventory.utils.StringUtils.prepareDashString;
12 import static org.opendaylight.transportpce.inventory.utils.StringUtils.prepareEmptyString;
14 import com.google.common.base.Preconditions;
16 import java.sql.Connection;
17 import java.sql.PreparedStatement;
18 import java.sql.ResultSet;
19 import java.sql.SQLException;
20 import java.util.concurrent.ExecutionException;
21 import javax.sql.DataSource;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.transportpce.common.Timeouts;
25 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
26 import org.opendaylight.transportpce.inventory.query.Queries;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.circuit.packs.CircuitPacks;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Info;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.shelf.Slots;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.shelves.Shelves;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 private static final Logger LOG = LoggerFactory.getLogger(INode.class);
39 private final DataSource dataSource;
40 private final DeviceTransactionManager deviceTransactionManager;
42 public INode(DataSource dataSource, DeviceTransactionManager deviceTransactionManager) {
43 this.dataSource = dataSource;
44 this.deviceTransactionManager = deviceTransactionManager;
47 public boolean addNode(Info deviceInfo) {
48 boolean sqlResult = false;
49 String query = Queries.getQuery().deviceInfoInsert().get();
50 LOG.debug("Running {} query ", query);
51 try (Connection connection = dataSource.getConnection();
52 PreparedStatement preparedStatement = connection.prepareStatement(query)) {
53 String[] prepareParameters = prepareDeviceInfoParameters(deviceInfo);
54 for (int i = 0; i < prepareParameters.length; i++) {
55 LOG.debug("Parameter {} has value {}", i + 1, prepareParameters[i]);
56 preparedStatement.setString(i + 1, prepareParameters[i]);
58 int executeUpdate = preparedStatement.executeUpdate();
59 LOG.info("#{} entries were added", executeUpdate);
61 } catch (SQLException e) {
62 LOG.error(e.getMessage(), e);
67 public boolean nodeExists(String nodeId) {
68 String selectTableSQL = "select count(*) node_exists from inv_dev_info where node_id = ?";
70 LOG.info("Checking if {} exists in DB", nodeId);
71 try (Connection connection = dataSource.getConnection();
72 PreparedStatement preparedStmt = connection.prepareStatement(selectTableSQL)) {
73 preparedStmt.setString(1, nodeId);
74 try (ResultSet rs = preparedStmt.executeQuery()) {
76 nodeExists = rs.getInt("node_exists");
77 LOG.debug("Found {} devices matching {}", nodeExists, nodeId);
80 } catch (SQLException e) {
81 LOG.error(e.getMessage(), e);
83 return nodeExists == 0 ? false : true;
86 public void getRoadmShelves(String nodeId) throws InterruptedException, ExecutionException {
87 InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class);
88 java.util.Optional<OrgOpenroadmDevice> deviceObject = deviceTransactionManager.getDataFromDevice(nodeId,
89 LogicalDatastoreType.OPERATIONAL, deviceIID, Timeouts.DEVICE_READ_TIMEOUT,
90 Timeouts.DEVICE_READ_TIMEOUT_UNIT);
92 LOG.info("Shelves size {}", deviceObject.get().getShelves().size());
93 try (Connection connection = dataSource.getConnection()) {
94 Preconditions.checkNotNull(connection);
95 for (int i = 0; i < deviceObject.get().getShelves().size(); i++) {
96 Shelves shelve = deviceObject.get().getShelves().get(i);
97 String shelfName = shelve.getShelfName();
99 LOG.info("Getting Shelve Details of {}", shelfName);
100 LOG.info("Slot Size {} ", shelve.getSlots().size());
102 persistShelveSlots(nodeId, shelve, connection);
104 persistShelves(nodeId, connection, shelve);
106 } catch (SQLException e1) {
107 LOG.error(e1.getMessage(), e1);
111 public void getCircuitPacks(String nodeId) throws InterruptedException, ExecutionException {
112 InstanceIdentifier<OrgOpenroadmDevice> deviceIID = InstanceIdentifier.create(OrgOpenroadmDevice.class);
113 java.util.Optional<OrgOpenroadmDevice> deviceObject =
114 deviceTransactionManager.getDataFromDevice(nodeId, LogicalDatastoreType.OPERATIONAL, deviceIID,
115 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
116 if (!deviceObject.isPresent()) {
117 LOG.warn("Device object {} was not found", nodeId);
120 LOG.info("Circuit pack size {}", deviceObject.get().getCircuitPacks().size());
122 try (Connection connection = dataSource.getConnection()) {
123 Preconditions.checkNotNull(connection);
124 for (int i = 0; i < deviceObject.get().getCircuitPacks().size(); i++) {
125 CircuitPacks cp = deviceObject.get().getCircuitPacks().get(i);
127 if (cp.getCpSlots() != null) {
128 persistCircuitPacksSlots(nodeId, cp, connection);
130 LOG.info("Everything {}", cp);
131 LOG.info("CP is {}", cp);
133 persistPorts(cp, connection);
135 persistCircuitPacks(nodeId, connection, cp);
137 } catch (SQLException e1) {
138 LOG.error(e1.getMessage(), e1);
142 private void persistCircuitPacks(String nodeId, Connection connection, CircuitPacks cp) {
143 String[] parameters = prepareCircuitPacksParameters(nodeId, cp);
144 String query = Queries.getQuery().deviceCircuitPackInsert().get();
145 try (PreparedStatement stmt = connection.prepareStatement(query)) {
146 for (int j = 0; j < parameters.length; j++) {
147 stmt.setString(j + 1, parameters[j]);
150 stmt.clearParameters();
151 } catch (SQLException e) {
152 LOG.error(e.getMessage(), e);
156 private void persistShelves(String nodeId, Connection connection, Shelves shelve) {
157 String[] shelvesParameter = prepareShelvesParameters(nodeId, shelve);
158 String query = Queries.getQuery().deviceShelfInsert().get();
159 try (PreparedStatement preparedStmt = connection.prepareStatement(query)) {
160 for (int j = 0; j < shelvesParameter.length; j++) {
161 preparedStmt.setString(j + 1, shelvesParameter[j]);
163 preparedStmt.execute();
164 preparedStmt.clearParameters();
165 } catch (SQLException e) {
166 LOG.error(e.getMessage(), e);
170 private void persistShelveSlots(String nodeId, Shelves shelves, Connection connection) {
171 String startTimetampStr = getCurrentTimestamp();
172 for (int i = 0; i < shelves.getSlots().size(); i++) {
173 Slots slot = shelves.getSlots().get(i);
174 LOG.info("Getting Slot Details of {}", slot.getSlotName());
175 String[] parameters = new String[] {nodeId, shelves.getShelfName(), slot.getSlotName(), slot.getLabel(),
176 slot.getProvisionedCircuitPack(), startTimetampStr, startTimetampStr};
177 String query = Queries.getQuery().deviceShelfSlotInsert().get();
178 try (PreparedStatement stmt = connection.prepareStatement(query)) {
179 for (int j = 0; j < parameters.length; j++) {
180 stmt.setString(j + 1, parameters[j]);
183 stmt.clearParameters();
184 } catch (SQLException e) {
185 LOG.error(e.getMessage(), e);
190 private void persistCircuitPacksSlots(String nodeId, CircuitPacks circuitPacks, Connection connection) {
191 LOG.warn("CP slots are not persisted yet");
194 private void persistPorts(CircuitPacks circuitPacks, Connection connection) {
195 LOG.warn("Ports are not persisted yet");
200 * Prepares parameters for device insert query.
202 * @param deviceInfo the device Info
203 * @return String[] a string
205 private static String[] prepareDeviceInfoParameters(Info deviceInfo) {
206 String startTimetampStr = getCurrentTimestamp();
208 return new String[] {prepareDashString(deviceInfo.getNodeId()), prepareDashString(deviceInfo.getNodeNumber()),
209 prepareDashString(deviceInfo.getNodeType().getName()), prepareDashString(deviceInfo.getClli()),
210 prepareDashString(deviceInfo.getVendor()), prepareDashString(deviceInfo.getModel()),
211 prepareDashString(deviceInfo.getSerialId()), prepareDashString(deviceInfo.getPrefixLength()),
212 prepareDashString(deviceInfo.getDefaultGateway()), prepareDashString(deviceInfo.getSource().getName()),
213 prepareDashString(deviceInfo.getCurrentIpAddress()),
214 prepareDashString(deviceInfo.getCurrentPrefixLength()),
215 prepareDashString(deviceInfo.getDefaultGateway()),
216 prepareDashString(deviceInfo.getMacAddress().getValue()),
217 prepareDashString(deviceInfo.getSoftwareVersion()), prepareDashString(deviceInfo.getTemplate()),
218 prepareDashString(deviceInfo.getCurrentDatetime()),
219 deviceInfo.getGeoLocation() != null ? prepareDashString(deviceInfo.getGeoLocation().getLatitude()) : "",
220 deviceInfo.getGeoLocation() != null ? prepareDashString(deviceInfo.getGeoLocation().getLongitude()) : "",
221 prepareDashString(deviceInfo.getMaxDegrees()), prepareDashString(deviceInfo.getMaxSrgs()), startTimetampStr,
226 * Prepares parameters for shelves.
228 * @param nodeId the node ID
229 * @param shelve the shelves
230 * @return String[] a string
232 private static String[] prepareShelvesParameters(String nodeId, Shelves shelve) {
233 String startTimestamp = getCurrentTimestamp();
235 return new String[] {nodeId, shelve.getShelfName(), shelve.getShelfType(), shelve.getRack(),
236 shelve.getShelfPosition(), prepareEmptyString(shelve.getAdministrativeState()), shelve.getVendor(),
237 shelve.getModel(), shelve.getSerialId(), shelve.getType(), shelve.getProductCode(),
238 prepareEmptyString(shelve.getManufactureDate()), shelve.getClei(), shelve.getHardwareVersion(),
239 prepareEmptyString(shelve.getOperationalState()), prepareEmptyString(shelve.getEquipmentState()),
240 prepareEmptyString(shelve.getDueDate()), startTimestamp, startTimestamp};
243 private static String[] prepareCircuitPacksParameters(String nodeId, CircuitPacks cpack) {
244 return new String[] {nodeId, cpack.getCircuitPackName(), cpack.getCircuitPackType(),
245 cpack.getCircuitPackProductCode(), prepareEmptyString(cpack.getAdministrativeState()),
246 cpack.getVendor(), cpack.getModel(), cpack.getSerialId(), cpack.getType(), cpack.getProductCode(),
247 prepareEmptyString(cpack.getManufactureDate()), cpack.getClei(), cpack.getHardwareVersion(),
248 prepareEmptyString(cpack.getOperationalState()), prepareEmptyString(cpack.getEquipmentState()),
249 cpack.getCircuitPackMode(), cpack.getShelf(), cpack.getSlot(), cpack.getSubSlot(),
250 cpack.getCircuitPackCategory().getType().getName(), cpack.getCircuitPackCategory().getExtension(),
251 prepareEmptyString(cpack.getDueDate()), cpack.getParentCircuitPack().getCircuitPackName(),
252 cpack.getParentCircuitPack().getCpSlotName()};