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.MessageEnum;
31 import org.onap.so.logger.MsoLogger;
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;
60 /** future - leave this commented
61 private String subnet_uuid;
62 private String dns_server_address;
63 private List<String> dns_nameservers;
64 private String dhcp_option_list;
67 @JsonProperty("network_ipam_refs_data_ipam_subnets_allocation_pools")
68 private List<ContrailSubnetPool> allocationPools = new ArrayList <> ();
70 @JsonProperty("network_ipam_refs_data_ipam_subnets_host_routes")
71 private final ContrailSubnetHostRoutes host_routes = new ContrailSubnetHostRoutes();
73 public ContrailSubnet() {
77 public String getDefaultGateway() {
78 return defaultGateway;
81 public void setDefaultGateway(String defaultGateway) {
82 this.defaultGateway = defaultGateway;
85 public ContrailSubnetIp getSubnet() {
89 public void setSubnet(ContrailSubnetIp subnet) {
93 public Boolean isEnableDhcp() {
97 public void setEnableDhcp(Boolean enableDhcp) {
98 this.enableDhcp = enableDhcp;
101 public String getSubnetName() {
105 public void setSubnetName(String subnetName) {
106 this.subnetName = subnetName;
109 public List<ContrailSubnetPool> getAllocationPools() {
110 return allocationPools;
113 public void setPools(List<ContrailSubnetPool> allocationPools) {
114 this.allocationPools = allocationPools;
117 public Boolean isAddrFromStart() {
118 return addrFromStart;
121 public void setAddrFromStart(Boolean addrFromStart) {
122 this.addrFromStart = addrFromStart;
125 public JsonNode toJsonNode()
127 JsonNode node = null;
130 ObjectMapper mapper = new ObjectMapper();
131 node = mapper.convertValue(this, JsonNode.class);
135 logger.error("{} {} Error creating JsonNode for Contrail Subnet: {} ", MessageEnum.RA_MARSHING_ERROR,
136 MsoLogger.ErrorCode.SchemaError.getValue(), subnetName, e);
142 public String toJsonString()
144 String jsonString = null;
147 ObjectMapper mapper = new ObjectMapper();
148 jsonString = mapper.writeValueAsString(this);
152 logger.error("{} {} Error creating JsonString for Contrail Subnet: {} ", MessageEnum.RA_MARSHING_ERROR,
153 MsoLogger.ErrorCode.SchemaError.getValue(), subnetName, e);
158 //poulate contrail subnet with input(from bopel) subnet
159 public void populateWith(Subnet inputSubnet)
161 if (inputSubnet != null)
163 if (!msoCommonUtils.isNullOrEmpty(inputSubnet.getSubnetName()))
164 subnetName = inputSubnet.getSubnetName();
166 subnetName = inputSubnet.getSubnetId();
167 enableDhcp = inputSubnet.getEnableDHCP();
168 defaultGateway = inputSubnet.getGatewayIp();
169 if (!msoCommonUtils.isNullOrEmpty(inputSubnet.getCidr()) )
171 int idx = inputSubnet.getCidr().indexOf("/");
174 subnet.setIpPrefix(inputSubnet.getCidr().substring(0, idx));
175 subnet.setIpPrefixLen(inputSubnet.getCidr().substring(idx+1));
178 if (inputSubnet.getAllocationPools() != null)
180 for (Pool pool : inputSubnet.getAllocationPools())
182 if ( !msoCommonUtils.isNullOrEmpty(pool.getStart()) && !msoCommonUtils.isNullOrEmpty(pool.getEnd()) )
184 ContrailSubnetPool csp = new ContrailSubnetPool();
185 csp.populateWith(pool);
186 allocationPools.add (csp);
190 if (inputSubnet.getHostRoutes() != null)
192 List<ContrailSubnetHostRoute> hrList = host_routes.getHost_routes();
193 for (HostRoute hr : inputSubnet.getHostRoutes())
195 if ( !msoCommonUtils.isNullOrEmpty(hr.getPrefix()) || !msoCommonUtils.isNullOrEmpty(hr.getNextHop()) )
197 ContrailSubnetHostRoute cshr = new ContrailSubnetHostRoute();
198 cshr.populateWith(hr);
207 public String toString() {
208 StringBuilder buf = new StringBuilder ();
209 for (ContrailSubnetPool pool : allocationPools)
211 buf.append(pool.toString());
213 return "ContrailSubnet [subnet=" + subnet.toString() + " default_gateway=" + defaultGateway
214 + " enable_dhcp=" + enableDhcp + " addr_from_start=" + addrFromStart + " subnet_name=" + subnetName + " allocation_pools=" + buf + " ]";