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