aboutsummaryrefslogtreecommitdiffstats
path: root/adapters/mso-network-adapter/src/main/java/org/openecomp/mso/adapters/network/ContrailSubnet.java
blob: 80b675465611c4efb54da13d5215de0a8d9d706f (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 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=========================================================
 */

package org.openecomp.mso.adapters.network;

import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.ObjectMapper;

import org.openecomp.mso.logger.MessageEnum;
import org.openecomp.mso.logger.MsoLogger;
import org.openecomp.mso.openstack.beans.Pool;
import org.openecomp.mso.openstack.beans.Subnet;
import static org.openecomp.mso.openstack.utils.MsoCommonUtils.isNullOrEmpty;

public class ContrailSubnet {
	private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);

	@JsonProperty("network_ipam_refs_data_ipam_subnets_subnet")
	private ContrailSubnetIp subnet = new ContrailSubnetIp();
	
	@JsonProperty("network_ipam_refs_data_ipam_subnets_default_gateway")
	private String default_gateway;
	
	@JsonProperty("network_ipam_refs_data_ipam_subnets_subnet_name")
	private String subnet_name;
	
	@JsonProperty("network_ipam_refs_data_ipam_subnets_enable_dhcp")
	private Boolean enable_dhcp;
	
	@JsonProperty("network_ipam_refs_data_ipam_subnets_addr_from_start")
	private Boolean addr_from_start = true;
	
	/** future - leave this commented
	private String subnet_uuid;
	private String dns_server_address;
	private List<String> dns_nameservers;
	private String dhcp_option_list;
	private String host_routes;
	**/
	
	@JsonProperty("network_ipam_refs_data_ipam_subnets_allocation_pools")
	private List<ContrailSubnetPool> allocation_pools =  new ArrayList <ContrailSubnetPool> ();

	public ContrailSubnet() {
		super();
	}

	public String getDefault_gateway() {
		return default_gateway;
	}

	public void setDefault_gateway(String default_gateway) {
		this.default_gateway = default_gateway;
	}

	public ContrailSubnetIp getSubnet() {
		return subnet;
	}

	public void setSubnet(ContrailSubnetIp subnet) {
		this.subnet = subnet;
	}

	public Boolean isEnable_dhcp() {
		return enable_dhcp;
	}

	public void setEnable_dhcp(Boolean enable_dhcp) {
		this.enable_dhcp = enable_dhcp;
	}

	public String getSubnet_name() {
		return subnet_name;
	}

	public void setSubnet_name(String subnet_name) {
		this.subnet_name = subnet_name;
	}

	public List<ContrailSubnetPool> getAllocation_pools() {
		return allocation_pools;
	}

	public void setPools(List<ContrailSubnetPool> allocation_pools) {
		this.allocation_pools = allocation_pools;
	}

	public Boolean isAddr_from_start() {
		return addr_from_start;
	}

	public void setAddr_from_start(Boolean addr_from_start) {
		this.addr_from_start = addr_from_start;
	}

	public JsonNode toJsonNode()
	{
		JsonNode node = null;
		try
		{
			ObjectMapper mapper = new ObjectMapper();
			node = mapper.convertValue(this, JsonNode.class);
		}
		catch (Exception e)
		{
			String error = "Error creating JsonNode for Contrail Subnet:" + subnet_name;
			LOGGER.error (MessageEnum.RA_MARSHING_ERROR, error, "", "", MsoLogger.ErrorCode.SchemaError, "Exception creating JsonNode for Contrail Subnet", e);
		}
		
		return node;
	}
	
	public String toJsonString()
	{
		String jsonString = null;
		try
		{
			ObjectMapper mapper = new ObjectMapper();
			jsonString = mapper.writeValueAsString(this);
		}
		catch (Exception e)
		{
			String error = "Error creating JsonString for Contrail Subnet:" + subnet_name;
			LOGGER.error (MessageEnum.RA_MARSHING_ERROR, error, "", "", MsoLogger.ErrorCode.SchemaError, "Exception creating JsonString for Contrail Subnet", e);
		}
		
		return jsonString;
	}
	//poulate contrail subnet with input(from bopel) subnet
	public void populateWith(Subnet i_subnet)
	{
		if (i_subnet != null)
		{
			if (!isNullOrEmpty(i_subnet.getSubnetName()))
				subnet_name = i_subnet.getSubnetName();
			else
				subnet_name = i_subnet.getSubnetId();
			enable_dhcp = i_subnet.getEnableDHCP();
			default_gateway = i_subnet.getGatewayIp();
			if (!isNullOrEmpty(i_subnet.getCidr()) )
			{
				int idx = i_subnet.getCidr().indexOf("/");
				if (idx != -1)
				{
					subnet.setIp_prefix(i_subnet.getCidr().substring(0, idx));
					subnet.setIp_prefix_len(i_subnet.getCidr().substring(idx+1));
				}
			}
			if (i_subnet.getAllocationPools() != null)
			{
				for (Pool pool : i_subnet.getAllocationPools())
				{
					if ( !isNullOrEmpty(pool.getStart()) && !isNullOrEmpty(pool.getEnd()) )
					{		
						ContrailSubnetPool csp = new ContrailSubnetPool();
						csp.populateWith(pool);
						allocation_pools.add (csp);
					}
				}
			}
		}
	}

	@Override
	public String toString() {
		
		StringBuilder buf = new StringBuilder ();
		for (ContrailSubnetPool pool : allocation_pools)
		{
			 buf.append(pool.toString());
		}
		return "ContrailSubnet [subnet=" + subnet.toString() + " default_gateway=" + default_gateway
				+ " enable_dhcp=" + enable_dhcp +  " addr_from_start=" + addr_from_start + " subnet_name=" + subnet_name + " allocation_pools=" + buf + " ]";
	}
}