4ece3771c13e75909e35a59ab969eaa9ac32f375
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / introspection / sideeffect / OwnerCheck.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.aai.introspection.sideeffect;
22
23 import java.io.UnsupportedEncodingException;
24 import java.net.URISyntaxException;
25
26 import java.util.List;
27 import java.util.Map.Entry;
28 import java.util.Optional;
29 import org.apache.tinkerpop.gremlin.structure.Vertex;
30 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
31 import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException;
32 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
33 import org.onap.aai.exceptions.AAIException;
34 import org.onap.aai.introspection.Introspector;
35 import org.onap.aai.schema.enums.PropertyMetadata;
36 import org.onap.aai.serialization.db.DBSerializer;
37 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
38
39 public class OwnerCheck extends SideEffect {
40
41     public OwnerCheck(Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) {
42         super(obj, self, dbEngine, serializer);
43     }
44
45     @Override
46     protected void processURI(Optional<String> completeUri, Entry<String, String> entry)
47         throws AAIException {
48         if (!isAuthorized(serializer.getGroups(), self)) {
49
50             throw new AAIException("AAI_3304",
51                 "Group(s) :" + serializer.getGroups() + " not authorized to perform function");
52
53         } //else skip processing because no required properties were specified
54
55     }
56
57     public static boolean isAuthorized(java.util.Set<String> groups, Vertex vertex) {
58         if (groups != null && !groups.isEmpty()) {
59             List<Vertex> owningEntity = vertex.graph().traversal()
60                 .V(vertex)
61                 .bothE("org.onap.relationships.inventory.BelongsTo")
62                 .otherV()
63                 .has("aai-node-type", "owning-entity")
64                 .toList();
65
66             if(!owningEntity.isEmpty()) {
67                 VertexProperty owningEntityName = owningEntity.get(0).property("owning-entity-name");
68
69                 return groups.contains(owningEntityName.orElseGet(null));
70             }
71         }
72
73         return true;
74     }
75
76     @Override
77     protected PropertyMetadata getPropertyMetadata() {
78         return PropertyMetadata.OWNER_CHECK;
79     }
80
81     @Override
82     protected boolean replaceWithWildcard() {
83         return false;
84     }
85
86 }