Working gateway

Change-Id: I8ca690fe9d1b7f8e20b438df1ddd48d6b2f99326
diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 0000000..93f1361
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,2 @@
+node_modules
+npm-debug.log
diff --git a/.eslintrc.txt b/.eslintrc.txt
new file mode 100644
index 0000000..3d48345
--- /dev/null
+++ b/.eslintrc.txt
@@ -0,0 +1,35 @@
+{
+    "ecmaFeatures": {
+        "blockBindings": true,
+        "forOf": true,
+        "destructuring": true,
+        "arrowFunctions": true,
+        "templateStrings": true,
+        "generators": true
+    },
+    "env": {
+        "node": true,
+        "es6": true
+    },
+    "rules": {
+        "quotes": [2, "single"],
+        "no-undef": [2],
+        "object-curly-spacing": [2, "never"],
+        "no-unused-vars": [2, {"vars": "all", "args": "after-used"}],
+        "space-before-blocks": [2, {"keywords": "always", "functions":"always"}],
+        "brace-style": [2, "stroustrup", { "allowSingleLine": true }],
+        "no-param-reassign": [2, {"props": false}],
+        "max-len": [2, 120, 4],
+        "eqeqeq": [1, "smart"],
+        "new-cap": [2, {"capIsNewExceptions": ["Router"]}],
+        "func-names": 0,
+        "object-shorthand": [0, "never"],
+        "wrap-iife": [2, "any"],
+        "no-loop-func": 0,
+        "no-console": 0,
+        "padded-blocks": 0
+    },
+    "globals" :{
+        "require": true
+    }
+}
\ No newline at end of file
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..97656ff
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+node_modules/
+src/config/*.yml
+src/config/*.yaml
+npm-debug.log
diff --git a/.gitreview b/.gitreview
new file mode 100644
index 0000000..c88e515
--- /dev/null
+++ b/.gitreview
@@ -0,0 +1,6 @@
+[gerrit]
+host=gerrit.opencord.org
+port=29418
+project=xos-rest-gw.git
+defaultremote=origin
+defaultbranch=master
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..a4f1d03
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,24 @@
+# To build use: docker build -t xosproject/xos-nb-rest .
+# To run use: docker run -p 3000:3000 -d xosproject/xos-nb-rest
+
+FROM node:argon
+
+# Set environment variables
+ENV CODE_SOURCE .
+ENV CODE_DEST /var/www
+
+# Create app directory
+WORKDIR ${CODE_DEST}
+
+# Install app dependencies
+COPY ${CODE_SOURCE}/package.json ${CODE_DEST}
+RUN npm install --production
+
+# Bundle app source
+COPY ${CODE_SOURCE}/src ${CODE_DEST}/src
+
+# Create a folder for logs
+RUN mkdir ${CODE_DEST}/logs
+
+EXPOSE 3000
+CMD [ "npm", "start" ]
\ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..818bca7
--- /dev/null
+++ b/README.md
@@ -0,0 +1,58 @@
+# XOS Northbound Rest Interface
+
+This is an abstraction layer that provide REST APIs and WebSockets access to XOS.
+
+## Development
+
+This software can be executed on your local machine as long as NodeJs is installed.
+
+To start it use: `npm install && npm start`
+
+Alternatively you can invoke the start script `node src/server.js` directly to pass addictional paramenters:
+
+- `--config mycfg.yml` to specify a different config file
+- `LOG_LEVEL=error|warn|info|debug` to enable different logging level (default is `warn`)
+
+If you are actively working on this project we suggest you to take a look to [nodemon](https://nodemon.io/) to observe changes in you code and automatically restart the server.
+
+### Config
+
+By default the used config is:
+```
+default:
+  xos:
+    host: xos
+    port: 9999
+  redis:
+    host: redis
+    port: 6379
+  gateway:
+    port: 3000
+```
+
+You can create a file called `config.yml` and replace one or more value.
+You can also have different config files and load them using the `--config` flag (see above for usage).
+
+### Tests
+
+A comprehensive test suite defined using [Mocha](https://mochajs.org/), [Chai](http://chaijs.com/) and [Sinon](http://sinonjs.org/) is provided togheter with the project. To execute existing tests use `npm test`, while if you are currently working on it you can execute them in watch mode using `npm run test:dev`.
+
+Styleguide are also applied using [EsLint](http://eslint.org/), to check style execute `npm run lint`.
+
+## Notes
+
+### Sample request to core authenticated method:
+
+```
+curl -H "x-csrftoken: TOKEN" -H "cookie: xoscsrftoken=TOKEN;xossessionid=SESSION_ID" 127.0.0.1:3000/api/core/instances/
+```
+
+### Websocket
+
+Socket.io client library will be available at: `http://localhost:3000/socket.io/socket.io.js`
+
+Remember that `redis` in the Frontend config is linked
+
+### Test Client
+
+To start the client: `cd spec; browser-sync start --server`
\ No newline at end of file
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..52d1ccf
--- /dev/null
+++ b/package.json
@@ -0,0 +1,37 @@
+{
+  "name": "xos_nb_rest",
+  "version": "1.0.0",
+  "description": "Northbound REST and WebSocket interfaces for XOS",
+  "main": "src/server.js",
+  "scripts": {
+    "start": "node src/server.js",
+    "test": "mocha spec/**/*.spec.js",
+    "test:dev": "mocha -w spec/**/*.spec.js",
+    "lint": "eslint ."
+  },
+  "author": "Open Networking Laboratory",
+  "license": "Apache-2.0",
+  "dependencies": {
+    "body-parser": "^1.15.2",
+    "cors": "^2.8.1",
+    "express": "^4.14.0",
+    "lodash": "^4.17.2",
+    "node-yaml-config": "0.0.4",
+    "redis": "^2.6.3",
+    "socket.io": "^1.7.1",
+    "superagent": "^3.1.0",
+    "winston": "^2.3.0",
+    "yargs": "^6.4.0"
+  },
+  "devDependencies": {
+    "chai": "^3.5.0",
+    "eslint": "^3.11.1",
+    "eslint-plugin-import": "^2.2.0",
+    "fakeredis": "^1.0.3",
+    "mocha": "^3.2.0",
+    "mockery": "^2.0.0",
+    "sinon": "^1.17.6",
+    "sinon-chai": "^2.8.0",
+    "supertest": "^2.0.1"
+  }
+}
diff --git a/spec/.eslintrc b/spec/.eslintrc
new file mode 100644
index 0000000..30a6ba1
--- /dev/null
+++ b/spec/.eslintrc
@@ -0,0 +1,10 @@
+{
+    "globals" :{
+        "describe": true,
+        "it": true,
+        "before": true,
+        "beforeEach": true,
+        "after": true,
+        "afterEach": true
+    }
+}
\ No newline at end of file
diff --git a/spec/core_proxy.spec.js b/spec/core_proxy.spec.js
new file mode 100644
index 0000000..c5bb9ad
--- /dev/null
+++ b/spec/core_proxy.spec.js
@@ -0,0 +1,183 @@
+(function () {
+  'use strict';
+  
+  const chai = require('chai');
+  const expect = chai.expect;
+  const sinonChai = require('sinon-chai');
+  const supertest = require('supertest');
+  const mockery = require('mockery');
+  chai.use(sinonChai);
+
+  const request = require('superagent');
+  const stub = require('./spec_helper.js');
+
+  const configMock = {
+    xos: {
+      host: 'http://test-xos-url',
+      port: '80'
+    }
+  };
+
+  let app;
+
+  describe('The core proxy routes', () => {
+
+    // stub for GET method
+    stub.makeStub('getSuccess', request, 'get', cb => {
+        cb(null, {
+        status: 200, 
+        body: {msg: 'successfully proxied'}
+      });
+    });
+
+    // stub for POST method
+    stub.makeStub('postSuccess', request, 'post', cb => {
+        cb(null, {
+        status: 201, 
+        body: {msg: 'successfully proxied'}
+      });
+    });
+
+    // stub for PUT method
+    stub.makeStub('putSuccess', request, 'put', cb => {
+        cb(null, {
+        status: 200, 
+        body: {msg: 'successfully proxied'}
+      });
+    });
+
+    // stub for DELETE method
+    stub.makeStub('deleteSuccess', request, 'delete', cb => {
+        cb(null, {
+        status: 204
+      });
+    });
+
+    // mocking the config.rest module
+    before(() => {
+      mockery.enable({
+        warnOnReplace: true,
+        warnOnUnregistered: false
+      });
+      mockery.registerMock('../config/config.js', configMock);
+
+      app = require('../src/server.js').app;
+    });
+
+    after(() => {
+      mockery.deregisterMock('../config/config.js');
+      mockery.disable();
+    });
+
+    it('should read XOS address from config.rest module', (done) => {
+      const myStub = stub.getStub('getSuccess');
+
+      supertest(app)
+      .get('/api/core/')
+      .end((err) => {
+        if (err) return done(err);
+        expect(myStub.get.called).to.be.true;
+        expect(myStub.get).to.have.been.calledWith('http://test-xos-url:80/api/core/');
+        done();
+      });
+    });
+
+    it('should pass token and cookies along with the request', (done) => {
+      const myStub = stub.getStub('getSuccess');
+
+      supertest(app)
+      .get('/api/core/')
+      .set('Accept', 'application/json')
+      .set('x-csrftoken', 'testToken')
+      .set('cookie', 'testCookie')
+      .end(function(err) {
+        if (err) return done(err);
+        expect(myStub.set.getCall(0)).to.have.been.calledWith('x-csrftoken', 'testToken');
+        expect(myStub.set.getCall(1)).to.have.been.calledWith('cookie', 'testCookie');
+        done();
+      });
+    });
+
+    it('should pass query paramenters along with the request', (done) => {
+      const myStub = stub.getStub('getSuccess');
+
+      supertest(app)
+      .get('/api/core/instances/?no_hyperlink=1&node=1')
+      .end((err) => {
+        if (err) return done(err);
+        expect(myStub.get.called).to.be.true;
+        expect(myStub.get).to.have.been.calledWith('http://test-xos-url:80/api/core/instances/?no_hyperlink=1&node=1');
+        done();
+      });
+    });
+
+    it('should proxy GET request to XOS', (done) => {
+      supertest(app)
+      .get('/api/core/')
+      .set('Accept', 'application/json')
+      .set('x-csrftoken', 'testToken')
+      .set('cookie', 'testCookie')
+      .end(function(err, res) {
+        if (err) return done(err);
+        expect(res.status).to.equal(200);
+        expect(res.body).to.deep.equal({msg: 'successfully proxied'});
+        done();
+      });
+    });
+
+    it('should proxy POST request to XOS', (done) => {
+
+      const myStub = stub.getStub('postSuccess');
+
+      supertest(app)
+      .post('/api/core/')
+      .send({foo: 'bar'})
+      .set('Accept', 'application/json')
+      .set('x-csrftoken', 'testToken')
+      .set('cookie', 'testCookie')
+      .end(function(err, res) {
+        if (err) return done(err);
+        expect(myStub.send.getCall(0)).to.have.been.calledWith({foo: 'bar'});
+        expect(res.status).to.equal(201);
+        expect(res.body).to.deep.equal({msg: 'successfully proxied'});
+        done();
+      });
+    });
+
+    it('should proxy PUT request to XOS', (done) => {
+
+      const myStub = stub.getStub('putSuccess');
+
+      supertest(app)
+      .put('/api/core/')
+      .send({foo: 'bar'})
+      .set('Accept', 'application/json')
+      .set('x-csrftoken', 'testToken')
+      .set('cookie', 'testCookie')
+      .end(function(err, res) {
+        if (err) return done(err);
+        expect(myStub.send.getCall(0)).to.have.been.calledWith({foo: 'bar'});
+        expect(res.status).to.equal(200);
+        expect(res.body).to.deep.equal({msg: 'successfully proxied'});
+        done();
+      });
+    });
+
+    it('should proxy DELETE request to XOS', (done) => {
+
+      const myStub = stub.getStub('deleteSuccess');
+
+      supertest(app)
+      .delete('/api/core/')
+      .set('Accept', 'application/json')
+      .set('x-csrftoken', 'testToken')
+      .set('cookie', 'testCookie')
+      .end(function(err, res) {
+        if (err) return done(err);
+        expect(myStub.send).not.to.have.been.called;
+        expect(res.status).to.equal(204);
+        done();
+      });
+    });
+  });
+})();
\ No newline at end of file
diff --git a/spec/index.html b/spec/index.html
new file mode 100644
index 0000000..4f97749
--- /dev/null
+++ b/spec/index.html
@@ -0,0 +1,60 @@
+<!doctype html>
+<html>
+  <head>
+    <title>Socket.IO chat</title>
+    <script src="http://10.1.8.44:3000/socket.io/socket.io.js"></script>
+    <script
+      src="https://code.jquery.com/jquery-1.12.4.min.js"
+      integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ="
+      crossorigin="anonymous"></script>
+    <script src="https://rawgit.com/notifyjs/notifyjs/master/dist/notify.js"></script>
+
+    <style>
+      body {
+        max-width: 100%;
+      }
+    </style>
+<script>
+  /* global io $ alert*/
+  function enableSocket(auth) {
+
+    auth.user = JSON.parse(auth.user);
+
+    var socket = io.connect('http://10.1.8.44:3000', {
+      query: 'name=' + auth.user.username + '&token=' + auth.xoscsrftoken + '&sessionId=' + auth.xossessionid + '&id=' + auth.user.id
+    });
+    socket.on('event', function(data) {
+      var p = $('body').append('<p></p>');
+      p.append('<b>' + data.model + '</b><br/>');
+      p.append('<i>' + JSON.stringify(data.msg.changed_fields) + '</i>');
+      p.append('<pre>' + JSON.stringify(data.msg.object) + '</pre>');
+      console.log(data.object);
+      $.notify(data.model + ': ' + JSON.stringify(data.msg.object.name) + 'updated', 'success');
+    });
+  }
+
+  function login() {
+    var username = $('#username').val();
+    var password = $('#password').val();
+
+    $.get('http://10.1.8.44:3000/api/utility/login?username='+username+'&password='+password,
+      function(res) {
+        enableSocket(res);
+        $('#login').hide();
+      }
+    )
+    .fail(function(e) {
+      alert( 'error' );
+      console.log(e);
+    });
+  }
+</script>
+  </head>
+  <body>
+    <form onsubmit="login()" id="login">
+      <input type="text" id="username">
+      <input type="password" id="password">
+      <input type="button" value="Login" onclick="login()">
+    </form>
+  </body>
+</html>
\ No newline at end of file
diff --git a/spec/redis.spec.js b/spec/redis.spec.js
new file mode 100644
index 0000000..e607e5c
--- /dev/null
+++ b/spec/redis.spec.js
@@ -0,0 +1,86 @@
+(function () {
+  'use strict';
+  
+  const chai = require('chai');
+  const expect = chai.expect;
+  const sinon = require('sinon');
+  const sinonChai = require('sinon-chai');
+  const mockery = require('mockery');
+  chai.use(sinonChai);
+  const fakeredis = require('fakeredis');
+
+  const client = fakeredis.createClient('test-client');
+  const publisher = fakeredis.createClient('test-client');
+
+  const socketSpy = sinon.spy();
+  const mockSocket = {
+    get: () => {
+      return {
+        emit: socketSpy
+      }
+    }
+  };
+  const channelName = 'Site';
+
+  describe('The event system', () => {
+
+    before((done) => {
+
+      // Enable mockery to mock objects
+      mockery.enable({
+          warnOnReplace: false,
+          warnOnUnregistered: false
+      });
+
+      // Stub the createClient method to *always* return the client created above
+      sinon.stub(fakeredis, 'createClient', () => client);
+
+      // Override the redis module with our fakeredis instance
+      mockery.registerMock('redis', fakeredis);
+
+      // mock the socketIo client to have a spy
+      mockery.registerMock('./websocket.js', mockSocket);
+
+      require('../src/controllers/redis.js');
+      setTimeout(() => {
+        done();
+      }, 1000);
+    });
+
+    after(() => {
+      mockery.disable();
+      fakeredis.createClient.restore();
+    });
+
+    // run after each test
+    beforeEach(() => {
+      client.unsubscribe(channelName);
+      client.subscribe(channelName);
+      publisher.flushdb();
+    });
+
+    it('should send a websocket event when it receive a redis event that is not JSON', (done) => {
+      publisher.publish(channelName, 'I am sending a message.');
+      setTimeout(() => {
+        expect(socketSpy).to.have.been.called;
+        expect(socketSpy).to.have.been.calledWith('event', {
+          model: channelName,
+          msg: 'I am sending a message.'
+        });
+        done();
+      }, 500)
+    });
+
+    it('should send a websocket event when it receive a redis event that is JSON', (done) => {
+      publisher.publish(channelName, JSON.stringify({msg: 'Json Message'}));
+      setTimeout(() => {
+        expect(socketSpy).to.have.been.called;
+        expect(socketSpy).to.have.been.calledWith('event', {
+          model: channelName,
+          msg: {msg: 'Json Message'}
+        });
+        done();
+      }, 1000)
+    });
+  });
+})();
\ No newline at end of file
diff --git a/spec/spec_helper.js b/spec/spec_helper.js
new file mode 100644
index 0000000..abedb29
--- /dev/null
+++ b/spec/spec_helper.js
@@ -0,0 +1,42 @@
+(function () {
+  'use strict';
+  
+  const sinon = require('sinon');
+
+  let stubCache = {};
+
+  exports.makeStub = (name, target, method, cb) => {
+    
+    let methodStub, prototypeStub;
+
+    function SuperAgentStub(end) {
+      this.end = end;
+      this.set = sinon.stub().returns(this);
+      this.send = sinon.stub().returns(this);
+      return this;
+    }
+
+    beforeEach(() => {
+      methodStub = sinon.stub(target, method);
+
+      prototypeStub = new SuperAgentStub(cb);
+
+      methodStub.returns(prototypeStub);
+
+      // cache stub (for use in tests)
+      stubCache[name] = {
+        set: prototypeStub.set,
+        send: prototypeStub.send
+      };
+      stubCache[name][method] = methodStub
+    });
+
+    afterEach(() => {
+      target[method].restore();
+    });
+    
+  };
+
+  exports.getStub = name => stubCache[name];
+
+})();
\ No newline at end of file
diff --git a/spec/websocket.spec.js b/spec/websocket.spec.js
new file mode 100644
index 0000000..2822223
--- /dev/null
+++ b/spec/websocket.spec.js
@@ -0,0 +1,73 @@
+(function () {
+  'use strict';
+
+  const chai = require('chai');
+  const expect = chai.expect;
+  const sinonChai = require('sinon-chai');
+  chai.use(sinonChai);
+  const io = require('socket.io-client');
+  const server = require('../src/server.js');
+
+  describe('basic socket.io example', function() {
+
+    var client;
+
+    beforeEach(function(done) {
+      // Start our server
+      server.start();
+
+      // connect a client to the server
+      client = io.connect('http://localhost:3000', {
+        query: 'name=test@xos.org&token=testToken&sessionId=testSession&id=1'
+      });
+
+      // when is connected start testing
+      client.on('connect', () => {
+        done();
+      });
+    });
+
+    afterEach((done) => {
+      // disconnect the client
+      if(client.connected) {
+        client.disconnect();
+      }
+      done();
+    });
+
+    it('should store user details for a new connection', () => {
+      const clients = require('../src/controllers/clients.js');
+      const user = clients.clients[0];
+      expect(user.name).to.equal('test@xos.org')
+    });
+
+    it('should not store the same user twice', (done) => {
+
+      // connect a client to the server
+      const client2 = io.connect('http://localhost:3000', {
+        query: 'name=test@xos.org&token=testToken&sessionId=testSession&id=1'
+      });
+
+      // when is connected start testing
+      client2.on('connect', () => {
+        setTimeout(() => {
+          const clients = require('../src/controllers/clients.js');
+          expect(clients.clients.length).to.equal(1)
+          done();
+        }, 100);
+      });
+
+    });
+
+    it('should remove a user on disconnect', (done) => {
+      client.disconnect();
+      // we need to wait for the event to be dispatched
+      setTimeout(() => {
+        const clients = require('../src/controllers/clients.js');
+        expect(clients.clients.length).to.equal(0)
+        done();
+      }, 100);
+    });
+
+  });
+})();
\ No newline at end of file
diff --git a/src/config/config.js b/src/config/config.js
new file mode 100644
index 0000000..2671a26
--- /dev/null
+++ b/src/config/config.js
@@ -0,0 +1,37 @@
+(function () {
+  'use strict';
+  
+  // NOTE do we still need CLI args? 
+  // Won't be better to use NODE_ENV and the native node-yaml-config feature
+
+  const argv = require('yargs').argv;
+  const path = require('path');
+  const yaml_config = require('node-yaml-config');
+  const logger = require('../config/logger.js');
+
+  // if a config file is specified in as a CLI arguments use that one
+  const cfgFile = argv.config || 'config.yml';
+
+  let config;
+  try {
+    logger.log('debug', `Loading ${cfgFile}`);
+    config = yaml_config.load(path.join(__dirname, cfgFile));
+  }
+  catch(e) {
+    logger.log('debug', `No ${cfgFile} found, using default params`);
+  }
+
+  module.exports = {
+    xos: {
+      host: (config && config.xos) ? config.xos.host : 'xos',
+      port: (config && config.xos) ? config.xos.port : 9999
+    },
+    redis: {
+      host: (config && config.redis) ? config.redis.host : 'redis',
+      port: (config && config.redis) ? config.redis.port : 6379
+    },
+    gateway: {
+      port: (config && config.gateway) ? config.gateway.port : 3000
+    }
+  };
+})();
\ No newline at end of file
diff --git a/src/config/logger.js b/src/config/logger.js
new file mode 100644
index 0000000..df49409
--- /dev/null
+++ b/src/config/logger.js
@@ -0,0 +1,34 @@
+(function () {
+  'use strict';
+  
+  const winston = require('winston');
+  const fs = require('fs');
+  const path = require('path');
+  const level = process.env.LOG_LEVEL || 'warn';
+  winston.level = level;
+
+  const logFile = path.join(__dirname, '../../logs/xos-nb-rest');
+
+  // clear old logs
+  ['error', 'debug'].forEach(l => {
+    try {
+      fs.statSync(`${logFile}.${l}.log`)
+      fs.unlinkSync(`${logFile}.${l}.log`);
+    }
+    catch(e) {
+      // log does not exist
+    }
+  });
+
+  // create a custom logger with colorized console and persistance to file
+  const logger = new (winston.Logger)({
+    transports: [
+      new (winston.transports.Console)({level: level, colorize: true}),
+      new (winston.transports.File)({name: 'error-log', level: 'error', filename: `${logFile}.error.log`}),
+      new (winston.transports.File)({name: 'debug-log', level: 'debug', filename: `${logFile}.debug.log`})
+    ]
+  });
+
+  module.exports = logger;
+
+})();
\ No newline at end of file
diff --git a/src/controllers/clients.js b/src/controllers/clients.js
new file mode 100644
index 0000000..26270bc
--- /dev/null
+++ b/src/controllers/clients.js
@@ -0,0 +1,18 @@
+(function () {
+  'use strict';
+  const _ = require('lodash');
+  const clients = [];
+
+  exports.clients = clients;
+
+  exports.add = (client) => {
+    // TODO check id that client is already there
+    if(!_.find(clients, ({id: client.id}))) {
+      clients.push(client);
+    }
+  }
+
+  exports.remove = (client) => {
+    _.remove(clients, {id: client.id});
+  }
+})();
\ No newline at end of file
diff --git a/src/controllers/redis.js b/src/controllers/redis.js
new file mode 100644
index 0000000..0277cab
--- /dev/null
+++ b/src/controllers/redis.js
@@ -0,0 +1,57 @@
+(function () {
+  'use strict';
+  
+  const socketIo = require('./websocket.js');
+  const config = require('../config/config.js').redis;
+  const logger = require('../config/logger.js');
+  const socket = socketIo.get();
+
+  var redis = require('redis');
+  var client = redis.createClient({
+    host: config.host,
+    port: config.port
+  });
+
+  client.on('error', function (err) {
+    logger.log('error', err);
+  });
+
+  client.on('ready', function () {
+    logger.log('info', 'Redis connected');
+  });
+
+  client.on('subscribe', function (channel) {
+    logger.log('debug', `Subscribed to channel: ${channel}`);
+  });
+
+  client.on('message', function (channel, message) {
+    logger.log('debug', 'sub channel ' + channel + ': ' + message);
+
+    let msg;
+    try {
+      msg = JSON.parse(message);
+      // TODO find the user that needs to be notified for msg.object update
+      socket.emit('event', {model: channel, msg: msg});
+    }
+    catch(e) {
+      // send the event also if it is not JSON
+      msg = message;
+      socket.emit('event', {model: channel, msg: msg});
+    }
+
+  });
+
+  const watchedCollections = [
+    'Instance',
+    'Node',
+    'Service',
+    'Slice',
+    'Site',
+    'Subscriber',
+    'Tenant'
+  ];
+
+  watchedCollections.forEach(c => {
+    client.subscribe(c);
+  });
+})();
\ No newline at end of file
diff --git a/src/controllers/websocket.js b/src/controllers/websocket.js
new file mode 100644
index 0000000..e048419
--- /dev/null
+++ b/src/controllers/websocket.js
@@ -0,0 +1,39 @@
+(function () {
+  'use strict';
+
+  const clients = require('./clients.js');
+  const logger = require('../config/logger.js');
+
+  let io;
+
+  exports.create = function(server) {
+    // INSTANTIATE SOCKET.IO
+    // =============================================================================
+
+    io = require('socket.io').listen(server);
+
+    // LISTEN TO "CONNECTION" EVENT (FROM SOCKET.IO)
+    // =============================================================================
+
+    io.on('connection', function (socket) {
+      logger.log('debug', 'connect %j', socket.handshake.query);
+      clients.add(socket.handshake.query);
+
+      socket.emit('connected', {message : 'Welcome to XOS'});
+
+      socket.on('disconnect', function(reason) {
+        clients.remove(socket.handshake.query);
+        logger.log('debug', 'disconnect %s %j', reason, socket.handshake.query);
+      });
+    });
+
+  };
+
+  exports.get = () => io;
+
+  // USAGE
+  // const socketIo = require('./controllers/websocket.js');
+  // const socket = socketIo.get();
+  // socket.emit('eventName', data);
+
+})(); 
\ No newline at end of file
diff --git a/src/routes/core_proxy.js b/src/routes/core_proxy.js
new file mode 100644
index 0000000..0290347
--- /dev/null
+++ b/src/routes/core_proxy.js
@@ -0,0 +1,48 @@
+(function () {
+  'use strict';
+
+  const request = require('superagent');
+  const logger = require('../config/logger.js');
+
+  module.exports = function(app) {
+
+    const proxyRequest = (req, res) => {
+
+      const config = require('../config/config.js').xos;
+      // pick the correct method from superAgent
+      const makeReq = request[req.method.toLowerCase()];
+
+      // start the request
+      let sentReq = makeReq(`${config.host}:${config.port}${req.url}`);
+      
+      // if needed add a body to the request
+      if(req.method === 'POST' || req.method === 'PUT') {
+        sentReq = sentReq
+          .send(req.body)
+      }
+
+      // extend with auth info
+      sentReq = sentReq
+        .set('x-csrftoken', req.headers['x-csrftoken'] || null)
+        .set('cookie', req.headers.cookie || null)
+
+      // handle response
+      sentReq
+        .end((err, r) => {
+          if(err) {
+            logger.log('error', err);
+            return res.status(500).send(err);
+          }
+          logger.log('debug', r.status, r.body);
+          return res.status(r.status).type('json').send(r.body);
+        });
+    };
+
+    app.all('/api/core', proxyRequest);
+    app.all('/api/core/*', proxyRequest);
+    app.all('/api/services', proxyRequest);
+    app.all('/api/services/*', proxyRequest);
+    app.all('/api/utility', proxyRequest);
+    app.all('/api/utility/*', proxyRequest);
+  };
+})();
\ No newline at end of file
diff --git a/src/server.js b/src/server.js
new file mode 100644
index 0000000..da541ce
--- /dev/null
+++ b/src/server.js
@@ -0,0 +1,58 @@
+(function () {
+  'use strict';
+  
+  const express = require('express');
+  const app = express();
+  const config = require('./config/config.js').gateway;
+  const bodyParser = require('body-parser');
+  const cors = require('cors');
+  const socketIo = require('./controllers/websocket.js');
+  const logger = require('./config/logger.js');
+
+  // Apply middlewares
+  app.use(cors());
+  app.use(bodyParser.json());
+
+  // Load routes
+  require('./routes/core_proxy.js')(app);
+
+  app.get('/', function(req, res) {
+    res.send('Hello world');
+  });
+
+  const startServer = () => {
+
+    // if is running just return it
+    if(app.server) {
+      return app.server;
+    }
+
+    const server =  app.listen(config.port, function() {
+      logger.info(`Express is listening to http://localhost:${config.port}`);
+
+      // once server is ready setup WebSocket
+      socketIo.create(server);
+
+      // start redis
+      require('./controllers/redis.js');
+    });
+    app.server = server;
+    return server;
+  };
+
+  const stopServer = () => {
+    if(app.server) {
+      app.server.close();
+    }
+  }
+
+  if(!module.parent) {
+    startServer();
+  }
+
+  module.exports = {
+    app: app,
+    start: startServer,
+    stop: stopServer
+  };
+})();
\ No newline at end of file