Add side effect to check pnf owning entity
[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 (serializer.getGroups() != null && !serializer.getGroups().isEmpty()) {
49             List<Vertex> owningEntity = self.graph().traversal()
50                 .V(self)
51                 .inE("org.onap.relationships.inventory.BelongsTo")
52                 .outV()
53                 .has("aai-node-type", "owning-entity")
54                 .toList();
55
56             if(!owningEntity.isEmpty()) {
57                 VertexProperty owningEntityName = owningEntity.get(0).property("owning-entity-name");
58
59                 if(!serializer.getGroups().contains(owningEntityName.orElseGet(null))) {
60                     throw new AAIException("AAI_3304",
61                         "Group(s) :" + serializer.getGroups() + " not authorized to perform function");
62                 }
63             }
64         } //else skip processing because no required properties were specified
65
66     }
67
68     @Override
69     protected PropertyMetadata getPropertyMetadata() {
70         return PropertyMetadata.OWNER_CHECK;
71     }
72
73     @Override
74     protected boolean replaceWithWildcard() {
75         return false;
76     }
77
78 }