AafTopicSetupService implementation
[dmaap/dbcapi.git] / src / main / java / org / onap / dmaap / dbcapi / aaf / AafNamespace.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dmaap
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 IBM.
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
22 package org.onap.dmaap.dbcapi.aaf;
23
24 import org.apache.log4j.Logger;
25 import org.onap.dmaap.dbcapi.util.DmaapConfig;
26
27 import java.util.ArrayList;
28 import java.util.Objects;
29
30
31 public class AafNamespace extends AafObject  {
32         private static final Logger logger = Logger.getLogger(AafNamespace.class);
33
34         private String  name;
35         private ArrayList<String> admin;
36         private ArrayList<String> responsible;
37
38         // in some environments, an AAF Namespace must be owned by a human.
39         // So, when needed, this var can be set via a property
40         private static String NsOwnerIdentity;
41
42         public AafNamespace(String ns, String identity ) {
43                 super();
44                 DmaapConfig p = (DmaapConfig)DmaapConfig.getConfig();
45                 NsOwnerIdentity = p.getProperty( "aaf.NsOwnerIdentity", "");
46                 this.admin = new ArrayList<>();
47                 this.responsible = new ArrayList<>();
48
49                 this.name = ns;
50                 this.admin.add( identity );
51                 this.responsible.add( NsOwnerIdentity );
52         }
53         public void setName( String ns ) {
54                 this.name = ns;
55         }
56         public String getName() {
57                 return name;
58         }
59         public ArrayList<String> getAdmin() {
60                 return admin;
61         }
62         public void setAdmin(ArrayList<String> admin) {
63                 this.admin = admin;
64         }
65         public ArrayList<String> getResponsible() {
66                 return responsible;
67         }
68         public void setResponsible(ArrayList<String> responsible) {
69                 this.responsible = responsible;
70         }
71
72
73         // given an Array of Strings, return a String that is a separated list of quoted strings.
74         // e.g. input [ a, b, c ]
75         //       output  "a", "b", "c"
76         private String separatedList( ArrayList<String> list, String sep ) {
77                 if (list.isEmpty()) return null;
78                 String aList = "";
79                 String delim = "";
80                 for( String item: list) {
81                         if( ! item.isEmpty()) {
82                                 aList += String.format( "%s\"%s\"", delim, item );
83                                 delim = sep;
84                         }
85                 }
86                 return aList;
87         }
88
89         public String toJSON() {
90
91                 String postJSON = String.format(" { \"name\": \"%s\", \"admin\": [",
92                                 this.getName()
93                                  );
94                 postJSON += separatedList( this.getAdmin(), "," );
95                 postJSON += "], \"responsible\":[";
96                 postJSON += separatedList( this.getResponsible(), ",");
97                 postJSON += "]}";
98                 logger.info( "returning JSON: " + postJSON);
99
100                 return postJSON;
101         }
102
103         @Override
104         public boolean equals(Object o) {
105                 if (this == o) return true;
106                 if (o == null || getClass() != o.getClass()) return false;
107                 AafNamespace that = (AafNamespace) o;
108                 return Objects.equals(name, that.name) &&
109                                 Objects.equals(admin, that.admin) &&
110                                 Objects.equals(responsible, that.responsible);
111         }
112
113         @Override
114         public int hashCode() {
115                 return Objects.hash(name, admin, responsible);
116         }
117 }