Update sdc-tosca version to 1.4.7
[ccsdk/sli/northbound.git] / ueb-listener / src / main / java / org / onap / ccsdk / sli / northbound / uebclient / SdncNodeModel.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.sli.northbound.uebclient;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.Map;
28
29 import org.onap.sdc.tosca.parser.api.ISdcCsarHelper;
30 import org.onap.sdc.tosca.parser.impl.SdcPropertyNames;
31 import org.onap.sdc.toscaparser.api.NodeTemplate;
32 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class SdncNodeModel extends SdncBaseModel {
37         
38         private static final Logger LOG = LoggerFactory
39                         .getLogger(SdncNodeModel.class);
40         
41         private String serviceUUID = null;
42         private String ecompGeneratedNaming = null;
43         private String [] bindingUuids = null; 
44         
45         // Using ASDC TOSCA Parser 17.07
46         public SdncNodeModel(ISdcCsarHelper sdcCsarHelper, NodeTemplate nodeTemplate, DBResourceManager jdbcDataSource) {
47                 
48                 super(sdcCsarHelper, nodeTemplate, jdbcDataSource);
49
50                 // extract inpuecompGeneratedNamingts
51                 String ecompGeneratedNaming = extractBooleanInputDefaultValue(SdcPropertyNames.PROPERTY_NAME_SERVICENAMING_DEFAULT_ECOMPGENERATEDNAMING);
52                 addParameter("ecomp_generated_naming",ecompGeneratedNaming);
53                 addParameter("naming_policy", extractInputDefaultValue(SdcPropertyNames.PROPERTY_NAME_SERVICENAMING_DEFAULT_NAMINGPOLICY));
54                 
55                 // extract properties
56                 addParameter("network_type", extractValue (nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKTYPE));
57                 addParameter("network_role", extractValue (nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKROLE));
58                 addParameter("network_scope", extractValue (nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKSCOPE));
59                 addParameter("network_technology", extractValue (nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKTECHNOLOGY));
60
61                 // extract properties - network_assignments
62                 addParameter("is_shared_network", extractBooleanValue (nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKASSIGNMENTS_ISSHAREDNETWORK));
63                 addParameter("is_external_network", extractBooleanValue (nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKASSIGNMENTS_ISEXTERNALNETWORK));
64                 String trunkNetworkIndicator = extractBooleanValue(nodeTemplate, "network_assignments#is_trunked");
65                 addParameter("trunk_network_indicator", trunkNetworkIndicator);
66
67                 // extract properties - network_assignments - ipv4_subnet_default_assignment
68                 String useIpv4 = extractBooleanValue(nodeTemplate, "network_assignments#ipv4_subnet_default_assignment#use_ipv4");
69                 addParameter("use_ipv4", useIpv4);
70                 addParameter("ipv4_dhcp_enabled", extractBooleanValue(nodeTemplate, "network_assignments#ipv4_subnet_default_assignment#dhcp_enabled"));
71                 if (useIpv4.contains("Y")) {
72                         addParameter("ipv4_ip_version", "ipv4");
73                 }
74                 addParameter("ipv4_cidr_mask", extractValue(nodeTemplate, "network_assignments#ipv4_subnet_default_assignment#cidr_mask"));
75                 addParameter("eipam_v4_address_plan", extractValue(nodeTemplate, "network_assignments#ipv4_subnet_default_assignment#ip_network_address_plan"));
76                 
77                 // extract properties - network_assignments - ipv6_subnet_default_assignment
78                 String useIpv6 = extractBooleanValue(nodeTemplate, "network_assignments#ipv6_subnet_default_assignment#use_ipv6");
79                 addParameter("use_ipv6", useIpv6);
80                 addParameter("ipv6_dhcp_enabled", extractBooleanValue(nodeTemplate, "network_assignments#ipv6_subnet_default_assignment#dhcp_enabled"));
81                 if (useIpv6.contains("Y")) {
82                         addParameter("ipv6_ip_version", "ipv6");
83                 }
84                 addParameter("ipv6_cidr_mask", extractValue(nodeTemplate, "network_assignments#ipv6_subnet_default_assignment#cidr_mask"));
85                 addParameter("eipam_v6_address_plan", extractValue(nodeTemplate, "network_assignments#ipv6_subnet_default_assignment#ip_network_address_plan"));
86                 
87                 // extract properties - provider_network
88                 addParameter("is_provider_network", extractBooleanValue (nodeTemplate, SdcPropertyNames.PROPERTY_NAME_PROVIDERNETWORK_ISPROVIDERNETWORK));
89                 addParameter("physical_network_name", extractValue(nodeTemplate, SdcPropertyNames.PROPERTY_NAME_PROVIDERNETWORK_PHYSICALNETWORKNAME));
90
91                 // extract properties - network_flows
92                 addParameter("is_bound_to_vpn", extractBooleanValue (nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKFLOWS_ISBOUNDTOVPN));
93
94                 // extract properties - network_flows - vpn_bindings
95                 String vpnBindingString = extractValue (nodeTemplate, SdcPropertyNames.PROPERTY_NAME_NETWORKFLOWS_VPNBINDING);
96                 bindingUuids = vpnBindingString.split(",");
97
98 }
99
100         public String getServiceUUID() {
101                 return serviceUUID;
102         }
103         public void setServiceUUID(String serviceUUID) {
104                 this.serviceUUID = serviceUUID;
105         }
106
107         public String getEcompGeneratedNaming() {
108                 return ecompGeneratedNaming;
109         }
110         public void setEcompGeneratedNaming(String ecompGeneratedNaming) {
111                 this.ecompGeneratedNaming = ecompGeneratedNaming;
112                 if (ecompGeneratedNaming != null && !ecompGeneratedNaming.isEmpty()) {
113                         params.put("ecomp_generated_naming", "\"" + ecompGeneratedNaming + "\"");                       
114                 }
115         }
116
117         public void insertNetworkModelData () throws IOException {
118                 try {
119                         // Clean up NETWORK_MODEL data for this customization_uuid and service_uuid? 
120                         cleanUpExistingToscaData("NETWORK_MODEL", "customization_uuid", getCustomizationUUID());
121                         cleanUpExistingToscaData("VPN_BINDINGS", "network_customization_uuid", getCustomizationUUID());
122                         LOG.info("Call insertToscaData for NETWORK_MODEL customizationUUID = " + getCustomizationUUID());
123                         insertToscaData(getSql(model_yaml), null);
124                         insertToscaData(getVpnBindingsSql(), null);
125                 } catch (IOException e) {
126                         LOG.error("Could not insert Tosca CSAR data into the NETWORK_MODEL table");
127                         throw new IOException (e);
128                 }
129         }
130
131         public void insertRelatedNetworkRoleData () throws IOException {
132                 
133                 Object propertyValue = sdcCsarHelper.getNodeTemplatePropertyValueAsObject(nodeTemplate, "network_assignments#related_networks");
134                 ArrayList<Map<String, String>> relatedNetworkList = (ArrayList)propertyValue;
135
136                 String networkModelCustomizationUUID = getCustomizationUUID();
137                 
138                 if (relatedNetworkList != null) {
139                 
140                         try {
141                                 cleanUpExistingToscaData("RELATED_NETWORK_ROLE", "network_model_customization_uuid", networkModelCustomizationUUID);
142                         } catch (IOException e) {
143                                 LOG.error("Could not clean up Tosca CSAR data in the RELATED_NETWORK_ROLE table");
144                                 throw new IOException (e);
145                         }
146
147                         for (Map<String, String> relatedNetworkValue : relatedNetworkList) {
148                                 LOG.debug("Node Template [" + nodeTemplate.getName() + "], property [" + "related_network_role" + "] property value: " + relatedNetworkValue.get("related_network_role"));
149                 
150                                 String relatedNetworkRoleValue = relatedNetworkValue.get("related_network_role");
151                                 
152                                 try {
153                                         // Table cleanup RELATED_NETWORK_ROLE occurs per network
154                                         // If related_network_role for this service already exist in RELATED_NETWORK_ROLE, don't attempt insertion
155                                         Map<String, String> relatedNetworkRoleParamsCheck = new HashMap<String, String>();
156                                         addParameter("related_network_role", relatedNetworkRoleValue, relatedNetworkRoleParamsCheck);
157                                         addParameter("network_model_customization_uuid", networkModelCustomizationUUID, relatedNetworkRoleParamsCheck);
158                                         if (checkForExistingToscaData("RELATED_NETWORK_ROLE", relatedNetworkRoleParamsCheck) == false) { 
159                                                 relatedNetworkRoleParamsCheck.remove("related_network_role");
160                                         LOG.info("Call insertToscaData for RELATED_NETWORK_ROLE where network_model_customization_uuid = " + networkModelCustomizationUUID);
161                                         insertToscaData(buildSql("RELATED_NETWORK_ROLE", "related_network_role", "\"" + relatedNetworkRoleValue + "\"", model_yaml, relatedNetworkRoleParamsCheck), null);
162                                         }
163                                 } catch (IOException e) {
164                                         LOG.debug("Could not insert Tosca CSAR data into the RELATED_NETWORK_ROLE table");
165                                         throw new IOException (e);
166                                 }
167                         }       
168                 }       
169                         else {
170                         LOG.debug("Node Template [" + nodeTemplate.getName() + "], property [" + "related_networks" + "] property value: " + null);
171                 }
172                 
173         }
174         
175         public String getSql(String model_yaml) {
176                 
177                 StringBuilder sb = new StringBuilder();
178                 sb.append("INSERT into NETWORK_MODEL (service_uuid, customization_uuid, model_yaml, ");
179                 
180                 int paramCount = 0;
181                 for (String paramKey :  params.keySet()) {
182                         paramCount++;
183                     sb.append(paramKey);
184                     if (paramCount < params.size()) sb.append(", ");
185                 }
186                 
187                 sb.append(") values (" + serviceUUID + ", " + getCustomizationUUID() + ", \"" + model_yaml + "\", ");
188
189                 paramCount = 0;
190                 for (String paramKey :  params.keySet()) {
191                         paramCount++;
192                         String paramValue = params.get(paramKey);
193                     sb.append(paramValue);
194                     if (paramCount < params.size()) sb.append(", ");
195                 }
196
197                 sb.append(");");
198                 return sb.toString();
199         }
200
201         public String getVpnBindingsSql() {
202                 
203                 StringBuilder sb = new StringBuilder();
204                 for (int i=0; i < bindingUuids.length; i++) {
205                         sb.append("INSERT into VPN_BINDINGS (network_customization_uuid, binding_uuid) values (" + getCustomizationUUID() + ", \"" + bindingUuids[i] + "\"); ");
206                 }
207
208                 return sb.toString();
209         }
210         
211 }