Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / main / java / org / onap / vid / model / aaiTree / Network.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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.vid.model.aaiTree;
22
23 import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
24 import static org.onap.vid.aai.util.AAITreeConverter.NETWORK_ROLE;
25 import static org.onap.vid.aai.util.AAITreeConverter.NETWORK_TYPE;
26 import static org.onap.vid.aai.util.AAITreeConverter.PHYSICAL_NETWORK_NAME;
27 import static org.onap.vid.aai.util.AAITreeConverter.SERVICE_INSTANCE;
28 import static org.onap.vid.aai.util.AAITreeConverter.SERVICE_INSTANCE_SERVICE_INSTANCE_ID;
29 import static org.onap.vid.aai.util.AAITreeConverter.SERVICE_INSTANCE_SERVICE_INSTANCE_NAME;
30 import static org.onap.vid.aai.util.AAITreeConverter.TENANT;
31 import static org.onap.vid.aai.util.AAITreeConverter.TENANT_TENANT_NAME;
32 import static org.onap.vid.aai.util.AAITreeConverter.VPN_BINDING;
33
34 import com.fasterxml.jackson.annotation.JsonInclude;
35 import org.apache.commons.collections.CollectionUtils;
36 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Relationship;
37 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.RelationshipList;
38 import org.onap.vid.aai.util.AAITreeNodeUtils;
39
40 @JsonInclude(NON_NULL)
41 public class Network extends Node {
42
43     private String role;
44     private String physicalName;
45     private String serviceName;
46     private String serviceUUID;
47     private String tenantName;
48     private Boolean isBoundToVpn;
49     private RouteTarget routeTarget;
50
51     public Network(){}
52
53     private Network(AAITreeNode node) {
54         super(node);
55         fillCloudConfigurationProperties(this, node.getCloudConfiguration());
56     }
57
58     public static Network from(AAITreeNode node) {
59         Network network = new Network(node);
60         network.setInstanceType(readValueAsStringFromAdditionalProperties(node, NETWORK_TYPE));
61         network.setRole(readValueAsStringFromAdditionalProperties(node, NETWORK_ROLE));
62         network.setPhysicalName(readValueAsStringFromAdditionalProperties(node, PHYSICAL_NETWORK_NAME));
63         RelationshipList relationshipList = node.getRelationshipList();
64         Relationship serviceInstanceRelationship = AAITreeNodeUtils.findFirstRelationshipByRelatedTo(relationshipList, SERVICE_INSTANCE).orElse(null);
65         if (serviceInstanceRelationship != null) {
66             network.setServiceName(AAITreeNodeUtils.findFirstValue(serviceInstanceRelationship.getRelatedToPropertyList(), SERVICE_INSTANCE_SERVICE_INSTANCE_NAME).orElse(null));
67             network.setServiceUUID(AAITreeNodeUtils.findFirstValue(serviceInstanceRelationship.getRelationDataList(), SERVICE_INSTANCE_SERVICE_INSTANCE_ID).orElse(null));
68         }
69         AAITreeNodeUtils.findFirstRelationshipByRelatedTo(relationshipList, TENANT).ifPresent(
70                 tenantRelationship -> network.setTenantName(AAITreeNodeUtils.findFirstValue(tenantRelationship.getRelatedToPropertyList(), TENANT_TENANT_NAME).orElse(null))
71         );
72         // We are ignoring "is-bound-to-vpn" parameter from additionalProperties because there is a requirement to define vpn binding presence from by related-to: vpn-binding
73         network.setBoundToVpn(AAITreeNodeUtils.findFirstRelationshipByRelatedTo(relationshipList, VPN_BINDING).isPresent());
74
75         //get the route target
76         node.getChildren().stream()
77                 .filter(x->x.getType()== NodeType.VPN_BINDING)                      // get all VPN_BINDING related to the network
78                 .map(x->VpnBindingKt.from(x))                                       // create VPN_BINDING nodes
79                 .filter(x-> CollectionUtils.isNotEmpty(x.getRouteTargets()))        // get the RouteTargets that are not empty
80                 .findFirst()                                                        // get the first one
81                 .ifPresent(x->network.setRouteTarget(x.getRouteTargets().get(0)));  // If there is a route target - add it to the network
82         return network;
83     }
84
85     public String getRole() {
86         return role;
87     }
88
89     public void setRole(String role) {
90         this.role = role;
91     }
92
93     public String getPhysicalName() {
94         return physicalName;
95     }
96
97     public void setPhysicalName(String physicalName) {
98         this.physicalName = physicalName;
99     }
100
101     public String getServiceName() {
102         return serviceName;
103     }
104
105     public void setServiceName(String serviceName) {
106         this.serviceName = serviceName;
107     }
108
109     public String getServiceUUID() {
110         return serviceUUID;
111     }
112
113     public void setServiceUUID(String serviceUUID) {
114         this.serviceUUID = serviceUUID;
115     }
116
117     public String getTenantName() {
118         return tenantName;
119     }
120
121     public void setTenantName(String tenantName) {
122         this.tenantName = tenantName;
123     }
124
125     public Boolean isBoundToVpn() {
126         return isBoundToVpn;
127     }
128
129     public void setBoundToVpn(Boolean boundToVpn) {
130         isBoundToVpn = boundToVpn;
131     }
132
133     public RouteTarget getRouteTarget() {
134         return routeTarget;
135     }
136
137     public void setRouteTarget(RouteTarget routeTarget) {
138         this.routeTarget = routeTarget;
139     }
140 }