org.onosproject.north.aaa.api.parser.impl.ApplicationParser.java Source code

Java tutorial

Introduction

Here is the source code for org.onosproject.north.aaa.api.parser.impl.ApplicationParser.java

Source

/*
 * Copyright 2014-2016 Open Networking Laboratory
 *
 * 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.
 */

package org.onosproject.north.aaa.api.parser.impl;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.onosproject.north.aaa.api.JsonParserService;
import org.onosproject.north.aaa.api.ParseException;
import org.onosproject.north.aaa.store.Application;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;

/**
 * Parse and build application class object from applications.json file.
 */
public class ApplicationParser implements JsonParserService<Application> {
    @Override
    public Set<Application> parseJson(InputStream stream) throws IOException, ParseException {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonNode = mapper.readTree(stream);
        JsonParser jp = jsonNode.traverse();
        Set<Application> applicationSet = new HashSet<>();

        // continue parsing the token till the end of input is reached
        while (!jp.isClosed()) {
            // get the token
            JsonToken token = jp.nextToken();
            // if its the last token then we are done
            if (token == null) {
                break;
            }

            if (JsonToken.FIELD_NAME.equals(token) && "applications".equals(jp.getCurrentName())) {
                token = jp.nextToken();
                if (!JsonToken.START_ARRAY.equals(token)) {
                    // bail out
                    throw new ParseException("expected ARRAY after applications");
                }

                while (true) {
                    token = jp.nextToken();
                    if (JsonToken.END_ARRAY.equals(token)) {
                        // bail out
                        break;
                    }

                    if (JsonToken.START_OBJECT.equals(token)) {
                        Application app = jsonToApplication(jp);
                        applicationSet.add(app);
                    }
                }
            }
        }
        return applicationSet;
    }

    public Application jsonToApplication(JsonParser jp) throws ParseException, IOException {
        Application.Builder builder = Application.builder();

        while (true) {
            JsonToken token = jp.nextToken();
            if (JsonToken.END_OBJECT.equals(token)) {
                // bail out
                break;
            }

            if (JsonToken.FIELD_NAME.equals(token) && "appId".equals(jp.getCurrentName())) {
                jp.nextToken();
                builder.buildAppId(jp.getText());
            } else if (JsonToken.FIELD_NAME.equals(token) && "appDesc".equals(jp.getCurrentName())) {
                jp.nextToken();
                builder.buildAppDesc(jp.getText());
            } else if (JsonToken.FIELD_NAME.equals(token) && "appRoles".equals(jp.getCurrentName())) {
                jp.nextToken();
                String roles = jp.getText();

                if ("admin".equals(roles) || "normal".equals(roles) || "security".equals(roles)) {
                    builder.buildAppRoles(roles);
                } else {
                    // bail out
                    throw new ParseException("appRoles must be set to \"admin\", \"normal\" or \"security\"");
                }

            } else if (JsonToken.FIELD_NAME.equals(token) && "devId".equals(jp.getCurrentName())) {
                jp.nextToken();
                builder.buildDevId(jp.getText());
            } else if (JsonToken.FIELD_NAME.equals(token) && "clientId".equals(jp.getCurrentName())) {
                jp.nextToken();
                builder.buildClientId(jp.getText());
            }
        }
        return builder.buildAll();
    }
}