[AAI-2175] Change aai champ container processes to run as non-root on the host
[aai/champ.git] / champ-lib / champ-core / src / main / java / org / onap / aai / champcore / model / ChampRelationship.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ===================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  */
21 package org.onap.aai.champcore.model;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import java.util.Objects;
27 import java.util.Optional;
28
29 import org.onap.aai.champcore.model.fluent.relationship.CreateChampRelationshipable;
30 import org.onap.aai.champcore.model.fluent.relationship.impl.CreateChampRelationshipableImpl;
31
32 import com.fasterxml.jackson.annotation.JsonIgnore;
33 import com.fasterxml.jackson.annotation.JsonProperty;
34
35 public final class ChampRelationship implements ChampElement {
36         private String type; //AKA edge label
37         private  Optional<Object> key;
38         private  Map<String, Object> properties;
39         private  ChampObject source;
40         private  ChampObject target;
41
42         public static CreateChampRelationshipable create() {
43                 return new CreateChampRelationshipableImpl();
44         }
45
46         public ChampRelationship() { //Not instantiable
47         }
48         
49         private ChampRelationship(Builder builder) {
50                 this.properties = builder.properties;
51                 this.source = builder.source;
52                 this.target = builder.target;
53                 this.type = builder.type;
54                 this.key = builder.key;
55         }
56
57         @JsonIgnore
58         public Optional<Object> getKey() {
59                 if (key == null) {
60                         return Optional.empty ();
61                 } else {
62                         return key;
63                 }
64         }
65
66     @JsonProperty("key")
67     public String getKeyValue() {
68       return (getKey().isPresent() ? getKey().get() : "").toString();
69     }
70            
71         public ChampObject getSource() {
72                 return source;
73         }
74         
75         public ChampObject getTarget() {
76                 return target;
77         }
78         
79         public String getType() {
80                 return type;
81         }
82         
83         @SuppressWarnings("unchecked")
84         public <T> Optional<T> getProperty(String key) {
85                 if (!properties.containsKey(key)) return Optional.empty();
86                 
87                 return Optional.of((T) properties.get(key));
88         }
89
90         public Map<String, Object> getProperties() {
91                 return properties;
92         }
93
94         public static class Builder {
95                 private final Map<String, Object> properties = new HashMap<String, Object> ();
96                 private final ChampObject source;
97                 private final ChampObject target;
98                 private final String type;
99
100                 private Optional<Object> key = Optional.empty();
101
102                 public Builder(ChampObject source, ChampObject target, String type) {
103                         this.source = source;
104                         this.target = target;
105                         this.type = type;
106                 }
107                 
108                 public Builder(ChampRelationship relationship) {
109                         this.source = relationship.source;
110                         this.target = relationship.target;
111                         this.type = relationship.type;
112
113                         properties.putAll(relationship.getProperties());
114                 }
115
116                 public Builder key(Object key) {
117                         this.key = Optional.of(key);
118                         return this;
119                 }
120
121                 public Builder properties(Map<String, Object> properties) {
122                         for (Entry<String, Object> property : properties.entrySet()) {
123                                 property(property.getKey(), property.getValue());
124                         }
125
126                         return this;
127                 }
128
129                 public Builder property(String key, Object value) {
130                         if (ChampRelationship.ReservedPropertyKeys.contains(key)) throw new IllegalArgumentException("Cannot make use of reserved property key " + key);
131
132                         properties.put(key, value);
133                         return this;
134                 }
135                 
136                 public ChampRelationship build() {
137                         return new ChampRelationship(this);
138                 }
139         }
140
141         @Override
142         public boolean equals(Object object) {
143                 if (this == object) return true;
144                 if (object instanceof ChampRelationship) {
145                         final ChampRelationship champRelationship = (ChampRelationship) object;
146
147                         if (getKey().isPresent() && champRelationship.getKey().isPresent()) {
148                                 if (getKey().get().equals(champRelationship.getKey().get())) return true;
149                         }
150                 }
151                 
152                 return false;
153         }
154
155         @Override
156         public int hashCode() {
157                 return Objects.hashCode(getKey());
158         }
159
160         @Override
161         public String toString() {
162                 return "{key: " + (getKey().isPresent() ? getKey().get() : "") 
163                                 + ", type: " + getType()
164                                 + ", source: " + getSource()
165                                 + ", target: " + getTarget()
166                                 + ", properties: " + getProperties() + "}";
167         }
168
169         public enum ReservedPropertyKeys {
170                 CHAMP_RELATIONSHIP_TYPE ("relationshipType"),
171                 CHAMP_RELATIONSHIP_KEY ("key");
172
173                 private final String text;
174
175                 private ReservedPropertyKeys(final String text) {
176                         this.text = text;
177                 } 
178                 
179                 public String toString() {
180                         return text;
181                 }
182
183                 public static boolean contains(String key) {
184                         for (ReservedPropertyKeys choice : ReservedPropertyKeys.values()) {
185                                 if (choice.toString().equals(key)) return true;
186                         }
187
188                         return false;
189                 }
190         }
191
192         public enum ReservedTypes {
193                 ANY ("ANY");
194
195                 private final String text;
196                 
197                 private ReservedTypes(final String text) {
198                         this.text = text;
199                 }
200                 
201                 public String toString() {
202                         return text;
203                 }
204         }
205
206         @Override
207         public boolean isObject() {
208                 return false;
209         }
210
211         @Override
212         public ChampObject asObject() {
213                 throw new UnsupportedOperationException("Cannot call asObject() on ChampRelationship");
214         }
215
216         @Override
217         public boolean isRelationship() {
218                 return true;
219         }
220
221         @Override
222         public ChampRelationship asRelationship() {
223                 return this;
224         }
225 }