Commit 496ef80e authored by Kostis Trantzas's avatar Kostis Trantzas
Browse files

Merge branch...

Merge branch '1-provision-of-code-and-readme-file-for-service-specification-importing-utility' into 'develop'

Resolve "Provision of code and readme file for Service Specification importing utility"

See merge request osl/utilities/service-specification-importing!2
parents 4f160fab 5cd86cf3
Loading
Loading
Loading
Loading
+51 −70
Original line number Diff line number Diff line
# Service Specification importing
```markdown
# Service Specification Uploader

## Overview
The Service Specification Uploader is a Java-based application designed to upload service and resource specifications to a specified server. It supports both physical and logical resource specifications and handles various relationships and attachments.

## Prerequisites
- Java 17
- Maven
- Docker (optional, for containerized deployment)

## Getting started
## Setup

To make it easy for you to get started with GitLab, here's a list of recommended next steps.

Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!

## Add your files

- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:

```
cd existing_repo
git remote add origin https://labs.etsi.org/rep/osl/utilities/service-specification-importing.git
git branch -M main
git push -uf origin main
### Clone the Repository
```sh
git clone https://github.com/your-repo/service-specification-uploader.git
cd service-specification-uploader
```

## Integrate with your tools

- [ ] [Set up project integrations](https://labs.etsi.org/rep/osl/utilities/service-specification-importing/-/settings/integrations)

## Collaborate with your team

- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Automatically merge when pipeline succeeds](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)

## Test and Deploy

Use the built-in continuous integration in GitLab.

- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing(SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
### Build the Project
```sh
mvn clean install
```

***
### Configuration
Ensure you have a `config.properties` file in the root directory with the necessary configurations.

# Editing this README
### Running the Application
```sh
java -jar target/servicespecificationuploader.jar --configfile config.properties --servicespecfolderpath /path/to/your/data-folder
```

When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thank you to [makeareadme.com](https://www.makeareadme.com/) for this template.
## Docker Deployment

## Suggestions for a good README
Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
### Build Docker Image
```sh
docker build -t service-specification-uploader .
```

## Name
Choose a self-explaining name for your project.
### Run Docker Container
```sh
docker run -v /local/path/to/config.properties:/app/config.properties -v /local/path/to/your-data-folder:/app/data-folder service-specification-uploader /app/data-folder additional-value

## Description
Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
docker run -v C:\openslice\servicespecificationuploader\src\main\resources\config.properties:/app/config.properties -v C:\openslice\servicespecificationuploader:/app/data-folder servicespecificationuploader  "c:\openslice\servicespecificationfetcher\f51cb185-6663-49e8-b195-83deeae32eed 
```

## Badges
On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
## Logging
The application uses SLF4J with Logback for logging. Logs are written to both the console and a file named `app.log`.

## Visuals
Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
### Logback Configuration
The `logback.xml` file is located in `src/main/resources` and can be customized as needed.

## Installation
Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
## Methods

## Usage
Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
### `postPhysicalResourceSpec`
Uploads a physical resource specification.

## Support
Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
### `postLogicalResourceSpec`
Uploads a logical resource specification.

## Roadmap
If you have ideas for releases in the future, it is a good idea to list them in the README.
### `replaceStrings`
Replaces strings in a JSON node based on a provided mapping.

## Contributing
State if you are open to contributions and what your requirements are for accepting them.

For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.

You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.

## Authors and acknowledgment
Show your appreciation to those who have contributed to the project.
1. Fork the repository.
2. Create a new branch (`git checkout -b feature-branch`).
3. Commit your changes (`git commit -am 'Add new feature'`).
4. Push to the branch (`git push origin feature-branch`).
5. Create a new Pull Request.

## License
For open source projects, say how it is licensed.

## Project status
If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
This project is licensed under the Apache Version 2.0 License.
```
 No newline at end of file

pom.xml

0 → 100644
+100 −0
Original line number Diff line number Diff line
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.etsi.osl.util</groupId>
  <artifactId>servicespecificationuploader</artifactId>
  <version>0.0.1-SNAPSHOT</version>
    <!-- Specify the Java version you're using -->
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- Jackson JSON Processor -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.3</version>
        </dependency>

        <!-- Google Gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>

        <!-- Your custom library dependency -->
	    <dependency>
	        <groupId>org.etsi.osl</groupId>
	        <artifactId>org.etsi.osl.model.tmf</artifactId>
	        <version>1.0.0</version>
	    </dependency>

        <!-- SnakeYAML -->
        <dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>2.0</version>
        </dependency>

        <!-- Commons cli -->
        <!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.9.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <!-- Maven Compiler Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>17</source>
                    <target>17</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>org.etsi.osl.util.ServiceSpecificationUploader</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.4.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <createDependencyReducedPom>true</createDependencyReducedPom>
                            <transformers>
                                <!-- Ensures that the manifest specifies the main class -->
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>org.etsi.osl.util.ServiceSpecificationUploader</mainClass> <!-- Fully Qualified Name -->
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>  
+197 −0
Original line number Diff line number Diff line
package org.etsi.osl.util;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.node.*;
import org.yaml.snakeyaml.Yaml;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.*;

public class JsonStringReplacer {

    // Logger instance
    private static final Logger logger = LoggerFactory.getLogger(JsonStringReplacer.class);

    // Static variable to hold mappings
    private static Map<String, String> mappings = new HashMap<>();

    /**
     * Sets the YAML file path and loads mappings.
     * If the file is missing, it sets mappings to an empty map and continues.
     *
     * @param yamlFilePath The path to the YAML mappings file.
     */
    public static void setYamlFilePath(String yamlFilePath) {
        try {
            mappings = loadYamlMappings(yamlFilePath);
        } catch (FileNotFoundException e) {
            // Mappings file is missing; proceed with empty mappings
            logger.warn("Mappings file not found: {}. Proceeding without replacements.", yamlFilePath);
            mappings = new HashMap<>(); // Ensure mappings is empty
        } catch (IOException e) {
            // Other I/O errors
            logger.error("Error reading mappings file: {}. Proceeding without replacements.", e.getMessage());
            mappings = new HashMap<>(); // Ensure mappings is empty
        } catch (Exception e) {
            // Handle any other exceptions
            logger.error("Unexpected error: {}. Proceeding without replacements.", e.getMessage(), e);
            mappings = new HashMap<>(); // Ensure mappings is empty
        }
    }

    /**
     * Replaces strings in a JSON content based on the loaded mappings.
     * If mappings are empty, it returns the original JSON content.
     *
     * @param jsonContent The input JSON content as a string.
     * @return The modified JSON content as a string.
     * @throws IOException If an I/O error occurs during processing.
     */
    public static String replaceJsonStrings(String jsonContent) throws IOException {
        // If mappings are empty, return the original JSON content
        if (mappings == null || mappings.isEmpty()) {
            logger.info("Mappings are empty. Returning original JSON content.");
            return jsonContent;
        }

        // Create an ObjectMapper for JSON parsing
        ObjectMapper mapper = new ObjectMapper();

        // Parse the JSON content into a JsonNode
        JsonNode rootNode = mapper.readTree(jsonContent);

        // Perform string replacements
        replaceStrings(rootNode);

        // Convert the modified JsonNode back into a JSON string
        String result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode);
        logger.debug("Replacements completed successfully.");
        return result;
    }

    /**
     * Loads mappings from a YAML file.
     *
     * @param yamlFilePath The file path to the YAML mappings file.
     * @return A Map containing the mappings.
     * @throws IOException If an error occurs while reading the YAML file.
     */
    private static Map<String, String> loadYamlMappings(String yamlFilePath) throws IOException {
        Yaml yaml = new Yaml();
        Map<String, String> loadedMappings = new HashMap<>();

        try (InputStream inputStream = new FileInputStream(yamlFilePath)) {
            Map<String, Object> yamlMaps = yaml.load(inputStream);

            if (yamlMaps == null || !yamlMaps.containsKey("mappings")) {
                logger.warn("The YAML file does not contain a 'mappings' key. Proceeding without replacements.");
                return loadedMappings; // Return empty mappings
            }

            Object mappingsObj = yamlMaps.get("mappings");

            if (mappingsObj instanceof List) {
                List<?> mappingsList = (List<?>) mappingsObj;
                for (Object item : mappingsList) {
                    if (item instanceof Map) {
                        Map<?, ?> mapItem = (Map<?, ?>) item;
                        Object fromObj = mapItem.get("from");
                        Object toObj = mapItem.get("to");

                        if (fromObj instanceof String && toObj instanceof String) {
                            loadedMappings.put((String) fromObj, (String) toObj);
                        } else {
                            logger.warn("Invalid mapping entry: 'from' and 'to' must be strings. Skipping entry.");
                        }
                    } else {
                        logger.warn("Invalid mapping format. Each mapping entry must be a map with 'from' and 'to' keys. Skipping entry.");
                    }
                }
            } else if (mappingsObj instanceof Map) {
                Map<?, ?> mappingsMap = (Map<?, ?>) mappingsObj;
                for (Map.Entry<?, ?> entry : mappingsMap.entrySet()) {
                    if (entry.getKey() instanceof String && entry.getValue() instanceof String) {
                        loadedMappings.put((String) entry.getKey(), (String) entry.getValue());
                    } else {
                        logger.warn("Invalid mapping entry: keys and values must be strings. Skipping entry.");
                    }
                }
            } else {
                logger.warn("Unrecognized 'mappings' format in YAML file. Proceeding without replacements.");
            }
        }

        logger.info("Loaded {} mappings from YAML file.", loadedMappings.size());
        return loadedMappings;
    }

    /**
     * Recursively replaces strings in a JsonNode based on the loaded mappings.
     *
     * @param node The JsonNode to process.
     */
    private static void replaceStrings(JsonNode node) {
        if (node == null || mappings == null || mappings.isEmpty()) {
            return;
        }

        if (node.isObject()) {
            ObjectNode objNode = (ObjectNode) node;
            Iterator<Map.Entry<String, JsonNode>> fields = objNode.fields();
            while (fields.hasNext()) {
                Map.Entry<String, JsonNode> entry = fields.next();
                JsonNode childNode = entry.getValue();

                if (childNode.isTextual()) {
                    String textValue = childNode.asText();
                    if (mappings.containsKey(textValue)) {
                        String replacement = mappings.get(textValue);
                        objNode.put(entry.getKey(), replacement);
                        logger.debug("Replaced text '{}' with '{}' in object field '{}'.", textValue, replacement, entry.getKey());
                    }
                } else {
                    replaceStrings(childNode);
                }
            }
        } else if (node.isArray()) {
            ArrayNode arrayNode = (ArrayNode) node;
            for (int i = 0; i < arrayNode.size(); i++) {
                JsonNode element = arrayNode.get(i);

                if (element.isTextual()) {
                    String textValue = element.asText();
                    if (mappings.containsKey(textValue)) {
                        String replacement = mappings.get(textValue);
                        arrayNode.set(i, new TextNode(replacement));
                        logger.debug("Replaced text '{}' with '{}' in array index {}.", textValue, replacement, i);
                    }
                } else {
                    replaceStrings(element);
                }
            }
        }
        // Other node types are not modified
    }

    // Example usage in the main method
    public static void main(String[] args) {
        String jsonInput = "{ \"greeting\": \"string1\", \"farewell\": \"string2\", \"nested\": { \"message\": \"string3\" }, \"array\": [\"string1\", \"hello\", \"string2\"] }";
        String yamlFilePath = "org/etsi/osl/util/mappings.yaml"; // Adjust the path as needed

        // Set the YAML path from another class or context
        JsonStringReplacer.setYamlFilePath(yamlFilePath);

        try {
            // Perform the replacement
            String result = JsonStringReplacer.replaceJsonStrings(jsonInput);

            // Output the modified JSON
            System.out.println("Modified JSON:");
            System.out.println(result);
        } catch (IOException e) {
            logger.error("An error occurred during JSON processing: {}", e.getMessage(), e);
        }
    }
}
+175 −0

File added.

Preview size limit exceeded, changes collapsed.

+60 −0
Original line number Diff line number Diff line
package org.etsi.osl.util;

import org.etsi.osl.tmf.common.model.TimePeriod;
import org.etsi.osl.tmf.rcm634.model.ResourceSpecification;
import org.etsi.osl.tmf.rcm634.model.ResourceSpecificationCreate;
import org.etsi.osl.tmf.rcm634.model.ResourceSpecificationUpdate;

import java.util.ArrayList;

public class ResourceSpecificationPayloadsMaker {

    // Change ServiceSpecificationCreate to ResourceSpecificationCreate
    public static ResourceSpecificationCreate createResourceSpecDataFromFile(ResourceSpecification resourceSpecFromFile) {
        // Parse the JSON file into a ServSpecCr object
        ResourceSpecificationCreate newResourceSpec = new ResourceSpecificationCreate();
        newResourceSpec.setName(resourceSpecFromFile.getName());
        newResourceSpec.setDescription(resourceSpecFromFile.getDescription());
        //newServSpec.setAttachment(new ArrayList<>(resourceSpecFromFile.getAttachment()));
        newResourceSpec.setIsBundle(resourceSpecFromFile.isIsBundle());
        //newServSpec.setRelatedParty(new ArrayList<>(resourceSpecFromFile.getRelatedParty()));
        //newServSpec.setResourceSpecification(new ArrayList<>(resourceSpecFromFile.getResourceSpecification()));
        //newServSpec.setServiceLevelSpecification(new ArrayList<>(resourceSpecFromFile.getServiceLevelSpecification()));
        //newServSpec.setServiceSpecCharacteristic(new ArrayList<>(resourceSpecFromFile.getServiceSpecCharacteristic()));
        //newServSpec.setServiceSpecRelationship(new ArrayList<>(resourceSpecFromFile.getServiceSpecRelationship()));
        newResourceSpec.setVersion(resourceSpecFromFile.getVersion());
        newResourceSpec.setValidFor(new TimePeriod());

        if (resourceSpecFromFile.getType() != null) {
            newResourceSpec.setType( resourceSpecFromFile.getType() );
        }
        return newResourceSpec;
    }

    public static ResourceSpecificationUpdate updateResourceSpecDataFromFile(ResourceSpecification resourceSpecFromFile) {
        ResourceSpecificationUpdate newResourceSpecToUpdate = new ResourceSpecificationUpdate();

        if (resourceSpecFromFile.getName() != null) {
            newResourceSpecToUpdate.setName(resourceSpecFromFile.getName());
        }

        if (resourceSpecFromFile.getDescription() != null) {
            newResourceSpecToUpdate.setDescription(resourceSpecFromFile.getDescription());
        }

        if (resourceSpecFromFile.isIsBundle() != null) {
            newResourceSpecToUpdate.setIsBundle(resourceSpecFromFile.isIsBundle());
        }

        if (resourceSpecFromFile.getVersion() != null) {
            newResourceSpecToUpdate.setVersion(resourceSpecFromFile.getVersion());
        }

        if (resourceSpecFromFile.getResourceSpecCharacteristic() != null) {
            System.out.println("ServiceSpecCharacteristic: " + resourceSpecFromFile.getResourceSpecCharacteristic());
            newResourceSpecToUpdate.setResourceSpecificationCharacteristic(new ArrayList<>(resourceSpecFromFile.getResourceSpecCharacteristic()));
        }

        return newResourceSpecToUpdate;
    }
}
 No newline at end of file
Loading