added svcapi ui and camunda code
[it/otf.git] / otf-service-api / src / main / java / org / oran / otf / api / service / impl / VirtualTestHeadServiceImpl.java
1 /*  Copyright (c) 2019 AT&T Intellectual Property.                             #\r
2 #                                                                              #\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
6 #                                                                              #\r
7 #       http://www.apache.org/licenses/LICENSE-2.0                             #\r
8 #                                                                              #\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
15 \r
16 \r
17 package org.oran.otf.api.service.impl;\r
18 \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
38 \r
39 import javax.ws.rs.core.Response;\r
40 import java.util.Date;\r
41 import java.util.Optional;\r
42 \r
43 @Service\r
44 public class VirtualTestHeadServiceImpl implements VirtualTestHeadService {\r
45 \r
46     @Autowired\r
47     private UserRepository userRepository;\r
48     @Autowired\r
49     private GroupRepository groupRepository;\r
50 \r
51     @Autowired\r
52     TestHeadRepository testHeadRepository;\r
53 \r
54     @Autowired\r
55     MongoTemplate mongoTemplate;\r
56 \r
57     private static final String logPrefix = Utility.getLoggerPrefix();\r
58 \r
59     @Override\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
63         }\r
64 \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
72         }\r
73 \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
79                     String.format(\r
80                             "The group (id: %s) associated with the test head does not exist.",\r
81                             testHeadGroupId));\r
82         }\r
83 \r
84         // try to find the user for the mechanizedId used to make this request\r
85         User user = Utilities.findUserByAuthHeader(authorization, userRepository);\r
86         if (user == null) {\r
87             return Utilities.Http.BuildResponse.badRequestWithMessage(\r
88                     "No user associated with mechanized identifier used for this request.");\r
89         }\r
90 \r
91         if (!PermissionChecker.hasPermissionTo(user, testHeadGroup, UserPermission.Permission.WRITE, groupRepository)) {\r
92             String error =\r
93                     String.format(\r
94                             "Unauthorized the write to test head with name, %s.",\r
95                             testHeadGroupId);\r
96             return ResponseUtility.Build.unauthorizedWithMessage(error);\r
97         }\r
98 \r
99         return updateTestHeadFields(testHead, newTestHead, user);\r
100     }\r
101 \r
102     private Response updateTestHeadFields(TestHead testHead, TestHead newTestHead, User user) {\r
103         Query select = Query.query(Criteria.where("_id").is(testHead.get_id()));\r
104         Update update = new Update();\r
105 \r
106         if (newTestHead.getTestHeadName() != null) {\r
107             if (doesTestHeadWithNameExist(newTestHead.getTestHeadName())) {\r
108                 String error =\r
109                         String.format(\r
110                                 "Cant change testHeadName to %s since it already exists.",\r
111                                 newTestHead.getTestHeadName());\r
112                 return ResponseUtility.Build.badRequestWithMessage(error);\r
113             }\r
114             testHead.setTestHeadName(newTestHead.getTestHeadName());\r
115             update.set("testHeadName", newTestHead.getTestHeadName());\r
116         }\r
117         if (newTestHead.getTestHeadDescription() != null) {\r
118             testHead.setTestHeadDescription(newTestHead.getTestHeadDescription());\r
119             update.set("testHeadDescription", newTestHead.getTestHeadDescription());\r
120         }\r
121         if (newTestHead.getHostname() != null) {\r
122             testHead.setHostname(newTestHead.getHostname());\r
123             update.set("hostname", newTestHead.getHostname());\r
124         }\r
125         if (newTestHead.getPort() != null) {\r
126             testHead.setPort(newTestHead.getPort());\r
127             update.set("port", newTestHead.getPort());\r
128         }\r
129         if (newTestHead.getResourcePath() != null) {\r
130             testHead.setResourcePath(newTestHead.getResourcePath());\r
131             update.set("resourcePath", newTestHead.getResourcePath());\r
132         }\r
133         if (newTestHead.getAuthorizationType() != null) {\r
134             testHead.setAuthorizationType(newTestHead.getAuthorizationType());\r
135             update.set("authorizationType", newTestHead.getAuthorizationType());\r
136         }\r
137         if (newTestHead.getAuthorizationCredential() != null) {\r
138             testHead.setAuthorizationCredential(newTestHead.getAuthorizationCredential());\r
139             update.set("authorizationCredential", newTestHead.getAuthorizationCredential());\r
140         }\r
141         if (newTestHead.getAuthorizationEnabled() != null) {\r
142             testHead.setAuthorizationEnabled(newTestHead.getAuthorizationEnabled());\r
143             update.set("authorizationEnabled", newTestHead.getAuthorizationEnabled());\r
144         }\r
145         if (newTestHead.getVthInputTemplate() != null) {\r
146             testHead.setVthInputTemplate(newTestHead.getVthInputTemplate());\r
147             update.set("vthInputTemplate", newTestHead.getVthInputTemplate());\r
148         }\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
153 \r
154         mongoTemplate.updateFirst(select, update, "testHeads");\r
155         return ResponseUtility.Build.okRequestWithObject(testHead);\r
156     }\r
157 \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
163     }\r
164 }\r