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 static org.openecomp.mso.openstack.utils.MsoCommonUtils.isNullOrEmpty;
25 import java.util.ArrayList;
26 import java.util.List;
28 import org.openecomp.mso.logger.MessageEnum;
29 import org.openecomp.mso.logger.MsoLogger;
30 import org.openecomp.mso.openstack.beans.HostRoute;
31 import org.openecomp.mso.openstack.beans.Pool;
32 import org.openecomp.mso.openstack.beans.Subnet;
34 import com.fasterxml.jackson.annotation.JsonProperty;
35 import com.fasterxml.jackson.databind.JsonNode;
36 import com.fasterxml.jackson.databind.ObjectMapper;
38 public class ContrailSubnet {
39 private static MsoLogger logger = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
41 @JsonProperty("network_ipam_refs_data_ipam_subnets_subnet")
42 private ContrailSubnetIp subnet = new ContrailSubnetIp();
44 @JsonProperty("network_ipam_refs_data_ipam_subnets_default_gateway")
45 private String defaultGateway;
47 @JsonProperty("network_ipam_refs_data_ipam_subnets_subnet_name")
48 private String subnetName;
50 @JsonProperty("network_ipam_refs_data_ipam_subnets_enable_dhcp")
51 private Boolean enableDhcp;
53 @JsonProperty("network_ipam_refs_data_ipam_subnets_addr_from_start")
54 private Boolean addrFromStart = true;
55 /** future - leave this commented
56 private String subnet_uuid;
57 private String dns_server_address;
58 private List<String> dns_nameservers;
59 private String dhcp_option_list;
62 @JsonProperty("network_ipam_refs_data_ipam_subnets_allocation_pools")
63 private List<ContrailSubnetPool> allocationPools = new ArrayList <> ();
65 @JsonProperty("network_ipam_refs_data_ipam_subnets_host_routes")
66 private ContrailSubnetHostRoutes host_routes = new ContrailSubnetHostRoutes();
68 public ContrailSubnet() {
72 public String getDefaultGateway() {
73 return defaultGateway;
76 public void setDefaultGateway(String defaultGateway) {
77 this.defaultGateway = defaultGateway;
80 public ContrailSubnetIp getSubnet() {
84 public void setSubnet(ContrailSubnetIp subnet) {
88 public Boolean isEnableDhcp() {
92 public void setEnableDhcp(Boolean enableDhcp) {
93 this.enableDhcp = enableDhcp;
96 public String getSubnetName() {
100 public void setSubnetName(String subnetName) {
101 this.subnetName = subnetName;
104 public List<ContrailSubnetPool> getAllocationPools() {
105 return allocationPools;
108 public void setPools(List<ContrailSubnetPool> allocationPools) {
109 this.allocationPools = allocationPools;
112 public Boolean isAddrFromStart() {
113 return addrFromStart;
116 public void setAddrFromStart(Boolean addrFromStart) {
117 this.addrFromStart = addrFromStart;
120 public JsonNode toJsonNode()
122 JsonNode node = null;
125 ObjectMapper mapper = new ObjectMapper();
126 node = mapper.convertValue(this, JsonNode.class);
130 String error = "Error creating JsonNode for Contrail Subnet:" + subnetName;
131 logger.error (MessageEnum.RA_MARSHING_ERROR, error, "", "", MsoLogger.ErrorCode.SchemaError, "Exception creating JsonNode for Contrail Subnet", e);
137 public String toJsonString()
139 String jsonString = null;
142 ObjectMapper mapper = new ObjectMapper();
143 jsonString = mapper.writeValueAsString(this);
147 String error = "Error creating JsonString for Contrail Subnet:" + subnetName;
148 logger.error (MessageEnum.RA_MARSHING_ERROR, error, "", "", MsoLogger.ErrorCode.SchemaError, "Exception creating JsonString for Contrail Subnet", e);
153 //poulate contrail subnet with input(from bopel) subnet
154 public void populateWith(Subnet inputSubnet)
156 if (inputSubnet != null)
158 if (!isNullOrEmpty(inputSubnet.getSubnetName()))
159 subnetName = inputSubnet.getSubnetName();
161 subnetName = inputSubnet.getSubnetId();
162 enableDhcp = inputSubnet.getEnableDHCP();
163 defaultGateway = inputSubnet.getGatewayIp();
164 if (!isNullOrEmpty(inputSubnet.getCidr()) )
166 int idx = inputSubnet.getCidr().indexOf("/");
169 subnet.setIpPrefix(inputSubnet.getCidr().substring(0, idx));
170 subnet.setIpPrefixLen(inputSubnet.getCidr().substring(idx+1));
173 if (inputSubnet.getAllocationPools() != null)
175 for (Pool pool : inputSubnet.getAllocationPools())
177 if ( !isNullOrEmpty(pool.getStart()) && !isNullOrEmpty(pool.getEnd()) )
179 ContrailSubnetPool csp = new ContrailSubnetPool();
180 csp.populateWith(pool);
181 allocationPools.add (csp);
185 if (inputSubnet.getHostRoutes() != null)
187 List<ContrailSubnetHostRoute> hrList = host_routes.getHost_routes();
188 for (HostRoute hr : inputSubnet.getHostRoutes())
190 if ( !isNullOrEmpty(hr.getPrefix()) || !isNullOrEmpty(hr.getNextHop()) )
192 ContrailSubnetHostRoute cshr = new ContrailSubnetHostRoute();
193 cshr.populateWith(hr);
202 public String toString() {
203 StringBuilder buf = new StringBuilder ();
204 for (ContrailSubnetPool pool : allocationPools)
206 buf.append(pool.toString());
208 return "ContrailSubnet [subnet=" + subnet.toString() + " default_gateway=" + defaultGateway
209 + " enable_dhcp=" + enableDhcp + " addr_from_start=" + addrFromStart + " subnet_name=" + subnetName + " allocation_pools=" + buf + " ]";