Grounded in the Cloud
cancel
Showing results for 
Search instead for 
Did you mean: 

Using Java in JavaScript in HP Operations Orchestration scriptlets

Frisendal

In this blog post, we will jump into more technical details than you might be accustomed to seeing in this blog. Today, we will explore the richness of scriptlets enabling advanced users of HP Operation Orchestration 10 (HP OO) IT process automation software to fully use all of the functionalities that come with Java in order to create advanced operations. It has been written for the advanced OO user.

 

 

Creating custom operations in HP OO

HP Operations Orchestration already comes with many thousands operations out of the box and even more can be found on the OO community for free - perfectly suited for automating your Day 2 Operations. But, before we jump into the technical details, let’s first take a look at the options available to create new operations.

 

1) Copying existing operations

The simplest example is when you have an application that provides a Command Line Interface (CLI) and you have an operation that needs a complex command with several parameters. For example, the Docker content pack from the OO community has an operation called “Create detached container” with the command:

sudo docker run --name ${containerName} -d ${imageID} ${cmdParams}

This operation is in fact a copy of “SSH Command” with certain inputs fixed and the three additional inputs you see above added.

 

2) Using the REST Wizard

The REST is just one of many wizards that allow you to create hundreds of operations in a few mins. Simple load a Swagger or WADL definition and select the methods that you need, and the wizards will create the operations for you. For example, if you use the Artifactory artifact repository, you can find the system’s REST definition using the link: http://server:port/artifactory/api/application.wadl.

 

The following illustration shows an example of how simple it is to create operations from e.g. the Jira REST API: js1.png

 

3) Using the SOAP or PowerShell wizards

As with the REST Wizard, HP OO 10 has wizards for creating operations directly from WDSL definitions or by loading PowerShell modules. All the wizards are simple next-next-next tools that only take a minute or two to run.

 

  

Creating JavaScript operations

HP OO comes with so many out-of-the-box operations that there are very few things that cannot be easily achieved. In addition, HP OO allows you to create scriptlets based on JavaScript which can be simpler than creating a flow to perform the same functionality.

 

Imagine that we want to remove duplicates values from a list. I usually start by creating a copy of the “Do Nothing” operation (located here: /Base [1.4.3]/Library/Utility Operations/Flow Variable Manipulation/Do Nothing), give it a proper name, delete the four default inputs, and give it a proper description. Let’s say that we add a new input called “list”. We then just need the following code:

var dupArray = list.split(",");
var uniqueArray = [];
for(var i=0; i<dupArray.length; i++) {
  if(uniqueArray.indexOf(dupArray[i]) == -1) uniqueArray.push(dupArray[i]);
}
scriptletResponse = "success";
scriptletResult = uniqueArray.join(",");

Which splits the list (assuming the delimiter is comma) into an array, and then we iterate over the list, keeping only values that we haven’t seen before. This is a simple way to add a bit more functionality.

 

 

Creating scriptlets using native Java objects

OO uses Rhino as a JavaScript engine and the current version of HP OO 10.20.0001 comes with Rhino 1.7 r3. You can easily find the jar in which in the default location is here:

C:\Program Files\Hewlett-Packard\Operations Orchestration 10\studio\lib\js-1.7R3.jar

If you want to test JavaScript outside HP OO Studio, all you need to do is to run the jar like:

java -jar js-1.7R3.jar

And to learn more about the JavaScript in Rhino, the reference is found here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference

 

MD5SUM

So let’s jump into it. The first example shows how to calculate the MD5 sum of a string (i.e. any flow variable). The example also shows that some Java types are not very easily handled in Rhino, so we will need a helper function to convert a string to a bytes array. Next we use standard Java classes to create the MD5 sum as shown in the code below:

 

js2.png

 

The md5 function uses the MessageDigest classes to create the functionality. Also note that we can create new java objects with the “new” keyword.

 

AES Encrypt

Let’s try a bit more advanced example to test the options. In the following we will create two operations to encrypt and decrypt a string using the AES encryption algorithm.

To encrypt we will create a scriptlet and set one input called “clearText”. In this example we will let the scriptlet create a random string used as the key to encrypt and decrypt – you could also make this an input if you like. We need two helper functions this time; as before a string-to-bytes-array and also a byte-array-to-hex-string. The resulting scriptlet code is:

 

js3.png

 

As you can see from the code, I like to make a comment on the lines that create java objects to remember their type as everything in JavaScript gets declared as var. Lines 27-30 create the key, and we set up the cipher in lines 31-33. We encrypt the input (clearText) in line 34. The result is the encrypted text, and we also create a flow variable with the AES key, which is needed to decrypt the text (in line 36).

Disclaimer: You should verify the code with your company’s security experts to ensure that it follows any guidelines and requirements that you might have, before you use this in a business solution. The code has been created to highlight the use of Java in in JavaScript, and not as secure encryption solution.

 

AES Decrypt

This script requires two inputs; the encrypted text (called encryptedString) and the key (called AESkey). We use similar techniques as above and the code looks like the following:

 

js4.png

 

The key is created lines 19-20, and we set up the cipher in lines 21-22. The decryption is done in line 24.

 

As mentioned above, this example is not meant to provide you with a secure solution, but the point is to show how we can use classes from Java within JavaScript. There are certain data types that need to be converted using helper functions, so I hope that you can reuse those parts, and quickly get started.

 

You can find all the code in the attached text file.

 

 

You can try Operations Orchestration Community Edition for yourself today (using Docker: find OO CE on Dockerhub).

 

If you have any questions or comments, you can leave them in box below or discuss this topic on the OO Practitioner Forum. And for more HP OO guides and resources, visit the community for more information.

 

To know more about HP OO in general, please see our product page.

 

Finally, visit us at HP Discover, June 2-4, in Las Vegas.

HP Software OO RnD, Community Assistance Team, https://hpln.hp.com/group/operations-orchestration
  • operational intelligence
About the Author

Frisendal

Cloud and Automation solution architect in HP Software Research &amp; Development.

Events
28-30 November
Madrid, Spain
Discover 2017 Madrid
Join us for Hewlett Packard Enterprise Discover 2017 Madrid, taking place 28-30 November at the Feria de Madrid Convention Center
Read more
See posts for dates
Online
HPE Webinars - 2017
Find out about this year's live broadcasts and on-demand webinars.
Read more
View all