AT&T 1712 and 1802 release code
[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 static org.openecomp.mso.openstack.utils.MsoCommonUtils.isNullOrEmpty;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.openecomp.mso.logger.MessageEnum;
29 import org.openecomp.mso.logger.MsoLogger;
30 import org.openecomp.mso.openstack.beans.HostRoute;
31 import org.openecomp.mso.openstack.beans.Pool;
32 import org.openecomp.mso.openstack.beans.Subnet;
33
34 import com.fasterxml.jackson.annotation.JsonProperty;
35 import com.fasterxml.jackson.databind.JsonNode;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37
38 public class ContrailSubnet {
39         private static MsoLogger logger = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA);
40
41         @JsonProperty("network_ipam_refs_data_ipam_subnets_subnet")
42         private ContrailSubnetIp subnet = new ContrailSubnetIp();
43         
44         @JsonProperty("network_ipam_refs_data_ipam_subnets_default_gateway")
45         private String defaultGateway;
46         
47         @JsonProperty("network_ipam_refs_data_ipam_subnets_subnet_name")
48         private String subnetName;
49         
50         @JsonProperty("network_ipam_refs_data_ipam_subnets_enable_dhcp")
51         private Boolean enableDhcp;
52         
53         @JsonProperty("network_ipam_refs_data_ipam_subnets_addr_from_start")
54         private Boolean addrFromStart = true;   
55         /** future - leave this commented
56         private String subnet_uuid;
57         private String dns_server_address;
58         private List<String> dns_nameservers;
59         private String dhcp_option_list;
60         **/
61         
62         @JsonProperty("network_ipam_refs_data_ipam_subnets_allocation_pools")
63         private List<ContrailSubnetPool> allocationPools =  new ArrayList <> ();
64
65         @JsonProperty("network_ipam_refs_data_ipam_subnets_host_routes")
66         private ContrailSubnetHostRoutes host_routes = new ContrailSubnetHostRoutes();
67         
68         public ContrailSubnet() {
69                 super();
70         }
71
72         public String getDefaultGateway() {
73                 return defaultGateway;
74         }
75
76         public void setDefaultGateway(String defaultGateway) {
77                 this.defaultGateway = defaultGateway;
78         }
79
80         public ContrailSubnetIp getSubnet() {
81                 return subnet;
82         }
83
84         public void setSubnet(ContrailSubnetIp subnet) {
85                 this.subnet = subnet;
86         }
87
88         public Boolean isEnableDhcp() {
89                 return enableDhcp;
90         }
91
92         public void setEnableDhcp(Boolean enableDhcp) {
93                 this.enableDhcp = enableDhcp;
94         }
95
96         public String getSubnetName() {
97                 return subnetName;
98         }
99
100         public void setSubnetName(String subnetName) {
101                 this.subnetName = subnetName;
102         }
103
104         public List<ContrailSubnetPool> getAllocationPools() {
105                 return allocationPools;
106         }
107
108         public void setPools(List<ContrailSubnetPool> allocationPools) {
109                 this.allocationPools = allocationPools;
110         }
111
112         public Boolean isAddrFromStart() {
113                 return addrFromStart;
114         }
115
116         public void setAddrFromStart(Boolean addrFromStart) {
117                 this.addrFromStart = addrFromStart;
118         }
119
120         public JsonNode toJsonNode()
121         {
122                 JsonNode node = null;
123                 try
124                 {
125                         ObjectMapper mapper = new ObjectMapper();
126                         node = mapper.convertValue(this, JsonNode.class);
127                 }
128                 catch (Exception e)
129                 {
130                         String error = "Error creating JsonNode for Contrail Subnet:" + subnetName;
131                         logger.error (MessageEnum.RA_MARSHING_ERROR, error, "", "", MsoLogger.ErrorCode.SchemaError, "Exception creating JsonNode for Contrail Subnet", e);
132                 }
133                 
134                 return node;
135         }
136         
137         public String toJsonString()
138         {
139                 String jsonString = null;
140                 try
141                 {
142                         ObjectMapper mapper = new ObjectMapper();
143                         jsonString = mapper.writeValueAsString(this);
144                 }
145                 catch (Exception e)
146                 {
147                         String error = "Error creating JsonString for Contrail Subnet:" + subnetName;
148                         logger.error (MessageEnum.RA_MARSHING_ERROR, error, "", "", MsoLogger.ErrorCode.SchemaError, "Exception creating JsonString for Contrail Subnet", e);
149                 }
150                 
151                 return jsonString;
152         }
153         //poulate contrail subnet with input(from bopel) subnet
154         public void populateWith(Subnet inputSubnet)
155         {
156                 if (inputSubnet != null)
157                 {
158                         if (!isNullOrEmpty(inputSubnet.getSubnetName()))
159                                 subnetName = inputSubnet.getSubnetName();
160                         else
161                                 subnetName = inputSubnet.getSubnetId();
162                         enableDhcp = inputSubnet.getEnableDHCP();
163                         defaultGateway = inputSubnet.getGatewayIp();
164                         if (!isNullOrEmpty(inputSubnet.getCidr()) )
165                         {
166                                 int idx = inputSubnet.getCidr().indexOf("/");
167                                 if (idx != -1)
168                                 {
169                                         subnet.setIpPrefix(inputSubnet.getCidr().substring(0, idx));
170                                         subnet.setIpPrefixLen(inputSubnet.getCidr().substring(idx+1));
171                                 }
172                         }
173                         if (inputSubnet.getAllocationPools() != null)
174                         {
175                                 for (Pool pool : inputSubnet.getAllocationPools())
176                                 {
177                                         if ( !isNullOrEmpty(pool.getStart()) && !isNullOrEmpty(pool.getEnd()) )
178                                         {               
179                                                 ContrailSubnetPool csp = new ContrailSubnetPool();
180                                                 csp.populateWith(pool);
181                                                 allocationPools.add (csp);
182                                         }
183                                 }
184                         }
185                         if (inputSubnet.getHostRoutes() != null)
186                         {
187                                 List<ContrailSubnetHostRoute> hrList = host_routes.getHost_routes();
188                                 for (HostRoute hr : inputSubnet.getHostRoutes())
189                                 {
190                                         if ( !isNullOrEmpty(hr.getPrefix()) || !isNullOrEmpty(hr.getNextHop()) )
191                                         {               
192                                                 ContrailSubnetHostRoute cshr = new ContrailSubnetHostRoute();
193                                                 cshr.populateWith(hr);
194                                                 hrList.add (cshr);
195                                         }
196                                 }
197                         }
198                 }
199         }
200
201         @Override
202         public String toString() {
203                 StringBuilder buf = new StringBuilder ();
204                 for (ContrailSubnetPool pool : allocationPools)
205                 {
206                          buf.append(pool.toString());
207                 }
208                 return "ContrailSubnet [subnet=" + subnet.toString() + " default_gateway=" + defaultGateway
209                                 + " enable_dhcp=" + enableDhcp +  " addr_from_start=" + addrFromStart + " subnet_name=" + subnetName + " allocation_pools=" + buf + " ]";
210         }
211
212 }