fa61372e1a2d0b5d10e80664a3142d9cc2240668
[aaf/authz.git] / authz-cass / src / main / java / com / att / dao / aaf / cass / Namespace.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.dao.aaf.cass;\r
25 \r
26 import java.io.ByteArrayOutputStream;\r
27 import java.io.DataInputStream;\r
28 import java.io.DataOutputStream;\r
29 import java.io.IOException;\r
30 import java.nio.ByteBuffer;\r
31 import java.util.ArrayList;\r
32 import java.util.List;\r
33 import java.util.Map.Entry;\r
34 \r
35 import com.att.cssa.rserv.Pair;\r
36 import com.att.dao.Bytification;\r
37 import com.att.dao.CassDAOImpl;\r
38 import com.att.dao.Loader;\r
39 \r
40 \r
41 public class Namespace implements Bytification {\r
42         public static final int MAGIC=250935515;\r
43         public static final int VERSION=1;\r
44         public static final int BUFF_SIZE=48;\r
45 \r
46         public String name;\r
47         public List<String> owner;\r
48         public List<String> admin;\r
49         public List<Pair<String,String>> attrib;\r
50         public String description;\r
51         public Integer type;\r
52         public String parent;\r
53         public Namespace() {}\r
54         \r
55         public Namespace(NsDAO.Data ndd) {\r
56                 name = ndd.name;\r
57                 description = ndd.description;\r
58                 type = ndd.type;\r
59                 parent = ndd.parent;\r
60                 if(ndd.attrib!=null && !ndd.attrib.isEmpty()) {\r
61                         attrib = new ArrayList<Pair<String,String>>();\r
62                         for( Entry<String, String> entry : ndd.attrib.entrySet()) {\r
63                                 attrib.add(new Pair<String,String>(entry.getKey(),entry.getValue()));\r
64                         }\r
65                 }\r
66         }\r
67         \r
68         public Namespace(NsDAO.Data ndd,List<String> owner, List<String> admin) {\r
69                 name = ndd.name;\r
70                 this.owner = owner;\r
71                 this.admin = admin;\r
72                 description = ndd.description;\r
73                 type = ndd.type;\r
74                 parent = ndd.parent;\r
75                 if(ndd.attrib!=null && !ndd.attrib.isEmpty()) {\r
76                         attrib = new ArrayList<Pair<String,String>>();\r
77                         for( Entry<String, String> entry : ndd.attrib.entrySet()) {\r
78                                 attrib.add(new Pair<String,String>(entry.getKey(),entry.getValue()));\r
79                         }\r
80                 }\r
81         }\r
82 \r
83         public NsDAO.Data data() {\r
84                 NsDAO.Data ndd = new NsDAO.Data();\r
85                 ndd.name = name;\r
86                 ndd.description = description;\r
87                 ndd.parent = parent;\r
88                 ndd.type = type;\r
89                 return ndd;\r
90         }\r
91 \r
92         @Override\r
93         public ByteBuffer bytify() throws IOException {\r
94                 ByteArrayOutputStream baos = new ByteArrayOutputStream();\r
95                 DataOutputStream os = new DataOutputStream(baos);\r
96 \r
97                 Loader.writeHeader(os,MAGIC,VERSION);\r
98                 Loader.writeString(os, name);\r
99                 os.writeInt(type);\r
100                 Loader.writeStringSet(os,admin);\r
101                 Loader.writeStringSet(os,owner);\r
102                 Loader.writeString(os,description);\r
103                 Loader.writeString(os,parent);\r
104 \r
105                 return ByteBuffer.wrap(baos.toByteArray());\r
106         }\r
107 \r
108         @Override\r
109         public void reconstitute(ByteBuffer bb) throws IOException {\r
110                 DataInputStream is = CassDAOImpl.toDIS(bb);\r
111                 /*int version = */Loader.readHeader(is,MAGIC,VERSION);\r
112                 // If Version Changes between Production runs, you'll need to do a switch Statement, and adequately read in fields\r
113                 \r
114                 byte[] buff = new byte[BUFF_SIZE];\r
115                 name = Loader.readString(is, buff);\r
116                 type = is.readInt();\r
117                 admin = Loader.readStringList(is,buff);\r
118                 owner = Loader.readStringList(is,buff);\r
119                 description = Loader.readString(is,buff);\r
120                 parent = Loader.readString(is,buff);\r
121                 \r
122         }\r
123 \r
124         /* (non-Javadoc)\r
125          * @see java.lang.Object#hashCode()\r
126          */\r
127         @Override\r
128         public int hashCode() {\r
129                 return name.hashCode();\r
130         }\r
131         \r
132 \r
133         /* (non-Javadoc)\r
134          * @see java.lang.Object#toString()\r
135          */\r
136         @Override\r
137         public String toString() {\r
138                 return name.toString();\r
139         }\r
140 \r
141         /* (non-Javadoc)\r
142          * @see java.lang.Object#equals(java.lang.Object)\r
143          */\r
144         @Override\r
145         public boolean equals(Object arg0) {\r
146                 if(arg0==null || !(arg0 instanceof Namespace)) {\r
147                         return false;\r
148                 }\r
149                 return name.equals(((Namespace)arg0).name);\r
150         }\r
151 \r
152 }\r