Skip to content

Commit

Permalink
implement capture meta (#68)
Browse files Browse the repository at this point in the history
* implement capture meta

* Changes for pull request #68

* Now returns capture_id as well

* Get all and Remove capture meta procedures

* Complete endpoints for Capture meta feature

* E2E tests for new endpoints on Capture Meta

* Procedure change to return last inserted ID without extra variable assigning

* No longer defaults to 0 when returning ID on error

* Removed exception signature from tests
  • Loading branch information
Nefarious46 authored Aug 6, 2024
1 parent a7b6b09 commit edf191d
Show file tree
Hide file tree
Showing 12 changed files with 1,209 additions and 5 deletions.
11 changes: 6 additions & 5 deletions runConfigurations/SchemaWithoutData.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/V10__Trigger_duplicate_host.sql" />
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/V11__Trigger_duplicate_tag.sql" />
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/V12__Trigger_duplicate_host_g_x_log_g.sql" />
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/V13__updateRestrictTriggerCapture.sql" />
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/V17__updateRestrictTriggerHostXCapture.sql" />
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/V16__updateRestrictTriggerHostGroup.sql" />
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/V15__updateRestrictTriggerHost.sql" />
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/V14__updateRestrictTriggerCaptureGroup.sql" />
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/V18__HostPrimaryKey.sql" />
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/R__Procedure_add_FileCaptureMeta.sql" />
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/R__Procedure_add_host_group.sql" />
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/R__Procedure_add_g_x_g.sql" />
Expand Down Expand Up @@ -70,8 +66,13 @@
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/R__Procedure_Remove_Storage.sql" />
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/R__Procedure_Remove_Flow_Storage.sql" />
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/R__Procedure_Remove_Capture_Storage.sql" />
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/V18__add_application_meta.sql" />
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/R__Procedure_Retrieve_Capture_meta.sql" />
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/R__Procedure_add_capture_meta.sql" />
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/R__Procedure_Remove_Capture_Meta.sql" />
<script-file value="$PROJECT_DIR$/src/main/resources/db/migration/R__Procedure_Retrieve_All_Capture_metas.sql" />
<script-mode>FILE</script-mode>
<data-source id="4ca053e2-bcb1-4dbb-bb2d-719dc30f70f1" namespace="schema/&quot;cfe_18&quot;" />
<data-source id="75d30341-17bd-4dce-8efc-451abfa760eb" />
<method v="2" />
</configuration>
</component>
66 changes: 66 additions & 0 deletions src/main/java/com/teragrep/cfe18/CaptureMetaMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Main data management system (MDMS) cfe_18
* Copyright (C) 2021 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/
package com.teragrep.cfe18;

import com.teragrep.cfe18.handlers.entities.CaptureMeta;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface CaptureMetaMapper {
CaptureMeta addNewCaptureMeta(
int capture_id,
String capture_meta_key,
String capture_meta_value);

List<CaptureMeta> getCaptureMeta(int capture_id);

List<CaptureMeta> getAllCaptureMetas();

CaptureMeta deleteCaptureMeta(int capture_id);

}
204 changes: 204 additions & 0 deletions src/main/java/com/teragrep/cfe18/handlers/CaptureMetaController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*
* Main data management system (MDMS) cfe_18
* Copyright (C) 2021 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/
package com.teragrep.cfe18.handlers;

import com.teragrep.cfe18.CaptureMetaMapper;
import com.teragrep.cfe18.handlers.entities.CaptureMeta;
import com.teragrep.cfe18.handlers.entities.Flow;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.json.JSONObject;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping(path="/capture/meta")
@SecurityRequirement(name="api")
public class CaptureMetaController {

private static final Logger LOGGER = LoggerFactory.getLogger(CaptureMetaController.class);

@Autowired
DataSource dataSource;

@Autowired
SqlSessionTemplate sqlSessionTemplate;

@Autowired
CaptureMetaMapper captureMetaMapper;


@RequestMapping(path="/{capture_id}",method= RequestMethod.GET, produces="application/json")
@Operation(summary = "Fetch capture meta by capture id")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Found the capture meta",
content = {@Content(mediaType = "application/json",
schema = @Schema(implementation = CaptureMeta.class))}),
@ApiResponse(responseCode = "400", description = "Capture meta does not exist",
content = @Content)
})
public ResponseEntity<?> getApplicationMeta(@PathVariable("capture_id") int capture_id) {
try {
List<CaptureMeta> am = captureMetaMapper.getCaptureMeta(capture_id);
return new ResponseEntity<>(am, HttpStatus.OK);
} catch(Exception ex){
JSONObject jsonErr = new JSONObject();
jsonErr.put("id", capture_id);
final Throwable cause = ex.getCause();
if (cause instanceof SQLException) {
LOGGER.error((cause).getMessage());
String state = ((SQLException) cause).getSQLState();
if (state.equals("42000")) {
jsonErr.put("message", "Capture meta does not exist with given ID");
return new ResponseEntity<>(jsonErr.toString(), HttpStatus.BAD_REQUEST);
}
}
return new ResponseEntity<>("Unexpected error", HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@RequestMapping(path="/",method=RequestMethod.PUT,produces="application/json")
@Operation(summary = "Insert new capture meta for capture")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Capture meta created for capture",
content = {@Content(mediaType = "application/json",
schema = @Schema(implementation = CaptureMeta.class))}),
@ApiResponse(responseCode = "400", description = "Capture does not exist for inserting metadata",
content = @Content),
@ApiResponse(responseCode = "500", description = "Internal server error, contact admin", content = @Content)
})
public ResponseEntity<String> newCaptureMeta(@RequestBody CaptureMeta newCaptureMeta){
LOGGER.info("About to insert <[{}]>", newCaptureMeta);
JSONObject jsonErr = new JSONObject();
jsonErr.put("id", newCaptureMeta.getCapture_id());
try {
CaptureMeta cm = captureMetaMapper.addNewCaptureMeta(
newCaptureMeta.getCapture_id(),
newCaptureMeta.getCapture_meta_key(),
newCaptureMeta.getCapture_meta_value()
);
LOGGER.debug("Values returned <[{}]>",cm);
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", cm.getCapture_id());
jsonObject.put("message", "New capture meta created for = " + cm.getCapture_id());
return new ResponseEntity<>(jsonObject.toString(), HttpStatus.CREATED);
} catch (Exception ex) {
final Throwable cause = ex.getCause();
if (cause instanceof SQLException) {
LOGGER.error((cause).getMessage());
String state = ((SQLException) cause).getSQLState();
if (state.equals("42000")) {
jsonErr.put("message", "Capture does not exist");
return new ResponseEntity<>(jsonErr.toString(), HttpStatus.BAD_REQUEST);
}
}
return new ResponseEntity<>("Unexpected error", HttpStatus.INTERNAL_SERVER_ERROR);
}
}

@RequestMapping(path = "", method = RequestMethod.GET, produces = "application/json")
@Operation(summary = "Fetch all capture metas", description = "Will return empty list if there are no capture metas to fetch")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Capture metas fetched",
content = {@Content(mediaType = "application/json",
schema = @Schema(implementation = CaptureMeta.class))})
})
public List<CaptureMeta> getAllCaptureMetas(){
return captureMetaMapper.getAllCaptureMetas();
}

// Delete
@RequestMapping(path = "/{capture_id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "Delete capture meta")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Capture meta deleted",
content = {@Content(mediaType = "application/json",
schema = @Schema(implementation = CaptureMeta.class))}),
@ApiResponse(responseCode = "400", description = "Capture meta does not exist",
content = @Content),
@ApiResponse(responseCode = "500", description = "Internal server error, contact admin", content = @Content)
})
public ResponseEntity<String> removeCaptureMeta(@PathVariable("capture_id") int capture_id) {
LOGGER.info("Deleting Capture meta <[{}]>",capture_id);
try {
captureMetaMapper.deleteCaptureMeta(capture_id);
JSONObject j = new JSONObject();
j.put("id", capture_id);
j.put("message", "capture meta " + capture_id + " deleted.");
return new ResponseEntity<>(j.toString(), HttpStatus.OK);
} catch (Exception ex) {
JSONObject jsonErr = new JSONObject();
jsonErr.put("id", capture_id);
final Throwable cause = ex.getCause();
if (cause instanceof SQLException) {
LOGGER.error((cause).getMessage());
String state = ((SQLException) cause).getSQLState();
if (state.equals("45000")) {
jsonErr.put("message", "Record does not exist");
}
return new ResponseEntity<>(jsonErr.toString(), HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>("Unexpected error", HttpStatus.INTERNAL_SERVER_ERROR);
}

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Main data management system (MDMS) cfe_18
* Copyright (C) 2021 Suomen Kanuuna Oy
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://github.com/teragrep/teragrep/blob/main/LICENSE>.
*
*
* Additional permission under GNU Affero General Public License version 3
* section 7
*
* If you modify this Program, or any covered work, by linking or combining it
* with other code, such other code is not for that reason alone subject to any
* of the requirements of the GNU Affero GPL version 3 as long as this Program
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
* modifications.
*
* Supplemented terms under GNU Affero General Public License version 3
* section 7
*
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
* versions must be marked as "Modified version of" The Program.
*
* Names of the licensors and authors may not be used for publicity purposes.
*
* No rights are granted for use of trade names, trademarks, or service marks
* which are in The Program if any.
*
* Licensee must indemnify licensors and authors for any liability that these
* contractual assumptions impose on licensors and authors.
*
* To the extent this program is licensed as part of the Commercial versions of
* Teragrep, the applicable Commercial License may apply to this file if you as
* a licensee so wish it.
*/
package com.teragrep.cfe18.handlers.entities;

public class CaptureMeta {

public int capture_id;
public String capture_meta_key;
public String capture_meta_value;

public int getCapture_id() {
return capture_id;
}

public void setCapture_id(int capture_id) {
this.capture_id = capture_id;
}


public String getCapture_meta_key() {
return capture_meta_key;
}

public void setCapture_meta_key(String capture_meta_key) {
this.capture_meta_key = capture_meta_key;
}

public String getCapture_meta_value() {
return capture_meta_value;
}

public void setCapture_meta_value(String capture_meta_value) {
this.capture_meta_value = capture_meta_value;
}

@Override
public String toString() {
return "CaptureMeta{" +
"capture_id=" + capture_id +
", capture_meta_key='" + capture_meta_key + '\'' +
", capture_meta_value='" + capture_meta_value + '\'' +
'}';
}
}
Loading

0 comments on commit edf191d

Please sign in to comment.