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 defaultGateway;
45 @JsonProperty("network_ipam_refs_data_ipam_subnets_subnet_name")
46 private String subnetName;
48 @JsonProperty("network_ipam_refs_data_ipam_subnets_enable_dhcp")
49 private Boolean enableDhcp;
51 @JsonProperty("network_ipam_refs_data_ipam_subnets_addr_from_start")
52 private Boolean addrFromStart = 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> allocationPools = new ArrayList <> ();
65 public ContrailSubnet() {
69 public String getDefaultGateway() {
70 return defaultGateway;
73 public void setDefaultGateway(String defaultGateway) {
74 this.defaultGateway = defaultGateway;
77 public ContrailSubnetIp getSubnet() {
81 public void setSubnet(ContrailSubnetIp subnet) {
85 public Boolean isEnableDhcp() {
89 public void setEnableDhcp(Boolean enableDhcp) {
90 this.enableDhcp = enableDhcp;
93 public String getSubnetName() {
97 public void setSubnetName(String subnetName) {
98 this.subnetName = subnetName;
101 public List<ContrailSubnetPool> getAllocationPools() {
102 return allocationPools;
105 public void setPools(List<ContrailSubnetPool> allocationPools) {
106 this.allocationPools = allocationPools;
109 public Boolean isAddrFromStart() {
110 return addrFromStart;
113 public void setAddrFromStart(Boolean addrFromStart) {
114 this.addrFromStart = addrFromStart;
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:" + subnetName;
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:" + subnetName;
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 inputSubnet)
153 if (inputSubnet != null)
155 if (!isNullOrEmpty(inputSubnet.getSubnetName()))
156 subnetName = inputSubnet.getSubnetName();
158 subnetName = inputSubnet.getSubnetId();
159 enableDhcp = inputSubnet.getEnableDHCP();
160 defaultGateway = inputSubnet.getGatewayIp();
161 if (!isNullOrEmpty(inputSubnet.getCidr()) )
163 int idx = inputSubnet.getCidr().indexOf("/");
166 subnet.setIpPrefix(inputSubnet.getCidr().substring(0, idx));
167 subnet.setIpPrefixLen(inputSubnet.getCidr().substring(idx+1));
170 if (inputSubnet.getAllocationPools() != null)
172 for (Pool pool : inputSubnet.getAllocationPools())
174 if ( !isNullOrEmpty(pool.getStart()) && !isNullOrEmpty(pool.getEnd()) )
176 ContrailSubnetPool csp = new ContrailSubnetPool();
177 csp.populateWith(pool);
178 allocationPools.add (csp);
186 public String toString() {
188 StringBuilder buf = new StringBuilder ();
189 for (ContrailSubnetPool pool : allocationPools)
191 buf.append(pool.toString());
193 return "ContrailSubnet [subnet=" + subnet.toString() + " default_gateway=" + defaultGateway
194 + " enable_dhcp=" + enableDhcp + " addr_from_start=" + addrFromStart + " subnet_name=" + subnetName + " allocation_pools=" + buf + " ]";