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.onap.so.adapters.network;
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.onap.so.logger.MessageEnum;
27 import org.onap.so.logger.MsoLogger;
28 import org.onap.so.openstack.beans.HostRoute;
29 import org.onap.so.openstack.beans.Pool;
30 import org.onap.so.openstack.beans.Subnet;
31 import org.onap.so.openstack.utils.MsoCommonUtils;
32 import org.springframework.beans.factory.annotation.Autowired;
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 final MsoLogger logger = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA, ContrailSubnet.class);
41 private MsoCommonUtils msoCommonUtils;
43 @JsonProperty("network_ipam_refs_data_ipam_subnets_subnet")
44 private ContrailSubnetIp subnet = new ContrailSubnetIp();
46 @JsonProperty("network_ipam_refs_data_ipam_subnets_default_gateway")
47 private String defaultGateway;
49 @JsonProperty("network_ipam_refs_data_ipam_subnets_subnet_name")
50 private String subnetName;
52 @JsonProperty("network_ipam_refs_data_ipam_subnets_enable_dhcp")
53 private Boolean enableDhcp;
55 @JsonProperty("network_ipam_refs_data_ipam_subnets_addr_from_start")
56 private Boolean addrFromStart = true;
57 /** future - leave this commented
58 private String subnet_uuid;
59 private String dns_server_address;
60 private List<String> dns_nameservers;
61 private String dhcp_option_list;
64 @JsonProperty("network_ipam_refs_data_ipam_subnets_allocation_pools")
65 private List<ContrailSubnetPool> allocationPools = new ArrayList <> ();
67 @JsonProperty("network_ipam_refs_data_ipam_subnets_host_routes")
68 private final ContrailSubnetHostRoutes host_routes = new ContrailSubnetHostRoutes();
70 public ContrailSubnet() {
74 public String getDefaultGateway() {
75 return defaultGateway;
78 public void setDefaultGateway(String defaultGateway) {
79 this.defaultGateway = defaultGateway;
82 public ContrailSubnetIp getSubnet() {
86 public void setSubnet(ContrailSubnetIp subnet) {
90 public Boolean isEnableDhcp() {
94 public void setEnableDhcp(Boolean enableDhcp) {
95 this.enableDhcp = enableDhcp;
98 public String getSubnetName() {
102 public void setSubnetName(String subnetName) {
103 this.subnetName = subnetName;
106 public List<ContrailSubnetPool> getAllocationPools() {
107 return allocationPools;
110 public void setPools(List<ContrailSubnetPool> allocationPools) {
111 this.allocationPools = allocationPools;
114 public Boolean isAddrFromStart() {
115 return addrFromStart;
118 public void setAddrFromStart(Boolean addrFromStart) {
119 this.addrFromStart = addrFromStart;
122 public JsonNode toJsonNode()
124 JsonNode node = null;
127 ObjectMapper mapper = new ObjectMapper();
128 node = mapper.convertValue(this, JsonNode.class);
132 String error = "Error creating JsonNode for Contrail Subnet:" + subnetName;
133 logger.error (MessageEnum.RA_MARSHING_ERROR, error, "", "", MsoLogger.ErrorCode.SchemaError, "Exception creating JsonNode for Contrail Subnet", e);
139 public String toJsonString()
141 String jsonString = null;
144 ObjectMapper mapper = new ObjectMapper();
145 jsonString = mapper.writeValueAsString(this);
149 String error = "Error creating JsonString for Contrail Subnet:" + subnetName;
150 logger.error (MessageEnum.RA_MARSHING_ERROR, error, "", "", MsoLogger.ErrorCode.SchemaError, "Exception creating JsonString for Contrail Subnet", e);
155 //poulate contrail subnet with input(from bopel) subnet
156 public void populateWith(Subnet inputSubnet)
158 if (inputSubnet != null)
160 if (!msoCommonUtils.isNullOrEmpty(inputSubnet.getSubnetName()))
161 subnetName = inputSubnet.getSubnetName();
163 subnetName = inputSubnet.getSubnetId();
164 enableDhcp = inputSubnet.getEnableDHCP();
165 defaultGateway = inputSubnet.getGatewayIp();
166 if (!msoCommonUtils.isNullOrEmpty(inputSubnet.getCidr()) )
168 int idx = inputSubnet.getCidr().indexOf("/");
171 subnet.setIpPrefix(inputSubnet.getCidr().substring(0, idx));
172 subnet.setIpPrefixLen(inputSubnet.getCidr().substring(idx+1));
175 if (inputSubnet.getAllocationPools() != null)
177 for (Pool pool : inputSubnet.getAllocationPools())
179 if ( !msoCommonUtils.isNullOrEmpty(pool.getStart()) && !msoCommonUtils.isNullOrEmpty(pool.getEnd()) )
181 ContrailSubnetPool csp = new ContrailSubnetPool();
182 csp.populateWith(pool);
183 allocationPools.add (csp);
187 if (inputSubnet.getHostRoutes() != null)
189 List<ContrailSubnetHostRoute> hrList = host_routes.getHost_routes();
190 for (HostRoute hr : inputSubnet.getHostRoutes())
192 if ( !msoCommonUtils.isNullOrEmpty(hr.getPrefix()) || !msoCommonUtils.isNullOrEmpty(hr.getNextHop()) )
194 ContrailSubnetHostRoute cshr = new ContrailSubnetHostRoute();
195 cshr.populateWith(hr);
204 public String toString() {
205 StringBuilder buf = new StringBuilder ();
206 for (ContrailSubnetPool pool : allocationPools)
208 buf.append(pool.toString());
210 return "ContrailSubnet [subnet=" + subnet.toString() + " default_gateway=" + defaultGateway
211 + " enable_dhcp=" + enableDhcp + " addr_from_start=" + addrFromStart + " subnet_name=" + subnetName + " allocation_pools=" + buf + " ]";