Search in sources :

Example 1 with Tag

use of org.phoebus.olog.entity.Tag in project phoebus-olog by Olog.

the class TagRepository method deleteById.

@Override
public void deleteById(String tagName) {
    try {
        UpdateResponse response = client.update(new UpdateRequest(ES_TAG_INDEX, ES_TAG_TYPE, tagName).doc(jsonBuilder().startObject().field("state", State.Inactive).endObject()).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
        if (response.getResult().equals(UPDATED)) {
            BytesReference ref = client.get(new GetRequest(ES_TAG_INDEX, ES_TAG_TYPE, response.getId()), RequestOptions.DEFAULT).getSourceAsBytesRef();
            Tag deletedTag = mapper.readValue(ref.streamInput(), Tag.class);
            logger.log(Level.INFO, "Deleted tag " + deletedTag.toLogger());
        }
    } catch (DocumentMissingException e) {
        logger.log(Level.SEVERE, "Failed to delete tag: " + tagName + " because it does not exist", e);
        throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Failed to delete tag: " + tagName + " because it does not exist");
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Failed to delete tag: " + tagName, e);
        throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to delete tag: " + tagName);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) UpdateResponse(org.elasticsearch.action.update.UpdateResponse) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) GetRequest(org.elasticsearch.action.get.GetRequest) Tag(org.phoebus.olog.entity.Tag) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException)

Example 2 with Tag

use of org.phoebus.olog.entity.Tag in project phoebus-olog by Olog.

the class TagRepository method save.

/**
 */
@Override
public <S extends Tag> S save(S tag) {
    try {
        IndexRequest indexRequest = new IndexRequest(ES_TAG_INDEX, ES_TAG_TYPE, tag.getName()).source(mapper.writeValueAsBytes(tag), XContentType.JSON).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
        IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
        if (response.getResult().equals(CREATED) || response.getResult().equals(UPDATED)) {
            BytesReference ref = client.get(new GetRequest(ES_TAG_INDEX, ES_TAG_TYPE, response.getId()), RequestOptions.DEFAULT).getSourceAsBytesRef();
            Tag createdTag = mapper.readValue(ref.streamInput(), Tag.class);
            return (S) createdTag;
        }
        return null;
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Failed to create tag: " + tag, e);
        throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to create tag: " + tag);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IndexResponse(org.elasticsearch.action.index.IndexResponse) MultiGetRequest(org.elasticsearch.action.get.MultiGetRequest) GetRequest(org.elasticsearch.action.get.GetRequest) Tag(org.phoebus.olog.entity.Tag) IndexRequest(org.elasticsearch.action.index.IndexRequest) ResponseStatusException(org.springframework.web.server.ResponseStatusException) ResponseStatusException(org.springframework.web.server.ResponseStatusException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DocumentMissingException(org.elasticsearch.index.engine.DocumentMissingException)

Example 3 with Tag

use of org.phoebus.olog.entity.Tag in project phoebus-olog by Olog.

the class LogResourceTest method init.

@Before
public void init() {
    logbook1 = new Logbook("name1", "user");
    logbook2 = new Logbook("name2", "user");
    tag1 = new Tag("tag1");
    tag2 = new Tag("tag2");
    log1 = LogBuilder.createLog().id(1L).owner("owner").title("title").withLogbooks(Set.of(logbook1, logbook2)).description("description1").withTags(Set.of(tag1, tag2)).createDate(now).level("Urgent").build();
    log2 = LogBuilder.createLog().id(2L).owner("user").withLogbooks(Set.of(logbook1, logbook2)).description("description2").createDate(now).level("Urgent").build();
}
Also used : Logbook(org.phoebus.olog.entity.Logbook) Tag(org.phoebus.olog.entity.Tag) Before(org.junit.Before)

Example 4 with Tag

use of org.phoebus.olog.entity.Tag in project phoebus-olog by Olog.

the class TagRepositoryIT method createTag.

/**
 * Test the creation of a test tag
 *
 * @throws IOException
 */
@Test
public void createTag() throws IOException {
    tagRepository.save(testTag1);
    Optional<Tag> result = tagRepository.findById(testTag1.getName());
    assertThat("Failed to create Tag " + testTag1, result.isPresent() && result.get().equals(testTag1));
    // Manual cleanup since Olog does not delete things
    client.delete(new DeleteRequest(ES_TAG_INDEX, ES_TAG_TYPE, testTag1.getName()), RequestOptions.DEFAULT);
}
Also used : Tag(org.phoebus.olog.entity.Tag) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) Test(org.junit.Test)

Example 5 with Tag

use of org.phoebus.olog.entity.Tag in project phoebus-olog by Olog.

the class OlogTagsIT method handleTagsCreateCheck.

/**
 * Test {@link gov.bnl.log.OlogResourceDescriptors#TAG_RESOURCE_URI}.
 */
@Test
public void handleTagsCreateCheck() {
    // what
    // check(s) for create tags
    // e.g.
    // user without required role
    // content
    // json       - incomplete
    // name       - null, empty
    // state      - null, (empty, incorrect value (ok: Active, Inactive))
    // --------------------------------------------------------------------------------
    // Retrieve a Tag
    // x   List Tags
    // Create a Tag
    // x   Create Tags
    // Remove Tag
    Tag tag_check = new Tag();
    ObjectMapper mapper = new ObjectMapper();
    try {
        String[] response = ITUtil.doGetJson(HTTP_IP_PORT_OLOG_TAGS);
        ITUtil.assertResponseLength2CodeOKContent(response, ITUtil.EMPTY_JSON);
        Tag[] tags = new Tag[] { tag_t1_state_a, tag_t2_state_a, tag_t3_state_a, tag_t4_state_a, tag_t5_state_a, tag_t6_state_i, tag_t7_state_i, tag_t8_state_i, tag_t9_state_i, tag_t10_state_i, tag_check };
        response = ITUtil.runShellCommand(createCurlTagsForAdmin(mapper.writeValueAsString(tags)));
        ITUtil.assertResponseLength2Code(response, HttpURLConnection.HTTP_BAD_REQUEST);
        tag_check.setName(null);
        tags[10] = tag_check;
        response = ITUtil.runShellCommand(createCurlTagsForAdmin(mapper.writeValueAsString(tags)));
        ITUtil.assertResponseLength2Code(response, HttpURLConnection.HTTP_BAD_REQUEST);
        tag_check.setName("");
        tags[10] = tag_check;
        response = ITUtil.runShellCommand(createCurlTagsForAdmin(mapper.writeValueAsString(tags)));
        ITUtil.assertResponseLength2Code(response, HttpURLConnection.HTTP_BAD_REQUEST);
        response = ITUtil.doGetJson(HTTP_IP_PORT_OLOG_TAGS);
        ITUtil.assertResponseLength2CodeOKContent(response, ITUtil.EMPTY_JSON);
    } catch (IOException e) {
        fail();
    } catch (InterruptedException e) {
        fail();
    } catch (Exception e) {
        fail();
    }
}
Also used : Tag(org.phoebus.olog.entity.Tag) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

Tag (org.phoebus.olog.entity.Tag)17 IOException (java.io.IOException)10 Test (org.junit.Test)6 ResponseStatusException (org.springframework.web.server.ResponseStatusException)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 DocumentMissingException (org.elasticsearch.index.engine.DocumentMissingException)4 ArrayList (java.util.ArrayList)3 GetRequest (org.elasticsearch.action.get.GetRequest)3 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)3 BytesReference (org.elasticsearch.common.bytes.BytesReference)3 Logbook (org.phoebus.olog.entity.Logbook)3 List (java.util.List)2 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)2 IndexRequest (org.elasticsearch.action.index.IndexRequest)2 IndexResponse (org.elasticsearch.action.index.IndexResponse)2 Log (org.phoebus.olog.entity.Log)2 Property (org.phoebus.olog.entity.Property)2 InputStream (java.io.InputStream)1 URL (java.net.URL)1