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