ec523b87d9d05abcee7a9f65d36b96e8b976a916
[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 defaultGateway;
44         
45         @JsonProperty("network_ipam_refs_data_ipam_subnets_subnet_name")
46         private String subnetName;
47         
48         @JsonProperty("network_ipam_refs_data_ipam_subnets_enable_dhcp")
49         private Boolean enableDhcp;
50         
51         @JsonProperty("network_ipam_refs_data_ipam_subnets_addr_from_start")
52         private Boolean addrFromStart = 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> allocationPools =  new ArrayList <> ();
64
65         public ContrailSubnet() {
66                 super();
67         }
68
69         public String getDefaultGateway() {
70                 return defaultGateway;
71         }
72
73         public void setDefaultGateway(String defaultGateway) {
74                 this.defaultGateway = defaultGateway;
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 isEnableDhcp() {
86                 return enableDhcp;
87         }
88
89         public void setEnableDhcp(Boolean enableDhcp) {
90                 this.enableDhcp = enableDhcp;
91         }
92
93         public String getSubnetName() {
94                 return subnetName;
95         }
96
97         public void setSubnetName(String subnetName) {
98                 this.subnetName = subnetName;
99         }
100
101         public List<ContrailSubnetPool> getAllocationPools() {
102                 return allocationPools;
103         }
104
105         public void setPools(List<ContrailSubnetPool> allocationPools) {
106                 this.allocationPools = allocationPools;
107         }
108
109         public Boolean isAddrFromStart() {
110                 return addrFromStart;
111         }
112
113         public void setAddrFromStart(Boolean addrFromStart) {
114                 this.addrFromStart = addrFromStart;
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:" + subnetName;
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:" + subnetName;
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 inputSubnet)
152         {
153                 if (inputSubnet != null)
154                 {
155                         if (!isNullOrEmpty(inputSubnet.getSubnetName()))
156                                 subnetName = inputSubnet.getSubnetName();
157                         else
158                                 subnetName = inputSubnet.getSubnetId();
159                         enableDhcp = inputSubnet.getEnableDHCP();
160                         defaultGateway = inputSubnet.getGatewayIp();
161                         if (!isNullOrEmpty(inputSubnet.getCidr()) )
162                         {
163                                 int idx = inputSubnet.getCidr().indexOf("/");
164                                 if (idx != -1)
165                                 {
166                                         subnet.setIpPrefix(inputSubnet.getCidr().substring(0, idx));
167                                         subnet.setIpPrefixLen(inputSubnet.getCidr().substring(idx+1));
168                                 }
169                         }
170                         if (inputSubnet.getAllocationPools() != null)
171                         {
172                                 for (Pool pool : inputSubnet.getAllocationPools())
173                                 {
174                                         if ( !isNullOrEmpty(pool.getStart()) && !isNullOrEmpty(pool.getEnd()) )
175                                         {               
176                                                 ContrailSubnetPool csp = new ContrailSubnetPool();
177                                                 csp.populateWith(pool);
178                                                 allocationPools.add (csp);
179                                         }
180                                 }
181                         }
182                 }
183         }
184
185         @Override
186         public String toString() {
187                 
188                 StringBuilder buf = new StringBuilder ();
189                 for (ContrailSubnetPool pool : allocationPools)
190                 {
191                          buf.append(pool.toString());
192                 }
193                 return "ContrailSubnet [subnet=" + subnet.toString() + " default_gateway=" + defaultGateway
194                                 + " enable_dhcp=" + enableDhcp +  " addr_from_start=" + addrFromStart + " subnet_name=" + subnetName + " allocation_pools=" + buf + " ]";
195         }
196 }