2016-02-12 07:40:19 -06:00
|
|
|
/*
|
|
|
|
Copyright (C) 2016 Srivats P.
|
|
|
|
|
|
|
|
This file is part of "Ostinato"
|
|
|
|
|
|
|
|
This is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "devicegroupmodel.h"
|
|
|
|
|
|
|
|
#include "port.h"
|
|
|
|
|
|
|
|
#include "emulproto.pb.h"
|
2016-03-08 07:19:28 -06:00
|
|
|
#include "uint128.h"
|
|
|
|
|
|
|
|
#include <QHostAddress>
|
2020-03-07 01:30:17 -06:00
|
|
|
#include <QMimeData>
|
|
|
|
|
|
|
|
const QLatin1String kDeviceGroupsMimeType(
|
|
|
|
"application/vnd.ostinato.devicegroups");
|
2016-02-12 07:40:19 -06:00
|
|
|
|
|
|
|
enum {
|
|
|
|
kName,
|
2016-02-16 07:27:08 -06:00
|
|
|
kVlanCount,
|
|
|
|
kDeviceCount, // Across all vlans
|
2016-02-12 07:40:19 -06:00
|
|
|
kIp,
|
2016-03-08 07:19:28 -06:00
|
|
|
kIp4Address,
|
|
|
|
kIp6Address,
|
2016-02-12 07:40:19 -06:00
|
|
|
kFieldCount
|
|
|
|
};
|
|
|
|
|
|
|
|
static QStringList columns_ = QStringList()
|
|
|
|
<< "Name"
|
2016-02-16 07:27:08 -06:00
|
|
|
<< "Vlans"
|
|
|
|
<< "Devices"
|
2016-03-08 07:19:28 -06:00
|
|
|
<< "IP Stack"
|
|
|
|
<< "IPv4 Address"
|
|
|
|
<< "IPv6 Address";
|
2016-02-12 07:40:19 -06:00
|
|
|
|
|
|
|
DeviceGroupModel::DeviceGroupModel(QObject *parent)
|
|
|
|
: QAbstractTableModel(parent)
|
|
|
|
{
|
|
|
|
port_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DeviceGroupModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
if (!port_ || parent.isValid())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return port_->numDeviceGroups();
|
|
|
|
}
|
|
|
|
|
|
|
|
int DeviceGroupModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
if (parent.isValid())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return columns_.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DeviceGroupModel::headerData(
|
|
|
|
int section,
|
|
|
|
Qt::Orientation orientation,
|
|
|
|
int role) const
|
|
|
|
{
|
|
|
|
if (role != Qt::DisplayRole)
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
switch (orientation) {
|
|
|
|
case Qt::Horizontal:
|
|
|
|
return columns_[section];
|
|
|
|
case Qt::Vertical:
|
|
|
|
return QString("%1").arg(section + 1);
|
|
|
|
default:
|
|
|
|
Q_ASSERT(false); // Unreachable
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DeviceGroupModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!port_ || !index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
int dgIdx = index.row();
|
|
|
|
int field = index.column();
|
|
|
|
|
|
|
|
Q_ASSERT(dgIdx < port_->numDeviceGroups());
|
|
|
|
Q_ASSERT(field < kFieldCount);
|
|
|
|
|
2016-03-16 10:21:36 -05:00
|
|
|
const OstProto::DeviceGroup *devGrp = port_->deviceGroupByIndex(dgIdx);
|
2016-02-12 07:40:19 -06:00
|
|
|
|
2016-02-16 07:27:08 -06:00
|
|
|
Q_ASSERT(devGrp);
|
|
|
|
|
2016-02-12 07:40:19 -06:00
|
|
|
switch (field) {
|
|
|
|
case kName:
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
return QString::fromStdString(devGrp->core().name());
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
|
2016-02-16 07:27:08 -06:00
|
|
|
case kVlanCount:
|
2016-02-12 07:40:19 -06:00
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
2016-02-16 07:27:08 -06:00
|
|
|
if (int v = vlanCount(devGrp))
|
|
|
|
return v;
|
|
|
|
return QString("None");
|
2016-02-12 07:40:19 -06:00
|
|
|
case Qt::TextAlignmentRole:
|
2019-01-21 07:45:06 -06:00
|
|
|
return static_cast<int>(Qt::AlignRight|Qt::AlignVCenter);
|
2016-02-12 07:40:19 -06:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
|
2016-02-16 07:27:08 -06:00
|
|
|
case kDeviceCount:
|
2016-02-12 07:40:19 -06:00
|
|
|
switch (role) {
|
2016-02-16 07:27:08 -06:00
|
|
|
case Qt::DisplayRole:
|
|
|
|
return qMax(vlanCount(devGrp), 1)*devGrp->device_count();
|
2016-02-12 07:40:19 -06:00
|
|
|
case Qt::TextAlignmentRole:
|
2019-01-21 07:45:06 -06:00
|
|
|
return static_cast<int>(Qt::AlignRight|Qt::AlignVCenter);
|
2016-02-12 07:40:19 -06:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
case kIp:
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
if (devGrp->HasExtension(OstEmul::ip4))
|
|
|
|
if (devGrp->HasExtension(OstEmul::ip6))
|
|
|
|
return QString("Dual Stack");
|
|
|
|
else
|
|
|
|
return QString("IPv4");
|
|
|
|
else if (devGrp->HasExtension(OstEmul::ip6))
|
|
|
|
return QString("IPv6");
|
|
|
|
else
|
|
|
|
return QString("None");
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
|
2016-03-08 07:19:28 -06:00
|
|
|
case kIp4Address:
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
if (devGrp->HasExtension(OstEmul::ip4))
|
|
|
|
return QHostAddress(
|
2016-03-16 10:21:36 -05:00
|
|
|
devGrp->GetExtension(OstEmul::ip4)
|
|
|
|
.address()).toString();
|
2016-03-08 07:19:28 -06:00
|
|
|
else
|
|
|
|
return QString("--");
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
case kIp6Address:
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
if (devGrp->HasExtension(OstEmul::ip6)) {
|
2016-03-16 10:21:36 -05:00
|
|
|
OstEmul::Ip6Address ip = devGrp->GetExtension(
|
|
|
|
OstEmul::ip6).address();
|
2016-03-08 07:19:28 -06:00
|
|
|
return QHostAddress(
|
|
|
|
UInt128(ip.hi(), ip.lo()).toArray())
|
|
|
|
.toString();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return QString("--");
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
|
2016-02-12 07:40:19 -06:00
|
|
|
default:
|
2016-03-08 07:19:28 -06:00
|
|
|
Q_ASSERT(false); // unreachable!
|
2016-02-12 07:40:19 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
qWarning("%s: Unsupported field #%d", __FUNCTION__, field);
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeviceGroupModel::setData(
|
2016-06-01 10:21:29 -05:00
|
|
|
const QModelIndex & /*index*/,
|
|
|
|
const QVariant & /*value*/,
|
|
|
|
int /*role*/)
|
2016-02-12 07:40:19 -06:00
|
|
|
{
|
|
|
|
if (!port_)
|
|
|
|
return false;
|
|
|
|
|
2016-03-24 08:17:43 -05:00
|
|
|
// TODO; when implementing also implement flags() to
|
|
|
|
// return ItemIsEditable
|
2016-02-12 07:40:19 -06:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-07 01:30:17 -06:00
|
|
|
QStringList DeviceGroupModel::mimeTypes() const
|
|
|
|
{
|
|
|
|
return QStringList() << kDeviceGroupsMimeType;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMimeData* DeviceGroupModel::mimeData(const QModelIndexList &indexes) const
|
|
|
|
{
|
|
|
|
using ::google::protobuf::uint8;
|
|
|
|
|
|
|
|
if (indexes.isEmpty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// indexes may include multiple columns for a row - but we are only
|
|
|
|
// interested in rows 'coz we have a single data for all columns
|
|
|
|
// XXX: use QMap instead of QSet to keep rows in sorted order
|
|
|
|
QMap<int, int> rows;
|
|
|
|
foreach(QModelIndex index, indexes)
|
|
|
|
rows.insert(index.row(), index.row());
|
|
|
|
|
|
|
|
OstProto::DeviceGroupConfigList dgList;
|
|
|
|
dgList.mutable_port_id()->set_id(port_->id());
|
|
|
|
foreach(int row, rows) {
|
|
|
|
OstProto::DeviceGroup *devGrp = dgList.add_device_group();
|
|
|
|
devGrp->CopyFrom(*port_->deviceGroupByIndex(row));
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray data;
|
|
|
|
data.resize(dgList.ByteSize());
|
|
|
|
dgList.SerializeWithCachedSizesToArray((uint8*)data.data());
|
|
|
|
//qDebug("copy %s", dgList.DebugString().c_str());
|
|
|
|
//TODO: copy DebugString as text/plain?
|
|
|
|
|
|
|
|
QMimeData *mimeData = new QMimeData();
|
|
|
|
mimeData->setData(kDeviceGroupsMimeType, data);
|
|
|
|
return mimeData; // XXX: caller is expected to take ownership and free!
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeviceGroupModel::dropMimeData(const QMimeData *data,
|
|
|
|
Qt::DropAction action, int row, int /*column*/,
|
|
|
|
const QModelIndex &parent)
|
|
|
|
{
|
|
|
|
if (!data)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!data->hasFormat(kDeviceGroupsMimeType))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (action != Qt::CopyAction)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
OstProto::DeviceGroupConfigList dgList;
|
|
|
|
QByteArray ba(data->data(kDeviceGroupsMimeType));
|
|
|
|
dgList.ParseFromArray((void*)ba.constData(), ba.size());
|
|
|
|
//qDebug("paste %s", dgList.DebugString().c_str());
|
|
|
|
|
|
|
|
if ((row < 0) || (row > rowCount(parent)))
|
|
|
|
row = rowCount(parent);
|
|
|
|
|
|
|
|
// Delete rows that we are going to overwrite
|
|
|
|
int c = 0, count = dgList.device_group_size();
|
|
|
|
if (row < rowCount(parent))
|
|
|
|
removeRows(row, qMin(rowCount() - row, count));
|
|
|
|
|
|
|
|
beginInsertRows(parent, row, row+count-1);
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if (port_->newDeviceGroupAt(row+i, &dgList.device_group(i)))
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
endInsertRows();
|
|
|
|
|
|
|
|
if (c != count) {
|
|
|
|
qWarning("failed to copy rows in DeviceGroupModel at row %d; "
|
|
|
|
"requested = %d, actual = %d", row, count, c);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-22 07:17:51 -06:00
|
|
|
bool DeviceGroupModel::insertRows(
|
|
|
|
int row,
|
|
|
|
int count,
|
|
|
|
const QModelIndex &parent)
|
|
|
|
{
|
|
|
|
int c = 0;
|
|
|
|
|
|
|
|
Q_ASSERT(!parent.isValid());
|
|
|
|
|
|
|
|
beginInsertRows(parent, row, row+count-1);
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if (port_->newDeviceGroupAt(row))
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
endInsertRows();
|
|
|
|
|
|
|
|
if (c != count) {
|
|
|
|
qWarning("failed to insert rows in DeviceGroupModel at row %d; "
|
|
|
|
"requested = %d, actual = %d", row, count, c);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeviceGroupModel::removeRows(
|
|
|
|
int row,
|
|
|
|
int count,
|
|
|
|
const QModelIndex &parent)
|
|
|
|
{
|
|
|
|
int c = 0;
|
|
|
|
|
|
|
|
Q_ASSERT(!parent.isValid());
|
|
|
|
|
|
|
|
beginRemoveRows(parent, row, row+count-1);
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if (port_->deleteDeviceGroupAt(row))
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
endRemoveRows();
|
|
|
|
|
|
|
|
if (c != count) {
|
|
|
|
qWarning("failed to delete rows in DeviceGroupModel at row %d; "
|
|
|
|
"requested = %d, actual = %d", row, count, c);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-12 07:40:19 -06:00
|
|
|
void DeviceGroupModel::setPort(Port *port)
|
|
|
|
{
|
2018-03-14 11:28:56 -05:00
|
|
|
beginResetModel();
|
2016-02-12 07:40:19 -06:00
|
|
|
port_ = port;
|
2018-03-14 11:28:56 -05:00
|
|
|
endResetModel();
|
2016-02-12 07:40:19 -06:00
|
|
|
}
|
2016-02-16 07:27:08 -06:00
|
|
|
|
|
|
|
//
|
|
|
|
// ---------------------- Private Methods -----------------------
|
|
|
|
//
|
|
|
|
int DeviceGroupModel::vlanCount(const OstProto::DeviceGroup *deviceGroup) const
|
|
|
|
{
|
|
|
|
if (!deviceGroup->has_encap()
|
|
|
|
|| !deviceGroup->encap().HasExtension(OstEmul::vlan))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
OstEmul::VlanEmulation vlan = deviceGroup->encap()
|
|
|
|
.GetExtension(OstEmul::vlan);
|
|
|
|
int numTags = vlan.stack_size();
|
|
|
|
int count = 1;
|
|
|
|
|
|
|
|
for (int i = 0; i < numTags; i++)
|
|
|
|
count *= vlan.stack(i).count();
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|