summaryrefslogtreecommitdiffstats
path: root/authz-cass/src/main/java/org/onap/aaf/dao/aaf/cass/Namespace.java
blob: 98c4616557eb4049aa79ab33ef2912ec7b9f440b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package org.onap.aaf.dao.aaf.cass;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;

import org.onap.aaf.cssa.rserv.Pair;
import org.onap.aaf.dao.Bytification;
import org.onap.aaf.dao.CassDAOImpl;
import org.onap.aaf.dao.Loader;


public class Namespace implements Bytification {
	public static final int MAGIC=250935515;
	public static final int VERSION=1;
	public static final int BUFF_SIZE=48;

	public String name;
	public List<String> owner;
	public List<String> admin;
	public List<Pair<String,String>> attrib;
	public String description;
	public Integer type;
	public String parent;
	public Namespace() {}
	
	public Namespace(NsDAO.Data ndd) {
		name = ndd.name;
		description = ndd.description;
		type = ndd.type;
		parent = ndd.parent;
		if(ndd.attrib!=null && !ndd.attrib.isEmpty()) {
			attrib = new ArrayList<Pair<String,String>>();
			for( Entry<String, String> entry : ndd.attrib.entrySet()) {
				attrib.add(new Pair<String,String>(entry.getKey(),entry.getValue()));
			}
		}
	}
	
	public Namespace(NsDAO.Data ndd,List<String> owner, List<String> admin) {
		name = ndd.name;
		this.owner = owner;
		this.admin = admin;
		description = ndd.description;
		type = ndd.type;
		parent = ndd.parent;
		if(ndd.attrib!=null && !ndd.attrib.isEmpty()) {
			attrib = new ArrayList<Pair<String,String>>();
			for( Entry<String, String> entry : ndd.attrib.entrySet()) {
				attrib.add(new Pair<String,String>(entry.getKey(),entry.getValue()));
			}
		}
	}

	public NsDAO.Data data() {
		NsDAO.Data ndd = new NsDAO.Data();
		ndd.name = name;
		ndd.description = description;
		ndd.parent = parent;
		ndd.type = type;
		return ndd;
	}

	@Override
	public ByteBuffer bytify() throws IOException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream os = new DataOutputStream(baos);

		Loader.writeHeader(os,MAGIC,VERSION);
		Loader.writeString(os, name);
		os.writeInt(type);
		Loader.writeStringSet(os,admin);
		Loader.writeStringSet(os,owner);
		Loader.writeString(os,description);
		Loader.writeString(os,parent);

		return ByteBuffer.wrap(baos.toByteArray());
	}

	@Override
	public void reconstitute(ByteBuffer bb) throws IOException {
		DataInputStream is = CassDAOImpl.toDIS(bb);
		/*int version = */Loader.readHeader(is,MAGIC,VERSION);
		// If Version Changes between Production runs, you'll need to do a switch Statement, and adequately read in fields
		
		byte[] buff = new byte[BUFF_SIZE];
		name = Loader.readString(is, buff);
		type = is.readInt();
		admin = Loader.readStringList(is,buff);
		owner = Loader.readStringList(is,buff);
		description = Loader.readString(is,buff);
		parent = Loader.readString(is,buff);
		
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return name.hashCode();
	}
	

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return name.toString();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object arg0) {
		if(arg0==null || !(arg0 instanceof Namespace)) {
			return false;
		}
		return name.equals(((Namespace)arg0).name);
	}

}