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;
30 import org.onap.so.logger.ErrorCode;
31 import org.onap.so.logger.MessageEnum;
32 import org.onap.so.openstack.beans.HostRoute;
33 import org.onap.so.openstack.beans.Pool;
34 import org.onap.so.openstack.beans.Subnet;
35 import org.onap.so.openstack.utils.MsoCommonUtils;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.beans.factory.annotation.Autowired;
40 public class ContrailSubnet {
42 private static final Logger logger = LoggerFactory.getLogger(ContrailSubnet.class);
44 private MsoCommonUtils msoCommonUtils;
46 @JsonProperty("network_ipam_refs_data_ipam_subnets_subnet")
47 private ContrailSubnetIp subnet = new ContrailSubnetIp();
49 @JsonProperty("network_ipam_refs_data_ipam_subnets_default_gateway")
50 private String defaultGateway;
52 @JsonProperty("network_ipam_refs_data_ipam_subnets_subnet_name")
53 private String subnetName;
55 @JsonProperty("network_ipam_refs_data_ipam_subnets_enable_dhcp")
56 private Boolean enableDhcp;
58 @JsonProperty("network_ipam_refs_data_ipam_subnets_addr_from_start")
59 private Boolean addrFromStart = true;
61 * future - leave this commented private String subnet_uuid; private String dns_server_address; private List<String>
62 * dns_nameservers; private String dhcp_option_list;
65 @JsonProperty("network_ipam_refs_data_ipam_subnets_allocation_pools")
66 private List<ContrailSubnetPool> allocationPools = new ArrayList<>();
68 @JsonProperty("network_ipam_refs_data_ipam_subnets_host_routes")
69 private final ContrailSubnetHostRoutes host_routes = new ContrailSubnetHostRoutes();
71 public ContrailSubnet() {
75 public String getDefaultGateway() {
76 return defaultGateway;
79 public void setDefaultGateway(String defaultGateway) {
80 this.defaultGateway = defaultGateway;
83 public ContrailSubnetIp getSubnet() {
87 public void setSubnet(ContrailSubnetIp subnet) {
91 public Boolean isEnableDhcp() {
95 public void setEnableDhcp(Boolean enableDhcp) {
96 this.enableDhcp = enableDhcp;
99 public String getSubnetName() {
103 public void setSubnetName(String subnetName) {
104 this.subnetName = subnetName;
107 public List<ContrailSubnetPool> getAllocationPools() {
108 return allocationPools;
111 public void setPools(List<ContrailSubnetPool> allocationPools) {
112 this.allocationPools = allocationPools;
115 public Boolean isAddrFromStart() {
116 return addrFromStart;
119 public void setAddrFromStart(Boolean addrFromStart) {
120 this.addrFromStart = addrFromStart;
123 public JsonNode toJsonNode() {
124 JsonNode node = null;
126 ObjectMapper mapper = new ObjectMapper();
127 node = mapper.convertValue(this, JsonNode.class);
128 } catch (Exception e) {
129 logger.error("{} {} Error creating JsonNode for Contrail Subnet: {} ", MessageEnum.RA_MARSHING_ERROR,
130 ErrorCode.SchemaError.getValue(), subnetName, e);
136 public String toJsonString() {
137 String jsonString = null;
139 ObjectMapper mapper = new ObjectMapper();
140 jsonString = mapper.writeValueAsString(this);
141 } catch (Exception e) {
142 logger.error("{} {} Error creating JsonString for Contrail Subnet: {} ", MessageEnum.RA_MARSHING_ERROR,
143 ErrorCode.SchemaError.getValue(), subnetName, e);
149 // poulate contrail subnet with input(from bopel) subnet
150 public void populateWith(Subnet inputSubnet) {
151 if (inputSubnet != null) {
152 if (!msoCommonUtils.isNullOrEmpty(inputSubnet.getSubnetName()))
153 subnetName = inputSubnet.getSubnetName();
155 subnetName = inputSubnet.getSubnetId();
156 enableDhcp = inputSubnet.getEnableDHCP();
157 defaultGateway = inputSubnet.getGatewayIp();
158 if (!msoCommonUtils.isNullOrEmpty(inputSubnet.getCidr())) {
159 int idx = inputSubnet.getCidr().indexOf("/");
161 subnet.setIpPrefix(inputSubnet.getCidr().substring(0, idx));
162 subnet.setIpPrefixLen(inputSubnet.getCidr().substring(idx + 1));
165 if (inputSubnet.getAllocationPools() != null) {
166 for (Pool pool : inputSubnet.getAllocationPools()) {
167 if (!msoCommonUtils.isNullOrEmpty(pool.getStart())
168 && !msoCommonUtils.isNullOrEmpty(pool.getEnd())) {
169 ContrailSubnetPool csp = new ContrailSubnetPool();
170 csp.populateWith(pool);
171 allocationPools.add(csp);
175 if (inputSubnet.getHostRoutes() != null) {
176 List<ContrailSubnetHostRoute> hrList = host_routes.getHost_routes();
177 for (HostRoute hr : inputSubnet.getHostRoutes()) {
178 if (!msoCommonUtils.isNullOrEmpty(hr.getPrefix())
179 || !msoCommonUtils.isNullOrEmpty(hr.getNextHop())) {
180 ContrailSubnetHostRoute cshr = new ContrailSubnetHostRoute();
181 cshr.populateWith(hr);
190 public String toString() {
191 StringBuilder buf = new StringBuilder();
192 for (ContrailSubnetPool pool : allocationPools) {
193 buf.append(pool.toString());
195 return "ContrailSubnet [subnet=" + subnet.toString() + " default_gateway=" + defaultGateway + " enable_dhcp="
196 + enableDhcp + " addr_from_start=" + addrFromStart + " subnet_name=" + subnetName + " allocation_pools="