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