cancel
Showing results for 
Search instead for 
Did you mean: 

Trigger error alerts to clients based on payload

PavanKumar
Active Contributor
0 Kudos
951

Hi,

I am working in pi 7.4 dual stack having a requirement whenever there is a error in PI need to send error information to particular client. Client name will come in payload of message, So based on payload condition need to route alert emails to the particular clients.

Ex: If a message with client name field as 'X" has failed in PI with some error,this error message has to be routed to 'client X' mail ids similarly for other clients 'Y','Z' with customized body and subject.

Please suggest the approach for achieving this requirement.

Regards

Pavan

Accepted Solutions (1)

Accepted Solutions (1)

sugata_bagchi2
Active Contributor
0 Kudos

Hi Pavan,
You can use the below one -

package com.sap.pi.JSON2XMLConversion;
import org.json.JSONObject;
import org.json.XML;

import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
import java.io.InputStream;
import java.io.OutputStream;
public class JSON2XMLConversion extends AbstractTransformation
{
    public void transform(TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException 
    {
          InputStream in = arg0.getInputPayload().getInputStream();
          OutputStream out = arg1.getOutputPayload().getOutputStream();
          try 
          {
           byte[] b = new byte[in.available()];
           in.read(b);
           String JSONreq = new String(b);
           JSONreq = JSONreq.replace("&", "&");
           JSONreq = JSONreq.replace("<br>", "");           
           int contStart = JSONreq.indexOf("<Content>");
           int contEnd =  JSONreq.indexOf("</Content>");
           JSONreq = JSONreq.substring(contStart, contEnd).replace("<Content>", "");
           JSONObject obj = new JSONObject(JSONreq);
           //converting json to xml
            String xml_data = XML.toString(obj);
            xml_data = "<AlertSchema>".concat(xml_data).concat("</AlertSchema>");
            out.write(xml_data.getBytes());
          }
          catch (Exception e)
          {
              e.toString();
           }
     }
}

Considering an email input as XML as below -

This will be converted to XML like this - and you can map this to your target mail package structure to send email to specific client. I have used the UDS attribute ReprocessAccount here but you can change with the Client ID and in your next step mapping play around with that.

You have to add external jar in your build path / Maven - when building the above Java class in NWDS and also need to import in ESR as imported Archive.

https://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm

Hope this helps!

Thanks

Sugata

Answers (2)

Answers (2)

sugata_bagchi2
Active Contributor

Hello Pavan,
As you want to send the email Alert to specific email id everytime based on the client name in payload.

You can do the following - considering each client shares the same sender interface payload pattern

1. Create ICO1 - this is your actual scenario. Create an AlertRule for this interface. As soon as there is a failure in PI for this you can send the alert email to a specific email ID.e.g- abc@xyz.com.

You can also configure the UDS for this sender interface, so that you can include the client name in the alert email.

2. create ICO2- this will be a mail to mail scenario and you will send the email to the client as target receiver.

In this scenario your sender mail adapter will read the email ID where the alerts were sent form the first scenario. You also need a java mapping (as the email body is in JSON) to extract the UDS value- which is the client name. then in the 2nd step message mapping you can create a mail package structure. and in the 2nd step mapping you can either use Fix value or value mapping to get the email Id of the client.

You can convert the JSON from alert mail into a XML and then create a XSD from that XML. In your 2nd mapping you can use this XSD as your source and mail package as target.

Sample Alert Schema-

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="AlertSchema">
    <xs:complexType>
      <xs:sequence>
        <xs:element type="xs:string" name="AdapterType"/>
        <xs:element type="xs:string" name="Component"/>
        <xs:element type="xs:string" name="ErrCat"/>
        <xs:element type="xs:string" name="ErrCode"/>
        <xs:element type="xs:short" name="ErrLabel"/>
        <xs:element type="xs:string" name="ErrText"/>
        <xs:element type="xs:string" name="FromParty"/>
        <xs:element type="xs:string" name="FromService"/>
        <xs:element type="xs:string" name="Interface"/>
        <xs:element type="xs:anyURI" name="MonitoringUrl"/>
        <xs:element type="xs:string" name="MsgId"/>
        <xs:element type="xs:string" name="Namespace"/>
        <xs:element type="xs:string" name="RuleId"/>
        <xs:element type="xs:string" name="ScenarioId"/>
        <xs:element type="xs:string" name="ScenarioName"/>
        <xs:element type="xs:string" name="Severity"/>
        <xs:element type="xs:dateTime" name="Timestamp"/>
        <xs:element type="xs:string" name="ToParty"/>
        <xs:element type="xs:string" name="ToService"/>
        <xs:element name="UDSAttrs">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:int" name="ReprocessAccount"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

JSON to XML converter -

https://www.freeformatter.com/json-to-xml-converter.html

XML to XSD converter -

https://www.freeformatter.com/xsd-generator.html

Thanks

Sugata

PavanKumar
Active Contributor
0 Kudos

Hi Sugata,

Thank you very much for your reply

As i understand In the first scenario i need to configure internal mail id and uds for client name,so that in second scenario(mail to mail)mail sender adapter will read the alerts from internal mail id and will sent to client mail id using mail receiver.

Thanks for sharing the sample schema for source structure as mail package is deprecated can i go ahead and create mapping using mail package as target structure? please suggest

Could you please help in share the java mapping for to extract the value from uds

Regards

Pavan

PavanKumar
Active Contributor
0 Kudos

Hi Sugata,

Thank you very much for your efforts and sharing the code, I will try as you suggested and let you know the outcome.

Regards

Pavan

sugata_bagchi2
Active Contributor
0 Kudos

Sure, and in the code use this line -

Not sure why it is changed to below in the code section

JSONreq = JSONreq.replace("&", "&");