Change the header to SO
[so.git] / adapters / mso-network-adapter / src / main / java / org / openecomp / mso / adapters / network / ContrailSubnet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.mso.adapters.network;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.codehaus.jackson.JsonNode;
27 import org.codehaus.jackson.annotate.JsonProperty;
28 import org.codehaus.jackson.map.ObjectMapper;
29
30 import org.openecomp.mso.logger.MessageEnum;
31 import org.openecomp.mso.logger.MsoLogger;
32 import org.openecomp.mso.openstack.beans.Pool;
33 import org.openecomp.mso.openstack.beans.Subnet;
34 import static org.openecomp.mso.openstack.utils.MsoCommonUtils.isNullOrEmpty;
35
36 public class ContrailSubnet {
37         private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
38
39         @JsonProperty("network_ipam_refs_data_ipam_subnets_subnet")
40         private ContrailSubnetIp subnet = new ContrailSubnetIp();
41         
42         @JsonProperty("network_ipam_refs_data_ipam_subnets_default_gateway")
43         private String default_gateway;
44         
45         @JsonProperty("network_ipam_refs_data_ipam_subnets_subnet_name")
46         private String subnet_name;
47         
48         @JsonProperty("network_ipam_refs_data_ipam_subnets_enable_dhcp")
49         private Boolean enable_dhcp;
50         
51         @JsonProperty("network_ipam_refs_data_ipam_subnets_addr_from_start")
52         private Boolean addr_from_start = true;
53         
54         /** future - leave this commented
55         private String subnet_uuid;
56         private String dns_server_address;
57         private List<String> dns_nameservers;
58         private String dhcp_option_list;
59         private String host_routes;
60         **/
61         
62         @JsonProperty("network_ipam_refs_data_ipam_subnets_allocation_pools")
63         private List<ContrailSubnetPool> allocation_pools =  new ArrayList <ContrailSubnetPool> ();
64
65         public ContrailSubnet() {
66                 super();
67         }
68
69         public String getDefault_gateway() {
70                 return default_gateway;
71         }
72
73         public void setDefault_gateway(String default_gateway) {
74                 this.default_gateway = default_gateway;
75         }
76
77         public ContrailSubnetIp getSubnet() {
78                 return subnet;
79         }
80
81         public void setSubnet(ContrailSubnetIp subnet) {
82                 this.subnet = subnet;
83         }
84
85         public Boolean isEnable_dhcp() {
86                 return enable_dhcp;
87         }
88
89         public void setEnable_dhcp(Boolean enable_dhcp) {
90                 this.enable_dhcp = enable_dhcp;
91         }
92
93         public String getSubnet_name() {
94                 return subnet_name;
95         }
96
97         public void setSubnet_name(String subnet_name) {
98                 this.subnet_name = subnet_name;
99         }
100
101         public List<ContrailSubnetPool> getAllocation_pools() {
102                 return allocation_pools;
103         }
104
105         public void setPools(List<ContrailSubnetPool> allocation_pools) {
106                 this.allocation_pools = allocation_pools;
107         }
108
109         public Boolean isAddr_from_start() {
110                 return addr_from_start;
111         }
112
113         public void setAddr_from_start(Boolean addr_from_start) {
114                 this.addr_from_start = addr_from_start;
115         }
116
117         public JsonNode toJsonNode()
118         {
119                 JsonNode node = null;
120                 try
121                 {
122                         ObjectMapper mapper = new ObjectMapper();
123                         node = mapper.convertValue(this, JsonNode.class);
124                 }
125                 catch (Exception e)
126                 {
127                         String error = "Error creating JsonNode for Contrail Subnet:" + subnet_name;
128                         LOGGER.error (MessageEnum.RA_MARSHING_ERROR, error, "", "", MsoLogger.ErrorCode.SchemaError, "Exception creating JsonNode for Contrail Subnet", e);
129                 }
130                 
131                 return node;
132         }
133         
134         public String toJsonString()
135         {
136                 String jsonString = null;
137                 try
138                 {
139                         ObjectMapper mapper = new ObjectMapper();
140                         jsonString = mapper.writeValueAsString(this);
141                 }
142                 catch (Exception e)
143                 {
144                         String error = "Error creating JsonString for Contrail Subnet:" + subnet_name;
145                         LOGGER.error (MessageEnum.RA_MARSHING_ERROR, error, "", "", MsoLogger.ErrorCode.SchemaError, "Exception creating JsonString for Contrail Subnet", e);
146                 }
147                 
148                 return jsonString;
149         }
150         //poulate contrail subnet with input(from bopel) subnet
151         public void populateWith(Subnet i_subnet)
152         {
153                 if (i_subnet != null)
154                 {
155                         if (!isNullOrEmpty(i_subnet.getSubnetName()))
156                                 subnet_name = i_subnet.getSubnetName();
157                         else
158                                 subnet_name = i_subnet.getSubnetId();
159                         enable_dhcp = i_subnet.getEnableDHCP();
160                         default_gateway = i_subnet.getGatewayIp();
161                         if (!isNullOrEmpty(i_subnet.getCidr()) )
162                         {
163                                 int idx = i_subnet.getCidr().indexOf("/");
164                                 if (idx != -1)
165                                 {
166                                         subnet.setIp_prefix(i_subnet.getCidr().substring(0, idx));
167                                         subnet.setIp_prefix_len(i_subnet.getCidr().substring(idx+1));
168                                 }
169                         }
170                         if (i_subnet.getAllocationPools() != null)
171                         {
172                                 for (Pool pool : i_subnet.getAllocationPools())
173                                 {
174                                         if ( !isNullOrEmpty(pool.getStart()) && !isNullOrEmpty(pool.getEnd()) )
175                                         {               
176                                                 ContrailSubnetPool csp = new ContrailSubnetPool();
177                                                 csp.populateWith(pool);
178                                                 allocation_pools.add (csp);
179                                         }
180                                 }
181                         }
182                 }
183         }
184
185         @Override
186         public String toString() {
187                 
188                 StringBuilder buf = new StringBuilder ();
189                 for (ContrailSubnetPool pool : allocation_pools)
190                 {
191                          buf.append(pool.toString());
192                 }
193                 return "ContrailSubnet [subnet=" + subnet.toString() + " default_gateway=" + default_gateway
194                                 + " enable_dhcp=" + enable_dhcp +  " addr_from_start=" + addr_from_start + " subnet_name=" + subnet_name + " allocation_pools=" + buf + " ]";
195         }
196 }