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