added svcapi ui and camunda code
[it/otf.git] / otf-service-api / src / main / java / org / oran / otf / api / service / impl / VirtualTestHeadServiceImpl.java
diff --git a/otf-service-api/src/main/java/org/oran/otf/api/service/impl/VirtualTestHeadServiceImpl.java b/otf-service-api/src/main/java/org/oran/otf/api/service/impl/VirtualTestHeadServiceImpl.java
new file mode 100644 (file)
index 0000000..d2a662b
--- /dev/null
@@ -0,0 +1,164 @@
+/*  Copyright (c) 2019 AT&T Intellectual Property.                             #\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
+\r
+\r
+package org.oran.otf.api.service.impl;\r
+\r
+import org.oran.otf.api.Utilities;\r
+import org.oran.otf.api.service.VirtualTestHeadService;\r
+import org.oran.otf.common.model.Group;\r
+import org.oran.otf.common.model.TestHead;\r
+import org.oran.otf.common.model.User;\r
+import org.oran.otf.common.repository.GroupRepository;\r
+import org.oran.otf.common.repository.TestHeadRepository;\r
+import org.oran.otf.common.repository.UserRepository;\r
+import org.oran.otf.common.utility.Utility;\r
+import org.oran.otf.common.utility.database.Generic;\r
+import org.oran.otf.common.utility.http.ResponseUtility;\r
+import org.oran.otf.common.utility.permissions.PermissionChecker;\r
+import org.oran.otf.common.utility.permissions.UserPermission;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.data.mongodb.core.MongoTemplate;\r
+import org.springframework.data.mongodb.core.query.Criteria;\r
+import org.springframework.data.mongodb.core.query.Query;\r
+import org.springframework.data.mongodb.core.query.Update;\r
+import org.springframework.stereotype.Service;\r
+\r
+import javax.ws.rs.core.Response;\r
+import java.util.Date;\r
+import java.util.Optional;\r
+\r
+@Service\r
+public class VirtualTestHeadServiceImpl implements VirtualTestHeadService {\r
+\r
+    @Autowired\r
+    private UserRepository userRepository;\r
+    @Autowired\r
+    private GroupRepository groupRepository;\r
+\r
+    @Autowired\r
+    TestHeadRepository testHeadRepository;\r
+\r
+    @Autowired\r
+    MongoTemplate mongoTemplate;\r
+\r
+    private static final String logPrefix = Utility.getLoggerPrefix();\r
+\r
+    @Override\r
+    public Response updateVirtualTestHead(String authorization, String testHeadName, TestHead newTestHead) {\r
+        if (authorization == null) {\r
+            return Utilities.Http.BuildResponse.unauthorizedWithMessage("Missing authorization header.");\r
+        }\r
+\r
+        // try to find the test head\r
+        Optional<TestHead> optionalTestHead =\r
+                testHeadRepository.findByTestHeadName(testHeadName);\r
+        TestHead testHead = Utilities.resolveOptional(optionalTestHead);\r
+        if (testHead == null) {\r
+            return Utilities.Http.BuildResponse.badRequestWithMessage(\r
+                    String.format("A test head with identifier %s was not found.", testHeadName));\r
+        }\r
+\r
+        // try to find the group of the test head\r
+        String testHeadGroupId = testHead.getGroupId().toString();\r
+        Group testHeadGroup = Generic.findByIdGeneric(groupRepository, testHead.getGroupId());\r
+        if (testHeadGroup == null) {\r
+            return Utilities.Http.BuildResponse.badRequestWithMessage(\r
+                    String.format(\r
+                            "The group (id: %s) associated with the test head does not exist.",\r
+                            testHeadGroupId));\r
+        }\r
+\r
+        // try to find the user for the mechanizedId used to make this request\r
+        User user = Utilities.findUserByAuthHeader(authorization, userRepository);\r
+        if (user == null) {\r
+            return Utilities.Http.BuildResponse.badRequestWithMessage(\r
+                    "No user associated with mechanized identifier used for this request.");\r
+        }\r
+\r
+        if (!PermissionChecker.hasPermissionTo(user, testHeadGroup, UserPermission.Permission.WRITE, groupRepository)) {\r
+            String error =\r
+                    String.format(\r
+                            "Unauthorized the write to test head with name, %s.",\r
+                            testHeadGroupId);\r
+            return ResponseUtility.Build.unauthorizedWithMessage(error);\r
+        }\r
+\r
+        return updateTestHeadFields(testHead, newTestHead, user);\r
+    }\r
+\r
+    private Response updateTestHeadFields(TestHead testHead, TestHead newTestHead, User user) {\r
+        Query select = Query.query(Criteria.where("_id").is(testHead.get_id()));\r
+        Update update = new Update();\r
+\r
+        if (newTestHead.getTestHeadName() != null) {\r
+            if (doesTestHeadWithNameExist(newTestHead.getTestHeadName())) {\r
+                String error =\r
+                        String.format(\r
+                                "Cant change testHeadName to %s since it already exists.",\r
+                                newTestHead.getTestHeadName());\r
+                return ResponseUtility.Build.badRequestWithMessage(error);\r
+            }\r
+            testHead.setTestHeadName(newTestHead.getTestHeadName());\r
+            update.set("testHeadName", newTestHead.getTestHeadName());\r
+        }\r
+        if (newTestHead.getTestHeadDescription() != null) {\r
+            testHead.setTestHeadDescription(newTestHead.getTestHeadDescription());\r
+            update.set("testHeadDescription", newTestHead.getTestHeadDescription());\r
+        }\r
+        if (newTestHead.getHostname() != null) {\r
+            testHead.setHostname(newTestHead.getHostname());\r
+            update.set("hostname", newTestHead.getHostname());\r
+        }\r
+        if (newTestHead.getPort() != null) {\r
+            testHead.setPort(newTestHead.getPort());\r
+            update.set("port", newTestHead.getPort());\r
+        }\r
+        if (newTestHead.getResourcePath() != null) {\r
+            testHead.setResourcePath(newTestHead.getResourcePath());\r
+            update.set("resourcePath", newTestHead.getResourcePath());\r
+        }\r
+        if (newTestHead.getAuthorizationType() != null) {\r
+            testHead.setAuthorizationType(newTestHead.getAuthorizationType());\r
+            update.set("authorizationType", newTestHead.getAuthorizationType());\r
+        }\r
+        if (newTestHead.getAuthorizationCredential() != null) {\r
+            testHead.setAuthorizationCredential(newTestHead.getAuthorizationCredential());\r
+            update.set("authorizationCredential", newTestHead.getAuthorizationCredential());\r
+        }\r
+        if (newTestHead.getAuthorizationEnabled() != null) {\r
+            testHead.setAuthorizationEnabled(newTestHead.getAuthorizationEnabled());\r
+            update.set("authorizationEnabled", newTestHead.getAuthorizationEnabled());\r
+        }\r
+        if (newTestHead.getVthInputTemplate() != null) {\r
+            testHead.setVthInputTemplate(newTestHead.getVthInputTemplate());\r
+            update.set("vthInputTemplate", newTestHead.getVthInputTemplate());\r
+        }\r
+        testHead.setUpdatedAt(new Date());\r
+        update.set("updatedAt", testHead.getUpdatedAt());\r
+        testHead.setUpdatedBy(user.get_id());\r
+        update.set("updatedBy", user.get_id());\r
+\r
+        mongoTemplate.updateFirst(select, update, "testHeads");\r
+        return ResponseUtility.Build.okRequestWithObject(testHead);\r
+    }\r
+\r
+    // check if test head exists in database by name\r
+    private boolean doesTestHeadWithNameExist(String name) {\r
+        Optional<TestHead> optionalTestHead =\r
+                testHeadRepository.findByTestHeadName(name);\r
+        return optionalTestHead.isPresent();\r
+    }\r
+}\r