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