Network topology and inventory init
[transportpce.git] / inventory / src / main / java / org / opendaylight / transportpce / inventory / DeviceInventory.java
1 /*
2  * Copyright © 2017 AT&T and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.transportpce.inventory;
9
10 import java.sql.Connection;
11 import java.sql.PreparedStatement;
12 import java.sql.SQLException;
13 import java.text.SimpleDateFormat;
14 import java.util.Date;
15 import java.util.Optional;
16 import java.util.concurrent.ExecutionException;
17 import java.util.regex.Pattern;
18
19 import javax.sql.DataSource;
20
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.transportpce.common.Timeouts;
23 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.Info;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class DeviceInventory {
31     private static final String INSERT_ALARM_STRING =
32         "insert into inv_alarm_info(nodeid, probablecause, direction,extension,location,"
33             + "notificationid,type,raisetime,severity,circuitid,circuitpack,connection,degree,iface,"
34             + "internallink,physicallink,service,shelf,sharedriskgroup,port,portcircuitpack, create_date, update_date) "
35             + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
36
37     private static final Logger LOG = LoggerFactory.getLogger(DeviceInventory.class);
38
39     private final DataSource dataSource;
40     private final INode inode;
41     private final DeviceTransactionManager deviceTransactionManager;
42
43     public DeviceInventory(DataSource dataSource, INode inode,
44             DeviceTransactionManager deviceTransactionManager) {
45         this.dataSource = dataSource;
46         this.inode = inode;
47         this.deviceTransactionManager = deviceTransactionManager;
48     }
49
50     public void init() {
51         LOG.info("Initializing {}", DeviceInventory.class.getName());
52     }
53
54     public void initializeDevice(String deviceId) throws InterruptedException, ExecutionException {
55         InstanceIdentifier<Info> infoIID = InstanceIdentifier.create(OrgOpenroadmDevice.class).child(Info.class);
56         Optional<Info> infoOpt =
57                 this.deviceTransactionManager.getDataFromDevice(deviceId, LogicalDatastoreType.OPERATIONAL, infoIID,
58                         Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT);
59
60         Info deviceInfo;
61         if (infoOpt.isPresent()) {
62             deviceInfo = infoOpt.get();
63         } else {
64             LOG.warn("Could not get device info from DataBrooker");
65             return;
66         }
67         LOG.info("Creating Device Inventory {}", deviceInfo);
68         if (!this.inode.nodeExists(deviceId)) {
69             LOG.info("Adding node {} to inventory", deviceId);
70             this.inode.addNode(deviceInfo);
71             this.inode.getRoadmShelves(deviceId);
72             this.inode.getCircuitPacks(deviceId);
73         }
74     }
75
76     /**
77      * Stores the alarm into DB using {@link PreparedStatement}.
78      *
79      * @param alarmString an alarm string
80      * @return number of rows inserted
81      */
82     public int storeAlarm(String alarmString) {
83         String delimiter = "|";
84         String[] splitAlarmString = alarmString.split(Pattern.quote(delimiter));
85         int count = 0;
86         try (Connection connection = this.dataSource.getConnection();
87                 PreparedStatement statement = connection.prepareStatement(INSERT_ALARM_STRING)) {
88             LOG.debug("Inserting prepared stmt for {} query", INSERT_ALARM_STRING);
89             SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
90             java.util.Date startTimetamp = new Date();
91             String startTimetampStr = simpleDate.format(startTimetamp);
92
93             for (int i = 0; i < 21; i++) {
94                 String value = (splitAlarmString.length >= (i + 1)) ? splitAlarmString[i] : "";
95                 LOG.debug("Setting parameter {}, to {} in the insert alarm query", i + 1, value);
96                 statement.setString(i + 1, value);
97             }
98             statement.setString(22, startTimetampStr);
99             statement.setString(23, startTimetampStr);
100             LOG.debug("Setting current time and edited time to {}", startTimetampStr);
101             count = statement.executeUpdate();
102             LOG.debug("Statment {}, returned {}", INSERT_ALARM_STRING, count);
103             statement.clearParameters();
104         } catch (SQLException e) {
105             LOG.error(e.getMessage(), e);
106         }
107         return count;
108     }
109 }