2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.adapters.network;
25 import com.fasterxml.jackson.annotation.JsonProperty;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import java.util.ArrayList;
29 import java.util.List;
31 import org.onap.so.logger.ErrorCode;
32 import org.onap.so.logger.MessageEnum;
33 import org.onap.so.openstack.beans.HostRoute;
34 import org.onap.so.openstack.beans.Pool;
35 import org.onap.so.openstack.beans.Subnet;
36 import org.onap.so.openstack.utils.MsoCommonUtils;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
41 public class ContrailSubnet {
43 private static final Logger logger = LoggerFactory.getLogger(ContrailSubnet.class);
45 private MsoCommonUtils msoCommonUtils;
47 @JsonProperty("network_ipam_refs_data_ipam_subnets_subnet")
48 private ContrailSubnetIp subnet = new ContrailSubnetIp();
50 @JsonProperty("network_ipam_refs_data_ipam_subnets_default_gateway")
51 private String defaultGateway;
53 @JsonProperty("network_ipam_refs_data_ipam_subnets_subnet_name")
54 private String subnetName;
56 @JsonProperty("network_ipam_refs_data_ipam_subnets_enable_dhcp")
57 private Boolean enableDhcp;
59 @JsonProperty("network_ipam_refs_data_ipam_subnets_addr_from_start")
60 private Boolean addrFromStart = true;
61 /** future - leave this commented
62 private String subnet_uuid;
63 private String dns_server_address;
64 private List<String> dns_nameservers;
65 private String dhcp_option_list;
68 @JsonProperty("network_ipam_refs_data_ipam_subnets_allocation_pools")
69 private List<ContrailSubnetPool> allocationPools = new ArrayList <> ();
71 @JsonProperty("network_ipam_refs_data_ipam_subnets_host_routes")
72 private final ContrailSubnetHostRoutes host_routes = new ContrailSubnetHostRoutes();
74 public ContrailSubnet() {
78 public String getDefaultGateway() {
79 return defaultGateway;
82 public void setDefaultGateway(String defaultGateway) {
83 this.defaultGateway = defaultGateway;
86 public ContrailSubnetIp getSubnet() {
90 public void setSubnet(ContrailSubnetIp subnet) {
94 public Boolean isEnableDhcp() {
98 public void setEnableDhcp(Boolean enableDhcp) {
99 this.enableDhcp = enableDhcp;
102 public String getSubnetName() {
106 public void setSubnetName(String subnetName) {
107 this.subnetName = subnetName;
110 public List<ContrailSubnetPool> getAllocationPools() {
111 return allocationPools;
114 public void setPools(List<ContrailSubnetPool> allocationPools) {
115 this.allocationPools = allocationPools;
118 public Boolean isAddrFromStart() {
119 return addrFromStart;
122 public void setAddrFromStart(Boolean addrFromStart) {
123 this.addrFromStart = addrFromStart;
126 public JsonNode toJsonNode()
128 JsonNode node = null;
131 ObjectMapper mapper = new ObjectMapper();
132 node = mapper.convertValue(this, JsonNode.class);
136 logger.error("{} {} Error creating JsonNode for Contrail Subnet: {} ", MessageEnum.RA_MARSHING_ERROR,
137 ErrorCode.SchemaError.getValue(), subnetName, e);
143 public String toJsonString()
145 String jsonString = null;
148 ObjectMapper mapper = new ObjectMapper();
149 jsonString = mapper.writeValueAsString(this);
153 logger.error("{} {} Error creating JsonString for Contrail Subnet: {} ", MessageEnum.RA_MARSHING_ERROR,
154 ErrorCode.SchemaError.getValue(), subnetName, e);
159 //poulate contrail subnet with input(from bopel) subnet
160 public void populateWith(Subnet inputSubnet)
162 if (inputSubnet != null)
164 if (!msoCommonUtils.isNullOrEmpty(inputSubnet.getSubnetName()))
165 subnetName = inputSubnet.getSubnetName();
167 subnetName = inputSubnet.getSubnetId();
168 enableDhcp = inputSubnet.getEnableDHCP();
169 defaultGateway = inputSubnet.getGatewayIp();
170 if (!msoCommonUtils.isNullOrEmpty(inputSubnet.getCidr()) )
172 int idx = inputSubnet.getCidr().indexOf("/");
175 subnet.setIpPrefix(inputSubnet.getCidr().substring(0, idx));
176 subnet.setIpPrefixLen(inputSubnet.getCidr().substring(idx+1));
179 if (inputSubnet.getAllocationPools() != null)
181 for (Pool pool : inputSubnet.getAllocationPools())
183 if ( !msoCommonUtils.isNullOrEmpty(pool.getStart()) && !msoCommonUtils.isNullOrEmpty(pool.getEnd()) )
185 ContrailSubnetPool csp = new ContrailSubnetPool();
186 csp.populateWith(pool);
187 allocationPools.add (csp);
191 if (inputSubnet.getHostRoutes() != null)
193 List<ContrailSubnetHostRoute> hrList = host_routes.getHost_routes();
194 for (HostRoute hr : inputSubnet.getHostRoutes())
196 if ( !msoCommonUtils.isNullOrEmpty(hr.getPrefix()) || !msoCommonUtils.isNullOrEmpty(hr.getNextHop()) )
198 ContrailSubnetHostRoute cshr = new ContrailSubnetHostRoute();
199 cshr.populateWith(hr);
208 public String toString() {
209 StringBuilder buf = new StringBuilder ();
210 for (ContrailSubnetPool pool : allocationPools)
212 buf.append(pool.toString());
214 return "ContrailSubnet [subnet=" + subnet.toString() + " default_gateway=" + defaultGateway
215 + " enable_dhcp=" + enableDhcp + " addr_from_start=" + addrFromStart + " subnet_name=" + subnetName + " allocation_pools=" + buf + " ]";