summaryrefslogtreecommitdiffstats
path: root/src/usr/targeting/common/xmltohb/handle_duplicate.pl
blob: e41cea3c2298fee8cd18c85edd811f2b6b62982a (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/usr/targeting/common/xmltohb/handle_duplicate.pl $
#
# OpenPOWER HostBoot Project
#
# Contributors Listed Below - COPYRIGHT 2017
# [+] International Business Machines Corp.
#
#
# 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.
#
# IBM_PROLOG_END_TAG

#
#
# Usage:
#
#    handle_duplicate --ekbXmlFile=attribute_types_ekb.xml --hbXmlFile=attribute_types_src.xml
#                     --fapi2Header=attribute_service.H --outFile=attribute_types_full.xml
#
# Purpose:
#
#    This perl script merges together attribute_types_ekb and attribute_types_src into one output
#    file that can be consumed by either the FIPS build or the op-build process. The trick is that
#    we don't want the EKB attributes to override fapi-mapped attributes we have already defined in
#    hostboot's xml.
#

use strict;

use Getopt::Long;
use XML::Simple;
use Text::Wrap;
use Data::Dumper;
use POSIX;
use Env;
use XML::LibXML;
use File::Temp qw(tempfile);

require "fapi_utils.pl";

$XML::Simple::PREFERRED_PARSER = 'XML::Parser';


my $ekbXmlFullPath = "";
my $hbXmlFullPath    = "";
my $fapi2HeaderFullPath    = "";
my $outFullPath    = "";
my $usage = 0;


GetOptions("ekbXmlFile:s"  => \$ekbXmlFullPath,
           "hbXmlFile:s"   => \$hbXmlFullPath,
           "fapi2Header:s" => \$fapi2HeaderFullPath,
           "outFile:s"     => \$outFullPath,
           "help"          => \$usage);


if( ($ekbXmlFullPath eq "")
   || ($hbXmlFullPath eq "")
   || ($fapi2HeaderFullPath eq "")
   || ($outFullPath eq "") )
{
    display_help();
    exit 1;
}
elsif ($usage)
{
    display_help();
    exit 0;
}


my %completeAttr;

#attributes defined in attribute_types.xml w/ fapi2 mapping
my %hwpfAttributes;


#use the XML::Simple tool to convert the xml files into hashmaps
my $xml = new XML::Simple (KeyAttr=>[]);
use Digest::MD5 qw(md5_hex);


#Read in EKB attribute xml (fapiattrs.xml)
my $allEKBAttributes = $xml->XMLin($ekbXmlFullPath ,
    forcearray => ['attribute','hwpfToHbAttrMap','enumerationType','enumerator']);

#Read in HB attribute xml (attribute_types.xml)
my $allHBAttributes = $xml->XMLin($hbXmlFullPath ,
    forcearray => ['attribute','hwpfToHbAttrMap','enumerationType','enumerator']);


#Get a list of all the function backed fapi2 attributes
my @funcBackedAttr = getFuncionBackedAttrs($fapi2HeaderFullPath);

#Create a list of all the HB attributes with a HWPF mapping
foreach my $attribute (@{$allHBAttributes->{attribute}})
{
    if (exists $attribute->{hwpfToHbAttrMap} )
    {
        push (@{$hwpfAttributes{attribute}}, $attribute);
    }
}


#Looping variable
my $matchFound = 0;

#Loop over all of the EKB attributes and look for a duplcate in HB list
foreach my $ekbAttr (@{$allEKBAttributes->{attribute}})
{
    my $isFuncBacked = 0;
    $matchFound = 0;
    my $theHbAttr;
    my $ekbAttrId = $ekbAttr->{hwpfToHbAttrMap}[0]->{id};

    #check if this matches any in our list of func backed attrs
    foreach my $id (@funcBackedAttr)
    {
        if ($id eq $ekbAttrId)
        {
            $isFuncBacked = 1;
            last;
        }
    }

    #If it is a function backed attribute , no need to add it
    #we want the function to overrule anything else.
    if($isFuncBacked)
    {
        print "SKIPPING EKB $ekbAttrId - function backed\n";
        next;
    }

    #Loop over HB attrs until we find a match
    foreach my $hbAttr (@{$hwpfAttributes{attribute}})
    {
        my $hbFapiId  = $hbAttr->{hwpfToHbAttrMap}[0]->{id};

        if($ekbAttrId eq $hbFapiId)
        {
            $matchFound = 1;
            $theHbAttr = $hbAttr;
            last;
        }
    }

    #if no match was found we will assume this is a new attribute and add it
    if(!$matchFound)
    {
        push (@{$completeAttr{attribute}}, $ekbAttr);
    }
    #otherwise we need to check what was updated and handle it accordingly
    else
    {
        #Special case big hammer will ignore the generated version and
        # always choose what HB coded up
        my $ignoreEkb = 0;
        if( exists $theHbAttr->{ignoreEkb} )
        {
            $ignoreEkb;
        }

        #if the EKB's description was updated we will assume their description
        # to be more correct so just update the HB attr's description
        my $ekbDesc = $ekbAttr->{description};
        my $hbDesc  = $theHbAttr->{description};
        if($ekbDesc ne $hbDesc)
        {
            $theHbAttr->{description} = $ekbDesc;
        }

        #if persistancy has changed we want to notify the developer so cause a fail
        my $ekbPersist = $ekbAttr->{persistancy};
        my $hbPersist = $theHbAttr->{persistancy};
        if($ekbPersist ne $hbPersist)
        {
            die "ERROR Hostboot says persistancy of ".$ekbAttrId." is ".$hbPersist." and Fapi says it is ".$ekbPersist."\n";
        }

        #if array dimmensions have changed we want to notify the developer so cause a fail
        my $hbArrayDimmensions = getArrayDimmensions(%$theHbAttr);
        my $ekbArrayDimmensions = getArrayDimmensions(%$ekbAttr);
        if($hbArrayDimmensions ne $ekbArrayDimmensions)
        {
            die "ERROR Hostboot says array dimmensions of ".$ekbAttrId." is ".$hbArrayDimmensions." and Fapi says it is ".$ekbArrayDimmensions."\n";
        }

        #if attribute type has changed we want to notify the developer so cause a fail
        my $hbAttrType = getAttrType(%$theHbAttr);
        my $ekbAttrType = getAttrType(%$ekbAttr);
        if($hbAttrType ne $ekbAttrType)
        {
            die "ERROR Hostboot says type of ".$ekbAttrId." is ".$hbAttrType." and Fapi says it is ".$ekbAttrType."\n";
        }
    }
}

#also need to add all of the EKB enumerations
foreach my $ekbEnum (@{$allEKBAttributes->{enumerationType}})
{
    $matchFound = 0;
    my $ekbEnumId = $ekbEnum->{id};
    my $theHbEnum;
    #we dont want to add duplicates so check if the enumeration exists already in HB
    foreach my $hbEnum (@{$allHBAttributes->{enumerationType}})
    {
        my $hbEnumId  = $hbEnum->{id};

        if($ekbEnumId eq $hbEnumId)
        {
            $matchFound = 1;
            $theHbEnum = $hbEnum;
            last;
        }
    }

    #if not found already, then add the enum
    if(!$matchFound)
    {
        push (@{$completeAttr{enumerationType}}, $ekbEnum);
    }
    #if a copy already exists in HB then we just want to update the values and description
    else
    {
        $theHbEnum->{description} = $ekbEnum->{description};
        $theHbEnum->{enumerator} = $ekbEnum->{enumerator};
    }
}

#add all HB attributes to completeAttr (the hash holding output xml)
foreach my $hbAttr (@{$allHBAttributes->{attribute}})
{
    push (@{$completeAttr{attribute}}, $hbAttr);
}

#also add the HB enums to completeAttr hash
foreach my $hbEnum (@{$allHBAttributes->{enumerationType}})
{
    push (@{$completeAttr{enumerationType}}, $hbEnum);
}


#To make things look nicer we add a newline after each attribute or enumerationType
my $completeXml = $xml->XMLout(\%completeAttr, RootName => 'attributes', NoAttr => 1 );
my $complete_fh_temp = new File::Temp( UNLINK => 1 );

print $complete_fh_temp $completeXml;

seek $complete_fh_temp, 0, 0 or die "Seek $complete_fh_temp failed: $!\n";

open(my $complete_fh, '>', $outFullPath) || die;

foreach my $row (<$complete_fh_temp>)
{
    chomp $row;
    print $complete_fh  $row."\n";
    if(index($row, "</enumerationType>") != -1 ||
       index($row, "</attribute>") != -1)
    {
        print $complete_fh "\n";
    }
}

close $complete_fh;



sub display_help
{
    use File::Basename;
    my $scriptname = basename($0);
    print STDERR "
Description:

    This perl script merges together attribute_types_ekb and attribute_types_src into one output
    file that can be consumed by either the FIPS build or the op-build process. The trick is that
    we don't want the EKB attributes to override fapi-mapped attributes we have already defined in
    hostboot's xml.

Usage:

    $scriptname --help

    $scriptname --ekbXmlFile=generatedEkbAttrXml
              generatedEkbAttrXml is complete pathname of the attribute_types_ekb.xml file
              that is generated by create_ekb_targattr.pl

    $scriptname --hbXmlFile=srcAttrXml
            srcAttrXml is complete pathname of the attribute_types_src.xml, or file that
            consists of all the relevent srcs cat'ed together

    $scriptname --fapi2Header=attrServiceHeader
            attrServiceHeader is complete pathname of the attribute_service.H file from hostboot

    $scriptname --outFile=completeAttrXml
            completeAttrXml is the complete pathname of the attribute_types_full.xml , or file
            that will be consumed by downstream repo (FIPS or op-build)
\n";
}
OpenPOWER on IntegriCloud