1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
17 package org.oran.otf.api.service.impl;
\r
19 import org.oran.otf.api.Utilities;
\r
20 import org.oran.otf.api.service.VirtualTestHeadService;
\r
21 import org.oran.otf.common.model.Group;
\r
22 import org.oran.otf.common.model.TestHead;
\r
23 import org.oran.otf.common.model.User;
\r
24 import org.oran.otf.common.repository.GroupRepository;
\r
25 import org.oran.otf.common.repository.TestHeadRepository;
\r
26 import org.oran.otf.common.repository.UserRepository;
\r
27 import org.oran.otf.common.utility.Utility;
\r
28 import org.oran.otf.common.utility.database.Generic;
\r
29 import org.oran.otf.common.utility.http.ResponseUtility;
\r
30 import org.oran.otf.common.utility.permissions.PermissionChecker;
\r
31 import org.oran.otf.common.utility.permissions.UserPermission;
\r
32 import org.springframework.beans.factory.annotation.Autowired;
\r
33 import org.springframework.data.mongodb.core.MongoTemplate;
\r
34 import org.springframework.data.mongodb.core.query.Criteria;
\r
35 import org.springframework.data.mongodb.core.query.Query;
\r
36 import org.springframework.data.mongodb.core.query.Update;
\r
37 import org.springframework.stereotype.Service;
\r
39 import javax.ws.rs.core.Response;
\r
40 import java.util.Date;
\r
41 import java.util.Optional;
\r
44 public class VirtualTestHeadServiceImpl implements VirtualTestHeadService {
\r
47 private UserRepository userRepository;
\r
49 private GroupRepository groupRepository;
\r
52 TestHeadRepository testHeadRepository;
\r
55 MongoTemplate mongoTemplate;
\r
57 private static final String logPrefix = Utility.getLoggerPrefix();
\r
60 public Response updateVirtualTestHead(String authorization, String testHeadName, TestHead newTestHead) {
\r
61 if (authorization == null) {
\r
62 return Utilities.Http.BuildResponse.unauthorizedWithMessage("Missing authorization header.");
\r
65 // try to find the test head
\r
66 Optional<TestHead> optionalTestHead =
\r
67 testHeadRepository.findByTestHeadName(testHeadName);
\r
68 TestHead testHead = Utilities.resolveOptional(optionalTestHead);
\r
69 if (testHead == null) {
\r
70 return Utilities.Http.BuildResponse.badRequestWithMessage(
\r
71 String.format("A test head with identifier %s was not found.", testHeadName));
\r
74 // try to find the group of the test head
\r
75 String testHeadGroupId = testHead.getGroupId().toString();
\r
76 Group testHeadGroup = Generic.findByIdGeneric(groupRepository, testHead.getGroupId());
\r
77 if (testHeadGroup == null) {
\r
78 return Utilities.Http.BuildResponse.badRequestWithMessage(
\r
80 "The group (id: %s) associated with the test head does not exist.",
\r
84 // try to find the user for the mechanizedId used to make this request
\r
85 User user = Utilities.findUserByAuthHeader(authorization, userRepository);
\r
87 return Utilities.Http.BuildResponse.badRequestWithMessage(
\r
88 "No user associated with mechanized identifier used for this request.");
\r
91 if (!PermissionChecker.hasPermissionTo(user, testHeadGroup, UserPermission.Permission.WRITE, groupRepository)) {
\r
94 "Unauthorized the write to test head with name, %s.",
\r
96 return ResponseUtility.Build.unauthorizedWithMessage(error);
\r
99 return updateTestHeadFields(testHead, newTestHead, user);
\r
102 private Response updateTestHeadFields(TestHead testHead, TestHead newTestHead, User user) {
\r
103 Query select = Query.query(Criteria.where("_id").is(testHead.get_id())).addCriteria(Criteria.where("groupId").is(testHead.getGroupId()));
\r
104 Update update = new Update();
\r
106 if (newTestHead.getTestHeadName() != null) {
\r
107 if (doesTestHeadWithNameExist(newTestHead.getTestHeadName())) {
\r
110 "Cant change testHeadName to %s since it already exists.",
\r
111 newTestHead.getTestHeadName());
\r
112 return ResponseUtility.Build.badRequestWithMessage(error);
\r
114 testHead.setTestHeadName(newTestHead.getTestHeadName());
\r
115 update.set("testHeadName", newTestHead.getTestHeadName());
\r
117 if (newTestHead.getTestHeadDescription() != null) {
\r
118 testHead.setTestHeadDescription(newTestHead.getTestHeadDescription());
\r
119 update.set("testHeadDescription", newTestHead.getTestHeadDescription());
\r
121 if (newTestHead.getHostname() != null) {
\r
122 testHead.setHostname(newTestHead.getHostname());
\r
123 update.set("hostname", newTestHead.getHostname());
\r
125 if (newTestHead.getPort() != null) {
\r
126 testHead.setPort(newTestHead.getPort());
\r
127 update.set("port", newTestHead.getPort());
\r
129 if (newTestHead.getResourcePath() != null) {
\r
130 testHead.setResourcePath(newTestHead.getResourcePath());
\r
131 update.set("resourcePath", newTestHead.getResourcePath());
\r
133 if (newTestHead.getAuthorizationType() != null) {
\r
134 testHead.setAuthorizationType(newTestHead.getAuthorizationType());
\r
135 update.set("authorizationType", newTestHead.getAuthorizationType());
\r
137 if (newTestHead.getAuthorizationCredential() != null) {
\r
138 testHead.setAuthorizationCredential(newTestHead.getAuthorizationCredential());
\r
139 update.set("authorizationCredential", newTestHead.getAuthorizationCredential());
\r
141 if (newTestHead.getAuthorizationEnabled() != null) {
\r
142 testHead.setAuthorizationEnabled(newTestHead.getAuthorizationEnabled());
\r
143 update.set("authorizationEnabled", newTestHead.getAuthorizationEnabled());
\r
145 if (newTestHead.getVthInputTemplate() != null) {
\r
146 testHead.setVthInputTemplate(newTestHead.getVthInputTemplate());
\r
147 update.set("vthInputTemplate", newTestHead.getVthInputTemplate());
\r
149 testHead.setUpdatedAt(new Date());
\r
150 update.set("updatedAt", testHead.getUpdatedAt());
\r
151 testHead.setUpdatedBy(user.get_id());
\r
152 update.set("updatedBy", user.get_id());
\r
154 mongoTemplate.updateFirst(select, update, "testHeads");
\r
155 return ResponseUtility.Build.okRequestWithObject(testHead);
\r
158 // check if test head exists in database by name
\r
159 private boolean doesTestHeadWithNameExist(String name) {
\r
160 Optional<TestHead> optionalTestHead =
\r
161 testHeadRepository.findByTestHeadName(name);
\r
162 return optionalTestHead.isPresent();
\r