aboutsummaryrefslogtreecommitdiffstats
path: root/adapters/mso-openstack-adapters/src/main/java/org/onap/so/adapters/audit/HeatStackAudit.java
blob: 5c0d2d3019606835c0bf756edd94a007018f3137 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.adapters.audit;

import java.net.URI;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.onap.aai.domain.yang.LInterface;
import org.onap.aai.domain.yang.LInterfaces;
import org.onap.aai.domain.yang.Vlan;
import org.onap.aai.domain.yang.Vlans;
import org.onap.aai.domain.yang.Vserver;
import org.onap.so.openstack.utils.MsoHeatUtils;
import org.onap.so.openstack.utils.MsoNeutronUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.woorea.openstack.heat.model.Link;
import com.woorea.openstack.heat.model.Resource;
import com.woorea.openstack.heat.model.Resources;
import com.woorea.openstack.heat.model.Stack;
import com.woorea.openstack.quantum.model.Port;

@Component
public class HeatStackAudit {

    private static final String RESOURCES = "/resources";

    protected static final Logger logger = LoggerFactory.getLogger(HeatStackAudit.class);

    @Autowired
    protected MsoHeatUtils heat;

    @Autowired
    protected MsoNeutronUtils neutron;

    @Autowired
    protected AuditVServer auditVservers;

    public Optional<AAIObjectAuditList> auditHeatStack(String cloudRegion, String cloudOwner, String tenantId,
            String heatStackName) {
        try {
            logger.debug("Fetching Top Level Stack Information");
            Resources resources = heat.queryStackResources(cloudRegion, tenantId, heatStackName, 3);
            List<Resource> novaResources = resources.getList().stream()
                    .filter(p -> "OS::Nova::Server".equals(p.getType())).collect(Collectors.toList());
            List<Resource> resourceGroups = resources.getList().stream()
                    .filter(p -> "OS::Heat::ResourceGroup".equals(p.getType()) && p.getName().contains("subinterfaces"))
                    .collect(Collectors.toList());
            List<Optional<Port>> neutronPortDetails = retrieveNeutronPortDetails(resources, cloudRegion, tenantId);
            if (novaResources.isEmpty())
                return Optional.of(new AAIObjectAuditList());
            else {
                Set<Vserver> vserversToAudit = createVserverSet(resources, novaResources, neutronPortDetails);
                Set<Vserver> vserversWithSubInterfaces =
                        processSubInterfaces(cloudRegion, tenantId, resourceGroups, vserversToAudit);
                return auditVservers.auditVservers(vserversWithSubInterfaces, tenantId, cloudOwner, cloudRegion);
            }
        } catch (Exception e) {
            logger.error("Error during auditing stack resources", e);
            return Optional.empty();
        }
    }

    protected Set<Vserver> processSubInterfaces(String cloudRegion, String tenantId, List<Resource> resourceGroups,
            Set<Vserver> vServersToAudit) throws Exception {
        for (Resource resourceGroup : resourceGroups) {
            processResourceGroups(cloudRegion, tenantId, vServersToAudit, resourceGroup);
        }
        return vServersToAudit;
    }

    protected void processResourceGroups(String cloudRegion, String tenantId, Set<Vserver> vServersWithLInterface,
            Resource resourceGroup) throws Exception {
        Optional<Link> stackLink =
                resourceGroup.getLinks().stream().filter(link -> "nested".equals(link.getRel())).findAny();
        if (stackLink.isPresent()) {
            try {
                Optional<String> path = extractResourcePathFromHref(stackLink.get().getHref());
                if (path.isPresent()) {
                    logger.debug("Fetching nested Resource Stack Information");
                    Resources nestedResourceGroupResources =
                            heat.executeHeatClientRequest(path.get(), cloudRegion, tenantId, Resources.class);
                    processNestedResourceGroup(cloudRegion, tenantId, vServersWithLInterface,
                            nestedResourceGroupResources);
                } else
                    throw new Exception("Error finding Path from Self Link");
            } catch (Exception e) {
                logger.error("Error Parsing Link to obtain Path", e);
                throw new Exception("Error finding Path from Self Link");
            }
        }
    }

    protected void processNestedResourceGroup(String cloudRegion, String tenantId, Set<Vserver> vServersWithLInterface,
            Resources nestedResourceGroupResources) throws Exception {
        for (Resource resourceGroupNested : nestedResourceGroupResources) {
            Optional<Link> subInterfaceStackLink =
                    resourceGroupNested.getLinks().stream().filter(link -> "nested".equals(link.getRel())).findAny();
            if (subInterfaceStackLink.isPresent()) {
                addSubInterface(cloudRegion, tenantId, vServersWithLInterface, subInterfaceStackLink.get());
            }
        }
    }

    protected void addSubInterface(String cloudRegion, String tenantId, Set<Vserver> vServersWithLInterface,
            Link subInterfaceStackLink) throws Exception {
        Optional<String> resourcePath = extractResourcePathFromHref(subInterfaceStackLink.getHref());
        Optional<String> stackPath = extractStackPathFromHref(subInterfaceStackLink.getHref());
        if (resourcePath.isPresent() && stackPath.isPresent()) {
            logger.debug("Fetching nested Sub-Interface Stack Information");
            Stack subinterfaceStack =
                    heat.executeHeatClientRequest(stackPath.get(), cloudRegion, tenantId, Stack.class);
            Resources subinterfaceResources =
                    heat.executeHeatClientRequest(resourcePath.get(), cloudRegion, tenantId, Resources.class);
            if (subinterfaceStack != null) {
                addSubInterfaceToVserver(vServersWithLInterface, subinterfaceStack, subinterfaceResources);
            }
        } else
            throw new Exception("Error finding Path from Self Link");

    }

    protected void addSubInterfaceToVserver(Set<Vserver> vServersWithLInterface, Stack subinterfaceStack,
            Resources subinterfaceResources) throws Exception {
        String parentNeutronPortId = (String) subinterfaceStack.getParameters().get("port_interface");
        logger.debug("Parent neutron Port: {} on SubInterface: {}", parentNeutronPortId, subinterfaceStack.getId());
        for (Vserver auditVserver : vServersWithLInterface)
            for (LInterface lInterface : auditVserver.getLInterfaces().getLInterface())

                if (parentNeutronPortId.equals(lInterface.getInterfaceId())) {
                    logger.debug("Found Parent Port on VServer: {} on Port: {}", auditVserver.getVserverId(),
                            lInterface.getInterfaceId());
                    Resource contrailVm = subinterfaceResources.getList().stream()
                            .filter(resource -> "OS::ContrailV2::VirtualMachineInterface".equals(resource.getType()))
                            .findAny().orElse(null);
                    if (contrailVm == null) {
                        throw new Exception("Cannnot find Contrail Virtual Machine Interface on Stack: "
                                + subinterfaceStack.getId());
                    }
                    LInterface subInterface = new LInterface();
                    subInterface.setInterfaceId(contrailVm.getPhysicalResourceId());
                    subInterface.setIsPortMirrored(false);
                    subInterface.setInMaint(false);
                    subInterface.setIsIpUnnumbered(false);
                    String macAddr = (String) subinterfaceStack.getParameters().get("mac_address");
                    subInterface.setMacaddr(macAddr);

                    String namePrefix = (String) subinterfaceStack.getParameters().get("subinterface_name_prefix");
                    Integer vlanIndex = Integer.parseInt((String) subinterfaceStack.getParameters().get("counter"));
                    String vlanTagList = (String) subinterfaceStack.getParameters().get("vlan_tag");
                    List<String> subInterfaceVlanTagList = Arrays.asList(vlanTagList.split(","));
                    subInterface.setInterfaceName(namePrefix + "_" + subInterfaceVlanTagList.get(vlanIndex));
                    subInterface.setVlans(new Vlans());
                    Vlan vlan = new Vlan();
                    vlan.setInMaint(false);
                    vlan.setIsIpUnnumbered(false);
                    vlan.setVlanIdInner(Long.parseLong(subInterfaceVlanTagList.get(vlanIndex)));
                    vlan.setVlanInterface(namePrefix + "_" + subInterfaceVlanTagList.get(vlanIndex));
                    subInterface.getVlans().getVlan().add(vlan);
                    if (lInterface.getLInterfaces() == null)
                        lInterface.setLInterfaces(new LInterfaces());

                    lInterface.getLInterfaces().getLInterface().add(subInterface);
                } else
                    logger.debug("Did Not Find Parent Port on VServer: {} Parent Port: SubInterface: {}",
                            auditVserver.getVserverId(), lInterface.getInterfaceId(), subinterfaceStack.getId());
    }

    protected Set<Vserver> createVserverSet(Resources resources, List<Resource> novaResources,
            List<Optional<Port>> neutronPortDetails) {
        Set<Vserver> vserversToAudit = new HashSet<>();
        for (Resource novaResource : novaResources) {
            Vserver auditVserver = new Vserver();
            auditVserver.setLInterfaces(new LInterfaces());
            auditVserver.setVserverId(novaResource.getPhysicalResourceId());
            Stream<Port> filteredNeutronPorts = filterNeutronPorts(novaResource, neutronPortDetails);
            filteredNeutronPorts.forEach(port -> {
                LInterface lInterface = new LInterface();
                lInterface.setInterfaceId(port.getId());
                lInterface.setInterfaceName(port.getName());
                auditVserver.getLInterfaces().getLInterface().add(lInterface);
            });
            vserversToAudit.add(auditVserver);
        }
        return vserversToAudit;
    }

    /**
     * @param novaResource Single openstack resource that is of type Nova
     * @param neutronPorts List of Neutron ports created within the stack
     * @return Filtered list of neutron ports taht relate to the nova server in openstack
     */
    protected Stream<Port> filterNeutronPorts(Resource novaResource, List<Optional<Port>> neutronPorts) {
        List<Port> filteredNeutronPorts =
                neutronPorts.stream().filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
        return filteredNeutronPorts.stream()
                .filter(port -> port.getDeviceId().equalsIgnoreCase(novaResource.getPhysicalResourceId()));
    }

    /**
     * @param resources Resource stream created by the stack in openstack
     * @param cloudSiteId Unique site id to identify which openstack we talk to
     * @param tenantId The tenant within the cloud we are talking to where resouces exist
     * @return List of optional neutron ports found within the cloud site and tenant
     */
    protected List<Optional<Port>> retrieveNeutronPortDetails(Resources resources, String cloudSiteId,
            String tenantId) {
        return resources.getList().parallelStream().filter(resource -> "OS::Neutron::Port".equals(resource.getType()))
                .map(resource -> neutron.getNeutronPort(resource.getPhysicalResourceId(), tenantId, cloudSiteId))
                .collect(Collectors.toList());

    }

    protected Optional<String> extractResourcePathFromHref(String href) {
        try {
            Optional<String> stackPath = extractStackPathFromHref(href);
            if (stackPath.isPresent()) {
                return Optional.of(stackPath.get() + RESOURCES);
            } else
                return Optional.empty();
        } catch (Exception e) {
            logger.error("Error parsing URI", e);
        }
        return Optional.empty();
    }

    protected Optional<String> extractStackPathFromHref(String href) {
        try {
            URI uri = new URI(href);
            Pattern p = Pattern.compile("/stacks.*");
            Matcher m = p.matcher(uri.getPath());
            if (m.find()) {
                return Optional.of(m.group());
            } else
                return Optional.empty();
        } catch (Exception e) {
            logger.error("Error parsing URI", e);
        }
        return Optional.empty();
    }


}