diff options
author | Ravi Mantena <rx908f@att.com> | 2020-11-27 13:34:11 -0500 |
---|---|---|
committer | Ravi Mantena <rx908f@att.com> | 2020-12-08 14:13:43 -0500 |
commit | e3578fcdcd65c9f919c0f7cc16949b79b70d12b4 (patch) | |
tree | e85f8b4048dfc3e7118216894ee36c255cbd6211 /mod2/catalog-service/src/main | |
parent | 437e1f4e621d63cf576caa5de9c5c4804ea455f9 (diff) |
Policy Model Distribution POC Issue-ID: DCAEGEN2-1868
Issue-ID: DCAEGEN2-1868
Change-Id: Ife6421558af59405f3026f66950ff8f2d7aeba17
Signed-off-by: Ravi Mantena <rx908f@att.com>
Diffstat (limited to 'mod2/catalog-service/src/main')
24 files changed, 1508 insertions, 4 deletions
diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/exceptions/ErrorMessages.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/exceptions/ErrorMessages.java index 9b96066..27f335f 100644 --- a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/exceptions/ErrorMessages.java +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/exceptions/ErrorMessages.java @@ -30,4 +30,6 @@ public class ErrorMessages { public static final String MS_SERVICE_NAME_CONFLICT_MESSAGE = "Microservice with this core name already exists."; public static final String MS_SERVICE_NAME_VALIDATION_MESSAGE = "Service name is Invalid. Accepts lowercase letters and hyphens"; + public static final String POLICYMODEL_NAME_VERSION_CONFLICT_MESSAGE = "Policy Model with this name and version already exists."; + } diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/exceptions/policymodel/PolicyModelConflictException.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/exceptions/policymodel/PolicyModelConflictException.java new file mode 100644 index 0000000..8d5098f --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/exceptions/policymodel/PolicyModelConflictException.java @@ -0,0 +1,27 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.model.exceptions.policymodel; + +public class PolicyModelConflictException extends RuntimeException { + public PolicyModelConflictException(String message) { + super(message); + } +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/exceptions/policymodel/PolicyModelDistributionEnvNotFoundException.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/exceptions/policymodel/PolicyModelDistributionEnvNotFoundException.java new file mode 100644 index 0000000..dfc47e3 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/exceptions/policymodel/PolicyModelDistributionEnvNotFoundException.java @@ -0,0 +1,27 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.model.exceptions.policymodel; + +public class PolicyModelDistributionEnvNotFoundException extends RuntimeException { + public PolicyModelDistributionEnvNotFoundException(String message) { + super(message); + } +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/exceptions/policymodel/PolicyModelNotFoundException.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/exceptions/policymodel/PolicyModelNotFoundException.java new file mode 100644 index 0000000..16e30c7 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/exceptions/policymodel/PolicyModelNotFoundException.java @@ -0,0 +1,27 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.model.exceptions.policymodel; + +public class PolicyModelNotFoundException extends RuntimeException { + public PolicyModelNotFoundException(String message) { + super(message); + } +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/policymodel/DistributionInfo.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/policymodel/DistributionInfo.java new file mode 100644 index 0000000..1d15623 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/policymodel/DistributionInfo.java @@ -0,0 +1,42 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.model.policymodel; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * A model class which represents Distribution Info of a Policy Model entity + */ +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) +public class DistributionInfo { + + private String url; + private PolicyModelStatus status; + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/policymodel/PolicyModel.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/policymodel/PolicyModel.java new file mode 100644 index 0000000..d4b5bf0 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/policymodel/PolicyModel.java @@ -0,0 +1,54 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.model.policymodel; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.onap.dcaegen2.platform.mod.model.restapi.MetadataRequest; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +import java.util.ArrayList; +import java.util.List; + +/** + * A model class which represents Policy Model entity + */ +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) +@Document("policy-model") +public class PolicyModel { + @Id + private String id; + private String name; + private String content; + private String owner; + private String version; + private MetadataRequest metadata; + private List<DistributionInfo> distributionInfo = new ArrayList<>(); + +}
\ No newline at end of file diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/policymodel/PolicyModelDistributionEnv.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/policymodel/PolicyModelDistributionEnv.java new file mode 100644 index 0000000..a750bcb --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/policymodel/PolicyModelDistributionEnv.java @@ -0,0 +1,28 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.model.policymodel; + +/** + * Supported Environments for Policy Model Distribution + */ +public enum PolicyModelDistributionEnv { + DEV,PST,ETE,PROD +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/policymodel/PolicyModelStatus.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/policymodel/PolicyModelStatus.java new file mode 100644 index 0000000..11e3283 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/policymodel/PolicyModelStatus.java @@ -0,0 +1,28 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.model.policymodel; + +/** + * Supported statuses for Policy Model entity + */ +public enum PolicyModelStatus { + SUCCESS, FAILED, NOTDISTRIBUTED +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/restapi/MetadataRequest.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/restapi/MetadataRequest.java new file mode 100644 index 0000000..279c596 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/restapi/MetadataRequest.java @@ -0,0 +1,46 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.model.restapi; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Date; +import java.util.List; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +@JsonInclude(JsonInclude.Include.NON_NULL) +public class MetadataRequest { + + private String createdBy; + private Date createdOn; + private String updatedBy; + private Date updatedOn; + private String notes = ""; + private List<String> labels; + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/restapi/PolicyModelCreateRequest.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/restapi/PolicyModelCreateRequest.java new file mode 100644 index 0000000..a4258f4 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/restapi/PolicyModelCreateRequest.java @@ -0,0 +1,59 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.model.restapi; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; + +/** + * A model that represent request body to create Policy Model entity. + */ + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class PolicyModelCreateRequest { + + @NotBlank + @Pattern(regexp = "^([a-z0-9](-[a-z0-9])*)+$", message = "Policy Model name is invalid. Accepts lower alphanumerics and hyphens.") + @Size(min = 5, max = 50, message = "Policy Model name length cannot exceed 50 characters") + private String name; + + @NotBlank + private String content; + + @NotBlank + private String owner; + + @NotBlank + @Pattern(regexp = "[0-9].[0-9].[0-9]", message = "Policy Model version is invalid. Should be in x.x.x format.") + private String version; + + private MetadataRequest metadata; + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/restapi/PolicyModelDistributionRequest.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/restapi/PolicyModelDistributionRequest.java new file mode 100644 index 0000000..9feff64 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/restapi/PolicyModelDistributionRequest.java @@ -0,0 +1,45 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.model.restapi; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModelDistributionEnv; + +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; + +/** + * A model that represent request body to Distribute Policy Model entity. + */ + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class PolicyModelDistributionRequest { + + private PolicyModelDistributionEnv env; + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/restapi/PolicyModelUpdateRequest.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/restapi/PolicyModelUpdateRequest.java new file mode 100644 index 0000000..08d1e65 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/model/restapi/PolicyModelUpdateRequest.java @@ -0,0 +1,54 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.model.restapi; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; + +/** + * A model that represent request body to create Policy Model entity. + */ + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class PolicyModelUpdateRequest { + + @Pattern(regexp = "^([a-z0-9](-[a-z0-9])*)+$", message = "Policy Model name is invalid. Accepts alphanumerics and hyphens.") + @Size(min = 5, max = 50, message = "Policy Model name length cannot exceed 50 characters") + private String name; + + private String content; + + private String owner; + + @Pattern(regexp = "[0-9].[0-9].[0-9]", message = "Policy Model version is invalid. Should be in x.x.x format.") + private String version; + + private MetadataRequest metadata; + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/mongo/policymodel/PolicyModelMongoGateway.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/mongo/policymodel/PolicyModelMongoGateway.java new file mode 100644 index 0000000..e4c8fc2 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/mongo/policymodel/PolicyModelMongoGateway.java @@ -0,0 +1,60 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.mongo.policymodel; + +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel; +import org.onap.dcaegen2.platform.mod.web.service.policymodel.PolicyModelGateway; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Sort; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Optional; + +/** + * Mongo implementation of Policy Model + */ + +@Service +public class PolicyModelMongoGateway implements PolicyModelGateway { + + @Autowired + private PolicyModelMongoRepo repo; + + @Override + public List<PolicyModel> findAll() { + Sort sortByCreatedDate = Sort.by(Sort.Direction.DESC, "metadata.createdOn"); + return repo.findAll(sortByCreatedDate); + } + + @Override + public Optional<PolicyModel> findById(String id) { + return repo.findById(id); + } + + @Override + public PolicyModel save(PolicyModel newPolicyModel) { return repo.save(newPolicyModel); } + + @Override + public Optional<PolicyModel> findByNameAndVersion(String name, String version) { return repo.findByNameIgnoreCaseAndVersionIgnoreCase(name,version); } + + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/mongo/policymodel/PolicyModelMongoRepo.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/mongo/policymodel/PolicyModelMongoRepo.java new file mode 100644 index 0000000..491de22 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/mongo/policymodel/PolicyModelMongoRepo.java @@ -0,0 +1,39 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.mongo.policymodel; + +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + + +/** + * An interface to use Spring MongoRepository for Policy Model + */ + +@Repository +public interface PolicyModelMongoRepo extends MongoRepository<PolicyModel, String> { + + Optional<PolicyModel> findByNameIgnoreCaseAndVersionIgnoreCase(String name, String version); + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/util/PolicyModelUtils.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/util/PolicyModelUtils.java new file mode 100644 index 0000000..8d7b643 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/util/PolicyModelUtils.java @@ -0,0 +1,186 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.util; + +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.SslContextBuilder; +import io.netty.handler.ssl.util.InsecureTrustManagerFactory; +import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelDistributionEnvNotFoundException; +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModelDistributionEnv; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpHeaders; +import org.springframework.http.client.reactive.ReactorClientHttpConnector; +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.client.ExchangeFilterFunctions; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.netty.http.client.HttpClient; + +import javax.annotation.PostConstruct; +import javax.net.ssl.SSLException; +import java.util.HashMap; +import java.util.Map; + +/** + * Policy Model Service Utils to get URL, Username, Password, Webclient + */ + +@Component +public class PolicyModelUtils { + + @Value("${url.path}") + private String urlpath; + + @Value("${dev.server}") + private String devServer; + + @Value("${dev.port}") + private String devServerPort; + + @Value("${dev.user}") + private String devServerUser; + + @Value("${dev.password}") + private String devServerUserPassword; + + @Value("${pst.server}") + private String pstServer; + + @Value("${pst.port}") + private String pstServerPort; + + @Value("${pst.user}") + private String pstServerUser; + + @Value("${pst.password}") + private String pstServerUserPassword; + + @Value("${ete.server}") + private String eteServer; + + @Value("${ete.port}") + private String eteServerPort; + + @Value("${ete.user}") + private String eteServerUser; + + @Value("${ete.password}") + private String eteServerUserPassword; + + @Value("${prod.server}") + private String prodServer; + + @Value("${prod.port}") + private String prodServerPort; + + @Value("${prod.user}") + private String prodServerUser; + + @Value("${prod.password}") + private String prodServerUserPassword; + + Map<String, String> envToUrlMap,envToUserNameMap,envToPasswordMap; + + /** + * Creates a Policy Model Distribution Engine URL for the Environment requested + */ + @PostConstruct + public void init() { + envToUrlMap = new HashMap<>(); + envToUserNameMap = new HashMap(); + envToPasswordMap = new HashMap(); + + envToUrlMap.put(PolicyModelDistributionEnv.DEV.name(), "https://"+ devServer + ":" + devServerPort + urlpath); + envToUrlMap.put(PolicyModelDistributionEnv.PST.name(), "https://"+ pstServer + ":" + pstServerPort + urlpath); + envToUrlMap.put(PolicyModelDistributionEnv.ETE.name(), "https://"+ eteServer + ":" + eteServerPort + urlpath); + envToUrlMap.put(PolicyModelDistributionEnv.PROD.name(), "https://"+ prodServer + ":" + prodServerPort + urlpath); + + envToUserNameMap.put(PolicyModelDistributionEnv.DEV.name(), devServerUser); + envToUserNameMap.put(PolicyModelDistributionEnv.PST.name(), pstServerUser); + envToUserNameMap.put(PolicyModelDistributionEnv.ETE.name(), eteServerUser); + envToUserNameMap.put(PolicyModelDistributionEnv.PROD.name(), prodServerUser); + + envToPasswordMap.put(PolicyModelDistributionEnv.DEV.name(), devServerUserPassword); + envToPasswordMap.put(PolicyModelDistributionEnv.PST.name(), pstServerUserPassword); + envToPasswordMap.put(PolicyModelDistributionEnv.ETE.name(), eteServerUserPassword); + envToPasswordMap.put(PolicyModelDistributionEnv.PROD.name(), prodServerUserPassword); + } + + /** + * Generates a Policy Model Distribution Engine URL for the Environment + * + * @param env + * @return + */ + public String getPolicyEngineURL(String env) { + if(!envToUrlMap.containsKey(env)) throw new PolicyModelDistributionEnvNotFoundException(String.format("Policy Model Environment with env %s invalid", env)); + return envToUrlMap.get(env); + } + + + + /** + * Generates a Policy Model Distribution Engine UserName for the Environment + * + * @param env + * @return + */ + + public String getUserName(String env) { + if(!envToUserNameMap.containsKey(env)) throw new PolicyModelDistributionEnvNotFoundException(String.format("Policy Model Environment with env %s invalid", env)); + return envToUserNameMap.get(env); + } + + + /** + * Generates a Policy Model Distribution Engine Password for the Environment + * + * @param env + * @return + */ + + public String getPassword(String env) { + if(!envToPasswordMap.containsKey(env)) throw new PolicyModelDistributionEnvNotFoundException(String.format("Policy Model Environment with env %s invalid", env)); + return envToPasswordMap.get(env); + } + + /** + * Generates a Policy Model Distribution Engine Webclient for the Environment + * + * @param env + * @return + */ + public WebClient getWebClient(String env) throws SSLException { + String userName = getUserName(env); + String password = getPassword(env); + + SslContext sslContext = SslContextBuilder.forClient() + .trustManager(InsecureTrustManagerFactory.INSTANCE) + .build(); + HttpClient httpClient = HttpClient.create().secure(t -> t.sslContext(sslContext)); + return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient)) + .defaultHeader(HttpHeaders.CONTENT_TYPE, "application/yaml") + .filter(ExchangeFilterFunctions.basicAuthentication(userName, password)) + .build(); + } + + + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/AppExceptionHandler.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/AppExceptionHandler.java index 1a2f5f9..b8aeca6 100644 --- a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/AppExceptionHandler.java +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/AppExceptionHandler.java @@ -20,6 +20,9 @@ package org.onap.dcaegen2.platform.mod.web.controller; +import com.fasterxml.jackson.core.JsonParseException; +import com.google.gson.Gson; +import lombok.extern.slf4j.Slf4j; import org.onap.dcaegen2.platform.mod.model.exceptions.MissingRequestBodyException; import org.onap.dcaegen2.platform.mod.model.exceptions.OperationNotAllowedException; import org.onap.dcaegen2.platform.mod.model.exceptions.ResourceConflictException; @@ -27,12 +30,10 @@ import org.onap.dcaegen2.platform.mod.model.exceptions.basemicroservice.BaseMicr import org.onap.dcaegen2.platform.mod.model.exceptions.common.UserNotPassedException; import org.onap.dcaegen2.platform.mod.model.exceptions.deploymentartifact.DeploymentArtifactNotFound; import org.onap.dcaegen2.platform.mod.model.exceptions.msinstance.MsInstanceNotFoundException; +import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelNotFoundException; import org.onap.dcaegen2.platform.mod.model.exceptions.specification.SpecificationInvalid; import org.onap.dcaegen2.platform.mod.model.restapi.ErrorResponse; import org.onap.dcaegen2.platform.mod.model.restapi.GenericErrorResponse; -import com.fasterxml.jackson.core.JsonParseException; -import com.google.gson.Gson; -import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.validation.FieldError; @@ -54,6 +55,9 @@ import java.util.Map; @Slf4j public class AppExceptionHandler { + /** + * Exception Handler for Invalid Component Spec + */ @ExceptionHandler(value = {WebClientResponseException.class}) public ResponseEntity<ErrorResponse> handleCompSpecInvalidException (WebClientResponseException ex, WebRequest request) { @@ -61,6 +65,9 @@ public class AppExceptionHandler { (new ErrorResponse(ex.getResponseBodyAsString()), HttpStatus.INTERNAL_SERVER_ERROR); } + /** + * Exception Handler for Json Parsing + */ @ExceptionHandler(value = {JsonParseException.class}) public ResponseEntity<GenericErrorResponse> handleJsonParsedException (JsonParseException ex, WebRequest request) { @@ -72,6 +79,9 @@ public class AppExceptionHandler { return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); } + /** + * Exception Handler for Invalid Specification + */ @ExceptionHandler public ResponseEntity<GenericErrorResponse> specificationInvalid(SpecificationInvalid ex) { Map<String, Object> errorResponse = new Gson().fromJson(ex.getMessage(), Map.class); @@ -82,6 +92,9 @@ public class AppExceptionHandler { return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); } + /** + * Exception Handler for Missing requested Body + */ @ExceptionHandler public ResponseEntity<GenericErrorResponse> missingRequestBodyException(MissingRequestBodyException ex){ GenericErrorResponse response = new GenericErrorResponse(); @@ -91,17 +104,35 @@ public class AppExceptionHandler { return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST); } + /** + * Exception Handler for missing User + */ @ExceptionHandler public ResponseEntity<ErrorResponse> resolveUserNotPassedException(UserNotPassedException ex){ return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST); } + /** + * Exception Handler for MsInstance Not Found + */ @ExceptionHandler public ResponseEntity<ErrorResponse> resolveMsInstanceNotFoundException(MsInstanceNotFoundException ex) { log.error(ex.getMessage()); return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST); } + /** + * Exception Handler for Policy Model Not Found + */ + @ExceptionHandler + public ResponseEntity<ErrorResponse> resolvePolicyModelNotFoundException(PolicyModelNotFoundException ex) { + log.error(ex.getMessage()); + return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST); + } + + /** + * Exception Handler for Policy Model Not Found + */ @ExceptionHandler public ResponseEntity<ErrorResponse> resolveDeploymentArtifactNotFound(DeploymentArtifactNotFound ex) { log.error(ex.getMessage()); @@ -109,6 +140,9 @@ public class AppExceptionHandler { HttpStatus.BAD_REQUEST); } + /** + * Exception Handler for Operation Not Allowed + */ @ExceptionHandler public ResponseEntity<ErrorResponse> resolveOperationNotAllowed(OperationNotAllowedException ex) { log.error(ex.getMessage()); @@ -116,17 +150,26 @@ public class AppExceptionHandler { HttpStatus.CONFLICT); } + /** + * Exception Handler for Resource Conflict + */ @ExceptionHandler public ResponseEntity<ErrorResponse> resolveResourceConflict(ResourceConflictException ex) { return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.CONFLICT); } + /** + * Exception Handler for Resource Not Found + */ @ExceptionHandler(value = {BaseMicroserviceNotFoundException.class}) public ResponseEntity<ErrorResponse> resolveResourceNotFoundExcetions(RuntimeException ex) { log.error(ex.getMessage(), ex); return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.CONFLICT); } + /** + * Exception Handler for Bean Validation + */ @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<GenericErrorResponse> resolveBeanValidationException(MethodArgumentNotValidException ex){ log.error(ex.getMessage()); diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/PolicyModelController.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/PolicyModelController.java new file mode 100644 index 0000000..44ad88f --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/PolicyModelController.java @@ -0,0 +1,114 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.web.controller; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; +import org.onap.dcaegen2.platform.mod.model.exceptions.common.UserNotPassedException; +import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelConflictException; +import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelDistributionEnvNotFoundException; +import org.onap.dcaegen2.platform.mod.model.policymodel.DistributionInfo; +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel; +import org.onap.dcaegen2.platform.mod.model.restapi.ErrorResponse; +import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelCreateRequest; +import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelUpdateRequest; +import org.onap.dcaegen2.platform.mod.web.service.policymodel.PolicyModelService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +import javax.validation.Valid; +import javax.validation.constraints.NotBlank; +import java.util.List; + +/** + * Controller class to manage Policy Model's REST endpoints + */ +@CrossOrigin +@RestController +@Api(tags = "Policy Model", description = "APIs to manage Policy Model Rest endpoints") +@RequestMapping("/api/policy-model") +@Slf4j +public class PolicyModelController { + + @Autowired + PolicyModelService policyModelService; + + /** + * Controller class to manage Policy Model's GET all policy model's + */ + @GetMapping + @ApiOperation("Get all Policy Models") + public List<PolicyModel> getAll() { + return policyModelService.getAll(); + } + + /** + * Controller class to manage Policy Model's GET by policy model id + */ + @GetMapping("/{modelId}") + @ApiOperation("Get specific Policy model") + public PolicyModel getPolicyModelById(@PathVariable String modelId) { + log.info(modelId); + return policyModelService.getPolicyModelById(modelId); + } + + /** + * Controller class to manage Policy Model's POST + */ + @PostMapping + @ApiOperation("Create a Policy Model") + @ResponseStatus(HttpStatus.CREATED) + public PolicyModel createPolicyModel(@RequestBody @Valid PolicyModelCreateRequest request, @RequestParam @NotBlank String user) { + return policyModelService.createPolicyModel(request,user); + } + + /** + * Controller class to manage Policy Model's PATCH by policy model id + */ + @PatchMapping("/{modelId}") + @ApiOperation("Patch a Policy Model") + @ResponseStatus(HttpStatus.OK) + public PolicyModel patchPolicyModel(@RequestBody @Valid PolicyModelUpdateRequest request, @PathVariable String modelId, @RequestParam @NotBlank String user) { + return policyModelService.updatePolicyModel(request, modelId,user); + } + + /** + * Controller class to manage Policy Model's conflict exception + */ + @ExceptionHandler + public ResponseEntity<ErrorResponse> resolvePolicyModelConflict(PolicyModelConflictException ex) { + return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.CONFLICT); + } + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/PolicyModelDistributionController.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/PolicyModelDistributionController.java new file mode 100644 index 0000000..3a3571c --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/controller/PolicyModelDistributionController.java @@ -0,0 +1,82 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.web.controller; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; +import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelDistributionEnvNotFoundException; +import org.onap.dcaegen2.platform.mod.model.restapi.ErrorResponse; +import org.onap.dcaegen2.platform.mod.web.service.policymodel.PolicyModelDistributionService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +/** + * Controller class to manage Policy Model's REST endpoints + */ +@CrossOrigin +@RestController +@Api(tags = "Policy Model Distribution", description = "APIs to manage Policy Model Distribution Rest endpoints") +@RequestMapping("/api/policy-type") +@Slf4j +public class PolicyModelDistributionController { + + @Autowired + PolicyModelDistributionService policyModelDistributionService; + + /** + * Controller class to manage Policy Model Distribution's GET by policy model id + */ + @GetMapping("/{modelId}") + @ApiOperation("Get the status of specific Policy model Distribution") + public ResponseEntity getPolicyModelDistributionById(@RequestParam("env") String env,@PathVariable String modelId) { + log.info(modelId); + return policyModelDistributionService.getPolicyModelDistributionById(env,modelId); + } + + /** + * Controller class to manage Policy Model Distribution's POST by policy model id + */ + @PostMapping("/{modelId}") + @ApiOperation("Distribute a specific Policy Model") + public ResponseEntity distributePolicyModelById(@RequestParam("env") String env, @PathVariable String modelId) { + return policyModelDistributionService.distributePolicyModel(env,modelId); + } + + /** + * Controller class to manage Policy Model Distribution Environment Not Found exception + */ + @ExceptionHandler + public ResponseEntity<ErrorResponse> resolvePolicyModelDistributionEnvNotFound(PolicyModelDistributionEnvNotFoundException ex){ + return new ResponseEntity<>(new ErrorResponse(ex.getMessage()), HttpStatus.BAD_REQUEST); + } + + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelDistributionService.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelDistributionService.java new file mode 100644 index 0000000..8847a63 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelDistributionService.java @@ -0,0 +1,43 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.web.service.policymodel; + +import org.onap.dcaegen2.platform.mod.model.policymodel.DistributionInfo; +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel; +import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelCreateRequest; +import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelDistributionRequest; +import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelUpdateRequest; +import org.springframework.http.ResponseEntity; + +import java.io.IOException; +import java.util.List; + +/** + * An interface to access Policy Model Services + */ + +public interface PolicyModelDistributionService { + + ResponseEntity getPolicyModelDistributionById(String env, String modelId); + + ResponseEntity distributePolicyModel(String env, String modelId); + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelDistributionServiceImpl.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelDistributionServiceImpl.java new file mode 100644 index 0000000..d0646d2 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelDistributionServiceImpl.java @@ -0,0 +1,183 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.web.service.policymodel; + +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.SslContextBuilder; +import io.netty.handler.ssl.util.InsecureTrustManagerFactory; +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelDistributionEnvNotFoundException; +import org.onap.dcaegen2.platform.mod.model.policymodel.DistributionInfo; +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel; +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModelDistributionEnv; +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModelStatus; +import org.onap.dcaegen2.platform.mod.util.PolicyModelUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.reactive.ReactorClientHttpConnector; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.Base64Utils; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.reactive.function.client.ClientResponse; +import org.springframework.web.reactive.function.client.ExchangeFilterFunctions; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; +import reactor.netty.http.client.HttpClient; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * Policy Model Service implementation + */ + +@Service +@Setter +@Slf4j +public class PolicyModelDistributionServiceImpl implements PolicyModelDistributionService { + + @Autowired + PolicyModelServiceImpl policyModelServiceImpl; + + @Autowired + PolicyModelGateway policyModelGateway; + + @Autowired + PolicyModelUtils policyModelUtils; + + /** + * Gets the Policy Model Distribution by Policy Model ID + * @param env + * @param modelId + * @return + */ + @Override + public ResponseEntity getPolicyModelDistributionById(String env,String modelId) { + + String responseBody = null; + HttpStatus httpStatus; + String url = policyModelUtils.getPolicyEngineURL(env); + PolicyModel policyModel = policyModelServiceImpl.getPolicyModelById(modelId); + + try{ + WebClient webClient = policyModelUtils.getWebClient(env); + WebClient.RequestHeadersSpec<?> requestHeadersSpec = webClient.method(HttpMethod.GET).uri(url + "/" + policyModel.getName()); + httpStatus = requestHeadersSpec.exchange().map(response -> response.statusCode()).block(); + if (httpStatus.is2xxSuccessful()) { + responseBody = requestHeadersSpec.retrieve().bodyToMono(String.class).block(); + } else if(httpStatus.is4xxClientError()){ + if(httpStatus.value() == 401) { + responseBody = "Authentication Error"; + } else if(httpStatus.value() == 403) { + responseBody = "Authorization Error"; + } else if(httpStatus.value() == 404) { + responseBody = "Resource Not Found"; + } + } else if(httpStatus.is5xxServerError()){ + responseBody = "Internal Server Error"; + } + else { + responseBody = "Problem in getting the Policy Model."; + } + }catch(Exception ex){ + log.error("Problem in getting the Policy Model."); + log.error("error: ", ex); + httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; + responseBody = "Problem in getting the Policy Model."; + + } + + return ResponseEntity.status(httpStatus).body(responseBody); + + } + + /** + * Distributes a Policy Model + * @param env + * @param modelId + * @return + */ + @Override + @Transactional + public ResponseEntity distributePolicyModel(String env, String modelId) { + + String responseBody = null; + HttpStatus httpStatus; + String url = policyModelUtils.getPolicyEngineURL(env); + PolicyModel policyModel = policyModelServiceImpl.getPolicyModelById(modelId); + String content = policyModel.getContent(); + DistributionInfo distributionInfo = DistributionInfo.builder().url(env).build(); + + try{ + WebClient webClient = policyModelUtils.getWebClient(env); + WebClient.RequestHeadersSpec<?> requestHeadersSpec = webClient.method(HttpMethod.POST).uri(url).bodyValue(content.getBytes()); + httpStatus = requestHeadersSpec.exchange().map(response -> response.statusCode()).block(); + if (httpStatus.is2xxSuccessful()) { + distributionInfo.setStatus(PolicyModelStatus.SUCCESS); + responseBody = requestHeadersSpec.retrieve().bodyToMono(String.class).block(); + } else if(httpStatus.is4xxClientError()){ + distributionInfo.setStatus(PolicyModelStatus.FAILED); + if(httpStatus.value() == 400) { + responseBody = "Invalid Body"; + } else if(httpStatus.value() == 401) { + responseBody = "Authentication Error"; + } else if(httpStatus.value() == 403) { + responseBody = "Authorization Error"; + } else if(httpStatus.value() == 406) { + responseBody = "Not Acceptable Version"; + } else if(httpStatus.value() == 415) { + responseBody = "UnSupported Media Type"; + } + } else if(httpStatus.is5xxServerError()){ + responseBody = "Internal Server Error"; + distributionInfo.setStatus(PolicyModelStatus.FAILED); + } + else { + distributionInfo.setStatus(PolicyModelStatus.FAILED); + responseBody = "Problem in Distributing the Policy Model."; + } + }catch(Exception ex){ + log.error("Problem in Distributing the Policy Model."); + log.error("error: ", ex); + distributionInfo.setStatus(PolicyModelStatus.FAILED); + httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; + responseBody = "Problem in Distributing the Policy Model."; + + } + List<DistributionInfo> distributionInfos = new ArrayList<>(); + distributionInfos.add(distributionInfo); + policyModel.setDistributionInfo(distributionInfos); + + policyModelGateway.save(policyModel); + return ResponseEntity.status(httpStatus).body(responseBody); + + } + + + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelGateway.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelGateway.java new file mode 100644 index 0000000..bda3998 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelGateway.java @@ -0,0 +1,42 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.web.service.policymodel; + +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel; + +import java.util.List; +import java.util.Optional; + +/** + * An interface to interact with Policy Model persistence + */ +public interface PolicyModelGateway { + + List<PolicyModel> findAll(); + + Optional<PolicyModel> findById(String id); + + PolicyModel save(PolicyModel newPolicyModel) ; + + Optional<PolicyModel> findByNameAndVersion(String name,String version); + + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelService.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelService.java new file mode 100644 index 0000000..61abf60 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelService.java @@ -0,0 +1,44 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.web.service.policymodel; + +import org.onap.dcaegen2.platform.mod.model.policymodel.DistributionInfo; +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel; +import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelCreateRequest; +import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelUpdateRequest; + +import java.util.List; + +/** + * An interface to access Policy Model Services + */ + +public interface PolicyModelService { + + List<PolicyModel> getAll(); + + PolicyModel getPolicyModelById(String modelId); + + PolicyModel createPolicyModel(PolicyModelCreateRequest request, String user); + + PolicyModel updatePolicyModel(PolicyModelUpdateRequest request, String modelId, String user); + +} diff --git a/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelServiceImpl.java b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelServiceImpl.java new file mode 100644 index 0000000..58fc447 --- /dev/null +++ b/mod2/catalog-service/src/main/java/org/onap/dcaegen2/platform/mod/web/service/policymodel/PolicyModelServiceImpl.java @@ -0,0 +1,203 @@ +/* + * ============LICENSE_START======================================================= + * org.onap.dcae + * ================================================================================ + * Copyright (c) 2020 AT&T Intellectual Property. 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. + * ============LICENSE_END========================================================= + */ + +package org.onap.dcaegen2.platform.mod.web.service.policymodel; + +import lombok.Setter; +import lombok.extern.slf4j.Slf4j; +import org.onap.dcaegen2.platform.mod.model.exceptions.ErrorMessages; +import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelConflictException; +import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelDistributionEnvNotFoundException; +import org.onap.dcaegen2.platform.mod.model.exceptions.policymodel.PolicyModelNotFoundException; +import org.onap.dcaegen2.platform.mod.model.policymodel.DistributionInfo; +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModel; +import org.onap.dcaegen2.platform.mod.model.policymodel.PolicyModelDistributionEnv; +import org.onap.dcaegen2.platform.mod.model.restapi.MetadataRequest; +import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelCreateRequest; +import org.onap.dcaegen2.platform.mod.model.restapi.PolicyModelUpdateRequest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Date; +import java.util.List; + +/** + * Policy Model Service implementation + */ + +@Service +@Setter +@Slf4j +public class PolicyModelServiceImpl implements PolicyModelService { + + @Value("${dev.server}") + private String devServer; + + @Value("${dev.port}") + private String devServerPort; + + @Value("${dev.user}") + private String devServerUser; + + @Value("${dev.password}") + private String devServerPassword; + + @Autowired + private PolicyModelGateway policyModelGateway; + + /** + * Lists all Policy Models + * + * @return + */ + @Override + public List<PolicyModel> getAll() { + return policyModelGateway.findAll(); + } + + /** + * List a Policy Model by policyModel ID + * + * @param id + * @return + */ + @Override + public PolicyModel getPolicyModelById(String id) { + return policyModelGateway.findById(id).orElseThrow(() -> new PolicyModelNotFoundException(String.format("Policy Model with id %s not found", id))); + } + + /** + * creates a Policy Model + * + * @param request + * @return + */ + @Override + @Transactional + public PolicyModel createPolicyModel(PolicyModelCreateRequest request, String user) { + checkIfPMNameAndVersionAlreadyExists(request.getName(), request.getVersion()); + PolicyModel policyModel = new PolicyModel(); + policyModel.setName(request.getName()); + policyModel.setContent(request.getContent()); + policyModel.setVersion(request.getVersion()); + policyModel.setOwner(request.getOwner()); + policyModel.setMetadata(getMetadataFields(request.getMetadata(), user)); + return policyModelGateway.save(policyModel); + } + + /** + * Update a Policy Model + * + * @param request + * @param modelId + * @return + */ + @Override + public PolicyModel updatePolicyModel(PolicyModelUpdateRequest request, String modelId, String user) { + PolicyModel policyModel = getPolicyModelById(modelId); + updateFields(request, policyModel,user); + return policyModelGateway.save(policyModel); + + } + + + /** + * Cerifies if a Policy Model Exist + * + * @param pmName + * @param pmVersion + * @return + */ + private void checkIfPMNameAndVersionAlreadyExists(String pmName, String pmVersion) { + if (policyModelGateway.findByNameAndVersion(pmName, pmVersion).isPresent()) + throw new PolicyModelConflictException(ErrorMessages.POLICYMODEL_NAME_VERSION_CONFLICT_MESSAGE); + } + + /** + * creates a Policy Model Metadata + * + * @param metadata + * @param user + * @return + */ + private MetadataRequest getMetadataFields(MetadataRequest metadata, String user) { + MetadataRequest metadataFields = MetadataRequest.builder().build(); + metadataFields.setCreatedBy(user); + metadataFields.setCreatedOn(new Date()); + if(metadata != null) { + if (metadata.getNotes() != null) + metadataFields.setNotes(metadata.getNotes()); + if (metadata.getLabels() != null) + metadataFields.setLabels(metadata.getLabels()); + } + return metadataFields; + } + + + /** + * Updates a Policy Model + * + * @param request + * @param policyModel + * @param user + * @return + */ + private void updateFields(PolicyModelUpdateRequest request, PolicyModel policyModel, String user) { + String name = request.getName(); + String version = request.getVersion(); + if (!(name.equalsIgnoreCase(policyModel.getName()) && version.equalsIgnoreCase(policyModel.getVersion()))) { + checkIfPMNameAndVersionAlreadyExists(name, version); + policyModel.setName(name); + policyModel.setVersion(version); + } + if (request.getOwner() != null) { + policyModel.setOwner(request.getOwner()); + } + + if (request.getContent() != null) { + policyModel.setContent(request.getContent()); + } + if (request.getMetadata() != null) { + updateMetadata(request,user); + policyModel.setMetadata(request.getMetadata()); + } + } + + + /** + * Updates a Policy Model Metadata + * + * @param request + * @param user + * @return + */ + private void updateMetadata(PolicyModelUpdateRequest request, String user) { + MetadataRequest metadataRequest = request.getMetadata(); + metadataRequest.setUpdatedBy(user); + metadataRequest.setUpdatedOn(new Date()); + } + + +} diff --git a/mod2/catalog-service/src/main/resources/application.properties b/mod2/catalog-service/src/main/resources/application.properties index 1f20c6a..f818c6c 100644 --- a/mod2/catalog-service/src/main/resources/application.properties +++ b/mod2/catalog-service/src/main/resources/application.properties @@ -21,4 +21,30 @@ #add connection to mongo db once its up and running spring.data.mongodb.host=mongo_db spring.data.mongodb.port=27017 -spring.data.mongodb.database=dcae_mod
\ No newline at end of file +spring.data.mongodb.database=dcae_mod + + + +#Distribution Info Environment details + +url.path=/policy/api/v1/policytypes + +dev.server=10.12.6.2 +dev.port=31317 +dev.user=healthcheck +dev.password=PASSWORD_GOES_HERE + +pst.server=policy-api +pst.port=6969 +pst.user=healthcheck +pst.password=PASSWORD_GOES_HERE + +ete.server=policy-api +ete.port=6969 +ete.user=healthcheck +ete.password=PASSWORD_GOES_HERE + +prod.server=policy-api +prod.port=6969 +prod.user=healthcheck +prod.password=PASSWORD_GOES_HERE
\ No newline at end of file |