2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.mso.adapters.network;
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.codehaus.jackson.JsonNode;
27 import org.codehaus.jackson.annotate.JsonProperty;
28 import org.codehaus.jackson.map.ObjectMapper;
30 import org.openecomp.mso.logger.MessageEnum;
31 import org.openecomp.mso.logger.MsoLogger;
32 import org.openecomp.mso.openstack.beans.Pool;
33 import org.openecomp.mso.openstack.beans.Subnet;
34 import static org.openecomp.mso.openstack.utils.MsoCommonUtils.isNullOrEmpty;
36 public class ContrailSubnet {
37 private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
39 @JsonProperty("network_ipam_refs_data_ipam_subnets_subnet")
40 private ContrailSubnetIp subnet = new ContrailSubnetIp();
42 @JsonProperty("network_ipam_refs_data_ipam_subnets_default_gateway")
43 private String default_gateway;
45 @JsonProperty("network_ipam_refs_data_ipam_subnets_subnet_name")
46 private String subnet_name;
48 @JsonProperty("network_ipam_refs_data_ipam_subnets_enable_dhcp")
49 private Boolean enable_dhcp;
51 @JsonProperty("network_ipam_refs_data_ipam_subnets_addr_from_start")
52 private Boolean addr_from_start = true;
54 /** future - leave this commented
55 private String subnet_uuid;
56 private String dns_server_address;
57 private List<String> dns_nameservers;
58 private String dhcp_option_list;
59 private String host_routes;
62 @JsonProperty("network_ipam_refs_data_ipam_subnets_allocation_pools")
63 private List<ContrailSubnetPool> allocation_pools = new ArrayList <ContrailSubnetPool> ();
65 public ContrailSubnet() {
69 public String getDefault_gateway() {
70 return default_gateway;
73 public void setDefault_gateway(String default_gateway) {
74 this.default_gateway = default_gateway;
77 public ContrailSubnetIp getSubnet() {
81 public void setSubnet(ContrailSubnetIp subnet) {
85 public Boolean isEnable_dhcp() {
89 public void setEnable_dhcp(Boolean enable_dhcp) {
90 this.enable_dhcp = enable_dhcp;
93 public String getSubnet_name() {
97 public void setSubnet_name(String subnet_name) {
98 this.subnet_name = subnet_name;
101 public List<ContrailSubnetPool> getAllocation_pools() {
102 return allocation_pools;
105 public void setPools(List<ContrailSubnetPool> allocation_pools) {
106 this.allocation_pools = allocation_pools;
109 public Boolean isAddr_from_start() {
110 return addr_from_start;
113 public void setAddr_from_start(Boolean addr_from_start) {
114 this.addr_from_start = addr_from_start;
117 public JsonNode toJsonNode()
119 JsonNode node = null;
122 ObjectMapper mapper = new ObjectMapper();
123 node = mapper.convertValue(this, JsonNode.class);
127 String error = "Error creating JsonNode for Contrail Subnet:" + subnet_name;
128 LOGGER.error (MessageEnum.RA_MARSHING_ERROR, error, "", "", MsoLogger.ErrorCode.SchemaError, "Exception creating JsonNode for Contrail Subnet", e);
134 public String toJsonString()
136 String jsonString = null;
139 ObjectMapper mapper = new ObjectMapper();
140 jsonString = mapper.writeValueAsString(this);
144 String error = "Error creating JsonString for Contrail Subnet:" + subnet_name;
145 LOGGER.error (MessageEnum.RA_MARSHING_ERROR, error, "", "", MsoLogger.ErrorCode.SchemaError, "Exception creating JsonString for Contrail Subnet", e);
150 //poulate contrail subnet with input(from bopel) subnet
151 public void populateWith(Subnet i_subnet)
153 if (i_subnet != null)
155 if (!isNullOrEmpty(i_subnet.getSubnetName()))
156 subnet_name = i_subnet.getSubnetName();
158 subnet_name = i_subnet.getSubnetId();
159 enable_dhcp = i_subnet.getEnableDHCP();
160 default_gateway = i_subnet.getGatewayIp();
161 if (!isNullOrEmpty(i_subnet.getCidr()) )
163 int idx = i_subnet.getCidr().indexOf("/");
166 subnet.setIp_prefix(i_subnet.getCidr().substring(0, idx));
167 subnet.setIp_prefix_len(i_subnet.getCidr().substring(idx+1));
170 if (i_subnet.getAllocationPools() != null)
172 for (Pool pool : i_subnet.getAllocationPools())
174 if ( !isNullOrEmpty(pool.getStart()) && !isNullOrEmpty(pool.getEnd()) )
176 ContrailSubnetPool csp = new ContrailSubnetPool();
177 csp.populateWith(pool);
178 allocation_pools.add (csp);
186 public String toString() {
188 StringBuilder buf = new StringBuilder ();
189 for (ContrailSubnetPool pool : allocation_pools)
191 buf.append(pool.toString());
193 return "ContrailSubnet [subnet=" + subnet.toString() + " default_gateway=" + default_gateway
194 + " enable_dhcp=" + enable_dhcp + " addr_from_start=" + addr_from_start + " subnet_name=" + subnet_name + " allocation_pools=" + buf + " ]";