192887b985d2937e37b52d801fdf7758f677595e
[aaf/authz.git] / auth / auth-cass / src / main / java / org / onap / aaf / auth / dao / cass / Namespace.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 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
22 package org.onap.aaf.auth.dao.cass;
23
24 import java.io.ByteArrayOutputStream;
25 import java.io.DataInputStream;
26 import java.io.DataOutputStream;
27 import java.io.IOException;
28 import java.nio.ByteBuffer;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map.Entry;
32
33 import org.onap.aaf.auth.dao.Bytification;
34 import org.onap.aaf.auth.dao.CassDAOImpl;
35 import org.onap.aaf.auth.dao.Loader;
36 import org.onap.aaf.auth.rserv.Pair;
37
38
39 public class Namespace implements Bytification {
40     public static final int MAGIC=250935515;
41     public static final int VERSION=1;
42     public static final int BUFF_SIZE=48;
43
44     public String name;
45     public List<String> owner;
46     public List<String> admin;
47     public List<Pair<String,String>> attrib;
48     public String description;
49     public Integer type;
50     public String parent;
51     public Namespace() {}
52     
53     public Namespace(NsDAO.Data ndd) {
54         name = ndd.name;
55         description = ndd.description;
56         type = ndd.type;
57         parent = ndd.parent;
58         if (ndd.attrib!=null && !ndd.attrib.isEmpty()) {
59             attrib = new ArrayList<>();
60             for ( Entry<String, String> entry : ndd.attrib.entrySet()) {
61                 attrib.add(new Pair<String,String>(entry.getKey(),entry.getValue()));
62             }
63         }
64     }
65     
66     public Namespace(NsDAO.Data ndd,List<String> owner, List<String> admin) {
67         name = ndd.name;
68         this.owner = owner;
69         this.admin = admin;
70         description = ndd.description;
71         type = ndd.type;
72         parent = ndd.parent;
73         if (ndd.attrib!=null && !ndd.attrib.isEmpty()) {
74             attrib = new ArrayList<>();
75             for ( Entry<String, String> entry : ndd.attrib.entrySet()) {
76                 attrib.add(new Pair<String,String>(entry.getKey(),entry.getValue()));
77             }
78         }
79     }
80
81     public NsDAO.Data data() {
82         NsDAO.Data ndd = new NsDAO.Data();
83         ndd.name = name;
84         ndd.description = description;
85         ndd.parent = parent;
86         ndd.type = type;
87         return ndd;
88     }
89
90     @Override
91     public ByteBuffer bytify() throws IOException {
92         ByteArrayOutputStream baos = new ByteArrayOutputStream();
93         DataOutputStream os = new DataOutputStream(baos);
94
95         Loader.writeHeader(os,MAGIC,VERSION);
96         Loader.writeString(os, name);
97         os.writeInt(type);
98         Loader.writeStringSet(os,admin);
99         Loader.writeStringSet(os,owner);
100         Loader.writeString(os,description);
101         Loader.writeString(os,parent);
102
103         return ByteBuffer.wrap(baos.toByteArray());
104     }
105
106     @Override
107     public void reconstitute(ByteBuffer bb) throws IOException {
108         DataInputStream is = CassDAOImpl.toDIS(bb);
109         /*int version = */Loader.readHeader(is,MAGIC,VERSION);
110         // If Version Changes between Production runs, you'll need to do a switch Statement, and adequately read in fields
111         
112         byte[] buff = new byte[BUFF_SIZE];
113         name = Loader.readString(is, buff);
114         type = is.readInt();
115         admin = Loader.readStringList(is,buff);
116         owner = Loader.readStringList(is,buff);
117         description = Loader.readString(is,buff);
118         parent = Loader.readString(is,buff);
119         
120     }
121
122     /* (non-Javadoc)
123      * @see java.lang.Object#hashCode()
124      */
125     @Override
126     public int hashCode() {
127         return name.hashCode();
128     }
129     
130
131     /* (non-Javadoc)
132      * @see java.lang.Object#toString()
133      */
134     @Override
135     public String toString() {
136         return name.toString();
137     }
138
139     /* (non-Javadoc)
140      * @see java.lang.Object#equals(java.lang.Object)
141      */
142     @Override
143     public boolean equals(Object arg0) {
144         if (arg0==null || !(arg0 instanceof Namespace)) {
145             return false;
146         }
147         return name.equals(((Namespace)arg0).name);
148     }
149
150 }