<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.onap.cps</groupId>
<onap.nexus.url>https://nexus.onap.org</onap.nexus.url>
<releaseNexusPath>/content/repositories/releases/</releaseNexusPath>
<snapshotNexusPath>/content/repositories/snapshots/</snapshotNexusPath>
+ <modelmapper.version>2.3.8</modelmapper.version>
<spock-core.version>2.0-M2-groovy-3.0</spock-core.version>
<springboot.version>2.3.3.RELEASE</springboot.version>
<springfox.version>3.0.0</springfox.version>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.modelmapper</groupId>
+ <artifactId>modelmapper</artifactId>
+ <version>${modelmapper.version}</version>
+ </dependency>
</dependencies>
</dependencyManagement>
</project>
\ No newline at end of file
type: string
requestBody:
content:
- multipart/form-data:
+ application/json:
schema:
- required:
- - file
- properties:
- multipartFile:
- type: string
- description: multipartFile
- format: binary
+ title: Anchor
+ description: anchor
+ $ref: '#/components/schemas/Anchor'
required: true
responses:
- 200:
- description: OK
+ 201:
+ description: Created
content:
application/json:
schema:
- type: object
- 201:
- description: Created
- content: {}
+ type: string
401:
description: Unauthorized
content: {}
404:
description: Not Found
content: {}
-components: {}
\ No newline at end of file
+components:
+ schemas:
+ Anchor:
+ type: object
+ title: Anchor
+ required:
+ - anchorName
+ - namespace
+ - revision
+ properties:
+ anchorName:
+ type: string
+ namespace:
+ type: string
+ revision:
+ type: string
\ No newline at end of file
<groupId>org.apache.commons</groupId>\r
<artifactId>commons-lang3</artifactId>\r
</dependency>\r
+ <dependency>\r
+ <groupId>org.modelmapper</groupId>\r
+ <artifactId>modelmapper</artifactId>\r
+ </dependency>\r
<dependency>\r
<groupId>org.springframework.boot</groupId>\r
<artifactId>spring-boot-starter-test</artifactId>\r
--- /dev/null
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ * Copyright (C) 2020 Nordix Foundation. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.onap.cps.config;\r
+\r
+import org.modelmapper.ModelMapper;\r
+import org.springframework.context.annotation.Bean;\r
+import org.springframework.context.annotation.Configuration;\r
+import springfox.documentation.builders.PathSelectors;\r
+import springfox.documentation.builders.RequestHandlerSelectors;\r
+import springfox.documentation.spi.DocumentationType;\r
+import springfox.documentation.spring.web.plugins.Docket;\r
+\r
+@Configuration\r
+public class CpsConfig {\r
+\r
+ /**\r
+ * Swagger configuration.\r
+ */\r
+ @Bean\r
+ public Docket api() {\r
+ return new Docket(DocumentationType.OAS_30).select().apis(RequestHandlerSelectors.any())\r
+ .paths(PathSelectors.any()).build();\r
+ }\r
+\r
+ /**\r
+ * ModelMapper configuration.\r
+ */\r
+ @Bean\r
+ public ModelMapper modelMapper() {\r
+ return new ModelMapper();\r
+ }\r
+}
\ No newline at end of file
import java.io.IOException;
import java.io.OutputStream;
import javax.validation.Valid;
+import org.modelmapper.ModelMapper;
import org.onap.cps.api.CpService;
+import org.onap.cps.api.model.AnchorDetails;
import org.onap.cps.exceptions.CpsException;
import org.onap.cps.exceptions.CpsValidationException;
import org.onap.cps.rest.api.CpsRestApi;
+import org.onap.cps.rest.model.Anchor;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
@Autowired
private CpService cpService;
+ @Autowired
+ private ModelMapper modelMapper;
+
+ /**
+ * Create a new anchor.
+ *
+ * @param anchor the anchor details object.
+ * @param dataspaceName the dataspace name.
+ * @return a ResponseEntity with the anchor name.
+ */
@Override
- public ResponseEntity<Object> createAnchor(@Valid MultipartFile multipartFile, String dataspaceName) {
- return null;
+ public final ResponseEntity<String> createAnchor(@Valid Anchor anchor, String dataspaceName) {
+ final AnchorDetails anchorDetails = modelMapper.map(anchor, AnchorDetails.class);
+ anchorDetails.setDataspace(dataspaceName);
+ final String anchorName = cpService.createAnchor(anchorDetails);
+ return new ResponseEntity<String>(anchorName, HttpStatus.CREATED);
}
@Override
try {
final Gson gson = new Gson();
gson.fromJson(getJsonString(multipartFile), Object.class);
- } catch (JsonSyntaxException e) {
+ } catch (final JsonSyntaxException e) {
throw new CpsValidationException("Not a valid JSON file.", e);
}
}
try {
final File file = File.createTempFile("tempFile", ".yang");
file.deleteOnExit();
-
try (OutputStream outputStream = new FileOutputStream(file)) {
outputStream.write(multipartFile.getBytes());
}
return file;
- } catch (IOException e) {
+ } catch (final IOException e) {
throw new CpsException(e);
}
}
private static String getJsonString(final MultipartFile multipartFile) {
try {
return new String(multipartFile.getBytes());
- } catch (IOException e) {
+ } catch (final IOException e) {
throw new CpsException(e);
}
}
+++ /dev/null
-/*
- * ============LICENSE_START=======================================================
- * Copyright (C) 2020 Bell Canada. All rights reserved.
- * ================================================================================
- * Licensed 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.
- *
- * SPDX-License-Identifier: Apache-2.0
- * ============LICENSE_END=========================================================
- */
-
-package org.onap.cps.swagger.config;
-
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-import springfox.documentation.builders.PathSelectors;
-import springfox.documentation.builders.RequestHandlerSelectors;
-import springfox.documentation.spi.DocumentationType;
-import springfox.documentation.spring.web.plugins.Docket;
-
-/**
- * Swagger configuration.
- */
-@Configuration
-public class SpringFoxConfig {
-
- /**
- * Define api configuration.
- */
- @Bean
- public Docket api() {
- return new Docket(DocumentationType.OAS_30)
- .select()
- .apis(RequestHandlerSelectors.any())
- .paths(PathSelectors.any())
- .build();
- }
-}
package org.onap.cps.spi.entities;
+import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "dataspace")
-public class Dataspace {
+public class Dataspace implements Serializable {
+
+ private static final long serialVersionUID = 8395254649813051882L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
package org.onap.cps.spi.entities;\r
\r
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;\r
+import java.io.Serializable;\r
import javax.persistence.Column;\r
import javax.persistence.Entity;\r
import javax.persistence.FetchType;\r
import javax.persistence.OneToOne;\r
import javax.validation.constraints.NotNull;\r
import lombok.AllArgsConstructor;\r
+import lombok.Builder;\r
import lombok.Getter;\r
import lombok.NoArgsConstructor;\r
import lombok.Setter;\r
@Entity\r
@AllArgsConstructor\r
@NoArgsConstructor\r
+@Builder\r
@TypeDefs({@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)})\r
-public class Fragment {\r
+public class Fragment implements Serializable {\r
+\r
+ private static final long serialVersionUID = 7737669789097119667L;\r
\r
@Id\r
@GeneratedValue(strategy = GenerationType.IDENTITY)\r
@Column(columnDefinition = "jsonb")\r
private String attributes;\r
\r
+ @Column(columnDefinition = "text")\r
+ private String anchorName;\r
+\r
+ @NotNull\r
@ManyToOne(fetch = FetchType.LAZY)\r
@JoinColumn(name = "dataspace_id")\r
private Dataspace dataspace;\r
@OneToOne(fetch = FetchType.LAZY)\r
@JoinColumn(name = "parent_id")\r
private Fragment parentFragment;\r
+\r
+ @OneToOne(fetch = FetchType.LAZY)\r
+ @JoinColumn(name = "module_id")\r
+ private ModuleEntity module;\r
}\r
--- /dev/null
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ * Copyright (C) 2020 Nordix Foundation. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.onap.cps.spi.impl;\r
+\r
+import org.onap.cps.api.model.AnchorDetails;\r
+import org.onap.cps.exceptions.CpsNotFoundException;\r
+import org.onap.cps.exceptions.CpsValidationException;\r
+import org.onap.cps.spi.FragmentPersistenceService;\r
+import org.onap.cps.spi.entities.Dataspace;\r
+import org.onap.cps.spi.entities.Fragment;\r
+import org.onap.cps.spi.entities.ModuleEntity;\r
+import org.onap.cps.spi.repository.DataspaceRepository;\r
+import org.onap.cps.spi.repository.FragmentRepository;\r
+import org.onap.cps.spi.repository.ModuleRepository;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.dao.DataIntegrityViolationException;\r
+import org.springframework.stereotype.Component;\r
+\r
+@Component\r
+public class FragmentPersistenceServiceImpl implements FragmentPersistenceService {\r
+\r
+ @Autowired\r
+ private DataspaceRepository dataspaceRepository;\r
+\r
+ @Autowired\r
+ private FragmentRepository fragmentRepository;\r
+\r
+ @Autowired\r
+ private ModuleRepository moduleRepository;\r
+\r
+ @Override\r
+ public String createAnchor(final AnchorDetails anchorDetails) {\r
+ try {\r
+ final Dataspace dataspace = dataspaceRepository.getByName(anchorDetails.getDataspace());\r
+ final ModuleEntity moduleEntity =\r
+ moduleRepository.getByDataspaceAndNamespaceAndRevision(dataspace,\r
+ anchorDetails.getNamespace(), anchorDetails.getRevision());\r
+\r
+ final Fragment fragment = Fragment.builder().xpath(anchorDetails.getAnchorName())\r
+ .anchorName(anchorDetails.getAnchorName())\r
+ .dataspace(dataspace).module(moduleEntity).build();\r
+\r
+ fragmentRepository.save(fragment);\r
+ return anchorDetails.getAnchorName();\r
+ } catch (final CpsNotFoundException ex) {\r
+ throw new CpsValidationException("Validation Error",\r
+ String.format("Dataspace and/or Module do not exist."));\r
+ } catch (final DataIntegrityViolationException ex) {\r
+ throw new CpsValidationException("Duplication Error",\r
+ String.format("Anchor with name %s already exist in dataspace %s.",\r
+ anchorDetails.getAnchorName(), anchorDetails.getDataspace()));\r
+ }\r
+ }\r
+}
\ No newline at end of file
@Component
public class ModelPersistencyServiceImpl implements ModelPersistencyService {
-
- private final ModuleRepository moduleRepository;
-
- private final DataspaceRepository dataspaceRepository;
+ @Autowired
+ private ModuleRepository moduleRepository;
@Autowired
- public ModelPersistencyServiceImpl(final ModuleRepository moduleRepository,
- final DataspaceRepository dataspaceRepository) {
- this.moduleRepository = moduleRepository;
- this.dataspaceRepository = dataspaceRepository;
- }
+ private DataspaceRepository dataspaceRepository;
@Override
public void storeModule(final String namespace, final String moduleContent, final String revision,
if (Boolean.FALSE.equals(dataspaceRepository.existsByName(dataspaceName))) {
dataspaceRepository.save(dataspace);
}
- dataspace.setId(dataspaceRepository.findByName(dataspaceName).getId());
+ dataspace.setId(dataspaceRepository.getByName(dataspaceName).getId());
final ModuleEntity moduleEntity = new ModuleEntity(namespace, moduleContent, revision, dataspace);
moduleRepository.save(moduleEntity);
}
package org.onap.cps.spi.repository;
+import java.util.Optional;
+import javax.validation.constraints.NotNull;
+import org.onap.cps.exceptions.CpsNotFoundException;
import org.onap.cps.spi.entities.Dataspace;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
public interface DataspaceRepository extends JpaRepository<Dataspace, Integer> {
Boolean existsByName(String name); //Checks if there are any records by name()
- Dataspace findByName(String name);
+ Optional<Dataspace> findByName(@NotNull String name);
+
+ default Dataspace getByName(@NotNull String name) {
+ return findByName(name).orElseThrow(
+ () -> new CpsNotFoundException("Not Found", "Dataspace " + name + " does not exist."));
+ }
}
\ No newline at end of file
--- /dev/null
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ * Copyright (C) 2020 Nordix Foundation. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.onap.cps.spi.repository;\r
+\r
+import org.onap.cps.spi.entities.Fragment;\r
+import org.springframework.data.jpa.repository.JpaRepository;\r
+import org.springframework.stereotype.Repository;\r
+\r
+@Repository\r
+public interface FragmentRepository extends JpaRepository<Fragment, Integer> {\r
+}
\ No newline at end of file
package org.onap.cps.spi.repository;
+import java.util.Optional;
+import javax.validation.constraints.NotNull;
+import org.onap.cps.exceptions.CpsNotFoundException;
+import org.onap.cps.spi.entities.Dataspace;
import org.onap.cps.spi.entities.ModuleEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ModuleRepository extends JpaRepository<ModuleEntity, Integer> {
+
+ Optional<ModuleEntity> findByDataspaceAndNamespaceAndRevision(@NotNull Dataspace dataspace,
+ @NotNull String namespace,
+ @NotNull String revision);
+
+ /**
+ * This method gets a ModuleEntity by dataspace, namespace and revision.
+ *
+ * @param dataspace the dataspace
+ * @param namespace the namespace
+ * @param revision the revision
+ * @return the ModuleEntity
+ * @throws CpsNotFoundException if ModuleEntity not found
+ */
+ default ModuleEntity getByDataspaceAndNamespaceAndRevision(@NotNull Dataspace dataspace, @NotNull String namespace,
+ @NotNull String revision) {
+ return findByDataspaceAndNamespaceAndRevision(dataspace, namespace,
+ revision)
+ .orElseThrow(() -> new CpsNotFoundException("Validation Error", String.format(
+ "Module with dataspace %s, revision %s does not exist in namespace %s.",
+ dataspace.getName(), revision, namespace)));
+ }
}
\ No newline at end of file
-CREATE TABLE IF NOT EXISTS RELATION_TYPE
-(
- RELATION_TYPE TEXT NOT NULL,
- ID SERIAL PRIMARY KEY
-);
-
-CREATE TABLE IF NOT EXISTS DATASPACE
-(
- ID SERIAL PRIMARY KEY,
- NAME TEXT NOT NULL,
- CONSTRAINT "UQ_NAME" UNIQUE (NAME)
-);
-
-CREATE TABLE IF NOT EXISTS SCHEMA_NODE
-(
- SCHEMA_NODE_IDENTIFIER TEXT NOT NULL,
- ID SERIAL PRIMARY KEY
-);
-
-CREATE TABLE IF NOT EXISTS MODULE
-(
- NAMESPACE TEXT NOT NULL,
- REVISION TEXT NOT NULL,
- MODULE_CONTENT TEXT NOT NULL,
- DATASPACE_ID BIGINT NOT NULL,
- ID SERIAL PRIMARY KEY,
- UNIQUE (DATASPACE_ID, NAMESPACE, REVISION),
- CONSTRAINT module_dataspace FOREIGN KEY (DATASPACE_ID) REFERENCES DATASPACE (id) ON UPDATE CASCADE ON DELETE CASCADE
-);
-
-CREATE TABLE IF NOT EXISTS FRAGMENT
-(
- ID BIGSERIAL PRIMARY KEY,
- XPATH TEXT NOT NULL,
- DATASPACE_ID INTEGER NOT NULL REFERENCES DATASPACE(ID),
- ATTRIBUTES JSONB,
- ANCHOR_ID BIGINT REFERENCES FRAGMENT(ID),
- PARENT_ID BIGINT REFERENCES FRAGMENT(ID),
- MODULE_ID INTEGER REFERENCES MODULE(ID),
- SCHEMA_NODE_ID INTEGER REFERENCES SCHEMA_NODE(ID)
-);
-
-CREATE TABLE IF NOT EXISTS RELATION
-(
- FROM_FRAGMENT_ID BIGINT NOT NULL REFERENCES FRAGMENT(ID),
- TO_FRAGMENT_ID BIGINT NOT NULL REFERENCES FRAGMENT(ID),
- RELATION_TYPE_ID INTEGER NOT NULL REFERENCES RELATION_TYPE(ID),
- FROM_REL_XPATH TEXT NOT NULL,
- TO_REL_XPATH TEXT NOT NULL,
- CONSTRAINT RELATION_PKEY PRIMARY KEY (TO_FRAGMENT_ID, FROM_FRAGMENT_ID, RELATION_TYPE_ID)
-);
-
-
-CREATE INDEX IF NOT EXISTS "FKI_FRAGMENT_DATASPACE_ID_FK" ON FRAGMENT USING BTREE(DATASPACE_ID) ;
-CREATE INDEX IF NOT EXISTS "FKI_FRAGMENT_MODULE_ID_FK" ON FRAGMENT USING BTREE(MODULE_ID) ;
-CREATE INDEX IF NOT EXISTS "FKI_FRAGMENT_PARENT_ID_FK" ON FRAGMENT USING BTREE(PARENT_ID) ;
-CREATE INDEX IF NOT EXISTS "FKI_FRAGMENT_ANCHOR_ID_FK" ON FRAGMENT USING BTREE(ANCHOR_ID) ;
-CREATE INDEX IF NOT EXISTS "PERF_SCHEMA_NODE_SCHEMA_NODE_ID" ON SCHEMA_NODE USING BTREE(SCHEMA_NODE_IDENTIFIER) ;
-CREATE INDEX IF NOT EXISTS "FKI_SCHEMA_NODE_ID_TO_ID" ON FRAGMENT USING BTREE(SCHEMA_NODE_ID) ;
-CREATE INDEX IF NOT EXISTS "FKI_RELATION_TYPE_ID_FK" ON RELATION USING BTREE(RELATION_TYPE_ID);
-CREATE INDEX IF NOT EXISTS "FKI_RELATIONS_FROM_ID_FK" ON RELATION USING BTREE(FROM_FRAGMENT_ID);
-CREATE INDEX IF NOT EXISTS "FKI_RELATIONS_TO_ID_FK" ON RELATION USING BTREE(TO_FRAGMENT_ID);
-CREATE INDEX IF NOT EXISTS "PERF_MODULE_MODULE_CONTENT" ON MODULE USING BTREE(MODULE_CONTENT);
+CREATE TABLE IF NOT EXISTS RELATION_TYPE\r
+(\r
+ RELATION_TYPE TEXT NOT NULL,\r
+ ID SERIAL PRIMARY KEY\r
+);\r
+\r
+CREATE TABLE IF NOT EXISTS DATASPACE\r
+(\r
+ ID SERIAL PRIMARY KEY,\r
+ NAME TEXT NOT NULL,\r
+ CONSTRAINT "UQ_NAME" UNIQUE (NAME)\r
+);\r
+\r
+CREATE TABLE IF NOT EXISTS SCHEMA_NODE\r
+(\r
+ SCHEMA_NODE_IDENTIFIER TEXT NOT NULL,\r
+ ID SERIAL PRIMARY KEY\r
+);\r
+\r
+CREATE TABLE IF NOT EXISTS MODULE\r
+(\r
+ ID SERIAL PRIMARY KEY,\r
+ NAMESPACE TEXT NOT NULL,\r
+ REVISION TEXT NOT NULL,\r
+ MODULE_CONTENT TEXT NOT NULL,\r
+ DATASPACE_ID BIGINT NOT NULL,\r
+ UNIQUE (DATASPACE_ID, NAMESPACE, REVISION),\r
+ CONSTRAINT MODULE_DATASPACE FOREIGN KEY (DATASPACE_ID) REFERENCES DATASPACE (id) ON UPDATE CASCADE ON DELETE CASCADE\r
+);\r
+\r
+CREATE TABLE IF NOT EXISTS FRAGMENT\r
+(\r
+ ID BIGSERIAL PRIMARY KEY,\r
+ XPATH TEXT NOT NULL,\r
+ ATTRIBUTES JSONB,\r
+ ANCHOR_NAME TEXT,\r
+ ANCHOR_ID BIGINT REFERENCES FRAGMENT(ID),\r
+ PARENT_ID BIGINT REFERENCES FRAGMENT(ID),\r
+ MODULE_ID INTEGER REFERENCES MODULE(ID),\r
+ DATASPACE_ID INTEGER NOT NULL REFERENCES DATASPACE(ID),\r
+ SCHEMA_NODE_ID INTEGER REFERENCES SCHEMA_NODE(ID),\r
+ UNIQUE (DATASPACE_ID, ANCHOR_NAME, XPATH)\r
+);\r
+\r
+CREATE TABLE IF NOT EXISTS RELATION\r
+(\r
+ FROM_FRAGMENT_ID BIGINT NOT NULL REFERENCES FRAGMENT(ID),\r
+ TO_FRAGMENT_ID BIGINT NOT NULL REFERENCES FRAGMENT(ID),\r
+ RELATION_TYPE_ID INTEGER NOT NULL REFERENCES RELATION_TYPE(ID),\r
+ FROM_REL_XPATH TEXT NOT NULL,\r
+ TO_REL_XPATH TEXT NOT NULL,\r
+ CONSTRAINT RELATION_PKEY PRIMARY KEY (TO_FRAGMENT_ID, FROM_FRAGMENT_ID, RELATION_TYPE_ID)\r
+);\r
+\r
+CREATE INDEX IF NOT EXISTS "FKI_FRAGMENT_DATASPACE_ID_FK" ON FRAGMENT USING BTREE(DATASPACE_ID) ;\r
+CREATE INDEX IF NOT EXISTS "FKI_FRAGMENT_MODULE_ID_FK" ON FRAGMENT USING BTREE(MODULE_ID) ;\r
+CREATE INDEX IF NOT EXISTS "FKI_FRAGMENT_PARENT_ID_FK" ON FRAGMENT USING BTREE(PARENT_ID) ;\r
+CREATE INDEX IF NOT EXISTS "FKI_FRAGMENT_ANCHOR_ID_FK" ON FRAGMENT USING BTREE(ANCHOR_ID) ;\r
+CREATE INDEX IF NOT EXISTS "PERF_SCHEMA_NODE_SCHEMA_NODE_ID" ON SCHEMA_NODE USING BTREE(SCHEMA_NODE_IDENTIFIER) ;\r
+CREATE INDEX IF NOT EXISTS "FKI_SCHEMA_NODE_ID_TO_ID" ON FRAGMENT USING BTREE(SCHEMA_NODE_ID) ;\r
+CREATE INDEX IF NOT EXISTS "FKI_RELATION_TYPE_ID_FK" ON RELATION USING BTREE(RELATION_TYPE_ID);\r
+CREATE INDEX IF NOT EXISTS "FKI_RELATIONS_FROM_ID_FK" ON RELATION USING BTREE(FROM_FRAGMENT_ID);\r
+CREATE INDEX IF NOT EXISTS "FKI_RELATIONS_TO_ID_FK" ON RELATION USING BTREE(TO_FRAGMENT_ID);\r
+CREATE INDEX IF NOT EXISTS "PERF_MODULE_MODULE_CONTENT" ON MODULE USING BTREE(MODULE_CONTENT);\r
CREATE UNIQUE INDEX IF NOT EXISTS "UQ_FRAGMENT_XPATH"ON FRAGMENT USING btree(xpath COLLATE pg_catalog."default" text_pattern_ops, dataspace_id);
\ No newline at end of file
package org.onap.cps.api;
import java.io.File;
-import java.io.IOException;
+import org.onap.cps.api.model.AnchorDetails;
+import org.onap.cps.exceptions.CpsValidationException;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
/**
* Configuration and persistency service interface which holds methods for parsing and storing yang models and data.
* @param yangModelContent the input stream
* @return the schema context
*/
- SchemaContext parseAndValidateModel(final String yangModelContent);
+ SchemaContext parseAndValidateModel(String yangModelContent);
/**
* Parse and validate a file representing a yang model to generate a schema context.
* @param yangModelFile the yang file
* @return the schema context
*/
- SchemaContext parseAndValidateModel(final File yangModelFile);
+ SchemaContext parseAndValidateModel(File yangModelFile);
/**
* Store schema context for a yang model.
* @param schemaContext the schema context
* @param dataspaceName the dataspace name
*/
- void storeSchemaContext(final SchemaContext schemaContext, final String dataspaceName);
+ void storeSchemaContext(SchemaContext schemaContext, String dataspaceName);
/**
* Store the JSON structure in the database.
* @param jsonStructure the JSON structure.
* @return entity ID.
*/
- Integer storeJsonStructure(final String jsonStructure);
+ Integer storeJsonStructure(String jsonStructure);
/**
* Read a JSON Object using the object identifier.
* @param jsonObjectId the JSON object identifier.
* @return the JSON structure.
*/
- String getJsonById(final int jsonObjectId);
+ String getJsonById(int jsonObjectId);
/**
* Delete a JSON Object using the object identifier.
*
* @param jsonObjectId the JSON object identifier.
*/
- void deleteJsonById(final int jsonObjectId);
+ void deleteJsonById(int jsonObjectId);
+
+ /**
+ * Create an anchor using provided anchorDetails object.
+ *
+ * @param anchorDetails the anchor details object.
+ * @return the anchor name.
+ * @throws CpsValidationException if input data is invalid.
+ */
+ String createAnchor(AnchorDetails anchorDetails);
}
import java.io.IOException;
import java.util.Optional;
import org.onap.cps.api.CpService;
+import org.onap.cps.api.model.AnchorDetails;
import org.onap.cps.exceptions.CpsException;
import org.onap.cps.exceptions.CpsValidationException;
import org.onap.cps.spi.DataPersistencyService;
+import org.onap.cps.spi.FragmentPersistenceService;
import org.onap.cps.spi.ModelPersistencyService;
import org.onap.cps.utils.YangUtils;
import org.opendaylight.yangtools.yang.common.Revision;
@Autowired
private DataPersistencyService dataPersistencyService;
+ @Autowired
+ private FragmentPersistenceService fragmentPersistenceService;
+
@Override
public final SchemaContext parseAndValidateModel(final String yangModelContent) {
writer.write(yangModelContent);
}
return parseAndValidateModel(tempFile);
- } catch (IOException e) {
+ } catch (final IOException e) {
throw new CpsException(e);
}
}
public final SchemaContext parseAndValidateModel(final File yangModelFile) {
try {
return YangUtils.parseYangModelFile(yangModelFile);
- } catch (YangParserException e) {
+ } catch (final YangParserException e) {
throw new CpsValidationException("Yang file validation failed", e.getMessage());
- } catch (IOException e) {
+ } catch (final IOException e) {
throw new CpsException(e);
}
}
@Override
public final void storeSchemaContext(final SchemaContext schemaContext, final String dataspaceName) {
for (final Module module : schemaContext.getModules()) {
- Optional<Revision> optionalRevision = module.getRevision();
- String revisionValue = optionalRevision.isPresent() ? optionalRevision.get().toString() : null;
+ final Optional<Revision> optionalRevision = module.getRevision();
+ final String revisionValue = optionalRevision.map(Object::toString).orElse(null);
modelPersistencyService.storeModule(module.getNamespace().toString(), module.toString(),
revisionValue, dataspaceName);
}
}
-}
+
+ @Override
+ public String createAnchor(AnchorDetails anchorDetails) {
+ return fragmentPersistenceService.createAnchor(anchorDetails);
+ }
+}
\ No newline at end of file
--- /dev/null
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ * Copyright (C) 2020 Nordix Foundation. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.onap.cps.api.model;\r
+\r
+import java.io.Serializable;\r
+import lombok.Getter;\r
+import lombok.NoArgsConstructor;\r
+import lombok.Setter;\r
+\r
+@Getter\r
+@Setter\r
+@NoArgsConstructor\r
+public class AnchorDetails implements Serializable {\r
+\r
+ private static final long serialVersionUID = 1464791260718603291L;\r
+\r
+ private String anchorName;\r
+\r
+ private String dataspace;\r
+\r
+ private String namespace;\r
+\r
+ private String revision;\r
+}
\ No newline at end of file
package org.onap.cps.exceptions;
import lombok.Getter;
-import org.springframework.core.NestedExceptionUtils;
/**
* CP Service exception.
*/
public class CpsException extends RuntimeException {
+ private static final long serialVersionUID = 5573438585188332404L;
+
@Getter
String details;
package org.onap.cps.exceptions;
-import lombok.Getter;
/**
* CP Service exception. Indicates the requested data being absent.
*/
public class CpsNotFoundException extends CpsException {
+ private static final long serialVersionUID = -1852996415384288431L;
+
/**
* Constructor.
*
--- /dev/null
+/*-\r
+ * ============LICENSE_START=======================================================\r
+ * Copyright (C) 2020 Nordix Foundation. All rights reserved.\r
+ * ================================================================================\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ * SPDX-License-Identifier: Apache-2.0\r
+ * ============LICENSE_END=========================================================\r
+ */\r
+\r
+package org.onap.cps.spi;\r
+\r
+import org.onap.cps.api.model.AnchorDetails;\r
+\r
+public interface FragmentPersistenceService {\r
+\r
+ /**\r
+ * Create an Anchor.\r
+ *\r
+ * @param anchorDetails the anchorDetails object.\r
+ * @return the anchor name.\r
+ */\r
+ String createAnchor(AnchorDetails anchorDetails);\r
+}\r
package org.onap.cps.api.impl
import org.onap.cps.TestUtils
+import org.onap.cps.api.model.AnchorDetails
+import org.onap.cps.exceptions.CpsNotFoundException
import org.onap.cps.exceptions.CpsValidationException
import org.onap.cps.spi.DataPersistencyService
+import org.onap.cps.spi.FragmentPersistenceService
import org.opendaylight.yangtools.yang.common.Revision
import org.opendaylight.yangtools.yang.model.api.SchemaContext
import spock.lang.Specification
class CpServiceImplSpec extends Specification {
def mockDataPersistencyService = Mock(DataPersistencyService)
+ def mockFragmentPersistenceService = Mock(FragmentPersistenceService)
def objectUnderTest = new CpServiceImpl()
def setup() {
objectUnderTest.dataPersistencyService = mockDataPersistencyService
+ objectUnderTest.fragmentPersistenceService = mockFragmentPersistenceService
}
def 'Cps Service provides to its client the id assigned by the system when storing a data structure'() {
then: 'the same exception is thrown by CPS'
thrown(IllegalStateException)
}
+
+ def 'Create an anchor with a non-existant dataspace'(){
+ given: 'that the dataspace does not exist service throws an exception'
+ AnchorDetails anchorDetails = new AnchorDetails()
+ anchorDetails.setDataspace('dummyDataspace')
+ mockFragmentPersistenceService.createAnchor(anchorDetails) >> {throw new CpsValidationException(_ as String, _ as String)}
+ when: 'we try to create a anchor with a non-existant dataspace'
+ objectUnderTest.createAnchor(anchorDetails)
+ then: 'the same exception is thrown by CPS'
+ thrown(CpsValidationException)
+ }
+
+ def 'Create an anchor with invalid dataspace, namespace and revision'(){
+ given: 'that the dataspace, namespace and revison combination does not exist service throws an exception'
+ AnchorDetails anchorDetails = new AnchorDetails()
+ anchorDetails.setDataspace('dummyDataspace')
+ anchorDetails.setNamespace('dummyNamespace')
+ anchorDetails.setRevision('dummyRevision')
+ mockFragmentPersistenceService.createAnchor(anchorDetails) >> {throw new CpsValidationException(_ as String, _ as String)}
+ when: 'we try to create a anchor with a non-existant dataspace, namespace and revison combination'
+ objectUnderTest.createAnchor(anchorDetails)
+ then: 'the same exception is thrown by CPS'
+ thrown(CpsValidationException)
+ }
+
+ def 'Create a duplicate anchor'(){
+ given: 'that the anchor already exist service throws an exception'
+ AnchorDetails anchorDetails = new AnchorDetails()
+ anchorDetails.setDataspace('dummyDataspace')
+ anchorDetails.setNamespace('dummyNamespace')
+ anchorDetails.setRevision('dummyRevision')
+ anchorDetails.setRevision('dummyAnchorName')
+ mockFragmentPersistenceService.createAnchor(anchorDetails) >> {throw new CpsValidationException(_ as String, _ as String)}
+ when: 'we try to create a duplicate anchor'
+ objectUnderTest.createAnchor(anchorDetails)
+ then: 'the same exception is thrown by CPS'
+ thrown(CpsValidationException)
+ }
+
+ def 'Create an anchor with supplied anchor name, dataspace, namespace and revision'(){
+ given: 'that the anchor does not pre-exist service creates an anchor'
+ AnchorDetails anchorDetails = new AnchorDetails()
+ anchorDetails.setDataspace('dummyDataspace')
+ anchorDetails.setNamespace('dummyNamespace')
+ anchorDetails.setRevision('dummyRevision')
+ anchorDetails.setRevision('dummyAnchorName')
+ mockFragmentPersistenceService.createAnchor(anchorDetails) >> 'dummyAnchorName'
+ expect: 'anchor name is returned by service'
+ objectUnderTest.createAnchor(anchorDetails) == 'dummyAnchorName'
+ }
}
\ No newline at end of file