Merge branch 'reporting' into 'master'

Reporting

See merge request entgra/carbon-device-mgt!445
feature/appm-store/pbac
Dharmakeerthi Lasantha 5 years ago
commit 38c0e0b544

@ -0,0 +1,78 @@
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.common.device.details;
import com.google.gson.Gson;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
import java.util.List;
public class DeviceDetailsWrapper {
DeviceInfo deviceInfo;
Device device;
List<Application> applications;
DeviceLocation location;
int tenantId;
public int getTenantId() {
return tenantId;
}
public void setTenantId(int tenantId) {
this.tenantId = tenantId;
}
public DeviceInfo getDeviceInfo() {
return deviceInfo;
}
public void setDeviceInfo(DeviceInfo deviceInfo) {
this.deviceInfo = deviceInfo;
}
public Device getDevice() {
return device;
}
public void setDevice(Device device) {
this.device = device;
}
public List<Application> getApplications() {
return applications;
}
public void setApplications(List<Application> applications) {
this.applications = applications;
}
public DeviceLocation getLocation() {
return location;
}
public void setLocation(DeviceLocation location) {
this.location = location;
}
public String getJSONString() {
Gson gson = new Gson();
return gson.toJson(this).toString();
}
}

@ -0,0 +1,44 @@
/*
* Copyright (c) 2020, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.common.exceptions;
public class EventPublishingException extends Exception {
private static final long serialVersionUID = -3251556311996070297L;
public EventPublishingException(String msg, Exception nestedEx) {
super(msg, nestedEx);
}
public EventPublishingException(String message, Throwable cause) {
super(message, cause);
}
public EventPublishingException(String msg) {
super(msg);
}
public EventPublishingException() {
super();
}
public EventPublishingException(Throwable cause) {
super(cause);
}
}

@ -139,4 +139,13 @@ public final class DeviceManagementConstants {
new Permission("/permission/admin/manage/portal", "ui.execute")
};
}
public static final class Report {
private Report() {
throw new AssertionError();
}
public static final String REPORTING_CONTEXT = "/event";
public static final String DEVICE_INFO_ENDPOINT = REPORTING_CONTEXT + "/device-info";
public static final String REPORTING_EVENT_HOST = "iot.reporting.event.host";
}
}

@ -25,10 +25,13 @@ import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.device.details.DeviceDetailsWrapper;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.EventPublishingException;
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation;
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
@ -38,6 +41,7 @@ import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO;
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDAOException;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import org.wso2.carbon.device.mgt.core.util.HttpReportingUtil;
import java.sql.SQLException;
import java.util.ArrayList;
@ -90,6 +94,18 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
device.getEnrolmentInfo().getId());
DeviceManagementDAOFactory.commitTransaction();
String reportingHost = System.getProperty(DeviceManagementConstants.Report
.REPORTING_EVENT_HOST);
if (reportingHost != null && !reportingHost.isEmpty()) {
DeviceDetailsWrapper deviceDetailsWrapper = new DeviceDetailsWrapper();
deviceDetailsWrapper.setDevice(device);
deviceDetailsWrapper.setDeviceInfo(deviceInfo);
deviceDetailsWrapper.getJSONString();
HttpReportingUtil.invokeApi(deviceDetailsWrapper.getJSONString(),
reportingHost + DeviceManagementConstants.Report.DEVICE_INFO_ENDPOINT);
}
//TODO :: This has to be fixed by adding the enrollment ID.
if (DeviceManagerUtil.isPublishDeviceInfoResponseEnabled()) {
Object[] metaData = {device.getDeviceIdentifier(), device.getType()};
@ -135,6 +151,9 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
} catch (DataPublisherConfigurationException e) {
DeviceManagementDAOFactory.rollbackTransaction();
throw new DeviceDetailsMgtException("Error occurred while publishing the device location information.", e);
} catch (EventPublishingException e) {
DeviceManagementDAOFactory.rollbackTransaction();
throw new DeviceDetailsMgtException("Error occurred while sending events", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}

@ -0,0 +1,47 @@
/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.core.util;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.wso2.carbon.device.mgt.common.exceptions.EventPublishingException;
import java.io.IOException;
public class HttpReportingUtil {
public static int invokeApi(String payload, String endpoint) throws EventPublishingException {
try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpPost apiEndpoint = new HttpPost(endpoint);
apiEndpoint.setHeader(HTTP.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString());
StringEntity requestEntity = new StringEntity(
payload, ContentType.APPLICATION_JSON);
apiEndpoint.setEntity(requestEntity);
HttpResponse response = client.execute(apiEndpoint);
return response.getStatusLine().getStatusCode();
} catch (IOException e) {
throw new EventPublishingException("Error occurred when " +
"invoking API. API endpoint: " + endpoint, e);
}
}
}
Loading…
Cancel
Save